File: rob.c

package info (click to toggle)
pennmush 1.8.0p4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,068 kB
  • ctags: 6,319
  • sloc: ansic: 68,221; sh: 7,056; perl: 1,150; makefile: 284
file content (576 lines) | stat: -rw-r--r-- 15,440 bytes parent folder | download | duplicates (2)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/**
 * \file rob.c
 *
 * \brief Kill and give.
 *
 * This file is called rob.c for historical reasons, and one day it'll
 * probably get folded into some other file.
 *
 *
 */

#include "config.h"
#include "copyrite.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "externs.h"
#include "mushdb.h"
#include "attrib.h"
#include "match.h"
#include "parse.h"
#include "flags.h"
#include "log.h"
#include "lock.h"
#include "dbdefs.h"
#include "game.h"
#include "confmagic.h"
#include "case.h"

static void do_give_to(dbref player, char *arg, int silent);

/** Set an object's money value, with limit-checking.
 * \param thing dbref of object.
 * \param amount amount to set object's pennies to.
 */
void
s_Pennies(dbref thing, int amount)
{
  if (amount < 0)
    amount = 0;
  else if (amount > HUGE_INT)
    amount = HUGE_INT;
  Pennies(thing) = amount;
}


/** The kill command - send an object back home.
 * \param player the enactor.
 * \param what name of object to kill.
 * \param cost amount to pay to kill.
 * \param slay if 1, this is the wizard 'slay' command instead.
 */
void
do_kill(dbref player, const char *what, int cost, int slay)
{
  dbref victim;
  char tbuf1[BUFFER_LEN], tbuf2[BUFFER_LEN], *tp;

  if (slay && !Wizard(player)) {
    notify(player, T("You do not have such power."));
    return;
  }
  victim = match_result(player, what, TYPE_PLAYER, MAT_NEAR_THINGS);

  if (player == victim) {
    notify(player, T("No suicide allowed."));
    return;
  }
  if (slay)
    do_log(LT_WIZ, player, victim, "SLAY");

  switch (victim) {
  case NOTHING:
    notify(player, T("I don't see that here."));
    break;
  case AMBIGUOUS:
    notify(player, T("I don't know what you mean!"));
    break;
  default:
    if (Suspect(player))
      flag_broadcast("WIZARD", 0,
		     T("Broadcast: Suspect %s tried to kill %s(#%d)."),
		     Name(player), Name(victim), victim);
    if (!Mobile(victim)) {
      notify(player, T("Sorry, you can only kill players and objects."));
    } else if ((Haven(Location(victim)) &&
		!Wizard(player)) ||
	       (controls(victim, Location(victim)) &&
		!controls(player, Location(victim)))) {
      notify(player, "Sorry.");
    } else if (NoKill(victim) && !Wizard(player) && (Owner(victim) != player)) {
      notify(player, T("That object cannot be killed."));
    } else {
      /* go for it */
      /* set cost */
      /* if this isn't called via slay */
      if (!slay) {
	if (cost < KILL_MIN_COST)
	  cost = KILL_MIN_COST;

	/* see if it works */
	if (!payfor(player, cost)) {
	  notify_format(player, T("You don't have enough %s."), MONIES);
	  break;
	}
      }
      if (((get_random_long(0, KILL_BASE_COST) < cost) || slay) &&
	  !Wizard(victim)) {
	/* you killed him */
	tp = tbuf1;
	safe_format(tbuf1, &tp, T("You killed %s!"), Name(victim));
	*tp = '\0';
	tp = tbuf2;
	safe_format(tbuf2, &tp, "killed %s!", Name(victim));
	*tp = '\0';
	do_halt(victim, "", victim);
	did_it(player, victim, "DEATH", tbuf1, "ODEATH", tbuf2, "ADEATH",
	       NOTHING);

	/* notify victim */
	notify_format(victim, T("%s killed you!"), Name(player));

	/* maybe pay off the bonus */
	/* if we were not called via slay */
	if (!slay) {
	  int payoff = cost * KILL_BONUS / 100;
	  if (payoff + Pennies(Owner(victim)) > Max_Pennies(Owner(victim)))
	    payoff = Max_Pennies(Owner(victim)) - Pennies(Owner(victim));
	  if (payoff > 0) {
	    notify_format(victim, T("Your insurance policy pays %d %s."),
			  payoff, ((payoff == 1) ? MONEY : MONIES));
	    giveto(Owner(victim), payoff);
	  } else {
	    notify(victim, T("Your insurance policy has been revoked."));
	  }
	}
	/* send him home */
	safe_tel(victim, HOME, 0);
	/* if victim is object also dequeue all commands */
      } else {
	/* notify player and victim only */
	notify(player, T("Your murder attempt failed."));
	notify_format(victim, T("%s tried to kill you!"), Name(player));
      }
      break;
    }
  }
}

/** the buy command
 * \param player the enactor/buyer
 * \param item the item to buy
 * \param from who to buy it from
 * \param cost the cost
 */
void
do_buy(dbref player, char *item, char *from, int price)
{
  dbref vendor;
  char *prices;
  char *plus;
  char *cost;
  char finditem[BUFFER_LEN];
  char buycost[BUFFER_LEN];
  int boughtit;
  int len;
  char buff[BUFFER_LEN], *bp;
  char obuff[BUFFER_LEN];
  char *r[BUFFER_LEN / 2];
  char *c[BUFFER_LEN / 2];
  char *buy_env[10] = { NULL };
  int affordable;
  int costcount, ci;
  int count, i;
  int low, high;		/* lower bound, upper bound of cost */
  ATTR *a;

  if (!GoodObject(Location(player)))
    return;

  vendor = Contents(Location(player));
  if (vendor == player)
    vendor = Next(player);

  if (from != NULL && *from) {
    switch (vendor =
	    match_result(player, from, TYPE_PLAYER,
			 MAT_NEAR_THINGS | MAT_ENGLISH)) {
    case NOTHING:
      notify(player, T("Buy from whom?"));
      return;
    case AMBIGUOUS:
      notify(player, T("I don't know who you mean!"));
      return;
    }
    if (vendor == player) {
      notify(player, T("You can't buy from yourself!"));
      return;
    }
  } else if (vendor == NOTHING) {
    notify(player, T("There's nobody here to buy things from."));
    return;
  } else {
    from = NULL;
  }

  if (!item || !*item || !(item = trim_space_sep(item, ' '))) {
    notify(player, T("Buy what?"));
    return;
  }

  bp = finditem;
  safe_str(item, finditem, &bp);
  safe_chr(':', finditem, &bp);
  *bp = '\0';
  for (bp = strchr(finditem, ' '); bp; bp = strchr(bp, ' '))
    *bp = '_';

  len = strlen(finditem);

  /* Scan pricelists */
  boughtit = -1;
  affordable = 1;
  do {
    a = atr_get(vendor, "PRICELIST");
    if (!a)
      continue;
    /* atr_value uses a static buffer, so we'll take advantage of that */
    prices = atr_value(a);
    upcasestr(prices);
    count = list2arr(r, BUFFER_LEN / 2, atr_value(a), ' ');
    if (!count)
      continue;
    for (i = 0; i < count; i++) {
      if (!strncasecmp(finditem, r[i], len)) {
	/* Check cost */
	cost = r[i] + len;
	if (!*cost)
	  continue;
	costcount = list2arr(c, BUFFER_LEN / 2, cost, ',');
	for (ci = 0; ci < costcount; ci++) {
	  cost = c[ci];
	  /* Formats:
	   * 10,2000+,10-100
	   */
	  if ((plus = strchr(cost, '-'))) {
	    *(plus++) = '\0';
	    if (!is_strict_integer(cost))
	      continue;
	    if (!is_strict_integer(plus))
	      continue;
	    low = parse_integer(cost);
	    high = parse_integer(plus);
	    if (price < 0) {
	      boughtit = low;
	    } else if (price >= low && price <= high) {
	      boughtit = price;
	    }
	  } else if ((plus = strchr(cost, '+'))) {
	    *(plus++) = '\0';
	    if (!is_strict_integer(cost))
	      continue;
	    low = parse_integer(cost);
	    if (price < 0) {
	      boughtit = low;
	    } else if (price > low) {
	      boughtit = price;
	    }
	  } else if (is_strict_integer(cost)) {
	    low = parse_integer(cost);
	    if (price < 0) {
	      boughtit = low;
	    } else if (low == price) {
	      boughtit = price;
	    }
	  } else {
	    continue;
	  }
	  if (boughtit >= 0) {
	    if (!payfor(player, boughtit)) {
	      affordable = 0;
	      boughtit = 0;
	      continue;
	    }
	    bp = strchr(finditem, ':');
	    if (bp)
	      *bp = '\0';
	    for (bp = finditem; *bp; bp++)
	      *bp = DOWNCASE(*bp);
	    bp = buff;
	    safe_format(buff, &bp, "You buy a %s from %s.",
			finditem, Name(vendor));
	    *bp = '\0';
	    bp = obuff;
	    safe_format(obuff, &bp, "buys a %s from %s.",
			finditem, Name(vendor));
	    buy_env[0] = finditem;
	    buy_env[1] = buycost;
	    bp = buycost;
	    safe_integer(boughtit, buycost, &bp);
	    *bp = '\0';
	    real_did_it(player, vendor, "BUY", buff, "OBUY", obuff, "ABUY",
			NOTHING, buy_env, NA_INTER_SEE);
	    return;
	  }
	}
      }
    }
  } while (!from && ((vendor = Next(vendor)) != NOTHING));

  if (price >= 0) {
    if (!from) {
      notify(player, T("I can't find that item with that price here."));
    } else {
      notify_format(player, T("%s isn't selling that item for that price"),
		    Name(vendor));
    }
  } else if (affordable) {
    if (!from) {
      notify(player, T("I can't find that item here."));
    } else {
      notify_format(player, T("%s isn't selling that item."), Name(vendor));
    }
  } else {
    notify(player, T("You can't afford that."));
  }
}

/** The give command.
 * \param player the enactor/giver.
 * \param recipient name of object to receive.
 * \param amnt name of object to be transferred, or amount of pennies.
 * \param silent if 1, hush the usual messages.
 */
void
do_give(dbref player, char *recipient, char *amnt, int silent)
{
  dbref who;
  int amount;
  char tbuf1[BUFFER_LEN];
  char *bp;

  /* If we have a recipient, but no amnt, try parsing for
   * 'give <amnt> to <recipient>' instead of 'give <recipient>=<amount>'
   */
  if (recipient && *recipient && (!amnt || !*amnt)) {
    do_give_to(player, recipient, silent);
    return;
  }

  /* check recipient */
  switch (who =
	  match_result(player, recipient, TYPE_PLAYER,
		       MAT_NEAR_THINGS | MAT_ENGLISH)) {
  case NOTHING:
    notify(player, T("Give to whom?"));
    return;
  case AMBIGUOUS:
    notify(player, T("I don't know who you mean!"));
    return;
  }

  /* Can't give to garbage... */
  if (IsGarbage(who)) {
    notify(player, T("Give to whom?"));
    return;
  }

  if (!is_strict_integer(amnt)) {
    /* We're giving an object */
    dbref thing;
    switch (thing =
	    match_result(player, amnt, TYPE_THING,
			 MAT_POSSESSION | MAT_ENGLISH)) {
    case NOTHING:
      notify(player, T("You don't have that!"));
      return;
    case AMBIGUOUS:
      notify(player, T("I don't know which you mean!"));
      return;
    default:
      /* if you can give yourself, that's like "enter". since we
       * do no lock check with give, we shouldn't be able to
       * do this.
       */
      if (thing == player) {
	notify(player, T("You can't give yourself away!"));
	return;
      }
      /* Don't give things to themselves. */
      if (thing == who) {
	notify(player, T("You can't give an object to itself!"));
	return;
      }
      if (!eval_lock(player, thing, Give_Lock)) {
	notify(player, T("You can't give that away."));
	return;
      }
      if (Mobile(thing) && (EnterOk(who) || controls(player, who))) {
	moveto(thing, who);

	/* Notify the giver with their GIVE message */
	bp = tbuf1;
	safe_format(tbuf1, &bp, T("You gave %s to %s."), Name(thing),
		    Name(who));
	*bp = '\0';
	did_it_with(player, player, "GIVE", tbuf1, "OGIVE", NULL,
		    "AGIVE", NOTHING, thing, who, NA_INTER_SEE);

	/* Notify the object that it's been given */
	notify_format(thing, T("%s gave you to %s."), Name(player), Name(who));

	/* Recipient gets success message on thing and receive on self */
	did_it(who, thing, "SUCCESS", NULL, "OSUCCESS", NULL, "ASUCCESS",
	       NOTHING);
	bp = tbuf1;
	safe_format(tbuf1, &bp, T("%s gave you %s."), Name(player),
		    Name(thing));
	*bp = '\0';
	did_it_with(who, who, "RECEIVE", tbuf1, "ORECEIVE", NULL,
		    "ARECEIVE", NOTHING, thing, player, NA_INTER_SEE);
      } else
	notify(player, T("Permission denied."));
    }
    return;
  }
  /* At this point, we're giving an amount. */
  amount = parse_integer(amnt);
  if (Pennies(who) + amount > Max_Pennies(who))
    amount = Max_Pennies(who) - Pennies(who);
  if (amount < 0 && !Can_Debit(player)) {
    notify(player, T("What is this, a holdup?"));
    return;
  } else if (amount == 0) {
    notify_format(player,
		  T("You must specify a positive number of %s."), MONIES);
    return;
  }
  if (Can_Debit(player) && (amount < 0) && (Pennies(who) + amount < 0))
    amount = -Pennies(who);
  /* try to do the give */
  if (!Moneybags(player) && !payfor(player, amount)) {
    notify_format(player, T("You don't have that many %s to give!"), MONIES);
  } else {
    char *pay_env[10] = { NULL };
    char paid[SBUF_LEN], *pb;

    pay_env[0] = pb = paid;

    /* objects work differently */
    if (IsThing(who)) {
      /* give pennies to an object */
      int cost = 0;
      ATTR *a;
      char *preserveq[NUMQ];
      char *preserves[10];
      char fbuff[BUFFER_LEN];
      char *fbp, *asave;
      char const *ap;

      a = atr_get(who, "COST");
      if (!a) {
	/* No cost attribute */
	notify_format(player, T("%s refuses your money."), Name(who));
	giveto(player, amount);
	return;
      }
      save_global_regs("give_save", preserveq);
      save_global_env("give_save", preserves);
      asave = safe_atr_value(a);
      ap = asave;
      fbp = fbuff;
      safe_integer_sbuf(amount, paid, &pb);
      *pb = '\0';
      global_eval_context.wenv[0] = paid;
      process_expression(fbuff, &fbp, &ap, who, player, player,
			 PE_DEFAULT, PT_DEFAULT, NULL);
      *fbp = '\0';
      free((Malloc_t) asave);
      restore_global_regs("give_save", preserveq);
      restore_global_env("give_save", preserves);
      if (amount < (cost = atoi(fbuff))) {
	notify(player, T("Feeling poor today?"));
	giveto(player, amount);
	return;
      }
      if (cost < 0)
	return;
      if ((amount - cost) > 0) {
	notify_format(player, T("You get %d in change."), amount - cost);
      } else {
	notify_format(player, T("You paid %d %s."), amount,
		      ((amount == 1) ? MONEY : MONIES));
      }
      giveto(player, amount - cost);
      giveto(who, cost);
      pb = paid;
      safe_integer_sbuf(cost, paid, &pb);
      *pb = '\0';
      real_did_it(player, who, "PAYMENT", NULL, "OPAYMENT", NULL, "APAYMENT",
		  NOTHING, pay_env, NA_INTER_SEE);
      return;
    } else {
      /* give pennies to a player */
      if (amount > 0) {
	notify_format(player,
		      T("You give %d %s to %s."), amount,
		      ((amount == 1) ? MONEY : MONIES), Name(who));
      } else {
	notify_format(player, T("You took %d %s from %s!"), abs(amount),
		      ((abs(amount) == 1) ? MONEY : MONIES), Name(who));
      }
      if (IsPlayer(who) && !silent) {
	if (amount > 0) {
	  notify_format(who, T("%s gives you %d %s."), Name(player),
			amount, ((amount == 1) ? MONEY : MONIES));
	} else {
	  notify_format(who, T("%s took %d %s from you!"), Name(player),
			abs(amount), ((abs(amount) == 1) ? MONEY : MONIES));
	}
      }
      giveto(who, amount);
      safe_integer_sbuf(amount, paid, &pb);
      *pb = '\0';
      real_did_it(player, who, "PAYMENT", NULL, "OPAYMENT", NULL, "APAYMENT",
		  NOTHING, pay_env, NA_INTER_SEE);
    }
  }
}



/** The other syntax of the give command.
 * \param player the enactor/giver.
 * \param arg "something to someone".
 * \param silent if 1, hush the usual messages.
 */
static void
do_give_to(dbref player, char *arg, int silent)
{
  char *s;

  /* Parse out the object and recipient */
  upcasestr(arg);
  s = (char *) string_match(arg, "TO ");
  if (!s) {
    notify(player, T("Did you want to give something *to* someone?"));
    return;
  }
  while ((s > arg) && isspace(*(s - 1))) {
    s--;
  }
  if (s == arg) {
    notify(player, T("Give what?"));
    return;
  }
  *s++ = '\0';
  s = (char *) string_match(s, "TO ");
  s += 3;
  while (*s && isspace(*s))
    s++;
  if (!*s) {
    notify(player, T("Give to whom?"));
    return;
  }
  /* At this point, 'arg' is the object, and 's' is the recipient.
   * But be double-safe to be sure we don't loop.
   */
  if (!*arg || !*s) {
    notify(player, T("I don't know what you mean."));
    return;
  }
  do_give(player, s, arg, silent);
  return;
}