-
Notifications
You must be signed in to change notification settings - Fork 82
/
actors.cc
5601 lines (5165 loc) · 150 KB
/
actors.cc
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
/*
* actors.cc - Game actors.
*
* Copyright (C) 1998-1999 Jeffrey S. Freedman
* Copyright (C) 2000-2022 The Exult Team
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "actors.h"
#include "Astar.h"
#include "Audio.h"
#include "Face_stats.h"
#include "Gump_manager.h"
#include "Paperdoll_gump.h"
#include "Zombie.h"
#include "actions.h"
#include "ammoinf.h"
#include "animate.h"
#include "armorinf.h"
#include "cheat.h"
#include "chunks.h"
#include "combat.h"
#include "combat_opts.h"
#include "contain.h"
#include "dir.h"
#include "effects.h"
#include "egg.h"
#include "exult.h"
#include "exult_constants.h"
#include "frameseq.h"
#include "frflags.h"
#include "game.h"
#include "gameclk.h"
#include "gamemap.h"
#include "gamewin.h"
#include "ignore_unused_variable_warning.h"
#include "imagewin.h"
#include "items.h"
#include "miscinf.h"
#include "monsters.h"
#include "monstinf.h"
#include "npcdollinf.h"
#include "npctime.h"
#include "objiter.h"
#include "palette.h"
#include "party.h"
#include "ready.h"
#include "spellbook.h"
#include "ucmachine.h"
#include "ucsched.h"
#include "ucscriptop.h"
#include "usefuns.h"
#include "weaponinf.h"
#include <algorithm> /* swap. */
#include <cstdlib>
#include <cstring>
#include <iostream> /* Debugging. */
#include <map>
#include <memory>
#include <set>
#ifdef USE_EXULTSTUDIO
# include "mouse.h"
# include "objserial.h"
# include "servemsg.h"
# include "server.h"
#endif
using std::cerr;
using std::cout;
using std::endl;
using std::rand;
using std::string;
using std::swap;
Game_object_shared Actor::editing;
extern bool combat_trace;
// Party positions
// Direction, Party num, xy (tile) from leader
//
// Please Don't Touch - Colourless
//
const short Actor::party_pos[4][10][2] = {
// North Facing
{ {-2, 2},
{2, 2},
{0, 4},
{-4, 4},
{4, 4},
{-2, 6},
{2, 6},
{0, 8},
{-4, 8},
{4, 8} },
// East Facing,
{{-2, -2},
{-2, 2},
{-4, 0},
{-4, -4},
{-4, 4},
{-6, -2},
{-6, 2},
{-8, 0},
{-8, -4},
{-8, 4}},
// South Facing
{{-2, -2},
{2, -2},
{0, -4},
{-4, -4},
{4, -4},
{-2, -6},
{2, -6},
{0, -8},
{-4, -8},
{4, -8}},
// West Facing
{ {2, -2},
{2, 2},
{4, 0},
{4, -4},
{4, 4},
{6, -2},
{6, 2},
{8, 0},
{8, -4},
{8, 4} }
};
// Actor frame to substitute when a frame is empty (as some are):
uint8 visible_frames[16]
= {Actor::standing, // Standing.
Actor::standing, // Steps.
Actor::standing,
Actor::standing, // Ready.
Actor::raise2_frame, // 1-handed strikes => 2-handed.
Actor::reach2_frame, Actor::strike2_frame,
Actor::raise1_frame, // 2-handed => 1-handed.
Actor::reach1_frame, Actor::strike1_frame,
Actor::standing, // When you can't sit...
Actor::kneel_frame, // When you can't bow.
Actor::bow_frame, // When you can't kneel.
Actor::standing, // Can't lie.
Actor::strike2_frame, // Can't raise hands.
Actor::ready_frame}; // Can't strech arms outward.
// Set up actor's frame lists.
// Most NPC's walk with a 'stand'
// frame between steps.
const int FRAME_NUM = 5;
uint8 npc_north_framenums[FRAME_NUM] = {0, 1, 0, 2, 0},
npc_south_framenums[FRAME_NUM] = {16, 17, 16, 18, 16},
npc_east_framenums[FRAME_NUM] = {48, 49, 48, 50, 48},
npc_west_framenums[FRAME_NUM] = {32, 33, 32, 34, 32};
static Frames_sequence npc_north_frames(FRAME_NUM, npc_north_framenums);
static Frames_sequence npc_south_frames(FRAME_NUM, npc_south_framenums);
static Frames_sequence npc_east_frames(FRAME_NUM, npc_east_framenums);
static Frames_sequence npc_west_frames(FRAME_NUM, npc_west_framenums);
// Avatar just walks left, right.
uint8 avatar_north_framenums[3] = {0, 1, 2},
avatar_south_framenums[3] = {16, 17, 18},
avatar_east_framenums[3] = {48, 49, 50},
avatar_west_framenums[3] = {32, 33, 34};
static Frames_sequence avatar_north_frames(3, avatar_north_framenums);
static Frames_sequence avatar_south_frames(3, avatar_south_framenums);
static Frames_sequence avatar_east_frames(3, avatar_east_framenums);
static Frames_sequence avatar_west_frames(3, avatar_west_framenums);
Frames_sequence* Actor::avatar_frames[4] = {nullptr, nullptr, nullptr, nullptr};
Frames_sequence* Actor::npc_frames[4] = {nullptr, nullptr, nullptr, nullptr};
const std::array sea_serpent_attack_frames = {1, 2, 3};
const std::array reach_attack_frames1 = {3, 6};
const std::array raise_attack_frames1 = {3, 4, 6};
const std::array fast_swing_attack_frames1 = {3, 5, 6};
const std::array slow_swing_attack_frames1 = {3, 4, 5, 6};
const std::array reach_attack_frames2 = {3, 9};
const std::array raise_attack_frames2 = {3, 7, 9};
const std::array fast_swing_attack_frames2 = {3, 8, 9};
const std::array slow_swing_attack_frames2 = {3, 7, 8, 9};
// inline int Is_attack_frame(int i) { return i >= 3 && i <= 9; }
inline bool Is_attack_frame(int i) {
return i == 6 || i == 9;
}
inline int Get_dir_from_frame(int i) {
return ((((i & 16) / 8) - ((i & 32) / 32)) + 4) % 4;
}
/**
* Provides attribute/value pairs.
*/
class Actor_attributes {
/// The attribute names. These are shared among all actors.
static std::set<string>* strings;
using Att_map = std::map<const char*, int>;
/// The attribute name > value map.
Att_map map;
public:
/// Basic constructor. Initializes the attribute names.
Actor_attributes() {
if (!strings) {
strings = new std::set<string>;
}
}
/// Sets an attribute's value and, if needed, adds it to
/// the attribute name list.
/// @param nm The name of the attribute to be set.
/// @param val Value to set the attribute to.
void set(const char* nm, int val) {
auto siter = strings->find(nm);
if (siter == strings->end()) {
siter = strings->insert(nm).first;
}
nm = siter->c_str();
map[nm] = val;
}
/// Gets an attribute's value, if it is in the list.
/// @param nm The name of the attribute to be gotten.
/// @return val Current value of the attribute, or
/// zero if the attribute is not on the list.
int get(const char* nm) { // Returns 0 if not set.
auto siter = strings->find(nm);
if (siter == strings->end()) {
return 0;
}
nm = siter->c_str();
auto it = map.find(nm);
return it == map.end() ? 0 : it->second;
}
/// Gets all attributes for the current actor.
/// @param attlist (name, value) vector containig all attributes.
void get_all(std::vector<std::pair<const char*, int>>& attlist) {
for (auto& it : map) {
attlist.emplace_back(it);
}
}
};
std::set<string>* Actor_attributes::strings = nullptr;
/**
* Get/create timers.
* @return
*/
Npc_timer_list* Actor::need_timers() {
if (!timers) {
timers = std::make_unique<Npc_timer_list>(this);
}
return timers.get();
}
/**
* Initialize frames, properties and spots.
*/
void Actor::init() {
if (!avatar_frames[0]) {
init_default_frames();
}
for (auto& prop : properties) {
prop = 0;
}
for (auto& spot : spots) {
spot = nullptr;
}
}
/**
* Find (best) ammo of given type.
* @param family Desired ammo family shape.
* @param needed Minimum quantity needed.
* @return Pointer to object, if found.
*/
Game_object* Actor::find_best_ammo(int family, int needed) {
Game_object* best = nullptr;
int best_strength = -20;
Game_object_vector vec; // Get list of all possessions.
vec.reserve(50);
get_objects(vec, c_any_shapenum, c_any_qual, c_any_framenum);
for (auto* obj : vec) {
if (obj->inside_locked()
|| !In_ammo_family(obj->get_shapenum(), family)) {
continue;
}
const Ammo_info* ainf = obj->get_info().get_ammo_info();
if (!ainf) { // E.g., musket ammunition doesn't have it.
continue;
}
// Can't use it.
if (obj->get_quantity() < needed) {
continue;
}
// Calc ammo strength.
int strength = ainf->get_base_strength();
// Favor those with more shots remaining.
if (obj->get_quantity() < 5 * needed) {
strength /= 3;
} else if (obj->get_quantity() < 10 * needed) {
strength /= 2;
}
if (strength > best_strength) {
best = obj;
best_strength = strength;
}
}
return best;
}
/**
* Get effective maximum range for weapon taking in consideration
* the actor's strength and combat.
* @param winf Pointer to weapon information of the current weapon,
* or null for no weapon.
* @param reach Weapon reach, or -1 to use weapon's.
* @return Weapon's effective range.
*/
int Actor::get_effective_range(const Weapon_info* winf, int reach) const {
if (reach < 0) {
if (!winf) {
const Monster_info* minf = get_info().get_monster_info();
return minf ? minf->get_reach()
: Monster_info::get_default()->get_reach();
}
reach = winf->get_range();
}
const int uses
= winf ? winf->get_uses() : static_cast<int>(Weapon_info::melee);
if (!uses || uses == Weapon_info::ranged) {
return reach;
} else {
int eff_range;
const int str = get_effective_prop(static_cast<int>(Actor::strength));
const int combat = get_effective_prop(static_cast<int>(Actor::combat));
if (str < combat) {
eff_range = str;
} else {
eff_range = combat;
}
if (uses == Weapon_info::good_thrown) {
eff_range *= 2;
}
if (eff_range < reach) {
eff_range = reach;
}
if (eff_range > 31) {
eff_range = 31;
}
return eff_range;
}
}
/**
* Find ammo used by weapon.
* @param weapon The weapon shape.
* @param needed Minimum quantity needed.
* @param recursive Whether or not to search inside backpacks and bags.
* @return Pointer to object if found.
*/
Game_object* Actor::find_weapon_ammo(int weapon, int needed, bool recursive) {
if (weapon < 0) {
return nullptr;
}
const Weapon_info* winf = ShapeID::get_info(weapon).get_weapon_info();
if (!winf) {
return nullptr;
}
const int family = winf->get_ammo_consumed();
if (family >= 0) {
Game_object* aobj = get_readied(quiver);
if (aobj && In_ammo_family(aobj->get_shapenum(), family)
&& aobj->get_quantity() >= needed) {
return aobj; // Already readied.
} else if (recursive) {
return find_best_ammo(family, needed);
}
return nullptr;
}
// Search readied weapons first.
static const Ready_type_Exult wspots[] = {lhand, rhand, back_2h, belt};
for (auto wspot : wspots) {
Game_object* obj = spots[wspot];
if (!obj || obj->get_shapenum() != weapon) {
continue;
}
const Shape_info& inf = obj->get_info();
if (family == -2) {
if (!inf.has_quality() || obj->get_quality() >= needed) {
return obj;
}
}
// Family -1 and family -3.
else if (obj->get_quantity() >= needed) {
return obj;
}
}
// Now recursively search all contents.
return recursive ? Container_game_object::find_weapon_ammo(weapon)
: nullptr;
}
/**
* Swap new ammo with old.
* @param newammo Pointer to new ammo object.
*/
void Actor::swap_ammo(Game_object* newammo) {
Game_object* aobj = get_readied(quiver);
if (aobj == newammo) {
return; // Already what we need.
// Keep these from getting deleted:
}
Game_object_shared aobj_shared;
Game_object_shared newammo_shared;
if (aobj) { // Something there already?
aobj->remove_this(&aobj_shared); // Remove it.
}
newammo->remove_this(&newammo_shared);
add(newammo, true); // Should go to the right place.
if (aobj) { // Put back old ammo.
add(aobj, true);
}
}
/**
* Recursivelly searches for ammunition for a given weapon, if needed.
* @param npc The NPC to be searched.
* @param bobj The weapon we want to check.
* @param ammo Optional pointer that receives a pointer to the best ammunition
* found for the given weapon.
* @param recursive Whether or not to search inside backpacks and bags.
* @return true if the weapon can be used, ammo is pointer to best ammunition.
*/
static inline bool Is_weapon_usable(
Actor* npc, Game_object* bobj, Game_object** ammo = nullptr,
bool recursive = true) {
if (ammo) {
*ammo = nullptr;
}
const Weapon_info* winf = bobj->get_info().get_weapon_info();
if (!winf) {
return false; // Not a weapon.
}
Game_object* aobj = nullptr; // Check ranged first.
int need_ammo = npc->get_weapon_ammo(
bobj->get_shapenum(), winf->get_ammo_consumed(),
winf->get_projectile(), true, &aobj, recursive);
if (!need_ammo) {
return true;
}
// Try melee if the weapon is not ranged.
else if (!aobj && winf->get_uses() != Weapon_info::ranged) {
need_ammo = npc->get_weapon_ammo(
bobj->get_shapenum(), winf->get_ammo_consumed(),
winf->get_projectile(), false, &aobj, recursive);
}
if (need_ammo && !aobj) {
return false;
}
if (ammo) {
*ammo = aobj;
}
return true;
}
/**
* Ready ammo for weapon being carried.
* @return Returns true if successful.
*/
bool Actor::ready_ammo() {
Game_object* weapon = spots[lhand];
if (!weapon) {
return false;
}
const Shape_info& info = weapon->get_info();
const Weapon_info* winf = info.get_weapon_info();
if (!winf) {
return false;
}
const int ammo = winf->get_ammo_consumed();
if (ammo < 0) {
// Ammo not needed.
return !winf->uses_charges() || !info.has_quality()
|| weapon->get_quality() > 0; // Uses charges, but none left.
}
Game_object* found = nullptr;
// Try non-recursive search for ammo first.
const bool usable = Is_weapon_usable(this, weapon, &found, false);
if (usable) { // Ammo is available and ready.
return true;
}
// Try recursive search now.
found = find_best_ammo(ammo);
if (!found) {
return false;
}
swap_ammo(found);
return true;
}
/**
* If no shield readied, look through all possessions for the best one.
* @return Returns true if successful.
*/
bool Actor::ready_best_shield() {
if (two_handed) {
return false;
}
if (spots[rhand]) {
const Shape_info& inf = spots[rhand]->get_info();
if (is_in_party() || inf.get_armor() || inf.get_armor_immunity()) {
return inf.get_armor() || inf.get_armor_immunity();
}
}
Game_object* old_rhand = nullptr;
Game_object_shared rhand_keep;
Game_object_shared best_keep;
if (spots[rhand]) { // remove old offhand item
old_rhand = spots[rhand];
old_rhand->remove_this(&rhand_keep);
}
Game_object_vector vec; // Get list of all possessions.
vec.reserve(50);
get_objects(vec, c_any_shapenum, c_any_qual, c_any_framenum);
Game_object* best = nullptr;
int best_strength = -20;
for (auto* obj : vec) {
if (obj->inside_locked()) {
continue;
}
const Shape_info& info = obj->get_info();
// Only want those that can be readied in hand.
const int ready = info.get_ready_type();
if (ready != lhand && ready != backpack) {
continue;
}
const Armor_info* arinf = info.get_armor_info();
if (!arinf) {
continue; // Not a shield.
}
if (spots[lhand] == obj) { // Don't take the weapon.
continue;
}
const int strength = arinf->get_base_strength();
if (strength > best_strength) {
best = obj;
best_strength = strength;
}
}
if (!best) {
if (old_rhand) { // add offhand item back to where it was
add(old_rhand, true);
}
return false;
}
// Spot is free already.
best->remove_this(&best_keep);
add(best, true); // Should go to the right place.
if (old_rhand && old_rhand != best) { // don't add twice
add(old_rhand, true);
}
return true;
}
/**
* If no weapon readied, look through all possessions for the best one.
* @return Returns true if successful.
*/
bool Actor::ready_best_weapon() {
int points;
if (Actor::get_weapon(points) != nullptr && ready_ammo()) {
ready_best_shield();
return true; // Already have one.
}
// Check for spellbook.
Game_object* obj = get_readied(lhand);
if (obj && obj->get_info().get_shape_class() == Shape_info::spellbook) {
if ((static_cast<Spellbook_object*>(obj))->can_do_spell(this)) {
ready_best_shield();
return true;
}
}
Game_object_vector vec; // Get list of all possessions.
vec.reserve(50);
get_objects(vec, c_any_shapenum, c_any_qual, c_any_framenum);
Game_object* best = nullptr;
Game_object* best_ammo = nullptr;
Game_object_shared keep1;
Game_object_shared keep2;
Game_object_shared best_keep;
int best_strength = -20;
int wtype = backpack;
for (auto* obj : vec) {
if (obj->inside_locked()) {
continue;
}
const Shape_info& info = obj->get_info();
const int ready = info.get_ready_type();
// backpack and rhand added for dragon breath and some spells
if (ready != lhand && ready != both_hands && ready != rhand
&& ready != backpack) {
continue;
}
const Weapon_info* winf = info.get_weapon_info();
if (!winf) {
continue; // Not a weapon.
}
Game_object* ammo_obj = nullptr;
if (!Is_weapon_usable(this, obj, &ammo_obj)) {
continue;
}
int strength = winf->get_base_strength();
strength += get_effective_range(winf);
if (strength > best_strength) {
wtype = ready;
best = obj;
best_ammo = ammo_obj != obj ? ammo_obj : nullptr;
best_strength = strength;
}
}
if (!best) {
ready_best_shield();
return false;
}
// If nothing is in left hand, nothing will happen.
Game_object* remove1 = spots[lhand];
Game_object* remove2 = nullptr;
if (wtype == both_hands && spots[rhand]) {
remove2 = spots[rhand];
}
// Prevent double removal and double add (can corrupt objects list).
// No need for similar check for remove1 as we wouldn't be here
// if remove1 were a weapon we could use.
if (remove2 == best) {
remove2 = nullptr;
}
// Free the spot(s).
if (remove1) {
remove1->remove_this(&keep1);
}
if (remove2) {
remove2->remove_this(&keep2);
}
best->remove_this(&best_keep);
if (wtype == rhand) { // tell it the correct ready spot
add_readied(best, lhand);
} else {
add(best, true); // Should go to the right place.
}
ready_best_shield(); // Also add a shield for 1-handed weapons.
if (remove1) { // Put back other things.
add(remove1, true);
}
if (remove2) {
add(remove2, true);
}
if (best_ammo) {
swap_ammo(best_ammo);
}
return true;
}
/**
* Try to store given object.
*/
bool Actor::empty_hand(Game_object* obj, Game_object_shared* keep) {
if (!obj) {
return true;
}
static const int chkspots[] = {belt, backpack};
add_dirty();
obj->remove_this(keep);
for (const int chkspot : chkspots) {
if (add_readied(obj, chkspot, true, true)) { // Slot free?
return true;
}
}
return false;
}
/**
* Try to store what is the hands.
*/
void Actor::empty_hands() {
Game_object* obj = spots[lhand];
Game_object_shared keep;
if (!empty_hand(obj, &keep)) {
add(obj, true);
}
obj = spots[rhand];
if (!empty_hand(obj, &keep)) {
add(obj, true);
}
}
/**
* Get effective weapon shape, taking casting frames in consideration.
* @return The shape to be displayed in-hand.
*/
int Actor::get_effective_weapon_shape() const {
if (get_casting_mode() == Actor::show_casting_frames) {
// Casting frames
return casting_shape;
} else {
Game_object* weapon = spots[lhand];
return weapon->get_shapenum();
}
}
/**
* Add dirty rectangle(s).
* @return Returns false if not on screen.
*/
bool Actor::add_dirty(bool figure_rect // Recompute weapon rectangle.
) {
if (!gwin->add_dirty(this)) {
return false;
}
if (figure_rect || get_casting_mode() == Actor::show_casting_frames) {
int weapon_x;
int weapon_y;
int weapon_frame;
if (figure_weapon_pos(weapon_x, weapon_y, weapon_frame)) {
const int shnum = get_effective_weapon_shape();
Shape_frame* wshape = ShapeID(shnum, weapon_frame).get_shape();
if (wshape) { // Set dirty area rel. to NPC.
weapon_rect = gwin->get_shape_rect(wshape, weapon_x, weapon_y);
} else {
weapon_rect.w = 0;
}
} else {
weapon_rect.w = 0;
}
}
if (weapon_rect.w > 0) { // Repaint weapon area too.
TileRect r = weapon_rect;
int xoff;
int yoff;
gwin->get_shape_location(this, xoff, yoff);
r.shift(xoff, yoff);
r.enlarge(c_tilesize / 2);
gwin->add_dirty(gwin->clip_to_win(r));
}
return true;
}
/**
* Change the frame and set to repaint areas.
* @param frnum The new frame.
*/
void Actor::change_frame(int frnum) {
bool on_map = get_chunk() != nullptr;
if (on_map) {
add_dirty(); // Set to repaint old area (if on map).
}
ShapeID id(get_shapenum(), frnum, get_shapefile());
Shape_frame* shape = id.get_shape();
if (!shape || shape->is_empty()) {
// Swap 1hand <=> 2hand frames.
frnum = (frnum & 48) | visible_frames[frnum & 15];
id.set_frame(frnum);
if (!(shape = id.get_shape()) || shape->is_empty()) {
frnum = (frnum & 48) | Actor::standing;
}
}
rest_time = 0;
set_frame(frnum);
if (on_map) {
add_dirty(true); // Set to repaint new.
}
}
/**
* See if it's blocked when trying to move to a new tile.
* @param t Tile to step to. Tz is possibly updated by this function.
* @param f Pointer to tile we are stepping from, or null for current tile.
* @param move_flags Additional movement flags to consider for step.
* @return Returns true if so, else false.
*/
bool Actor::is_blocked(
Tile_coord& t, // Tz possibly updated.
Tile_coord* f, // Step from here, or curpos if null.
const int move_flags) {
const Shape_info& info = get_info();
// Get dim. in tiles.
const int frame = get_framenum();
const int xtiles = info.get_3d_xtiles(frame);
const int ytiles = info.get_3d_ytiles(frame);
const int ztiles = info.get_3d_height();
t.fixme();
if (xtiles == 1 && ytiles == 1) { // Simple case?
Map_chunk* nlist = gmap->get_chunk(
t.tx / c_tiles_per_chunk, t.ty / c_tiles_per_chunk);
nlist->setup_cache();
int new_lift;
const bool blocked = nlist->is_blocked(
ztiles, t.tz, t.tx % c_tiles_per_chunk,
t.ty % c_tiles_per_chunk, new_lift,
move_flags | get_type_flags());
t.tz = new_lift;
return blocked;
}
return Map_chunk::is_blocked(
xtiles, ytiles, ztiles, f ? *f : get_tile(), t,
move_flags | get_type_flags());
}
/**
* Finds an object which blocks the destination tile.
* @param tile The (blocked) tile to check.
* @param dir Direction we are stepping from.
*/
Game_object* Actor::find_blocking(const Tile_coord& tile, int dir) {
ignore_unused_variable_warning(tile);
TileRect footprint = get_footprint();
const TileRect base = get_footprint();
switch (dir) {
case north:
footprint.shift(0, -1);
break;
case northeast:
footprint.shift(1, -1);
break;
case east:
footprint.shift(1, 0);
break;
case southeast:
footprint.shift(1, 1);
break;
case south:
footprint.shift(0, 1);
break;
case southwest:
footprint.shift(-1, 1);
break;
case west:
footprint.shift(-1, 0);
break;
case northwest:
footprint.shift(-1, -1);
break;
}
Game_object* block;
for (int i = footprint.x; i < footprint.x + footprint.w; i++) {
for (int j = footprint.y; j < footprint.y + footprint.h; j++) {
if (base.has_world_point(i, j)) {
continue;
} else if (
(block = Game_object::find_blocking(
Tile_coord(i, j, get_tile().tz)))
!= nullptr) {
return block;
}
}
}
return nullptr;
}
/**
* Move an object, and possibly change its shape too.
* @param old_chunk The actor's old chunk.
* @param new_chunk The chunk to which the actor is moving.
* @param new_sx New x coordinate.
* @param new_sy New y coordinate.
* @param new_frame The new frame.
* @param new_lift The new z coordinate.
*/
inline void Actor::movef(
Map_chunk* old_chunk, Map_chunk* new_chunk, int new_sx, int new_sy,
int new_frame, int new_lift) {
const Game_object_shared keep = shared_from_this();
if (old_chunk) { // Remove from current chunk.
old_chunk->remove(this);
}
set_shape_pos(new_sx, new_sy);
if (new_frame >= 0) {
change_frame(new_frame);
}
if (new_lift >= 0) {
set_lift(new_lift);
}
new_chunk->add(this);
}
/**
* Create character.
* @param nm The actor's name.
* @param shapenum The initial shape.
* @param num The NPC's number from npc.dat.
* @param uc The usecofe function to use.
*/
Actor::Actor(const std::string& nm, int shapenum, int num, int uc)
: name(nm), usecode(uc), usecode_assigned(false), unused(false),
npc_num(num), face_num(num), party_id(-1), temperature(0),
shape_save(-1), oppressor(-1), casting_mode(not_casting),
casting_shape(-1), target_tile(Tile_coord(-1, -1, 0)),
attack_weapon(-1), attack_mode(nearest),
schedule_type(Schedule::loiter), next_schedule(255),
restored_schedule(-1), dormant(true), hit(false),
combat_protected(false), user_set_attack(false), alignment(0),
charmalign(0), two_handed(false), two_fingered(false),
use_scabbard(false), use_neck(false), light_sources(0),
usecode_dir(0), type_flags(0), gear_immunities(0), gear_powers(0),
ident(0), skin_color(-1), action(nullptr), frame_time(0),
step_index(0), qsteps(0), weapon_rect(0, 0, 0, 0), rest_time(0) {
set_shape(shapenum, 0);
init();
frames = &npc_frames[0]; // Default: 5-frame walking.
}
/**
* Deletes actor.
*/
Actor::~Actor() {
purge_deleted_actions();
if (in_queue() && gwin->get_tqueue()) {
gwin->get_tqueue()->remove(this);
}
delete action;
}
/**
* Goes through the actor's readied gear and caches powers
* and immunities.
*/
void Actor::refigure_gear() {
static const Ready_type_Exult locs[]
= {head, belt, lhand, lfinger, legs, feet, rfinger,
rhand, torso, amulet, earrings, cloak, gloves};
int powers = 0;
int immune = 0;
light_sources = 0;
for (auto loc : locs) {
Game_object* worn = spots[loc];
if (worn) {
const Shape_info& info = worn->get_info();
const char rdy = info.get_ready_type();
if (info.is_light_source()
&& (loc != belt
|| (rdy != lhand && rdy != rhand && rdy != both_hands))) {
add_light_source(info.get_object_light(worn->get_framenum()));
}
powers |= info.get_object_flags(
worn->get_framenum(),
info.has_quality() ? worn->get_quality() : -1);
immune |= info.get_armor_immunity();
}
}
gear_immunities = immune;
gear_powers = powers;
}
void Actor::say_hunger_message() {