-
Notifications
You must be signed in to change notification settings - Fork 8
/
actors.cc
5241 lines (4819 loc) · 147 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-2013 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 <iostream> /* Debugging. */
#include <cstdlib>
#include <cstring>
#include <algorithm> /* swap. */
#include <set>
#include <map>
#include "chunks.h"
#include "gamemap.h"
#include "Astar.h"
#include "Audio.h"
#include "Gump_manager.h"
#include "Paperdoll_gump.h"
#include "Zombie.h"
#include "actions.h"
#include "actors.h"
#include "cheat.h"
#include "combat.h"
#include "combat_opts.h"
#include "dir.h"
#include "egg.h"
#include "exult.h"
#include "Face_stats.h"
#include "frameseq.h"
#include "game.h"
#include "gamewin.h"
#include "gameclk.h"
#include "imagewin.h"
#include "items.h"
#include "npctime.h"
#include "ready.h"
#include "ucmachine.h"
#include "party.h"
#include "monstinf.h"
#include "exult_constants.h"
#include "monsters.h"
#include "effects.h"
#include "palette.h"
#include "ucsched.h"
#include "ucscriptop.h"
#include "miscinf.h"
#include "animate.h"
#include "objiter.h"
#include "ammoinf.h"
#include "armorinf.h"
#include "frflags.h"
#include "weaponinf.h"
#include "npcdollinf.h"
#include "spellbook.h"
#include "usefuns.h"
#include "ignore_unused_variable_warning.h"
#include "array_size.h"
#ifdef USE_EXULTSTUDIO
#include "server.h"
#include "objserial.h"
#include "mouse.h"
#include "servemsg.h"
#endif
using std::cerr;
using std::cout;
using std::endl;
using std::memcpy;
using std::rand;
using std::string;
using std::swap;
Actor *Actor::editing = 0;
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.
Frames_sequence *Actor::avatar_frames[4] = {0, 0, 0, 0};
Frames_sequence *Actor::npc_frames[4] = {0, 0, 0, 0};
const signed char sea_serpent_attack_frames[] = {1, 2, 3};
const signed char reach_attack_frames1[] = {3, 6};
const signed char raise_attack_frames1[] = {3, 4, 6};
const signed char fast_swing_attack_frames1[] = {3, 5, 6};
const signed char slow_swing_attack_frames1[] = {3, 4, 5, 6};
const signed char reach_attack_frames2[] = {3, 9};
const signed char raise_attack_frames2[] = {3, 7, 9};
const signed char fast_swing_attack_frames2[] = {3, 8, 9};
const signed char slow_swing_attack_frames2[] = {3, 7, 8, 9};
// inline int Is_attack_frame(int i) { return i >= 3 && i <= 9; }
inline int 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;
typedef std::map<const char *, int> Att_map;
/// 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) {
std::set<string>::iterator 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.
std::set<string>::const_iterator siter = strings->find(nm);
if (siter == strings->end())
return 0;
nm = (*siter).c_str();
Att_map::const_iterator 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) {
Att_map::const_iterator it;
for (it = map.begin(); it != map.end(); ++it)
attlist.push_back(*it);
}
};
std::set<string> *Actor_attributes::strings = 0;
/**
* Get/create timers.
* @return
*/
Npc_timer_list *Actor::need_timers(
) {
if (!timers)
timers = new Npc_timer_list(this);
return timers;
}
/**
* Initialize frames, properties and spots.
*/
void Actor::init(
) {
if (!avatar_frames[0])
init_default_frames();
size_t i;
for (i = 0; i < array_size(properties); i++)
properties[i] = 0;
for (i = 0; i < array_size(spots); i++)
spots[i] = 0;
}
/**
* 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 = 0;
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 (Game_object_vector::const_iterator it = vec.begin();
it != vec.end(); ++it) {
Game_object *obj = *it;
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
) {
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();
}
int uses = winf ? winf->get_uses() : static_cast<int>(Weapon_info::melee);
if (!uses || uses == Weapon_info::ranged)
return reach;
else {
int eff_range;
int str = get_effective_prop(static_cast<int>(Actor::strength));
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 0;
const Weapon_info *winf = ShapeID::get_info(weapon).get_weapon_info();
if (!winf)
return 0;
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 0;
}
// Search readied weapons first.
static enum Ready_type_Exult wspots[] = {lhand, rhand, back_2h, belt};
const int num_weapon_spots = array_size(wspots);
for (int i = 0; i < num_weapon_spots; i++) {
Game_object *obj = spots[static_cast<int>(wspots[i])];
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) : 0;
}
/**
* 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.
if (aobj) // Something there already?
aobj->remove_this(1); // Remove it.
newammo->remove_this(1);
add(newammo, 1); // Should go to the right place.
if (aobj) // Put back old ammo.
add(aobj, 1);
}
/**
* 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 = 0,
bool recursive = true
) {
if (ammo)
*ammo = 0;
const Weapon_info *winf = bobj->get_info().get_weapon_info();
if (!winf)
return false; // Not a weapon.
Game_object *aobj = 0; // 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[static_cast<int>(lhand)];
if (!weapon)
return false;
const Shape_info &info = weapon->get_info();
const Weapon_info *winf = info.get_weapon_info();
if (!winf)
return false;
int ammo = winf->get_ammo_consumed();
if (ammo < 0) {
// Ammo not needed.
if (winf->uses_charges() && info.has_quality() &&
weapon->get_quality() <= 0)
return false; // Uses charges, but none left.
else
return true;
}
Game_object *found = 0;
// Try non-recursive search for ammo first.
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 = 0;
if (spots[rhand]) { // remove old offhand item
old_rhand = spots[rhand];
old_rhand->remove_this(1);
}
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 = 0;
int best_strength = -20;
for (Game_object_vector::const_iterator it = vec.begin();
it != vec.end(); ++it) {
Game_object *obj = *it;
if (obj->inside_locked())
continue;
const Shape_info &info = obj->get_info();
// Only want those that can be readied in hand.
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;
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, 1);
return false;
}
// Spot is free already.
best->remove_this(1);
add(best, 1); // Should go to the right place.
if (old_rhand && old_rhand != best) // don't add twice
add(old_rhand, 1);
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) != 0 && 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 = 0, *best_ammo = 0;
int best_strength = -20;
int wtype = backpack;
for (Game_object_vector::const_iterator it = vec.begin();
it != vec.end(); ++it) {
Game_object *obj = *it, *ammo_obj = 0;
if (obj->inside_locked())
continue;
const Shape_info &info = obj->get_info();
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.
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 : 0;
best_strength = strength;
}
}
if (!best) {
ready_best_shield();
return false;
}
// If nothing is in left hand, nothing will happen.
Game_object *remove1 = spots[lhand], *remove2 = 0;
if (wtype == both_hands)
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 = 0;
// Free the spot(s).
if (remove1)
remove1->remove_this(1);
if (remove2)
remove2->remove_this(1);
best->remove_this(1);
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
) {
if (!obj)
return true;
static int chkspots[] = {belt, backpack};
add_dirty();
obj->remove_this(1);
for (size_t i = 0; i < array_size(chkspots); i++)
if (add_readied(obj, chkspots[i], true, true)) // Slot free?
return true;
return false;
}
/**
* Try to store what is the hands.
*/
void Actor::empty_hands(
) {
Game_object *obj = spots[lhand];
if (!empty_hand(obj))
add(obj, true);
obj = spots[rhand];
if (!empty_hand(obj))
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(
) {
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 0 if not on screen.
*/
int Actor::add_dirty(
int figure_rect // Recompute weapon rectangle.
) {
if (!gwin->add_dirty(this))
return 0;
if (figure_rect || get_casting_mode() == Actor::show_casting_frames) {
int weapon_x, weapon_y, weapon_frame;
if (figure_weapon_pos(weapon_x, weapon_y, weapon_frame)) {
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.
Rectangle r = weapon_rect;
int xoff, 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 1;
}
/**
* Change the frame and set to repaint areas.
* @param frnum The new frame.
*/
void Actor::change_frame(
int frnum
) {
add_dirty(); // Set to repaint old area.
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);
add_dirty(1); // 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 1 if so, else 0.
*/
int 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.
int frame = get_framenum();
int xtiles = info.get_3d_xtiles(frame), ytiles = info.get_3d_ytiles(frame);
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;
int 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(
Tile_coord const &tile,
int dir
) {
ignore_unused_variable_warning(tile);
Rectangle footprint = get_footprint();
Rectangle 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))) != 0)
return block;
return 0;
}
/**
* 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
) {
if (old_chunk) // Remove from current chunk.
old_chunk->remove(this);
set_shape_pos(new_sx, new_sy);
if (new_frame >= 0)
set_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), usecode_name(""), unused(false),
npc_num(num), face_num(num), party_id(-1), atts(0), temperature(0),
shape_save(-1), oppressor(-1), target(0),
casting_mode(false), casting_shape(-1),
target_object(0), target_tile(Tile_coord(-1, -1, 0)), attack_weapon(-1),
attack_mode(nearest),
schedule_type(Schedule::loiter), next_schedule(255), schedule(0),
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(0),
frame_time(0), step_index(0), qsteps(0), timers(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();
delete schedule;
delete action;
delete timers;
delete atts;
}
/**
* Goes through the actor's readied gear and caches powers
* and immunities.
*/
void Actor::refigure_gear() {
static Ready_type_Exult locs[] = {head, belt, lhand, lfinger, legs,
feet, rfinger, rhand, torso, amulet,
earrings, cloak, gloves
};
int powers = 0, immune = 0;
light_sources = 0;
for (size_t i = 0; i < array_size(locs); i++) {
Game_object *worn = spots[static_cast<int>(locs[i])];
if (worn) {
const Shape_info &info = worn->get_info();
char rdy = info.get_ready_type();
if (info.is_light_source() && (locs[i] != belt ||
(rdy != lhand && rdy != rhand && rdy != both_hands)))
light_sources++;
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() {
int food = get_property(static_cast<int>(food_level));
if (food <= 0) { // Really low?
if (rand() % 4)
say(first_starving, last_starving);
} else if (food <= 4) {
if (rand() % 3)
say(first_needfood, last_needfood);
} else { //if (food <= 9)
if (rand() % 2)
say(first_hunger, last_hunger);
}
}
/**
* Decrement food level and print complaints if it gets too low.
* NOTE: Should be called every hour.
*/
void Actor::use_food(
) {
if (get_info().does_not_eat() || (gear_powers & Frame_flags::doesnt_eat))
return;
int food = get_property(static_cast<int>(food_level));
food -= (rand() % 4); // Average 1.5 level/hour.
set_property(static_cast<int>(food_level), food);
if (food > 9)
return;
say_hunger_message();
need_timers()->start_hunger(); // minute checks for damage and messages.
}
/**
* Periodic check for freezing.
* @param freeze True if the actor is freezing. This is usually the
* Avatar's freeze flag.
*/
void Actor::check_temperature(
bool freeze
) {
if (!freeze) { // Not in a cold area?
if (!temperature) // 0 means warm.
return; // Nothing to do.
// Warming up.
temperature -= (temperature >= 5 ? 5 : temperature);
if (rand() % 3 == 0) {
if (temperature >= 30)
say(first_warming_up, last_warming_up);
else
say(first_warming_up_2, last_warming_up_2);
}
return;
}
// Immune to cold by nature or an item?
if (get_info().is_cold_immune() || (gear_powers & Frame_flags::cold_immune))
return;
if (get_schedule_type() == Schedule::wait)
return; // Not following leader? Leave alone.
int warmth = figure_warmth(); // (This could be saved for speed.)
if (warmth >= 100) { // Enough clothing?
if (!temperature)
return; // Already warm.
int decr = 1 + (warmth - 100) / 10;
decr = decr > temperature ? temperature : decr;
temperature -= decr;
if (rand() % 3 == 0) {