/
thing_list.c
3876 lines (3740 loc) · 127 KB
/
thing_list.c
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
/******************************************************************************/
// Free implementation of Bullfrog's Dungeon Keeper strategy game.
/******************************************************************************/
/** @file thing_list.c
* Things list support.
* @par Purpose:
* Create and maintain list of things.
* @par Comment:
* None.
* @author Tomasz Lis
* @date 12 Feb 2009 - 24 Feb 2010
* @par Copying and copyrights:
* This program 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 2 of the License, or
* (at your option) any later version.
*/
/******************************************************************************/
#include "thing_list.h"
#include "bflib_basics.h"
#include "bflib_math.h"
#include "globals.h"
#include "bflib_sound.h"
#include "packets.h"
#include "lvl_script.h"
#include "light_data.h"
#include "thing_objects.h"
#include "thing_effects.h"
#include "thing_traps.h"
#include "thing_shots.h"
#include "thing_corpses.h"
#include "thing_stats.h"
#include "thing_physics.h"
#include "thing_creature.h"
#include "creature_senses.h"
#include "spdigger_stack.h"
#include "power_hand.h"
#include "magic.h"
#include "map_utils.h"
#include "ariadne_wallhug.h"
#include "config_objects.h"
#include "config_creature.h"
#include "config_magic.h"
#include "creature_states.h"
#include "creature_states_combt.h"
#include "player_instances.h"
#include "engine_camera.h"
#include "gui_topmsg.h"
#include "game_legacy.h"
#include "engine_redraw.h"
#include "keeperfx.hpp"
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************/
Thing_Class_Func class_functions[] = {
NULL,//TCls_Empty
update_object,
update_shot,
update_effect_element,
update_dead_creature,
update_creature,
update_effect,
process_effect_generator,
update_trap,
process_door,//TCls_Door
NULL,
NULL,
NULL,
NULL,//TCls_CaveIn
NULL,
NULL,
NULL,
};
unsigned long thing_create_errors = 0;
/******************************************************************************/
DLLIMPORT struct Thing *_DK_get_nearest_object_at_position(long stl_x, long stl_y);
DLLIMPORT void _DK_place_thing_in_mapwho(struct Thing *thing);
DLLIMPORT long _DK_collide_filter_thing_is_of_type(const struct Thing *creatng, const struct Thing *sectng, long blocked_flags, long shot_lvl);
/******************************************************************************/
/**
* Adds thing at beginning of a StructureList.
* @param thing
* @param list
*/
void add_thing_to_list(struct Thing *thing, struct StructureList *list)
{
if ((thing->alloc_flags & TAlF_IsInStrucList) != 0)
{
ERRORLOG("Thing is already in list");
return;
}
struct Thing *prevtng;
prevtng = INVALID_THING;
if (list->index > 0) {
prevtng = thing_get(list->index);
}
list->count++;
thing->alloc_flags |= TAlF_IsInStrucList;
thing->prev_of_class = 0;
thing->next_of_class = list->index;
if (!thing_is_invalid(prevtng)) {
prevtng->prev_of_class = thing->index;
}
list->index = thing->index;
}
void remove_thing_from_list(struct Thing *thing, struct StructureList *slist)
{
struct Thing *sibtng;
if ((thing->alloc_flags & TAlF_IsInStrucList) == 0)
return;
if (thing->index == slist->index)
{
slist->index = thing->next_of_class;
if (thing->next_of_class > 0)
{
sibtng = thing_get(thing->next_of_class);
if (!thing_is_invalid(sibtng))
{
sibtng->prev_of_class = 0;
}
}
thing->next_of_class = 0;
thing->prev_of_class = 0;
} else
{
if (thing->prev_of_class > 0)
{
sibtng = thing_get(thing->prev_of_class);
if (!thing_is_invalid(sibtng))
{
sibtng->next_of_class = thing->next_of_class;
}
}
if (thing->next_of_class > 0)
{
sibtng = thing_get(thing->next_of_class);
if (!thing_is_invalid(sibtng))
{
sibtng->prev_of_class = thing->prev_of_class;
}
}
thing->prev_of_class = 0;
thing->next_of_class = 0;
}
thing->alloc_flags &= ~TAlF_IsInStrucList;
if (slist->count <= 0) {
ERRORLOG("List has < 0 structures");
return;
}
slist->count--;
}
struct StructureList *get_list_for_thing_class(ThingClass class_id)
{
switch (class_id)
{
case TCls_Object:
return &game.thing_lists[TngList_Objects];
case TCls_Shot:
return &game.thing_lists[TngList_Shots];
case TCls_EffectElem:
return &game.thing_lists[TngList_EffectElems];
case TCls_DeadCreature:
return &game.thing_lists[TngList_DeadCreatrs];
case TCls_Creature:
return &game.thing_lists[TngList_Creatures];
case TCls_Effect:
return &game.thing_lists[TngList_Effects];
case TCls_EffectGen:
return &game.thing_lists[TngList_EffectGens];
case TCls_Trap:
return &game.thing_lists[TngList_Traps];
case TCls_Door:
return &game.thing_lists[TngList_Doors];
case TCls_AmbientSnd:
return &game.thing_lists[TngList_AmbientSnds];
case TCls_CaveIn:
return &game.thing_lists[TngList_CaveIns];
default:
return NULL;
}
}
/** Removes the given thing from a linked list which contains all things of the same class.
*
* @param thing The thing to be unlinked from list chain.
*/
void remove_thing_from_its_class_list(struct Thing *thing)
{
struct StructureList *slist;
slist = get_list_for_thing_class(thing->class_id);
if (slist != NULL) {
remove_thing_from_list(thing, slist);
}
}
ThingIndex get_thing_class_list_head(ThingClass class_id)
{
struct StructureList *slist;
slist = get_list_for_thing_class(class_id);
if (slist != NULL) {
return slist->index;
}
return 0;
}
/** Adds the given thing to a linked list which contains all things of the same class.
*
* @param thing The thing to be linked with list chain.
*/
void add_thing_to_its_class_list(struct Thing *thing)
{
struct StructureList *slist;
slist = get_list_for_thing_class(thing->class_id);
if (slist != NULL)
add_thing_to_list(thing, slist);
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_not_specdigger(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == TCls_Creature)
{
if ((get_creature_model_flags(thing) & CMF_IsSpecDigger) == 0)
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_call_bool_filter(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
Thing_Bool_Filter matcher_cb = (Thing_Bool_Filter)param->ptr3;
if ((matcher_cb != NULL) && matcher_cb(thing))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_thing_pos_thing_filter_is_enemy_which_can_be_attacked_by_creature(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
struct Thing *creatng;
creatng = thing_get(param->num1);
if (players_are_enemies(creatng->owner, thing->owner))
{
if (creature_will_attack_creature(creatng, thing))
{
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &creatng->mappos);
}
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long highest_score_thing_filter_is_enemy_within_distance_which_can_be_attacked_by_creature(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
struct Thing *creatng;
creatng = thing_get(param->num1);
if (creature_will_attack_creature(creatng, thing) && !creature_has_creature_in_combat(creatng, thing))
{
long distance;
CrAttackType attack_type;
distance = get_combat_distance(creatng, thing);
if (distance >= param->num2) {
return -1;
}
attack_type = creature_can_have_combat_with_creature(creatng, (struct Thing *)thing, distance, param->num3, 0);
if (attack_type > AttckT_Unset)
{
long score;
score = get_combat_score(creatng, thing, attack_type, distance);
return score;
}
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_is_enemy_of_able_to_attack_and_not_specdigger(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((thing->class_id == TCls_Creature) && players_are_enemies(param->plyr_idx, thing->owner))
{
if (!creature_is_being_unconscious(thing) && !thing_is_picked_up(thing) && !creature_is_kept_in_custody_by_enemy(thing))
{
if ((get_creature_model_flags(thing) & CMF_IsSpecDigger) == 0)
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_is_creature_of_model_owned_and_controlled_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == TCls_Creature)
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
if (!creature_is_being_unconscious(thing) && !thing_is_picked_up(thing) && !creature_is_kept_in_custody_by_enemy(thing))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_is_thing_of_class_and_model_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
if (!thing_is_picked_up(thing))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_can_be_keeper_power_target_simp(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if (can_cast_power_on_thing(param->plyr_idx, thing, param->model_id, param->num3))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_can_be_keeper_power_target(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if (can_cast_spell(param->plyr_idx, param->model_id,
coord_subtile(param->num1), coord_subtile(param->num2), thing, param->num3)) {
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/** Deprecated filter function. */
long creature_near_filter_is_enemy_of_and_not_specdigger(const struct Thing *thing, FilterParam plyr_idx)
{
if (thing->owner == plyr_idx)
return false;
return ((get_creature_model_flags(thing) & CMF_IsSpecDigger) == 0);
}
long creature_near_filter_is_owned_by(const struct Thing *thing, FilterParam plyr_idx)
{
struct SlabMap *slb;
if (thing->owner == plyr_idx)
{
return true;
}
if (creature_is_kept_in_custody(thing))
{
slb = get_slabmap_for_subtile(thing->mappos.x.stl.num, thing->mappos.y.stl.num);
if (slabmap_owner(slb) == plyr_idx)
return true;
}
return false;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_is_slappable(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if (!thing_is_picked_up(thing) && thing_slappable(thing, param->plyr_idx))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long near_map_block_thing_filter_is_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == param->class_id)
{
switch(param->class_id)
{
case TCls_Creature:
if ((param->plyr_idx == -1) || creature_near_filter_is_owned_by(thing, param->plyr_idx))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
break;
default:
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
// Prepare reference Coord3d struct for distance computation
struct Coord3d refpos;
refpos.x.val = param->num1;
refpos.y.val = param->num2;
refpos.z.val = 0;
// This function should return max value when the distance is minimal, so:
return LONG_MAX-get_2d_distance(&thing->mappos, &refpos);
}
break;
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_of_class_and_model_and_owned_by_or_allied_with(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == param->class_id)
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || players_are_mutual_allies(thing->owner,param->plyr_idx))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_of_class_and_model_and_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == param->class_id)
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_food_available_to_eat_and_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == TCls_Object)
{
if (object_is_mature_food(thing))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
if (thing->class_id == TCls_Creature)
{
if (creature_affected_by_spell(thing,SplK_Chicken) && (thing->health > 0))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_creature_of_model_training_and_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
struct CreatureControl *cctrl;
if (thing->class_id == TCls_Creature)
{
if ((thing->model == param->model_id) || (param->model_id == -1))
{
if ((thing->owner == param->plyr_idx) || (param->plyr_idx == -1))
{
if (((int)thing->index != param->num1) || (param->num1 == -1))
{
cctrl = creature_control_get_from_thing(thing);
if ((thing->active_state == CrSt_Training) && (cctrl->byte_9A > 1))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_call_bool_filter(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
Thing_Bool_Filter matcher_cb = (Thing_Bool_Filter)param->ptr3;
if ((matcher_cb != NULL) && matcher_cb(thing))
{
return LONG_MAX;
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_call_neg_bool_filter(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if ((param->model_id == -1) || (thing->model == param->model_id))
{
if ((param->plyr_idx == -1) || (thing->owner == param->plyr_idx))
{
Thing_Bool_Filter matcher_cb = (Thing_Bool_Filter)param->ptr3;
if ((matcher_cb != NULL) && !matcher_cb(thing))
{
return LONG_MAX;
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_trap_of_model_armed_and_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == TCls_Trap)
{
if ((thing->model == param->model_id) || (param->model_id == -1))
{
if ((thing->owner == param->plyr_idx) || (param->plyr_idx == -1))
{
if ((param->num1 && (thing->trap.num_shots != 0))
|| (!param->num1 && (thing->trap.num_shots == 0)))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_door_of_model_locked_and_owned_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if (thing->class_id == TCls_Door)
{
if ((thing->model == param->model_id) || (param->model_id == -1))
{
if ((thing->owner == param->plyr_idx) || (param->plyr_idx == -1))
{
if ((param->num1 && (thing->door.is_locked))
|| (!param->num1 && (!thing->door.is_locked)))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
/**
* Filter function.
* @param thing The thing being checked.
* @param param Parameters exchanged between filter calls.
* @param maximizer Previous value which made a thing pass the filter.
*/
long anywhere_thing_filter_is_gold_on_owned_ground_pickable_by(const struct Thing *thing, MaxTngFilterParam param, long maximizer)
{
if ((param->class_id == -1) || (thing->class_id == param->class_id))
{
if (((param->model_id == -1) && object_is_gold_pile(thing)) || (thing->model == param->model_id))
{
if (!thing_is_dragged_or_pulled(thing))
{
if ((param->plyr_idx == -1) || ((get_slab_owner_thing_is_on(thing) == param->plyr_idx) && can_thing_be_picked_up_by_player(thing, param->plyr_idx)))
{
// Return the largest value to stop sweeping
return LONG_MAX;
}
}
}
}
// If conditions are not met, return -1 to be sure thing will not be returned.
return -1;
}
TbBool delete_if_dead_creature(struct Thing *thing)
{
if (thing->class_id == TCls_DeadCreature) {
delete_thing_structure(thing, 0);
return true;
}
return false;
}
TngUpdateRet switch_object_on_destoyed_slab_to_new_owner(struct Thing *thing, ModTngFilterParam param)
{
SYNCDBG(18,"Starting for %s index %d",thing_model_name(thing),(int)thing->index);
if (thing_is_picked_up(thing) || thing_is_dragged_or_pulled(thing))
{
return TUFRet_Unchanged;
}
if (thing_is_object(thing))
{
if (object_is_gold_pile(thing) && (thing->owner != param->num1))
{
change_object_owner(thing, param->num1);
return TUFRet_Modified;
}
}
return TUFRet_Unchanged;
}
/**
* Makes per game turn update of all things in given StructureList.
* @param list List of things to process.
* @return Returns checksum computed from status of all things in list.
*/
TbBigChecksum update_things_in_list(struct StructureList *list)
{
struct Thing *thing;
unsigned long k;
TbBigChecksum sum;
int i;
SYNCDBG(18,"Starting");
sum = 0;
k = 0;
i = list->index;
while (i != 0)
{
thing = thing_get(i);
if (thing_is_invalid(thing))
{
ERRORLOG("Jump to invalid thing detected");
break;
}
i = thing->next_of_class;
// Per-thing code
if ((thing->alloc_flags & TAlF_IsFollowingLeader) == 0)
{
if ((thing->alloc_flags & TAlF_IsInLimbo) != 0) {
update_thing_animation(thing);
} else {
update_thing(thing);
}
}
sum += get_thing_checksum(thing);
// Per-thing code ends
k++;
if (k > THINGS_COUNT)
{
ERRORLOG("Infinite loop detected when sweeping things list");
erstat_inc(ESE_InfChainTngPerClass);
break;
}
}
SYNCDBG(19,"Finished, %d items, checksum %06lX",(int)k,(unsigned long)sum);
return sum;
}
/**
* Makes per game turn update of cave in things, using proper StructureList.
* @return Returns amount of cave in things in list.
*/
unsigned long update_cave_in_things(void)
{
struct Thing *thing;
unsigned long k;
int i;
k = 0;
const struct StructureList *slist;
slist = get_list_for_thing_class(TCls_CaveIn);
i = slist->index;
while (i != 0)
{
thing = thing_get(i);
if (thing_is_invalid(thing))
{
ERRORLOG("Jump to invalid thing detected");
break;
}
i = thing->next_of_class;
// Per-thing code
update_cave_in(thing);
// Per-thing code ends
k++;
if (k > THINGS_COUNT)
{
ERRORLOG("Infinite loop detected when sweeping things list");
erstat_inc(ESE_InfChainTngPerClass);
break;
}
}
return k;
}
/**
* Updates sounds of things from given StructureList.
* Returns amount of items in the list.
*/
unsigned long update_things_sounds_in_list(struct StructureList *list)
{
struct Thing *thing;
unsigned long k;
int i;
SYNCDBG(18,"Starting");
k = 0;
i = list->index;
while (i != 0)
{
thing = thing_get(i);
if (thing_is_invalid(thing))
{
ERRORLOG("Jump to invalid thing detected");
break;
}
i = thing->next_of_class;
// Per-thing code
update_thing_sound(thing);
// Per-thing code ends
k++;
if (k > THINGS_COUNT)
{
ERRORLOG("Infinite loop detected when sweeping things list");
erstat_inc(ESE_InfChainTngPerClass);
break;
}
}
return k;
}
unsigned long update_creatures_not_in_list(void)
{
struct Thing *thing;
unsigned long k;
int i;
SYNCDBG(18,"Starting");
k = 0;
i = game.thing_lists[TngList_Creatures].index;
while (i != 0)
{
thing = thing_get(i);
if (thing_is_invalid(thing))
{
ERRORLOG("Jump to invalid thing detected");
break;
}
i = thing->next_of_class;
// Per-thing code
if (thing->index == 0)
{
ERRORLOG("Some THING has been deleted during the processing of another thing");
break;
}
if ((thing->alloc_flags & TAlF_IsFollowingLeader) != 0)
{
if ((thing->alloc_flags & TAlF_IsInLimbo) != 0) {
update_thing_animation(thing);
} else {
update_thing(thing);