forked from steaphangreene/acidmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_shops.cpp
More file actions
475 lines (451 loc) · 14.4 KB
/
command_shops.cpp
File metadata and controls
475 lines (451 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// *************************************************************************
// This file is part of AcidMUD by Steaphan Greene
//
// Copyright 1999-2022 Steaphan Greene <steaphan@gmail.com>
//
// AcidMUD is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// AcidMUD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AcidMUD (see the file named "COPYING");
// If not, see <http://www.gnu.org/licenses/>.
//
// *************************************************************************
#include "color.hpp"
#include "commands.hpp"
#include "object.hpp"
#include "properties.hpp"
#include "tags.hpp"
#include "utils.hpp"
static bool check_shopkeeper(Object* shpkp, auto& shpkps, std::u8string_view& reason) {
if (shpkp->IsAct(act_t::DEAD)) {
reason = u8"Sorry, the shopkeeper is dead!\n";
} else if (shpkp->IsAct(act_t::DYING)) {
reason = u8"Sorry, the shopkeeper is dying!\n";
} else if (shpkp->IsAct(act_t::UNCONSCIOUS)) {
reason = u8"Sorry, the shopkeeper is unconscious!\n";
} else if (shpkp->IsAct(act_t::SLEEP)) {
reason = u8"Sorry, the shopkeeper is asleep!\n";
} else {
shpkps.push_back(shpkp);
return true;
}
return false;
}
int handle_command_list(
Object* body,
std::shared_ptr<Mind>& mind,
int cnum,
const std::u8string_view& args,
int stealth_t,
int stealth_s) {
if (!mind)
return 0;
auto people = body->Room()->PickObjects(u8"everyone", LOC_INTERNAL);
DArr64<Object*> shpkps;
std::u8string_view reason = u8"";
for (auto shpkp : people) { // Circle/TBA Shopkeepers
if (shpkp->Skill(prhash(u8"Sell Profit"))) {
check_shopkeeper(shpkp, shpkps, reason);
}
}
auto workers = body->Room()->Touching();
for (auto shpkp : workers) { // Acid Shopkeepers
if (shpkp->IsAct(act_t::WORK) && shpkp->ActTarg(act_t::SPECIAL_WORK) == body->Room()) {
check_shopkeeper(shpkp, shpkps, reason);
}
}
if (shpkps.size() == 0) {
if (mind) {
if (reason.length() > 0) {
mind->Send(reason);
} else {
mind->Send(u8"You can only do that around a shopkeeper.\n");
}
}
} else {
Object* shpkp = shpkps.front();
if (shpkp->ActTarg(act_t::WEAR_RSHOULDER) &&
shpkp->ActTarg(act_t::WEAR_RSHOULDER)->Skill(prhash(u8"Vortex"))) { // Circle/TBA
Object* vortex = shpkp->ActTarg(act_t::WEAR_RSHOULDER);
auto objs = vortex->Contents();
auto oobj = objs.front();
for (auto obj : objs) {
if (obj != objs.front() && obj->IsSameAs(*oobj))
continue;
size_t price = obj->Value();
if (shpkp->Skill(prhash(u8"Sell Profit")) >= 500 &&
obj->Skill(prhash(u8"Money")) != static_cast<int>(obj->Value())) { // Not 1-1 Money
price *= shpkp->Skill(prhash(u8"Sell Profit"));
price += 999;
price /= 1000;
}
if (mind) {
mind->Send(u8"{:>10}: {}\n", coins(price), obj->ShortDesc());
}
oobj = obj;
}
} else {
auto items = body->PickObjects(u8"everything", LOC_NEARBY);
bool have_stock = false;
for (auto item : items) {
if (item->ActTarg(act_t::SPECIAL_OWNER) == body->Room() &&
item->Position() == pos_t::PROP) {
have_stock = true;
size_t price = item->Value();
if (shpkp->Skill(prhash(u8"Sell Profit")) >= 500 &&
item->Skill(prhash(u8"Money")) != static_cast<int>(item->Value())) { // Not 1-1 Money
price *= shpkp->Skill(prhash(u8"Sell Profit"));
price += 999;
price /= 1000;
}
if (mind) {
if (item->Quantity() > 1) {
mind->Send(
CMAG u8"{:>10}: {} (x{})\n" CNRM,
coins(price),
item->ShortDesc(),
item->Quantity());
} else {
mind->Send(CMAG u8"{:>10}: {}\n" CNRM, coins(price), item->ShortDesc());
}
}
}
}
if (mind && !have_stock) {
mind->Send(u8"Nothing is for sale here, sorry.\n");
}
}
}
return 0;
}
int handle_command_buy(
Object* body,
std::shared_ptr<Mind>& mind,
int cnum,
const std::u8string_view& args,
int stealth_t,
int stealth_s) {
if (args.empty()) {
if (mind) {
mind->Send(u8"What do you want to buy?\n");
}
return 0;
}
auto people = body->PickObjects(u8"everyone", LOC_NEARBY);
DArr64<Object*> shpkps;
std::u8string_view reason = u8"";
for (auto shpkp : people) {
if (shpkp->Skill(prhash(u8"Sell Profit"))) { // Circle/TBA Shopkeepers
check_shopkeeper(shpkp, shpkps, reason);
}
}
auto workers = body->Room()->Touching();
for (auto shpkp : workers) { // Acid Shopkeepers
if (shpkp->IsAct(act_t::WORK) && shpkp->ActTarg(act_t::SPECIAL_WORK) == body->Room()) {
check_shopkeeper(shpkp, shpkps, reason);
}
}
if (shpkps.size() == 0) {
if (mind) {
if (reason.length() > 0) {
mind->Send(reason);
} else {
mind->Send(u8"You can only do that around a shopkeeper.\n");
}
}
} else {
auto items = body->PickObjects(args, LOC_NEARBY);
Object* shpkp = shpkps.front();
if (shpkp->ActTarg(act_t::WEAR_RSHOULDER) &&
shpkp->ActTarg(act_t::WEAR_RSHOULDER)->Skill(prhash(u8"Vortex"))) { // Circle/TBA
Object* vortex = shpkp->ActTarg(act_t::WEAR_RSHOULDER);
items = vortex->PickObjects(std::u8string(args), LOC_INTERNAL);
} else {
bool all_for_sale = true;
for (auto item : items) {
if (item->ActTarg(act_t::SPECIAL_OWNER) != body->Room() ||
item->Position() != pos_t::PROP) {
if (mind) {
mind->Send(CRED u8" Not For Sale: {}\n" CNRM, item->ShortDesc());
}
all_for_sale = false;
}
}
if (!all_for_sale) {
if (mind) {
mind->Send(CRED u8"Sorry, you can't buy that.\n" CNRM);
}
return 0;
}
}
if (items.size() == 0) {
if (mind) {
mind->Send(u8"The shopkeeper doesn't have that.\n");
}
return 0;
}
for (auto item : items) {
size_t price = item->Value() * item->Quantity();
if (item->HasSkill(prhash(u8"Priceless")) || item->HasSkill(prhash(u8"Cursed"))) {
if (mind) {
mind->Send(u8"You can't buy {}.\n", item->Noun(0, 0, body));
}
continue;
} else if (price == 0) {
if (mind) {
std::u8string mes = item->Noun(0, 0, body);
mes += u8" is worthless.\n";
mes[0] = ascii_toupper(mes[0]);
mind->Send(mes);
}
continue;
}
if (shpkp->HasSkill(prhash(u8"Sell Profit"))) { // Circle/TBA
if (item->Skill(prhash(u8"Money")) != static_cast<int>(item->Value())) { // Not 1-1 Money
price *= shpkp->Skill(prhash(u8"Sell Profit"));
price += 999;
price /= 1000;
}
}
if (mind) {
mind->Send(u8"{}: {}\n", coins(price), item->ShortDesc());
}
auto pay = body->PayFor(price);
if (pay.size() == 0) {
auto offer = body->CanPayFor(price);
if (offer > price) {
if (mind) {
mind->Send(
u8"You don't have change to pay {} (you'd have to pay {}).\n",
coins(price),
coins(offer));
}
auto refund = offer - price;
auto change = shpkp->PayFor(refund);
if (change.size() > 0) {
pay = body->PayFor(offer);
for (auto coin : change) {
body->Stash(coin, 0, 1);
}
if (mind) {
mind->Send(u8"You get {} from {}.\n", coins(refund), shpkp->ShortDesc());
}
} else {
if (mind) {
mind->Send(u8"...and I can't make change for that, sorry.\n");
}
}
} else {
if (mind) {
mind->Send(
u8"You can't afford the {} (you only have {}).\n", coins(price), coins(offer));
}
}
} else if (body->Stash(item, 0, 0)) {
body->Parent()->SendOut(
stealth_t,
stealth_s,
u8";s buys and stashes ;s.\n",
u8"You buy and stash ;s.\n",
body,
item);
item->StopAct(act_t::SPECIAL_OWNER);
item->SetPosition(pos_t::LIE);
for (auto coin : pay) {
shpkp->Stash(coin, 0, 1);
}
} else if (
((!body->IsAct(act_t::HOLD)) ||
body->ActTarg(act_t::HOLD) == body->ActTarg(act_t::WIELD) ||
body->ActTarg(act_t::HOLD) == body->ActTarg(act_t::WEAR_SHIELD)) &&
(!item->Travel(body))) {
if (body->IsAct(act_t::HOLD)) {
body->Parent()->SendOut(
stealth_t,
stealth_s,
u8";s stops holding ;s.\n",
u8"You stop holding ;s.\n",
body,
body->ActTarg(act_t::HOLD));
body->StopAct(act_t::HOLD);
}
body->AddAct(act_t::HOLD, item);
body->Parent()->SendOut(
stealth_t,
stealth_s,
u8";s buys and holds ;s.\n",
u8"You buy and hold ;s.\n",
body,
item);
item->StopAct(act_t::SPECIAL_OWNER);
item->SetPosition(pos_t::LIE);
for (auto coin : pay) {
shpkp->Stash(coin, 0, 1);
}
} else {
if (mind) {
mind->Send(u8"You can't stash or hold {}.\n", item->Noun(1));
}
}
}
}
return 0;
}
int handle_command_shops(
Object* body,
std::shared_ptr<Mind>& mind,
int cnum,
const std::u8string_view& args,
int stealth_t,
int stealth_s) {
if (args.empty()) {
if (mind) {
mind->Send(u8"What do you want to sell?\n");
}
return 0;
}
Object* item = body->PickObject(std::u8string(args), LOC_NOTWORN | LOC_INTERNAL);
if ((!item) && body->ActTarg(act_t::HOLD) &&
body->ActTarg(act_t::HOLD)->Parent() != body // Dragging
&& body->ActTarg(act_t::HOLD)->Matches(std::u8string(args))) {
item = body->ActTarg(act_t::HOLD);
}
if (!item) {
if (mind) {
mind->Send(u8"You want to sell what?\n");
}
return 0;
}
if (item->Skill(prhash(u8"Container")) || item->Skill(prhash(u8"Liquid Container"))) {
if (mind) {
std::u8string mes = item->Noun(0, 0, body);
mes += u8" is a container.";
mes += u8" You can't sell containers (yet).\n";
mes[0] = ascii_toupper(mes[0]);
mind->Send(mes);
}
return 0;
}
if (item->Contents(LOC_TOUCH | LOC_NOTFIXED).size() > 0) {
if (mind) {
std::u8string mes = item->Noun(0, 0, body);
mes += u8" is not empty.";
mes += u8" You must empty it before you can sell it.\n";
mes[0] = ascii_toupper(mes[0]);
mind->Send(mes);
}
return 0;
}
size_t price = item->Value() * item->Quantity();
if (item->HasSkill(prhash(u8"Priceless")) || item->HasSkill(prhash(u8"Cursed"))) {
if (mind) {
mind->Send(u8"You can't sell {}.\n", item->Noun(0, 0, body));
}
return 0;
}
if (price == 0) {
if (mind) {
mind->Send(u8"{} is worthless.\n", item->Noun(0, 0, body));
}
return 0;
}
auto people = body->Room()->PickObjects(u8"everyone", LOC_INTERNAL);
DArr64<Object*> shpkps;
std::u8string_view reason = u8"Sorry, nobody is buying that sort of thing here.\n";
price = 0;
for (auto shpkp : people) {
if (shpkp->IsAct(act_t::WORK) && shpkp->ActTarg(act_t::SPECIAL_WORK) == body->Room()) {
price = shpkp->WouldBuyFor(item);
if (price > 0) {
check_shopkeeper(shpkp, shpkps, reason);
break;
}
}
}
if (shpkps.size() == 0) {
if (mind) {
mind->Send(reason);
}
} else {
Object* vortex = nullptr;
Object* shpkp = shpkps.front();
if (shpkp->ActTarg(act_t::WEAR_RSHOULDER) &&
shpkp->ActTarg(act_t::WEAR_RSHOULDER)->Skill(prhash(u8"Vortex"))) { // Circle/TBA
vortex = shpkp->ActTarg(act_t::WEAR_RSHOULDER);
}
if (mind) {
mind->Send(u8"I'll give you {} for {}.\n", coins(price), item->ShortDesc());
}
if (cnum == COM_SELL) {
auto pay = shpkp->PayFor(price);
if (pay.size() <= 0) {
auto offer = shpkp->CanPayFor(price);
if (offer > price) {
if (mind) {
mind->Send(
u8"I don't have change to pay {}. You'd have to make change for {}.\n",
coins(price),
coins(offer));
}
auto refund = offer - price;
auto change = body->PayFor(refund);
if (change.size() > 0) {
pay = shpkp->PayFor(offer);
for (auto coin : change) {
shpkp->Stash(coin, 0, 1);
}
if (mind) {
mind->Send(u8"You pay {} to {}.\n", coins(refund), shpkp->ShortDesc());
}
} else {
if (mind) {
mind->Send(u8"...but you don't have that change.\n");
}
}
} else {
if (mind) {
mind->Send(u8"I can't afford the {} (I only have {}).\n", coins(price), coins(offer));
}
}
}
if (pay.size() > 0) {
body->Parent()->SendOut(
stealth_t, stealth_s, u8";s sells ;s.\n", u8"You sell ;s.\n", body, item);
bool success = true;
for (auto coin : pay) {
if (!body->Stash(coin)) {
success = false;
shpkp->Drop(coin);
}
}
if (vortex) {
item->Travel(vortex);
} else {
item->Travel(body->Room());
if (body->ActTarg(act_t::HOLD) == item) { // Stop Dragging It
body->StopAct(act_t::HOLD);
}
item->AddAct(act_t::SPECIAL_OWNER, body->Room());
item->SetPosition(pos_t::LIE); // FIXME: Support Resellers
item->TryCombine();
}
if (!success) {
if (mind) {
mind->Send(
u8"You couldn't stash {}, so at least some of it fell on the floor!\n",
coin_str(price));
}
}
}
}
}
return 0;
}