-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase.c
More file actions
4076 lines (3493 loc) · 120 KB
/
Copy pathbase.c
File metadata and controls
4076 lines (3493 loc) · 120 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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Part of Astonia Server (c) Daniel Brockhaus. Please read license.txt.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include "server.h"
#include "log.h"
#include "notify.h"
#include "direction.h"
#include "do.h"
#include "path.h"
#include "error.h"
#include "drdata.h"
#include "see.h"
#include "death.h"
#include "talk.h"
#include "effect.h"
#include "database.h"
#include "map.h"
#include "create.h"
#include "container.h"
#include "drvlib.h"
#include "tool.h"
#include "spell.h"
#include "effect.h"
#include "light.h"
#include "date.h"
#include "los.h"
#include "skill.h"
#include "item_id.h"
#include "libload.h"
#include "player_driver.h"
#include "task.h"
#include "poison.h"
#include "sector.h"
#include "lab.h"
#include "player.h"
#include "area.h"
#include "misc_ppd.h"
#include "consistency.h"
#include "act.h"
#include "staffer_ppd.h"
#include "transport.h"
// library helper functions needed for init
int ch_driver(int nr, int cn, int ret, int lastact); // character driver (decides next action)
int it_driver(int nr, int in, int cn); // item driver (special cases for use)
int ch_died_driver(int nr, int cn, int co); // called when a character dies
int ch_respawn_driver(int nr, int cn); // called when an NPC is about to respawn
// EXPORTED - character/item driver
int driver(int type, int nr, int obj, int ret, int lastact) {
switch (type) {
case CDT_DRIVER:
return ch_driver(nr, obj, ret, lastact);
case CDT_ITEM:
return it_driver(nr, obj, ret);
case CDT_DEAD:
return ch_died_driver(nr, obj, ret);
case CDT_RESPAWN:
return ch_respawn_driver(nr, obj);
default:
return 0;
}
}
//-----------------------
struct qa {
char *word[20];
char *answer;
int answer_code;
};
struct qa qa[] = {
{{"how", "are", "you", NULL}, "I'm fine!", 0},
{{"hello", NULL}, "Hello, %s!", 0},
{{"hi", NULL}, "Hi, %s!", 0},
{{"greetings", NULL}, "Greetings, %s!", 0},
{{"hail", NULL}, "And hail to you, %s!", 0},
{{"what's", "up", NULL}, "Everything that isn't nailed down.", 0},
{{"what", "is", "up", NULL}, "Everything that isn't nailed down.", 0},
{{"what's", "your", "name", NULL}, NULL, 1},
{{"what", "is", "your", "name", NULL}, NULL, 1},
{{"who", "are", "you", NULL}, NULL, 1},
{{"trade", NULL}, "I am not a normal merchant. Talk to Fred in Cameron or Jeremy in Aston instead.", 0},
{{"buy", NULL}, "I am not a normal merchant. Talk to Fred in Cameron or Jeremy in Aston instead.", 0},
{{"sell", NULL}, "I am not a normal merchant. Talk to Fred in Cameron or Jeremy in Aston instead.", 0},
{{"help", NULL}, "To start trading with someone, say: 'trade with <name>'. Then you hand me the items you wish to exchange. You can stop the deal at any time by saying: 'stop trade'. To check what items I am holding, say: 'show trade'. When you are satisfied with the deal, say 'accept trade'. Both parties must accept the deal to make it take place.", 1},
{{"repeat", NULL}, "To start trading with someone, say: 'trade with <name>'. Then you hand me the items you wish to exchange. You can stop the deal at any time by saying: 'stop trade'. To check what items I am holding, say: 'show trade'. When you are satisfied with the deal, say 'accept trade'. Both parties must accept the deal to make it take place.", 1}};
void lowerstrcpy(char *dst, char *src) {
while (*src) *dst++ = tolower(*src++);
*dst = 0;
}
int analyse_text_driver(int cn, int type, char *text, int co) {
char word[256];
char wordlist[20][256];
int n, w, q;
// ignore game messages
if (type == LOG_SYSTEM || type == LOG_INFO) return 0;
// ignore our own talk
if (cn == co) return 0;
if (!(ch[co].flags & CF_PLAYER)) return 0;
if (char_dist(cn, co) > 12) return 0;
if (!char_see_char(cn, co)) return 0;
while (isalpha(*text)) text++;
while (isspace(*text)) text++;
while (isalpha(*text)) text++;
if (*text == ':') text++;
while (isspace(*text)) text++;
if (*text == '"') text++;
n = w = 0;
while (*text) {
switch (*text) {
case ' ':
case ',':
case ':':
case '?':
case '!':
case '"':
case '.':
if (n) {
word[n] = 0;
lowerstrcpy(wordlist[w], word);
if (strcasecmp(wordlist[w], ch[cn].name)) {
if (w < 20) w++;
}
}
n = 0;
text++;
break;
default:
word[n++] = *text++;
if (n > 250) return 0;
break;
}
}
if (w) {
for (q = 0; q < sizeof(qa) / sizeof(struct qa); q++) {
for (n = 0; n < w && qa[q].word[n]; n++) {
//say(cn,"word = '%s'",wordlist[n]);
if (strcmp(wordlist[n], qa[q].word[n])) break;
}
if (n == w && !qa[q].word[n]) {
if (qa[q].answer) quiet_say(cn, qa[q].answer, ch[co].name, ch[cn].name);
else switch (qa[q].answer_code) {
case 1:
quiet_say(cn, "I'm %s.", ch[cn].name);
break;
}
break;
}
}
}
return 42;
}
struct macro_data {
int victim;
int v_ID;
int state;
int start;
int last;
int val1, val2;
};
struct macro_ppd {
int nextcheck; // realtime
int karma;
};
int macro_set_char(int cn, int x, int y, int nosteptrap) {
if (map[x + y * MAXMAP].flags & (MF_SOUNDBLOCK | MF_SHOUTBLOCK)) return 0;
return set_char(cn, x, y, nosteptrap);
}
int macro_drop_char(int cn, int x, int y, int nosteptrap) {
if (macro_set_char(cn, x, y, nosteptrap)) return 1;
// direct neighbors
if (x < MAXMAP - 1 && macro_set_char(cn, x + 1, y, nosteptrap)) return 1;
if (y < MAXMAP - 1 && macro_set_char(cn, x, y + 1, nosteptrap)) return 1;
if (x > 1 && macro_set_char(cn, x - 1, y, nosteptrap)) return 1;
if (y > 1 && macro_set_char(cn, x, y - 1, nosteptrap)) return 1;
// diagonal neighbors
if (x < MAXMAP - 1 && y < MAXMAP - 1 && macro_set_char(cn, x + 1, y + 1, nosteptrap)) return 1;
if (x > 1 && y < MAXMAP - 1 && macro_set_char(cn, x - 1, y + 1, nosteptrap)) return 1;
if (x < MAXMAP - 1 && y > 1 && macro_set_char(cn, x + 1, y - 1, nosteptrap)) return 1;
if (x > 1 && y > 1 && macro_set_char(cn, x - 1, y - 1, nosteptrap)) return 1;
return 0;
}
int macro_teleport_char_driver(int cn, int x, int y) {
int oldx, oldy;
if (abs(ch[cn].x - x) + abs(ch[cn].y - y) < 2) return 0;
oldx = ch[cn].x;
oldy = ch[cn].y;
remove_char(cn);
ch[cn].action = ch[cn].step = ch[cn].duration = 0;
if (macro_drop_char(cn, x, y, 0)) return 1;
drop_char(cn, oldx, oldy, 0);
return 0;
}
void macro_driver(int cn, int ret, int lastact) {
struct macro_data *dat;
struct macro_ppd *ppd;
int co, talkdir = 0, val;
struct msg *msg, *next;
char *text;
dat = set_data(cn, DRD_MACRODRIVER, sizeof(struct macro_data));
if (!dat) return; // oops...
// loop through our messages
for (msg = ch[cn].msg; msg; msg = next) {
next = msg->next;
// talk back
if (msg->type == NT_TEXT) {
co = msg->dat3;
if (co == dat->victim && ch[co].ID == dat->v_ID) {
text = (char *)(msg->dat2);
while (isalpha(*text)) text++;
while (isspace(*text)) text++;
while (isalpha(*text)) text++;
if (*text == ':') text++;
while (isspace(*text)) text++;
if (*text == '"') text++;
val = atoi(text);
if (val == dat->val1 + dat->val2) {
say(cn, "Very well, %s.", ch[co].name);
ppd = set_data(co, DRD_MACRO_PPD, sizeof(struct macro_ppd));
dlog(co, 0, "answered macro correctly");
if (ppd) {
ppd->karma = ppd->karma * 0.9 + 10;
ppd->nextcheck = realtime + 60 * 5 + RANDOM(60 * 60);
if (ppd->karma > 12) ppd->nextcheck += RANDOM(60 * 60 * 2);
if (ppd->karma > 25) ppd->nextcheck += RANDOM(60 * 60 * 3);
if (ppd->karma > 50) ppd->nextcheck += RANDOM(60 * 60 * 4);
if (ppd->karma > 65) ppd->nextcheck += RANDOM(60 * 60 * 5);
if (ppd->karma > 75) ppd->nextcheck += RANDOM(60 * 60 * 6);
}
if (isxmas) {
int in;
in = create_item("xmaspop");
if (in) {
if (!give_char_item(co, in)) destroy_item(in);
else say(cn, "Merry Christmas, %s!", ch[co].name);
}
} else if (RANDOM(20) == 0) {
say(cn, "Experience is a nice thing, isn't it?");
give_exp(co, level_value(ch[co].level) / 20 + 1);
}
dat->victim++;
dat->state = 0;
} else if (val) {
dlog(co, 0, "answered macro wrongly: '%s' (%d)", text, dat->val1 + dat->val2);
say(cn, "That's wrong, %s. I'll reduce your time by 30 seconds.", ch[co].name);
dat->start -= TICKS * 30;
}
tabunga(cn, co, (char *)(msg->dat2));
}
}
// got an item?
if (msg->type == NT_GIVE) {
if (ch[cn].citem) {
destroy_item(ch[cn].citem);
ch[cn].citem = 0;
}
}
remove_message(cn, msg);
}
// do something. whenever possible, call do_idle with as high a tick count
// as reasonable when doing nothing.
if (isxmas) {
strcpy(ch[cn].name, "Saint Nick");
strcpy(ch[cn].description, "A fat man in red with a ridiculous white beard.");
ch[cn].sprite = 13;
} else {
strcpy(ch[cn].name, "Macro Daemon");
strcpy(ch[cn].description, "Your friendly Macro Daemon.");
ch[cn].sprite = 161;
}
if (dat->state == 0) { // no victim yet
for (co = dat->victim; co < MAXCHARS; co++) {
if (!(ch[co].flags & CF_PLAYER)) continue;
if (ch[co].level < 5) continue; // dont bug newbies
if (realtime - ch[co].login_time < 60 * 5) continue; // dont ask directly after login
// removed if (ch[co].driver==CDR_LOSTCON && !(ch[co].flags&CF_LAG)) continue; // dont try disconnected players, but try those who're just testing lag control
if ((ch[co].flags & CF_INVISIBLE)) continue; // dont try invisible players
if (areaID == 3 && get_section(ch[co].x, ch[co].y) != 20) continue; // no asking in aston, unless in palace
if (get_section(ch[co].x, ch[co].y) == 114) continue; // no asking in UW lab
if (areaID == 22 && ch[co].x >= 76 && ch[co].y >= 19 && ch[co].x <= 94 && ch[co].y <= 37) continue; // no asking in regen lab ritual room
ppd = set_data(co, DRD_MACRO_PPD, sizeof(struct macro_ppd));
if (!ppd) continue;
if (realtime >= ppd->nextcheck) break;
}
if (co == MAXCHARS) { // none found, sleep
dat->victim = 1;
teleport_char_driver(cn, ch[cn].tmpx, ch[cn].tmpy);
do_idle(cn, TICKS);
return;
}
dat->victim = co;
dat->v_ID = ch[co].ID;
dat->state = 1;
}
if (dat->state == 1) { // teleport to victim
co = dat->victim;
if (ch[co].ID == dat->v_ID && macro_teleport_char_driver(cn, ch[co].x, ch[co].y)) {
dat->state = 2; // we're close to him now, go on
} else {
dat->victim++; // teleport didnt work, sleep and cycle to next victim
dat->state = 0;
do_idle(cn, TICKS);
return;
}
}
if (dat->state == 2) {
co = dat->victim;
if (ch[co].ID != dat->v_ID) {
dat->victim++;
dat->state = 0;
do_idle(cn, TICKS);
return;
}
dat->start = ticker;
dat->last = dat->start - TICKS * 31;
dat->val1 = RANDOM(5000) + 1;
dat->val2 = RANDOM(6) + 1;
dat->state = 3;
say(cn, "Hello, %s. I'm %s. I'll ask you a question. If you do not answer correctly within 5 minutes, I'll punish you.", ch[co].name, ch[cn].name);
talkdir = offset2dx(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y);
}
if (dat->state == 3) {
co = dat->victim;
if (ch[co].ID != dat->v_ID || !char_see_char(cn, co)) {
dat->state = 0;
dat->victim++;
do_idle(cn, TICKS);
return;
}
if (ticker - dat->start > TICKS * 60 * 7) {
dat->state = 4;
} else if (ticker - dat->last > TICKS * 30) {
say(cn, "Now, %s, answer me this: What is %d plus %d?", ch[co].name, dat->val1, dat->val2);
talkdir = offset2dx(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y);
dat->last = ticker;
dlog(co, 0, "asked by macro: %d + %d", dat->val1, dat->val2);
}
}
if (dat->state == 4) {
co = dat->victim;
if (ch[co].ID != dat->v_ID) {
dat->victim++;
dat->state = 0;
do_idle(cn, TICKS);
return;
}
say(cn, "Your time is up. Sorry, pal.");
talkdir = offset2dx(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y);
ppd = set_data(co, DRD_MACRO_PPD, sizeof(struct macro_ppd));
if (ppd) {
ppd->karma -= 50;
ppd->nextcheck = realtime + 5 * 60 + RANDOM(5 * 60);
}
task_punish_player(ch[co].ID, 0, 1, "Using macros");
dat->state = 0;
dat->victim++;
dlog(co, 0, "punished by macro");
}
if (talkdir) turn(cn, talkdir);
do_idle(cn, TICKS);
}
void potion_driver(int in, int cn) {
int in2 = 0, empty;
char buf[80];
if (!cn) return; // always make sure its not an automatic call if you don't handle it
if (!it[in].carried) return;
if (areaID == 33) { // long tunnel: no potions!
log_char(cn, LOG_SYSTEM, 0, "You sense that the potion would not work.");
return;
}
// no potions in teufelheim arena
if (areaID == 34 && (map[ch[cn].x + ch[cn].y * MAXMAP].flags & MF_ARENA)) {
log_char(cn, LOG_SYSTEM, 0, "You sense that the potion would not work.");
return;
}
if ((empty = it[in].drdata[0])) {
sprintf(buf, "empty_potion%d", empty);
in2 = create_item(buf);
if (!in2) return;
}
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s drinks a potion.", ch[cn].name);
ch[cn].hp = min(ch[cn].hp + it[in].drdata[1] * POWERSCALE, ch[cn].value[0][V_HP] * POWERSCALE);
ch[cn].mana = min(ch[cn].mana + it[in].drdata[2] * POWERSCALE, ch[cn].value[0][V_MANA] * POWERSCALE);
ch[cn].endurance = min(ch[cn].endurance + it[in].drdata[3] * POWERSCALE, ch[cn].value[0][V_ENDURANCE] * POWERSCALE);
if (empty) replace_item_char(in, in2);
else remove_item_char(in);
free_item(in);
}
int door_driver(int in, int cn) {
int m, in2, n;
if (!it[in].x) return 2;
if (!cn) { // called by timer
if (it[in].drdata[39]) it[in].drdata[39]--; // timer counter
if (!it[in].drdata[0]) return 2; // if the door is closed already, don't open it again
if (it[in].drdata[39]) return 2; // we have more outstanding timer calls, dont close now
}
if (!cn && it[in].drdata[5]) return 2; // no auto-close for this door
m = it[in].x + it[in].y * MAXMAP;
if (it[in].drdata[0] && (map[m].flags & (MF_MOVEBLOCK | MF_TMOVEBLOCK))) { // doorway is blocked
if (!cn) { // timer callback - restart
it[in].drdata[39]++; // timer counter
if (!it[in].drdata[5]) call_item(2, in, 0, ticker + TICKS * 5);
}
return 2;
}
// if the door needs a key, check if the character has it
if (cn && (it[in].drdata[1] || it[in].drdata[2])) {
for (n = 30; n < INVENTORYSIZE; n++)
if ((in2 = ch[cn].item[n]))
if (*(unsigned int *)(it[in].drdata + 1) == it[in2].ID) break;
if (n == INVENTORYSIZE) {
if (!(in2 = ch[cn].citem) || *(unsigned int *)(it[in].drdata + 1) != it[in2].ID) {
log_char(cn, LOG_SYSTEM, 0, "You need a key to use this door.");
return 2;
}
}
log_char(cn, LOG_SYSTEM, 0, "You use %s to %slock the door.", it[in2].name, it[in].drdata[0] ? "" : "un");
}
remove_lights(it[in].x, it[in].y);
if (cn) sound_area(it[in].x, it[in].y, 3);
else sound_area(it[in].x, it[in].y, 2);
if (it[in].drdata[0]) { // it is open, close
it[in].flags |= *(unsigned long long *)(it[in].drdata + 30);
if (it[in].flags & IF_MOVEBLOCK) map[m].flags |= MF_TMOVEBLOCK;
if (it[in].flags & IF_SIGHTBLOCK) map[m].flags |= MF_TSIGHTBLOCK;
if (it[in].flags & IF_SOUNDBLOCK) map[m].flags |= MF_TSOUNDBLOCK;
if (it[in].flags & IF_DOOR) map[m].flags |= MF_DOOR;
it[in].drdata[0] = 0;
it[in].sprite--;
if (it[in].drdata[7]) { // extended door covering three tiles?
m = it[in].x + it[in].y * MAXMAP;
if (map[m + 1].fsprite) map[m + 1].fsprite--;
if (map[m - 1].fsprite) map[m - 1].fsprite--;
if (map[m + MAXMAP].fsprite) map[m + MAXMAP].fsprite--;
if (map[m - MAXMAP].fsprite) map[m - MAXMAP].fsprite--;
}
} else { // it is closed, open
*(unsigned long long *)(it[in].drdata + 30) = it[in].flags & (IF_MOVEBLOCK | IF_SIGHTBLOCK | IF_DOOR | IF_SOUNDBLOCK);
it[in].flags &= ~(IF_MOVEBLOCK | IF_SIGHTBLOCK | IF_DOOR | IF_SOUNDBLOCK);
map[m].flags &= ~(MF_TMOVEBLOCK | MF_TSIGHTBLOCK | MF_DOOR | MF_TSOUNDBLOCK);
it[in].drdata[0] = 1;
it[in].sprite++;
if (it[in].drdata[7]) { // extended door covering three tiles?
m = it[in].x + it[in].y * MAXMAP;
if (map[m + 1].fsprite) map[m + 1].fsprite++;
if (map[m - 1].fsprite) map[m - 1].fsprite++;
if (map[m + MAXMAP].fsprite) map[m + MAXMAP].fsprite++;
if (map[m - MAXMAP].fsprite) map[m - MAXMAP].fsprite++;
}
it[in].drdata[39]++; // timer counter
if (!it[in].drdata[5]) call_item(2, in, 0, ticker + TICKS * 10);
}
reset_los(it[in].x, it[in].y);
if (!it[in].drdata[38] && !reset_dlight(it[in].x, it[in].y)) it[in].drdata[38] = 1;
add_lights(it[in].x, it[in].y);
return 1;
}
void double_door_driver(int in, int cn) {
int in2;
door_driver(in, cn);
if ((in2 = map[(it[in].x) + (it[in].y + 1) * MAXMAP].it) && it[in].drdata[0] != it[in2].drdata[0]) door_driver(in2, cn);
if ((in2 = map[(it[in].x) + (it[in].y - 1) * MAXMAP].it) && it[in].drdata[0] != it[in2].drdata[0]) door_driver(in2, cn);
if ((in2 = map[(it[in].x + 1) + (it[in].y) * MAXMAP].it) && it[in].drdata[0] != it[in2].drdata[0]) door_driver(in2, cn);
if ((in2 = map[(it[in].x - 1) + (it[in].y) * MAXMAP].it) && it[in].drdata[0] != it[in2].drdata[0]) door_driver(in2, cn);
}
void balltrap(int in, int cn) {
int dx, dy, power, dxs, dys;
if (!cn) return; // always make sure its not an automatic call if you don't handle it
if (ch[cn].flags & CF_PLAYER) return; // dont allow players to fire the cannon
dx = it[in].drdata[0] - 128;
dy = it[in].drdata[1] - 128;
power = it[in].drdata[2];
if (dx > 0) dxs = 1;
else if (dx < 0) dxs = -1;
else dxs = 0;
if (dy > 0) dys = 1;
else if (dy < 0) dys = -1;
else dys = 0;
create_ball(0, it[in].x + dxs, it[in].y + dys, it[in].x + dx, it[in].y + dy, power);
}
struct treasure_chest_ppd {
int last_access[200];
};
void chest_driver(int in, int cn) {
struct treasure_chest_ppd *ppd;
int nr, in2, n, timeout;
char name[80];
if (!cn) return; // always make sure its not an automatic call if you don't handle it
/*if (it[in].min_level>ch[cn].level) {
log_char(cn,LOG_SYSTEM,0,"You're not strong enough to use this.");
return;
}*/
// get chest number
nr = it[in].drdata[0];
if (nr >= sizeof(ppd->last_access) / sizeof(int)) {
elog("treasure_chest item driver: chest number (drdata[0]) must be below %d!", sizeof(ppd->last_access) / sizeof(int));
return;
}
// if the chest needs a key, check if the character has it
if (it[in].drdata[1] || it[in].drdata[2]) {
for (n = 30; n < INVENTORYSIZE; n++)
if ((in2 = ch[cn].item[n]))
if (*(unsigned int *)(it[in].drdata + 1) == it[in2].ID) break;
if (n == INVENTORYSIZE) {
if (!(in2 = ch[cn].citem) || *(unsigned int *)(it[in].drdata + 1) != it[in2].ID) {
log_char(cn, LOG_SYSTEM, 0, "You need a key to open this chest.");
return;
}
}
log_char(cn, LOG_SYSTEM, 0, "You use %s to unlock the chest.", it[in2].name);
}
if (ch[cn].citem) {
log_char(cn, LOG_SYSTEM, 0, "Please empty your 'hand' (mouse cursor) first.");
return;
}
ppd = set_data(cn, DRD_TREASURE_CHEST_PPD, sizeof(struct treasure_chest_ppd));
timeout = (*(unsigned short *)(it[in].drdata + 5)) * 60 * 60;
if (ppd->last_access[nr] && ppd->last_access[nr] + timeout > realtime) {
log_char(cn, LOG_SYSTEM, 0, "The chest is empty.");
return;
}
// after-first-death chests in area 1
if (it[in].drdata[7] && ch[cn].deaths < it[in].drdata[7]) {
log_char(cn, LOG_SYSTEM, 0, "The chest is empty.");
return;
}
sprintf(name, "treasure_%d", nr);
in2 = create_item(name);
if (!in2) {
elog("treasure_chest item driver: could not create treasure '%s'", name);
log_char(cn, LOG_SYSTEM, 0, "The chest is empty.");
return;
}
if (ch[cn].flags & CF_PLAYER) dlog(cn, in2, "took from treasure chest");
ch[cn].citem = in2;
ch[cn].flags |= CF_ITEMS;
it[in2].carried = cn;
ppd->last_access[nr] = realtime;
log_char(cn, LOG_SYSTEM, 0, "You got a %s.", it[in2].name);
}
void usetrap(int in, int cn) {
int in2, m, x, y;
if (!cn) return;
x = it[in].drdata[0];
y = it[in].drdata[1];
if (x < 0 || x >= MAXMAP || y < 0 || y >= MAXMAP) return;
m = x + y * MAXMAP;
if (!(in2 = map[m].it)) return;
call_item(it[in2].driver, in2, cn, ticker + TICKS / 2);
}
void steptrap(int in, int cn) {
int in2, m, x, y, dir;
if (!cn) { // use auto-call to set target
if (!it[in].drdata[0]) {
for (dir = 1; dir < 9; dir += 2) {
dx2offset(dir, &x, &y, NULL);
x += it[in].x;
y += it[in].y;
if (x > 0 && x < MAXMAP && y > 0 && y < MAXMAP) {
m = x + y * MAXMAP;
if ((in2 = map[m].it) && it[in2].driver && it[in2].driver != IDR_STEPTRAP) break;
}
dx2offset(dir, &x, &y, NULL);
x = it[in].x + x * 2;
y = it[in].y + y * 2;
if (x > 0 && x < MAXMAP && y > 0 && y < MAXMAP) {
m = x + y * MAXMAP;
if ((in2 = map[m].it) && it[in2].driver && it[in2].driver != IDR_STEPTRAP) break;
}
}
if (dir < 9) {
it[in].drdata[0] = x;
it[in].drdata[1] = y;
} else {
elog("steptrap at %d,%d: couldnt find target!", it[in].x, it[in].y);
}
}
return;
}
x = it[in].drdata[0];
y = it[in].drdata[1];
if (x < 0 || x >= MAXMAP || y < 0 || y >= MAXMAP) return;
m = x + y * MAXMAP;
if (!(in2 = map[m].it)) return;
call_item(it[in2].driver, in2, 0, ticker + 1);
}
void toylight_driver(int in, int cn) {
int light;
if (!cn) return; // we dont handle timer calls
if (it[in].drdata[0]) {
light = it[in].drdata[1];
remove_item_light(in);
it[in].drdata[0] = 0;
it[in].mod_value[0] = 0;
it[in].sprite--;
} else {
light = it[in].drdata[1];
it[in].drdata[0] = 1;
it[in].mod_value[0] = light;
it[in].sprite++;
add_item_light(in);
}
}
void nightlight_driver(int in, int cn) {
int light;
if (cn) return; // we handle ONLY timer calls
if (it[in].drdata[0] && dlight > 80) {
light = it[in].drdata[1];
remove_item_light(in);
it[in].drdata[0] = 0;
it[in].mod_value[0] = 0;
it[in].sprite--;
//add_light(it[in].x,it[in].y,-light,0);
}
if (!it[in].drdata[0] && dlight < 80) {
light = it[in].drdata[1];
it[in].drdata[0] = 1;
it[in].mod_value[0] = light;
it[in].sprite++;
add_item_light(in);
//add_light(it[in].x,it[in].y,light,0);
}
call_item(IDR_NIGHTLIGHT, in, 0, ticker + TICKS * 30);
}
void torch_driver(int in, int cn) {
int n;
if (!cn) { // timer call
for (n = 0; n < MAXMOD; n++) { // is the torch orbed?
if (it[in].mod_index[n] != V_LIGHT && it[in].mod_value[n] > 0 && it[in].mod_index[n] >= 0 && it[in].min_level != 200) {
it[in].min_level = 200;
if (it[in].carried) dlog(it[in].carried, in, "set min level to 200");
}
}
if (it[in].drdata[0]) { // torch burning?
if ((cn = it[in].carried)) {
if (map[ch[cn].x + ch[cn].y * MAXMAP].flags & MF_UNDERWATER) {
log_char(cn, LOG_SYSTEM, 0, "Your hear your torch hiss.");
it[in].drdata[0] = 0;
it[in].mod_value[0] = 0;
it[in].sprite++;
it[in].flags &= (~IF_NODECAY);
update_char(cn);
ch[cn].flags |= CF_ITEMS;
call_item(IDR_TORCH, in, 0, ticker + TICKS * 30);
return;
}
}
it[in].drdata[1]++;
if (it[in].drdata[1] > it[in].drdata[2]) {
if (it[in].carried) log_char(it[in].carried, LOG_SYSTEM, 0, "Your %s expired.", it[in].name);
if (ch[cn].flags & CF_PLAYER) dlog(cn, in, "dropped because it expired");
remove_item(in);
destroy_item(in);
return;
}
if (it[in].x) remove_item_light(in);
it[in].mod_index[0] = V_LIGHT;
it[in].mod_value[0] = min(it[in].drdata[3], it[in].drdata[3] * it[in].drdata[2] / (it[in].drdata[1] + 1) / 2);
if (it[in].carried) {
update_char(it[in].carried);
} else if (it[in].x) add_item_light(in);
call_item(IDR_TORCH, in, 0, ticker + TICKS * 30);
}
} else {
if (it[in].x) return;
for (n = 0; n < MAXMOD; n++) { // remove any non-light modifiers and turn them into orbs
int in2;
if (it[in].mod_index[n] != V_LIGHT && it[in].mod_value[n] > 0 && it[in].mod_index[n] >= 0 && (in2 = create_orb2(it[in].mod_index[n]))) {
if (give_char_item(cn, in2)) {
it[in].mod_value[n]--;
if (cn) dlog(cn, in, "created %s from torch", it[in2].name);
} else destroy_item(in2);
return;
}
}
if (it[in].drdata[0]) { // torch burning?
it[in].drdata[0] = 0;
it[in].mod_value[0] = 0;
it[in].sprite++;
it[in].flags &= (~IF_NODECAY);
update_char(cn);
ch[cn].flags |= CF_ITEMS;
} else {
if (map[ch[cn].x + ch[cn].y * MAXMAP].flags & MF_UNDERWATER) {
log_char(cn, LOG_SYSTEM, 0, "Obviously, thou canst not light thy torch under water.");
return;
}
it[in].drdata[0] = 1;
it[in].mod_index[0] = V_LIGHT;
it[in].mod_value[0] = min(it[in].drdata[3], it[in].drdata[3] * it[in].drdata[2] / (it[in].drdata[1] + 1) / 2);
it[in].sprite--;
it[in].flags |= IF_NODECAY;
update_char(cn);
ch[cn].flags |= CF_ITEMS;
call_item(IDR_TORCH, in, 0, ticker + TICKS * 30);
}
}
}
void recall_driver(int in, int cn) {
int oldx, oldy;
if (!cn) return; // no timer calls please
if (!it[in].carried) return; // can only use if item is carried
if (ch[cn].action == AC_DIE) return; // already dying, cannot use scroll...
if (ch[cn].level > it[in].drdata[0]) { // above rank restriction
log_char(cn, LOG_SYSTEM, 0, "This scroll is too weak to transport someone of your power.");
return;
}
// no recalls in teufelheim arena
if (areaID == 34 && (map[ch[cn].x + ch[cn].y * MAXMAP].flags & MF_ARENA)) {
log_char(cn, LOG_SYSTEM, 0, "You sense that the scroll would not work.");
return;
}
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s uses a scroll of recall and vanishes.", ch[cn].name);
if (ch[cn].resta != areaID) {
if (ch[cn].flags & CF_PLAYER) dlog(cn, in, "dropped because it was used");
remove_item(in);
if (!change_area(cn, ch[cn].resta, ch[cn].restx, ch[cn].resty)) {
log_char(cn, LOG_SYSTEM, 0, "Nothing happens - target area server is down.");
// give item back to char!!
}
destroy_item(in);
return;
}
oldx = ch[cn].x;
oldy = ch[cn].y;
remove_char(cn);
ch[cn].action = ch[cn].step = ch[cn].duration = 0;
player_driver_stop(ch[cn].player, 0);
if (!drop_char(cn, ch[cn].restx, ch[cn].resty, 0)) {
log_char(cn, LOG_SYSTEM, 0, "Please try again soon. Target is busy");
drop_char(cn, oldx, oldy, 0);
} else {
if (ch[cn].flags & CF_PLAYER) dlog(cn, in, "dropped because it was used");
remove_item(in);
destroy_item(in);
}
}
void teleport_driver(int in, int cn) {
int x, y, a, oldx, oldy, aflag, branflag, stopflag;
if (!cn) return; // always make sure its not an automatic call if you don't handle it
x = *(unsigned short *)(it[in].drdata + 0);
y = *(unsigned short *)(it[in].drdata + 2);
a = *(unsigned short *)(it[in].drdata + 4);
aflag = *(unsigned char *)(it[in].drdata + 10);
branflag = *(unsigned char *)(it[in].drdata + 11);
stopflag = *(unsigned char *)(it[in].drdata + 12);
if (branflag) {
struct transport_ppd *dat;
dat = set_data(cn, DRD_TRANSPORT_PPD, sizeof(struct transport_ppd));
if (!dat) return; // oops...
if (!(dat->seen & (1ull << 22)) || !(ch[cn].flags & CF_ARCH)) {
log_char(cn, LOG_SYSTEM, 0, "You've never been to the Brannington Transport. You may not pass.");
return;
}
}
if (aflag && !(ch[cn].flags & CF_ARCH)) {
log_char(cn, LOG_SYSTEM, 0, "This door will only allow Arches to pass.");
return;
}
if (it[in].max_level && ch[cn].level > it[in].max_level) {
log_char(cn, LOG_SYSTEM, 0, "This door will only allow level %d or lower to pass.", it[in].max_level);
return;
}
if (ch[cn].level < it[in].min_level) {
log_char(cn, LOG_SYSTEM, 0, "This door will only allow level %d or higher to pass.", it[in].min_level);
return;
}
if (x < 1 || x > MAXMAP - 2 || y < 1 || y > MAXMAP - 2) {
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s touches a teleport object but nothing happens - BUG (%d,%d,%d).", ch[cn].name, x, y, a);
return;
}
if (a) {
if (!(ch[cn].flags & CF_PLAYER)) return;
if (!change_area(cn, a, x, y)) {
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s touches a teleport object but nothing happens - target area server is down.", ch[cn].name);
}
return;
}
if (!it[in].drdata[6]) log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s touches a magic object and vanishes.", ch[cn].name);
oldx = ch[cn].x;
oldy = ch[cn].y;
remove_char(cn);
if (!drop_char_extended(cn, x, y, 6)) {
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s says: \"Please try again soon. Target is busy.\"", it[in].name);
drop_char(cn, oldx, oldy, 1);
}
if (!it[in].drdata[6]) log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s pops in.", ch[cn].name);
if (stopflag) {
if (ch[cn].player) player_driver_stop(ch[cn].player, 0);
}
}
void teleport_door_driver(int in, int cn) {
int x, y, oldx, oldy, dx, dy, co;
if (!cn) { // always make sure its not an automatic call if you don't handle it
it[in].max_level = it[in].drdata[1];
return;
}
dx = (ch[cn].x - it[in].x);
dy = (ch[cn].y - it[in].y);
if (dx && dy) return;
if (it[in].drdata[0] == 1 && dx == 1) return;
if (it[in].drdata[0] == 2 && dx == -1) return;
if (it[in].drdata[0] == 3 && dy == 1) return;
if (it[in].drdata[0] == 4 && dy == -1) return;
if (it[in].drdata[1] && ch[cn].level > it[in].drdata[1]) {
log_char(cn, LOG_SYSTEM, 0, "This door can only be used by characters of level %d or below.", it[in].drdata[1]);
return;
}
if (it[in].drdata[1] && (map[ch[cn].x + ch[cn].y * MAXMAP].flags & MF_CLAN)) {
log_char(cn, LOG_SYSTEM, 0, "This door can only be used to enter clan areas.");
return;
}
x = it[in].x - dx;
y = it[in].y - dy;
if (x < 1 || x > MAXMAP - 2 || y < 1 || y > MAXMAP - 2) {
log_area(ch[cn].x, ch[cn].y, LOG_INFO, cn, 10, "%s touches a teleport object but nothing happens - BUG (%d,%d).", ch[cn].name, x, y);
return;
}
if (it[in].drdata[1] && (map[x + y * MAXMAP].flags & MF_CLAN)) {