-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdrvlib.c
More file actions
2183 lines (1702 loc) · 63 KB
/
Copy pathdrvlib.c
File metadata and controls
2183 lines (1702 loc) · 63 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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "server.h"
#include "drdata.h"
#include "direction.h"
#include "error.h"
#include "notify.h"
#include "path.h"
#include "do.h"
#include "see.h"
#include "talk.h"
#include "map.h"
#include "container.h"
#include "timer.h"
#include "libload.h"
#include "spell.h"
#include "tool.h"
#include "effect.h"
#include "create.h"
#include "sector.h"
#include "act.h"
#include "date.h"
#include "player_driver.h"
#include "lostcon.h"
#include "balance.h"
#include "drvlib.h"
#include "fight.h"
#include "consistency.h"
#include "log.h"
int walk_or_use_driver(int cn, int dir) {
if (do_walk(cn, dir)) return 1;
//turn(cn,dir);
//if (char_swap(cn)) return 1;
if (do_use(cn, dir, 0)) return 1;
return 0;
}
int walk_swap_or_use_driver(int cn, int dir) {
if (do_walk(cn, dir)) return 1;
turn(cn, dir);
if (char_swap(cn)) return 1;
if (do_use(cn, dir, 0)) return 1;
return 0;
}
int move_driver(int cn, int tx, int ty, int mindist) {
int dir;
dir = pathfinder(ch[cn].x, ch[cn].y, tx, ty, mindist, NULL, 0);
if (dir == -1) return 0;
return walk_or_use_driver(cn, dir);
}
static int swap_check_target(int m) {
if (map[m].flags & MF_MOVEBLOCK) return 0;
if (map[m].flags & MF_DOOR) return 1;
if (map[m].ch && (ch[map[m].ch].flags & (CF_PLAYER | CF_PLAYERLIKE)) && ch[map[m].ch].action == AC_IDLE) return 1;
if (map[m].flags & MF_TMOVEBLOCK) return 0;
return 1;
}
int swap_move_driver(int cn, int tx, int ty, int mindist) {
int dir;
dir = pathfinder(ch[cn].x, ch[cn].y, tx, ty, mindist, swap_check_target, 0);
if (dir == -1) return 0;
return walk_swap_or_use_driver(cn, dir);
}
static int tmove_path(int m) {
if (map[m].flags & MF_MOVEBLOCK) return 0;
if (map[m].flags & MF_DOOR) return 1;
//if (map[m].flags&MF_TMOVEBLOCK) return 0;
return 1;
}
int tmove_driver(int cn, int tx, int ty, int mindist) {
int dir;
dir = pathfinder(ch[cn].x, ch[cn].y, tx, ty, mindist, tmove_path, 0);
if (dir == -1) return 0;
return do_walk(cn, dir);
}
// preliminary version - hunts down co and hits him
// future versions could include prediction of target
// movements, intercept course calculation etc.
int attack_driver(int cn, int co) {
int dx, dy, dir, dir1, dir2 = -1, cost1, cost2 = 999999;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (co < 1 || co >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (cn == co) {
error = ERR_SELF;
return 0;
}
if (!char_see_char(cn, co)) {
error = ERR_NOT_VISIBLE;
return 0;
}
if (!can_attack(cn, co)) {
error = ERR_ILLEGAL_ATTACK;
return 0;
}
dx = ch[co].x - ch[cn].x;
dy = ch[co].y - ch[cn].y;
// if we're close enough, hit him
if (dx == 0 && dy == 1) return do_attack(cn, DX_DOWN, co);
if (dx == 0 && dy == -1) return do_attack(cn, DX_UP, co);
if (dx == 1 && dy == 0) return do_attack(cn, DX_RIGHT, co);
if (dx == -1 && dy == 0) return do_attack(cn, DX_LEFT, co);
dx = ch[co].tox - ch[cn].x;
dy = ch[co].toy - ch[cn].y;
// if he's moving towards us, hit him
if (dx == 0 && dy == 1) return do_attack(cn, DX_DOWN, co);
if (dx == 0 && dy == -1) return do_attack(cn, DX_UP, co);
if (dx == 1 && dy == 0) return do_attack(cn, DX_RIGHT, co);
if (dx == -1 && dy == 0) return do_attack(cn, DX_LEFT, co);
dir1 = pathfinder(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y, 1, NULL, 0);
cost1 = pathcost();
if (ch[co].tox) {
dir2 = pathfinder(ch[cn].x, ch[cn].y, ch[co].tox, ch[co].toy, 1, NULL, 0);
cost2 = pathcost();
}
if (cost1 < cost2 || dir2 == -1) dir = dir1;
else dir = dir2;
if (dir == -1) {
dir = pathbestdir();
if (dir != -1) {
int dist;
dist = abs(ch[cn].x - ch[co].x) + abs(ch[cn].y - ch[co].y);
if (pathbestdist() < dist) return walk_or_use_driver(cn, dir);
else if (!(ch[cn].flags & CF_PLAYER)) return do_idle(cn, TICKS / 4);
else return 0;
}
} else return walk_or_use_driver(cn, dir);
return 0;
}
int attack_driver_direct(int cn, int co) {
int dx, dy, dir;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (co < 1 || co >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (cn == co) {
error = ERR_SELF;
return 0;
}
if (!char_see_char(cn, co)) {
error = ERR_NOT_VISIBLE;
return 0;
}
if (!can_attack(cn, co)) {
error = ERR_ILLEGAL_ATTACK;
return 0;
}
dx = ch[co].x - ch[cn].x;
dy = ch[co].y - ch[cn].y;
// if we're close enough, hit him
if (dx == 0 && dy == 1) return do_attack(cn, DX_DOWN, co);
if (dx == 0 && dy == -1) return do_attack(cn, DX_UP, co);
if (dx == 1 && dy == 0) return do_attack(cn, DX_RIGHT, co);
if (dx == -1 && dy == 0) return do_attack(cn, DX_LEFT, co);
dx = ch[co].tox - ch[cn].x;
dy = ch[co].toy - ch[cn].y;
// if he's moving towards us, hit him
if (dx == 0 && dy == 1) return do_attack(cn, DX_DOWN, co);
if (dx == 0 && dy == -1) return do_attack(cn, DX_UP, co);
if (dx == 1 && dy == 0) return do_attack(cn, DX_RIGHT, co);
if (dx == -1 && dy == 0) return do_attack(cn, DX_LEFT, co);
dir = pathfinder(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y, 1, NULL, 0);
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
int distance_driver(int cn, int co, int distance) {
int dir = -1;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (co < 1 || co >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (cn == co) {
error = ERR_SELF;
return 0;
}
if (!char_see_char(cn, co)) {
error = ERR_NOT_VISIBLE;
return 0;
}
if (step_char_dist(cn, co) == distance) {
error = ERR_ALREADY_THERE;
return 0;
}
if (ch[co].tox) dir = pathfinder(ch[cn].x, ch[cn].y, ch[co].tox, ch[co].toy, distance, NULL, 0);
if (dir == -1) dir = pathfinder(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y, distance, NULL, 0);
if (dir == -1) dir = pathbestdir();
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
int give_driver(int cn, int co) {
int dx, dy, dir, in;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (co < 1 || co >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (cn == co) {
error = ERR_SELF;
return 0;
}
if (!char_see_char(cn, co)) {
error = ERR_NOT_VISIBLE;
return 0;
}
if (!(in = ch[cn].citem)) {
error = ERR_NO_CITEM;
return 0;
}
if ((it[in].flags & IF_QUEST) && !(ch[co].flags & (CF_QUESTITEM | CF_GOD)) && !(ch[cn].flags & (CF_QUESTITEM | CF_GOD))) {
error = ERR_QUESTITEM;
return 0;
}
dx = ch[co].x - ch[cn].x;
dy = ch[co].y - ch[cn].y;
// if we're close enough, hit him
if (dx == 0 && dy == 1) return do_give(cn, DX_DOWN);
if (dx == 0 && dy == -1) return do_give(cn, DX_UP);
if (dx == 1 && dy == 0) return do_give(cn, DX_RIGHT);
if (dx == -1 && dy == 0) return do_give(cn, DX_LEFT);
dir = pathfinder(ch[cn].x, ch[cn].y, ch[co].x, ch[co].y, 1, NULL, 0);
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
// goto item and pick it up
int take_driver(int cn, int in) {
int dx, dy, dir;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (in < 1 || in >= MAXITEM) {
error = ERR_ILLEGAL_ITEMNO;
return 0;
}
if (!char_see_item(cn, in)) {
error = ERR_NOT_VISIBLE;
return 0;
}
dx = it[in].x - ch[cn].x;
dy = it[in].y - ch[cn].y;
// if we're close enough, get it
if (dx == 0 && dy == 1) return do_take(cn, DX_DOWN);
if (dx == 0 && dy == -1) return do_take(cn, DX_UP);
if (dx == 1 && dy == 0) return do_take(cn, DX_RIGHT);
if (dx == -1 && dy == 0) return do_take(cn, DX_LEFT);
dir = pathfinder(ch[cn].x, ch[cn].y, it[in].x, it[in].y, 1, NULL, 0);
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
// goto item and use it
int use_driver(int cn, int in, int spec) {
int dx, dy, dir;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (in < 1 || in >= MAXITEM) {
error = ERR_ILLEGAL_ITEMNO;
return 0;
}
if (!(it[in].flags & IF_USE)) {
error = ERR_NOT_USEABLE;
return 0;
}
//if (!char_see_item(cn,in)) { error=ERR_NOT_VISIBLE; return 0; }
dx = it[in].x - ch[cn].x;
dy = it[in].y - ch[cn].y;
// if we're close enough, use it
if (dx == 0 && dy == 1 && !(it[in].flags & IF_FRONTWALL)) return do_use(cn, DX_DOWN, spec);
if (dx == 0 && dy == -1) return do_use(cn, DX_UP, spec);
if (dx == 1 && dy == 0 && !(it[in].flags & IF_FRONTWALL)) return do_use(cn, DX_RIGHT, spec);
if (dx == -1 && dy == 0) return do_use(cn, DX_LEFT, spec);
if (it[in].flags & IF_FRONTWALL) {
dir = -1;
if (!(map[it[in].x + it[in].y * MAXMAP + 1].flags & (MF_MOVEBLOCK | MF_TMOVEBLOCK))) dir = pathfinder(ch[cn].x, ch[cn].y, it[in].x + 1, it[in].y, 0, NULL, 0);
if (dir == -1 && !(map[it[in].x + it[in].y * MAXMAP + MAXMAP].flags & (MF_MOVEBLOCK | MF_TMOVEBLOCK))) dir = pathfinder(ch[cn].x, ch[cn].y, it[in].x, it[in].y + 1, 0, NULL, 0);
} else dir = pathfinder(ch[cn].x, ch[cn].y, it[in].x, it[in].y, 1, NULL, 0);
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
// goto place and drop item there
int drop_driver(int cn, int x, int y) {
int dx, dy, dir, in, m;
if (cn < 1 || cn >= MAXCHARS) {
error = ERR_ILLEGAL_CHARNO;
return 0;
}
if (x < 1 || x >= MAXMAP - 1 || y < 1 || y >= MAXMAP - 1) {
error = ERR_ILLEGAL_COORDS;
return 0;
}
m = x + y * MAXMAP;
if ((map[m].flags & (MF_MOVEBLOCK | MF_TMOVEBLOCK)) || map[m].it) {
error = ERR_BLOCKED;
return 0;
}
if (!(in = ch[cn].citem)) {
error = ERR_NO_CITEM;
return 0;
}
if (it[in].flags & IF_QUEST) {
error = ERR_QUESTITEM;
return 0;
}
dx = x - ch[cn].x;
dy = y - ch[cn].y;
// if we're close enough, get it
if (dx == 0 && dy == 1) return do_drop(cn, DX_DOWN);
if (dx == 0 && dy == -1) return do_drop(cn, DX_UP);
if (dx == 1 && dy == 0) return do_drop(cn, DX_RIGHT);
if (dx == -1 && dy == 0) return do_drop(cn, DX_LEFT);
dir = pathfinder(ch[cn].x, ch[cn].y, x, y, 1, NULL, 0);
if (dir != -1) return walk_or_use_driver(cn, dir);
return 0;
}
// scan the surroundings for take-able items
#define SCANDIST 20
struct scan_item_data {
int x, y;
};
int scan_item_driver(int cn) {
struct scan_item_data *dat;
int x, y, xs, xe, ys, ye, m, diff, in;
dat = set_data(cn, DRD_SCANITEM, sizeof(struct scan_item_data));
if (!dat) return 0; // oops...
diff = max(abs(ch[cn].x - dat->x), abs(ch[cn].y - dat->y));
if (!dat->x || diff > (SCANDIST / 2 + SCANDIST / 4)) {
xs = max(1, ch[cn].x - SCANDIST);
ys = max(1, ch[cn].y - SCANDIST);
xe = min(MAXMAP - 2, ch[cn].x + SCANDIST);
ye = min(MAXMAP - 2, ch[cn].y + SCANDIST);
for (y = ys; y < ye; y++) {
for (x = xs; x < xe; x++) {
m = x + y * MAXMAP;
if ((in = map[m].it)) notify_char(cn, NT_ITEM, in, 0, 0);
}
}
dat->x = ch[cn].x;
dat->y = ch[cn].y;
return 0;
}
if (ch[cn].x != dat->x) { // scan left/right
xs = max(1, ch[cn].x - SCANDIST);
ys = max(1, ch[cn].y - SCANDIST);
xe = min(MAXMAP - 2, ch[cn].x + SCANDIST);
ye = min(MAXMAP - 2, ch[cn].y + SCANDIST);
if (ch[cn].x > dat->x) xs = min(MAXMAP - 2, dat->x + SCANDIST);
else xe = max(1, dat->x - SCANDIST);
for (y = ys; y < ye; y++) {
for (x = xs; x < xe; x++) {
m = x + y * MAXMAP;
if ((in = map[m].it)) notify_char(cn, NT_ITEM, in, 0, 0);
}
}
}
if (ch[cn].y != dat->y) { // scan up/down
xs = max(1, ch[cn].x - SCANDIST);
ys = max(1, ch[cn].y - SCANDIST);
xe = min(MAXMAP - 2, ch[cn].x + SCANDIST);
ye = min(MAXMAP - 2, ch[cn].y + SCANDIST);
if (ch[cn].y > dat->y) ys = min(MAXMAP - 2, dat->y + SCANDIST);
else ye = max(1, dat->y - SCANDIST);
for (y = ys; y < ye; y++) {
for (x = xs; x < xe; x++) {
m = x + y * MAXMAP;
if ((in = map[m].it)) notify_char(cn, NT_ITEM, in, 0, 0);
}
}
}
dat->x = ch[cn].x;
dat->y = ch[cn].y;
return 0;
}
struct char_mem_data {
int cnt, max;
unsigned int xID[0];
};
// add co to memory number nr (nr=0...7)
int mem_add_driver(int cn, int co, int nr) {
struct char_mem_data *dat;
unsigned int xID;
int n;
if (nr < 0 || nr > 7) return 0;
dat = set_data(cn, DRD_CHARMEM + nr, sizeof(struct char_mem_data));
if (!dat) return 0; // oops...
if (ch[co].ID) xID = ch[co].ID | 0x80000000;
else xID = ch[co].serial & 0x7fffffff;
// already there?
for (n = 0; n < dat->cnt; n++)
if (dat->xID[n] == xID) return 1;
// need more memory?
if (dat->cnt == dat->max) {
dat->max += 8;
dat = set_data(cn, DRD_CHARMEM + nr, sizeof(struct char_mem_data) + sizeof(unsigned int) * dat->max);
if (!dat) return 0;
}
n = dat->cnt++;
dat->xID[n] = xID;
return 1;
}
// check if co is in memory nr (nr=0...7)
// returns TRUE if yes, FALSE otherwise
int mem_check_driver(int cn, int co, int nr) {
struct char_mem_data *dat;
unsigned int xID;
int n;
if (nr < 0 || nr > 7) return 0;
dat = set_data(cn, DRD_CHARMEM + nr, sizeof(struct char_mem_data));
if (!dat) return 0; // oops...
if (ch[co].ID) xID = ch[co].ID | 0x80000000;
else xID = ch[co].serial & 0x7fffffff;
// already there?
for (n = 0; n < dat->cnt; n++)
if (dat->xID[n] == xID) return 1;
return 0;
}
void mem_erase_driver(int cn, int nr) {
struct char_mem_data *dat;
if (nr < 0 || nr > 7) return;
dat = set_data(cn, DRD_CHARMEM + nr, sizeof(struct char_mem_data));
if (!dat) return; // oops...
dat->cnt = 0;
}
// simple parser for name value pairs:
// x=5;y=12;z=15; etc.
// name and value must be at least 64 bytes.
// returns a ptr behind the semicolon of the current nv
// pair or NULL on error/end of string
char *nextnv(char *ptr, char *name, char *value) {
int nlen = 0, vlen = 0;
while (isspace(*ptr)) ptr++;
while (isalpha(*ptr) && nlen++ < 60) *name++ = *ptr++;
*name = 0;
while (isspace(*ptr)) ptr++;
if (*ptr == '=') ptr++;
else return NULL;
while (isspace(*ptr)) ptr++;
while ((isalnum(*ptr) || *ptr == '-') && vlen++ < 60) *value++ = *ptr++;
*value = 0;
if (*ptr == ';') ptr++;
else return NULL;
while (isspace(*ptr)) ptr++;
return ptr;
}
// guesstimate of the distance between cn
// and co. does not take walls etc. into account.
// one straight tile is 2, one diagonal tile is 3.
int char_dist(int cn, int co) {
int dx, dy;
dx = abs(ch[cn].x - ch[co].x);
dy = abs(ch[cn].y - ch[co].y);
if (dx > dy) return (dx << 1) + dy;
else return (dy << 1) + dx;
}
// guesstimate of the distance between f
// and t. does not take walls etc. into account.
// one straight tile is 2, one diagonal tile is 3.
int map_dist(int fx, int fy, int tx, int ty) {
int dx, dy;
dx = abs(fx - tx);
dy = abs(fy - ty);
if (dx > dy) return (dx << 1) + dy;
else return (dy << 1) + dx;
}
// exact number of tile between cn (max of dx,dy)
// and co. does not take walls etc. into account.
int tile_char_dist(int cn, int co) {
int dx, dy;
dx = abs(ch[cn].x - ch[co].x);
dy = abs(ch[cn].y - ch[co].y);
return max(dx, dy);
}
// exact number of tile between cn (sum of dx+dy)
// and co. does not take walls etc. into account.
// does use to-position if available
int step_char_dist(int cn, int co) {
int dx, dy;
if (ch[co].tox) {
dx = abs(ch[cn].x - ch[co].tox);
dy = abs(ch[cn].y - ch[co].toy);
} else {
dx = abs(ch[cn].x - ch[co].x);
dy = abs(ch[cn].y - ch[co].y);
}
return dx + dy;
}
// free up citem by either storing the item
// in the backpack (if there is room) or
// putting it away (on ground)
// returning TRUE means that we make an action
int free_citem_driver(int cn) {
int n;
// store citem in backpack
if (ch[cn].citem) {
for (n = 30; n < INVENTORYSIZE; n++) {
if (!ch[cn].item[n]) {
swap(cn, n);
break;
}
}
}
// if that fails, try to drop it
if (ch[cn].citem) {
if (do_drop(cn, DX_UP)) return 1;
if (do_drop(cn, DX_DOWN)) return 1;
if (do_drop(cn, DX_LEFT)) return 1;
if (do_drop(cn, DX_RIGHT)) return 1;
}
if (ch[cn].citem) return 0;
return 0;
}
// check if co is a valid enemy, that is:
// - alive
// - visible
// - not a member of our group
// - and in our memory <mem>. mem=-1 means dont check
int is_valid_enemy(int cn, int co, int mem) {
if (!co) return 0;
if (cn == co) return 0;
if (ch[cn].group == ch[co].group) return 0;
if (!can_attack(cn, co)) return 0;
if (!char_see_char(cn, co)) return 0;
if (mem == -1) return 1;
if (!mem_check_driver(cn, co, mem)) return 0;
return 1;
}
int remove_item(int in) {
if (it[in].x && it[in].carried) {
elog("remove_item: item %s (%d) pos and carried set", it[in].name, in);
}
if (it[in].x && it[in].contained) {
elog("remove_item: item %s (%d) pos and contained set", it[in].name, in);
}
if (it[in].carried && it[in].contained) {
elog("remove_item: item %s (%d) carried and contained set", it[in].name, in);
}
if (it[in].x) {
return remove_item_map(in);
}
if (it[in].carried) {
return remove_item_char(in);
}
if (it[in].contained) {
return remove_item_container(in);
}
return 0;
}
static void call_item_callback(int driver, int in, int cn, int serial, int dum2) {
if (!it[in].flags) return; // item was deleted, dont call driver
if (it[in].serial != serial) return; // different item by now, dont call driver
item_driver(driver, in, cn);
}
// call item driver at tick due with the parameters specified.
// for timeouts, like closing doors etc.
int call_item(int driver, int in, int cn, int due) {
return set_timer(due, call_item_callback, driver, in, cn, it[in].serial, 0);
}
//-------------- fight driver and helpers ------------------------
/*struct person
{
unsigned int cn;
unsigned int ID;
unsigned short lastx,lasty;
unsigned char visible;
unsigned char hurtme;
};
struct fight_driver_data
{
struct person enemy[10];
int start_dist; // distance from respawn point at which to start attacking
int stop_dist; // distance from respawn point at which to stop attacking
int char_dist; // distance from character we start attacking
int home_x,home_y; // position to compare start_dist and start_dist with
int lasthit;
};*/
int fight_driver_flee_eval_path(int x, int y, int dir) {
int n, score = 0, dx, dy, m, diag;
dx2offset(dir, &dx, &dy, &diag);
for (n = 0; n < 10; n++) {
x += dx;
y += dy;
if (x < 1 || x >= MAXMAP - 1 || y < 1 || y >= MAXMAP - 1) return score;
m = x + y * MAXMAP;
if (map[m].flags & (MF_MOVEBLOCK | MF_TMOVEBLOCK)) return score;
score += 300;
score -= max(map[m].light, map[m].dlight * dlight / 256);
}
return score;
}
int fight_driver_flee(int cn) {
int dir_score[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int dir, n, dist, co, bdir = 0, bscore = -99999, dx, dy, dt, score, mindist = 99;
struct fight_driver_data *dat;
if (!(dat = set_data(cn, DRD_FIGHTDRIVER, sizeof(struct fight_driver_data)))) return 0;
for (n = 0; n < 10; n++) {
if (!dat->enemy[n].cn) continue;
if (!dat->enemy[n].visible) continue;
co = dat->enemy[n].cn;
dist = char_dist(cn, co);
if (dist > 30) continue;
mindist = min(mindist, dist);
score = 5000 - dist * 50;
dx = ch[co].x - ch[cn].x;
dy = ch[co].y - ch[cn].y;
dt = abs(dx) + abs(dy);
if (!dt) continue; // sanity check, should never happen
if (dx > 0) {
dir_score[DX_RIGHT] -= score * abs(dx) / dt;
dir_score[DX_RIGHTUP] -= score * abs(dx) / dt / 2;
dir_score[DX_RIGHTDOWN] -= score * abs(dx) / dt / 2;
dir_score[DX_LEFT] += score * abs(dx) / dt / 4;
dir_score[DX_LEFTUP] += score * abs(dx) / dt / 8;
dir_score[DX_LEFTDOWN] += score * abs(dx) / dt / 8;
}
if (dx < 0) {
dir_score[DX_LEFT] -= score * abs(dx) / dt;
dir_score[DX_LEFTUP] -= score * abs(dx) / dt / 2;
dir_score[DX_LEFTDOWN] -= score * abs(dx) / dt / 2;
dir_score[DX_RIGHT] -= score * abs(dx) / dt / 4;
dir_score[DX_RIGHTUP] -= score * abs(dx) / dt / 8;
dir_score[DX_RIGHTDOWN] -= score * abs(dx) / dt / 8;
}
if (dy > 0) {
dir_score[DX_DOWN] -= score * abs(dy) / dt;
dir_score[DX_LEFTDOWN] -= score * abs(dy) / dt / 2;
dir_score[DX_RIGHTDOWN] -= score * abs(dy) / dt / 2;
dir_score[DX_UP] -= score * abs(dy) / dt / 4;
dir_score[DX_LEFTUP] -= score * abs(dy) / dt / 8;
dir_score[DX_RIGHTUP] -= score * abs(dy) / dt / 8;
}
if (dy < 0) {
dir_score[DX_UP] -= score * abs(dy) / dt;
dir_score[DX_LEFTUP] -= score * abs(dy) / dt / 2;
dir_score[DX_RIGHTUP] -= score * abs(dy) / dt / 2;
dir_score[DX_DOWN] -= score * abs(dy) / dt / 4;
dir_score[DX_LEFTDOWN] -= score * abs(dy) / dt / 8;
dir_score[DX_RIGHTDOWN] -= score * abs(dy) / dt / 8;
}
}
if (mindist > 30) return 0;
if (mindist < 10 && (ch[cn].endurance > 4 * POWERSCALE || ch[cn].speed_mode == SM_FAST)) {
ch[cn].speed_mode = SM_FAST;
} else if (mindist < 10) {
ch[cn].speed_mode = SM_NORMAL;
} else ch[cn].speed_mode = SM_STEALTH;
//say(cn,"mode=%d",ch[cn].speed_mode);
for (dir = 1; dir < 9; dir++) {
dir_score[dir] += fight_driver_flee_eval_path(ch[cn].x, ch[cn].y, dir);
if (dir_score[dir] > bscore) {
bdir = dir;
bscore = dir_score[dir];
}
}
if (bdir && do_walk(cn, bdir)) return 1;
return 0;
}
int fireball_driver(int cn, int co, int serial) {
int dir, dx, dy, dist, left, step, eta, n;
if (!ch[co].flags || ch[co].serial != serial) {
error = ERR_DEAD;
return 0;
}
if (ch[co].action != AC_WALK) return do_fireball(cn, ch[co].x, ch[co].y);
dir = ch[co].dir;
dx2offset(dir, &dx, &dy, NULL);
dist = char_dist(cn, co);
eta = dist / 2 + speed(ch[cn].value[0][V_SPEED], ch[cn].speed_mode, 8);
left = ch[co].duration - ch[co].step;
step = ch[co].duration;
eta -= left;
if (eta <= 0) return do_fireball(cn, ch[co].x, ch[co].y);
for (n = 1; n < 6; n++) {
eta -= step;
if (eta <= 0) return do_fireball(cn, ch[co].x + dx * n, ch[co].y + dy * n);
}
// too far away, time-wise to make any prediction. give up.
return do_fireball(cn, ch[co].x, ch[co].y);
}
int ball_driver(int cn, int co, int serial) {
if (!ch[co].flags || ch[co].serial != serial) {
error = ERR_DEAD;
return 0;
}
return do_ball(cn, ch[co].x, ch[co].y);
}
int fight_driver_dist_from_home(int cn, int co) {
struct fight_driver_data *dat;
int dx, dy;
if (!(dat = set_data(cn, DRD_FIGHTDRIVER, sizeof(struct fight_driver_data)))) return 0;
if (dat->home_x) {
dx = abs(ch[co].x - dat->home_x);
dy = abs(ch[co].y - dat->home_y);
} else {
dx = abs(ch[co].x - ch[cn].tmpx);
dy = abs(ch[co].y - ch[cn].tmpy);
}
if (dx > dy) return (dx << 1) + dy;
else return (dy << 1) + dx;
}
static int fight_driver_fireball_enemy_check(int cn, int co) {
struct fight_driver_data *dat;
int n;
if (!(dat = set_data(cn, DRD_FIGHTDRIVER, sizeof(struct fight_driver_data)))) return 0;
for (n = 0; n < 10; n++)
if (dat->enemy[n].cn == co) return 1;
return 0;
}
// --- helpers to determine effectiveness of different kinds of attacks ---
#define LOW_PRIO 1
#define MED_PRIO 500
#define HIGH_PRIO 750
static int earthmud_driver(int cn, int co, int str) {
int x, y;
if (ch[co].action == AC_WALK) {
x = ch[co].tox + ch[co].tox - ch[co].x;
y = ch[co].toy + ch[co].toy - ch[co].y;
} else {
x = ch[co].x;
y = ch[co].y;
}
if (!str) return x + y * MAXMAP;
return do_earthmud(cn, x, y, str);
}
static int earthrain_driver(int cn, int co, int str) {
int x, y;
if (ch[co].action == AC_WALK) {
x = ch[co].tox + ch[co].tox - ch[co].x;
y = ch[co].toy + ch[co].toy - ch[co].y;
} else {
x = ch[co].x;
y = ch[co].y;
}
if (!str) return x + y * MAXMAP;
return do_earthrain(cn, x, y, str);
}
// earthrain
/*static int check_earthrain_field(int x,int y)
{
int m,n,fn;
m=x+y*MAXMAP;
if (map[m].flags&(MF_SIGHTBLOCK|MF_TSIGHTBLOCK)) return 0;
for (n=0; n<4; n++) {
if ((fn=map[m].ef[n]) && ef[fn].type==EF_EARTHRAIN) return 0;
}
return 1;
}*/
// earthmud
static int check_earthmud_field(int x, int y) {
int m, n, fn;