-
Notifications
You must be signed in to change notification settings - Fork 0
/
combat.cc
1716 lines (1623 loc) · 42.7 KB
/
combat.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
/*
* combat.h - Combat scheduling.
*
* Copyright (C) 2000-2011 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 "combat.h"
#include "combat_opts.h"
#include "gamewin.h"
#include "gameclk.h"
#include "gamemap.h"
#include "paths.h"
#include "Astar.h"
#include "actions.h"
#include "items.h"
#include "effects.h"
#include "Audio.h"
#include "ready.h"
#include "game.h"
#include "monstinf.h"
#include "ucmachine.h"
#include "game.h"
#include "Gump_manager.h"
#include "spellbook.h"
#include "animate.h"
#include "ucsched.h"
#include "ucscriptop.h"
#include "cheat.h"
#include "ammoinf.h"
#include "weaponinf.h"
#include "ready.h"
#ifndef UNDER_EMBEDDED_CE
using std::cout;
using std::endl;
using std::rand;
#endif
using std::list;
unsigned long Combat_schedule::battle_time = (unsigned long) (-30000);
unsigned long Combat_schedule::battle_end_time = 0;
bool Combat::paused = false;
int Combat::difficulty = 0;
Combat::Mode Combat::mode = Combat::original;
bool Combat::show_hits = false;
bool Combat::charmed_more_difficult = false;
extern bool combat_trace;
const unsigned int dex_to_attack = 30;
static inline bool not_in_melee_range
(
const Weapon_info *winf, // 0 for monsters or natural weaponry.
int dist,
int reach
)
{
if (!winf)
return dist > reach;
return (winf->get_uses() == Weapon_info::ranged) || (dist > reach);
}
/*
* Is a given ammo shape in a given family.
*/
bool In_ammo_family(int shnum, int family)
{
if (shnum == family)
return true;
Ammo_info *ainf = ShapeID::get_info(shnum).get_ammo_info();
return (ainf != 0 && ainf->get_family_shape() == family);
}
/*
* Start music if battle has recently started.
*/
void Combat_schedule::start_battle
(
)
{
if (started_battle)
return;
// But only if Avatar is main char.
if (gwin->get_camera_actor() != gwin->get_main_actor())
return;
unsigned long curtime = Game::get_ticks();
// .5 minute since last start?
if (curtime - battle_time >= 30000)
{
Audio::get_ptr()->start_music_combat(rand()%2 ?
CSAttacked1 : CSAttacked2, 0);
battle_time = curtime;
battle_end_time = curtime - 1;
}
started_battle = true;
}
/*
* This (static) method is called when a monster dies. It checks to
* see if there are still hostile NPC's around. If not, it plays
* 'victory' music.
*/
void Combat_schedule::monster_died
(
)
{
if (battle_end_time >= battle_time)// Battle raging?
return; // No, it's over.
Actor_vector nearby; // Get all nearby NPC's.
gwin->get_nearby_npcs(nearby);
for (Actor_vector::const_iterator it = nearby.begin();
it != nearby.end(); ++it)
{
Actor *actor = *it;
if (!actor->is_dead() &&
actor->get_attack_mode() != Actor::flee &&
actor->get_effective_alignment() >= Npc_actor::hostile)
return; // Still possible enemies.
}
battle_end_time = Game::get_ticks();
// Figure #seconds battle lasted.
unsigned long len = (battle_end_time - battle_time)/1000;
bool hard = len > 15u && (rand()%60u < len);
Audio::get_ptr()->start_music_combat (hard ? CSBattle_Over
: CSVictory, 0);
}
/*
* This (static) method is called to stop attacking a given NPC.
* This can happen because the NPC died, fell asleep or became
* invisible.
*/
void Combat_schedule::stop_attacking_npc
(
Game_object *npc
)
{
Actor_vector nearby; // Get all nearby NPC's.
gwin->get_nearby_npcs(nearby);
for (Actor_vector::const_iterator it = nearby.begin();
it != nearby.end(); ++it)
{
Actor *actor = *it;
if (actor->get_target() == npc)
actor->set_target(0);
}
}
/*
* This (static) method is called to stop attacking a given NPC.
* This can happen because the NPC died or fell asleep.
*/
void Combat_schedule::stop_attacking_invisible
(
Game_object *npc
)
{
Actor_vector nearby; // Get all nearby NPC's.
gwin->get_nearby_npcs(nearby);
for (Actor_vector::const_iterator it = nearby.begin();
it != nearby.end(); ++it)
{
Actor *actor = *it;
if (actor->get_target() == npc)
{
Monster_info *minf = actor->get_info().get_monster_info();
if ((minf && minf->get_flags() & (1<<Monster_info::see_invisible))==0)
actor->set_target(0);
}
}
}
/*
* Can a given shape teleport? summon? turn invisible?
*/
static bool Can_teleport
(
Actor *npc
)
{
if (npc->get_flag(Obj_flags::no_spell_casting))
return false;
return npc->get_info().can_teleport();
}
static bool Can_summon
(
Actor *npc
)
{
if (npc->get_flag(Obj_flags::no_spell_casting))
return false;
return npc->get_info().can_summon();
}
static bool Can_be_invisible
(
Actor *npc
)
{
if (npc->get_flag(Obj_flags::no_spell_casting))
return false;
return npc->get_info().can_be_invisible();
}
/*
* Certain monsters (wisps, mages) can teleport during battle.
*/
bool Combat_schedule::teleport
(
)
{
Game_object *trg = npc->get_target(); // Want to get close to targ.
if (!trg)
return false;
unsigned int curtime = SDL_GetTicks();
if (curtime < teleport_time)
return false;
teleport_time = curtime + 2000 + rand()%2000;
Tile_coord dest = trg->get_tile();
dest.tx += 4 - rand()%8;
dest.ty += 4 - rand()%8;
dest = Map_chunk::find_spot(dest, 3, npc, 1);
if (dest.tx == -1)
return false; // No spot found.
Tile_coord src = npc->get_tile();
if (dest.distance(src) > 7 && rand()%2 != 0)
return false; // Got to give Avatar a chance to
// get away.
// Create fire-field where he was.
src.tz = npc->get_chunk()->get_highest_blocked(src.tz,
src.tx%c_tiles_per_chunk, src.ty%c_tiles_per_chunk);
if (src.tz < 0)
src.tz = 0;
eman->add_effect(new Fire_field_effect(src));
int sfx = Audio::game_sfx(43);
Audio::get_ptr()->play_sound_effect(sfx, npc); // The weird noise.
npc->move(dest.tx, dest.ty, dest.tz);
// Show the stars.
eman->add_effect(new Sprites_effect(7, npc, 0, 0, 0, 0));
return true;
}
/*
* Some monsters can do the "summon" spell.
*/
bool Combat_schedule::summon
(
)
{
#if 0 // ++++Testing
unsigned int curtime = SDL_GetTicks();
if (curtime < summon_time)
return false;
// Not again for 30-40 seconds.
summon_time = curtime + 30000 + rand()%10000;
#endif
ucmachine->call_usecode(0x685,
npc, Usecode_machine::double_click);
npc->start_std(); // Back into queue.
return true;
}
/*
* Some can turn invisible.
*/
bool Combat_schedule::be_invisible
(
)
{
#if 0 // ++++ Testing.
unsigned int curtime = SDL_GetTicks();
if (curtime < invisible_time)
return false;
// Not again for 40-60 seconds.
invisible_time = curtime + 40000 + rand()%20000;
if (npc->get_flag(Obj_flags::invisible))
return false; // Also don't to it again for a while.
#endif
new Object_sfx(npc, Audio::game_sfx(44));
gwin->get_effects()->add_effect(
new Sprites_effect(12, npc, 0, 0, 0, 0, 0, -1));
npc->set_flag(Obj_flags::invisible);
npc->add_dirty();
npc->start_std(); // Back into queue.
return true;
}
/*
* Off-screen?
*/
inline bool Off_screen
(
Game_window *gwin,
Game_object *npc
)
{
// See if off screen.
Tile_coord t = npc->get_tile();
Rectangle screen = gwin->get_win_tile_rect().enlarge(2);
return (!screen.has_world_point(t.tx, t.ty));
}
static inline bool is_enemy
(
int align, int other
)
{
switch (align)
{
case Npc_actor::friendly:
return other == Npc_actor::hostile
|| other == Npc_actor::unknown_align;
case Npc_actor::hostile:
return other == Npc_actor::friendly
|| other == Npc_actor::unknown_align;
case Npc_actor::neutral:
return false;
case Npc_actor::unknown_align:
return other == Npc_actor::hostile
|| other == Npc_actor::friendly;
}
return true; // This should never happen.
}
/*
* Find nearby opponents in the 9 surrounding chunks.
*/
void Combat_schedule::find_opponents
(
)
{
opponents.clear();
Game_window *gwin = Game_window::get_instance();
Actor_vector nearby; // Get all nearby NPC's.
gwin->get_nearby_npcs(nearby);
Actor *avatar = gwin->get_main_actor();
nearby.push_back(avatar); // Incl. Avatar!
bool npcCharmed = npc->get_flag(Obj_flags::charmed);
// See if we're a party member.
bool in_party = npc->is_in_party() || npc == avatar;
int npc_align = npc->get_effective_alignment();
Monster_info *minf = npc->get_info().get_monster_info();
bool see_invisible = minf ?
(minf->get_flags() & (1<<Monster_info::see_invisible))!=0 : false;
for (Actor_vector::const_iterator it = nearby.begin();
it != nearby.end(); ++it)
{
Actor *actor = *it;
if (actor->is_dead() || (!see_invisible && actor->get_flag(Obj_flags::invisible)) ||
(in_party && !npcCharmed && actor->get_flag(Obj_flags::asleep)) ||
((!in_party || npcCharmed) && actor->get_flag(Obj_flags::asleep) && !opponents.empty()))
continue; // Dead, sleeping or invisible.
if (is_enemy(npc_align, actor->get_effective_alignment())) {
opponents.push_back(actor);
if (combat_trace)
cout << npc->get_name() << " pushed back(1) " << actor->get_name() << endl;
}
else if (in_party) { // Attacking party member?
Game_object *t = actor->get_target();
if (!t)
continue;
// Is actor attacking a party member?
if ((t->get_flag(Obj_flags::in_party) || t == avatar) &&
t->as_actor() && t->as_actor()->get_effective_alignment() == npc_align) {
opponents.push_back(actor);
if (combat_trace)
cout << npc->get_name() << " pushed back(2) " << actor->get_name() << endl;
}
int oppressor = actor->get_oppressor();
if (oppressor < 0)
continue;
Actor *oppr = gwin->get_npc(oppressor);
// Is actor being attacked by a party member?
if ((oppr->get_flag(Obj_flags::in_party) || oppr == avatar) &&
oppr->get_effective_alignment() == npc_align) {
opponents.push_back(actor);
if (combat_trace)
cout << npc->get_name() << " pushed back(3) " << actor->get_name() << endl;
}
}
}
// None found? Use Avatar's if same effective alignment
if (opponents.empty() && in_party &&
avatar->get_effective_alignment() == npc_align)
{
Game_object *opp = avatar->get_target();
Actor *oppnpc = opp ? opp->as_actor() : 0;
if (oppnpc && oppnpc != npc
&& oppnpc->get_schedule_type()==Schedule::combat) {
opponents.push_back(oppnpc);
if (combat_trace)
cout << npc->get_name() << " pushed back (4) " << oppnpc->get_name() << endl;
}
}
}
/*
* Find 'protected' party member's attackers.
*
* Output: ->attacker in opponents, or opponents.end() if not found.
*/
list<Actor*>::iterator Combat_schedule::find_protected_attacker
(
)
{
if (!npc->is_in_party()) // Not in party?
return opponents.end();
Game_window *gwin = Game_window::get_instance();
Actor *party[9]; // Get entire party, including Avatar.
int cnt = gwin->get_party(party, 1);
Actor *prot_actor = 0;
for (int i = 0; i < cnt; i++)
if (party[i]->is_combat_protected())
{
prot_actor = party[i];
break;
}
if (!prot_actor) // Not found?
return opponents.end();
// Find closest attacker.
int dist, best_dist = 4*c_tiles_per_chunk;
list<Actor*>::iterator best_opp = opponents.end();
for (list<Actor*>::iterator it = opponents.begin();
it != opponents.end(); ++it)
{
Actor *opp = *it;
if (opp->get_target() == prot_actor &&
(dist = npc->distance(opp)) < best_dist)
{
best_dist = dist;
best_opp = it;
}
}
if (best_opp == opponents.end())
return opponents.end();
if (failures < 5 && yelled && rand()%2 && npc != prot_actor)
npc->say(first_will_help, last_will_help);
return best_opp;
}
/*
* Find a foe.
*
* Output: Opponent that was found.
*/
Game_object *Combat_schedule::find_foe
(
int mode // Mode to use.
)
{
if (combat_trace) {
cout << "'" << npc->get_name() << "' is looking for a foe" <<
endl;
}
int new_align = npc->get_effective_alignment();
if (new_align != alignment)
{ // Alignment changed.
opponents.clear();
alignment = new_align;
}
// Remove any that died.
for (list<Actor*>::iterator it = opponents.begin();
it != opponents.end(); )
{
if ((*it)->is_dead())
it = opponents.erase(it);
else
++it;
}
if (opponents.empty()) // No more from last scan?
{
find_opponents(); // Find all nearby.
if (practice_target) // For dueling.
return practice_target;
}
list<Actor*>::iterator new_opp_link = opponents.end();
switch ((Actor::Attack_mode) mode)
{
case Actor::weakest:
{
int str, least_str = 100;
for (list<Actor*>::iterator it = opponents.begin();
it != opponents.end(); ++it)
{
Actor *opp = *it;
str = opp->get_property(Actor::strength);
if (str < least_str)
{
least_str = str;
new_opp_link = it;
}
}
break;
}
case Actor::strongest:
{
int str, best_str = -100;
for (list<Actor*>::iterator it = opponents.begin();
it != opponents.end(); ++it)
{
Actor *opp = *it;
str = opp->get_property(Actor::strength);
if (str > best_str)
{
best_str = str;
new_opp_link = it;
}
}
break;
}
case Actor::nearest:
{
int best_dist = 4*c_tiles_per_chunk;
for (list<Actor*>::iterator it = opponents.begin();
it != opponents.end(); ++it)
{
Actor *opp = *it;
int dist = npc->distance(opp);
if (opp->get_attack_mode() == Actor::flee)
dist += 16; // Avoid fleeing.
if (dist < best_dist)
{
best_dist = dist;
new_opp_link = it;
}
}
break;
}
case Actor::protect:
new_opp_link = find_protected_attacker();
if (new_opp_link != opponents.end())
break; // Found one.
// FALL THROUGH to 'random'.
case Actor::random:
default: // Default to random.
if (!opponents.empty())
new_opp_link = opponents.begin();
break;
}
Actor *new_opponent;
if (new_opp_link == opponents.end())
new_opponent = 0;
else
{
new_opponent = *new_opp_link;
opponents.erase(new_opp_link);
}
return new_opponent;
}
/*
* Find a foe.
*
* Output: Opponent that was found.
*/
inline Game_object *Combat_schedule::find_foe
(
)
{
if (npc->get_attack_mode() == Actor::manual)
return 0; // Find it yourself.
return find_foe(static_cast<int>(npc->get_attack_mode()));
}
/*
* Handle the 'approach' state.
*/
void Combat_schedule::approach_foe
(
bool for_projectile // Want to attack with projectile.
// FOR NOW: Called as last resort,
// and we try to reach target.
)
{
int points;
Weapon_info *winf = npc->get_weapon(points, weapon_shape, weapon);
int dist = for_projectile ? 1 : winf ? winf->get_range() : 3;
Game_object *opponent = npc->get_target();
// Find opponent.
if (!opponent && !(opponent = find_foe()))
{
failures++;
npc->start(200, 400); // Try again in 2/5 sec.
return; // No one left to fight.
}
npc->set_target(opponent);
Actor::Attack_mode mode = npc->get_attack_mode();
Game_window *gwin = Game_window::get_instance();
// Time to run?
Monster_info *minf = npc->get_info().get_monster_info();
if ((!minf || !minf->cant_die()) &&
(mode == Actor::flee ||
(mode != Actor::berserk &&
(npc->get_type_flags()&MOVE_ALL) != 0 &&
npc != gwin->get_main_actor() &&
npc->get_property(Actor::health) < 3)))
{
run_away();
return;
}
if (rand()%4 == 0 && Can_teleport(npc) && // Try 1/4 to teleport.
teleport())
{
start_battle();
npc->start_std();
return;
}
PathFinder *path = new Astar();
// Try this for now:
Monster_pathfinder_client cost(npc, dist, opponent);
Tile_coord pos = npc->get_tile();
if (!path->NewPath(pos, opponent->get_tile(), &cost))
{ // Failed? Try nearest opponent.
failures++;
bool retry_ok = false;
if (npc->get_attack_mode() != Actor::manual)
{
Game_object *closest = find_foe(Actor::nearest);
if (!closest)
{ // No one nearby.
if (combat_trace)
cout << npc->get_name() << " has no opponents nearby."
<< endl;
npc->set_target(0);
retry_ok = false;
}
else if (closest != opponent)
{
opponent = closest;
npc->set_target(opponent);
Monster_pathfinder_client cost(npc, dist,
opponent);
retry_ok = (opponent != 0 && path->NewPath(
pos, opponent->get_tile(), &cost));
}
}
if (!retry_ok)
{
delete path; // Really failed. Try again in
// after wandering.
// Just try to walk towards opponent.
Tile_coord pos = npc->get_tile();
Tile_coord topos = opponent->get_tile();
int dirx = topos.tx > pos.tx ? 2
: (topos.tx < pos.tx ? -2 : (rand()%3 - 1));
int diry = topos.ty > pos.ty ? 2
: (topos.ty < pos.ty ? -2 : (rand()%3 - 1));
pos.tx += dirx * (1 + rand()%4);
pos.ty += diry * (1 + rand()%4);
npc->walk_to_tile(pos, 2*gwin->get_std_delay(),
500 + rand()%500);
failures++;
return;
}
}
failures = 0; // Clear count. We succeeded.
start_battle(); // Music if first time.
if (combat_trace) {
cout << npc->get_name() << " is pursuing " << opponent->get_name()
<< endl;
}
// First time (or 256th), visible?
if (!yelled && gwin->add_dirty(npc))
{
yelled++;
if (can_yell && rand()%2)// Half the time.
{
// Goblin?
if (Game::get_game_type() == SERPENT_ISLE &&
(npc->get_shapenum() == 0x1de ||
npc->get_shapenum() == 0x2b3 ||
npc->get_shapenum() == 0x2d5 ||
npc->get_shapenum() == 0x2e8))
npc->say(first_goblin_to_battle, last_goblin_to_battle);
else
npc->say(first_to_battle, last_to_battle);
}
}
int extra_delay = 0;
// Walk there, & check half-way.
npc->set_action(new Approach_actor_action(path, opponent,
for_projectile));
// Start walking. Delay a bit if
// opponent is off-screen.
npc->start(gwin->get_std_delay(), extra_delay +
(Off_screen(gwin, opponent) ?
5*gwin->get_std_delay() : gwin->get_std_delay()));
}
/*
* Check for a useful weapon at a given ready-spot.
*/
static Game_object *Get_usable_weapon
(
Actor *npc,
int index // Ready-spot to check.
)
{
Game_object *bobj = npc->get_readied(index);
if (!bobj)
return 0;
Shape_info& info = bobj->get_info();
Weapon_info *winf = info.get_weapon_info();
if (!winf)
return 0; // Not a weapon.
Game_object *aobj; // Check ranged first.
int need_ammo = npc->get_weapon_ammo(bobj->get_shapenum(),
winf->get_ammo_consumed(), winf->get_projectile(),
true, &aobj);
if (need_ammo)
{
if (!aobj) // Try melee.
need_ammo = npc->get_weapon_ammo(bobj->get_shapenum(),
winf->get_ammo_consumed(), winf->get_projectile(),
false, &aobj);
if (need_ammo && !aobj)
return 0;
}
if (info.get_ready_type() == both_hands &&
npc->get_readied(rhand) != 0)
return 0; // Needs two free hands.
return bobj;
}
/*
* Swap weapon with the one in the belt.
*
* Output: 1 if successful.
*/
static int Swap_weapons
(
Actor *npc
)
{
int index = belt;
Game_object *bobj = Get_usable_weapon(npc, index);
if (!bobj)
{
index = back_2h;
bobj = Get_usable_weapon(npc, index);
if (!bobj)
{ // Do thorough search for NPC's.
if (!npc->is_in_party())
return npc->ready_best_weapon();
else
return 0;
}
}
Game_object *oldweap = npc->get_readied(lhand);
if (oldweap)
npc->remove(oldweap);
npc->remove(bobj);
npc->add(bobj, 1); // Should go into weapon hand.
if (oldweap) // Put old where new one was.
npc->add_readied(oldweap, index, 1, 1);
return 1;
}
/*
* Begin a strike at the opponent.
*/
void Combat_schedule::start_strike
(
)
{
Game_object *opponent = npc->get_target();
bool check_lof = !no_blocking;
// Get difference in lift.
Weapon_info *winf = weapon_shape >= 0 ?
ShapeID::get_info(weapon_shape).get_weapon_info() : 0;
int dist = npc->distance(opponent);
int reach;
if (!winf)
{
Monster_info *minf = npc->get_info().get_monster_info();
reach = minf ? minf->get_reach() : Monster_info::get_default()->get_reach();
}
else
reach = winf->get_range();
bool ranged = not_in_melee_range(winf, dist, reach);
// Out of range?
if (!spellbook && npc->get_effective_range(winf, reach) < dist)
{
state = approach;
approach_foe(); // Get a path.
return;
}
else if (spellbook || ranged)
{
bool weapon_dead = false;
if (spellbook)
weapon_dead = !spellbook->can_do_spell(npc);
else if (winf)
{ // See if we can fire spell/projectile.
Game_object *ammo = 0;
int need_ammo = npc->get_weapon_ammo(weapon_shape,
winf->get_ammo_consumed(), winf->get_projectile(),
ranged, &ammo);
if (need_ammo && !ammo && !npc->ready_ammo())
weapon_dead = true;
}
if (weapon_dead)
{ // Out of ammo/reagents/charges.
if (npc->get_schedule_type() != Schedule::duel)
{ // Look in pack for ammo.
if (Swap_weapons(npc))
Combat_schedule::set_weapon();
else
set_hand_to_hand();
}
if (!npc->get_info().has_strange_movement())
npc->change_frame(npc->get_dir_framenum(
Actor::standing));
state = approach;
npc->set_target(0);
npc->start(200, 500);
return;
}
state = fire; // Clear to go.
}
else
{
check_lof = (reach > 1);
state = strike;
}
// At this point, we're within range, with state set.
if (check_lof &&
!Fast_pathfinder_client::is_straight_path(npc, opponent))
{
state = approach;
approach_foe(true); // Try to get adjacent.
return;
}
if (!started_battle)
start_battle(); // Play music if first time.
if (combat_trace) {
cout << npc->get_name() << " attacks " << opponent->get_name() << endl;
}
int dir = npc->get_direction(opponent);
signed char frames[12]; // Get frames to show.
int cnt = npc->get_attack_frames(weapon_shape, ranged, dir, frames);
if (cnt)
npc->set_action(new Frames_actor_action(frames, cnt, gwin->get_std_delay()));
npc->start(); // Get back into time queue.
int sfx = -1; // Play sfx.
if (winf)
sfx = winf->get_sfx();
if (sfx < 0 || !winf)
{
Monster_info *minf = ShapeID::get_info(
npc->get_shapenum()).get_monster_info();
if (minf)
sfx = minf->get_hitsfx();
}
if (sfx >= 0)
{
int delay = ranged ? cnt : cnt/2;
new Object_sfx(npc, sfx, delay * gwin->get_std_delay());
}
dex_points -= dex_to_attack;
}
/*
* This static method causes the NPC to attack a given target/tile
* using the given weapon shape as weapon. This does not add
* an attack animation; rather, it is the actual strike attempt.
*
* This function is called from (a) an intrinsic, (b) a script opcode
* or (c) the combat schedule.
*
* Output: Returns false the attack cannot be realized (no ammo,
* out of range, etc.) or if a melee attack misses, true otherwise.
*/
bool Combat_schedule::attack_target
(
Game_object *attacker, // Who/what is attacking.
Game_object *target, // Who/what is being attacked.
Tile_coord tile, // What tile is under fire, if no target.
int weapon, // What is being used as weapon.
// or < 0 for none.
bool combat // We got here from combat schedule.
)
{
// Bail out if no attacker or if no target and no valid tile.
if (!attacker || (!target && tile.tx == -1))
return false;
// Do not proceed if target is dead.
Actor *att = attacker->as_actor();
if (att && att->is_dead())
return false;
bool flash_mouse = !combat && att && gwin->get_main_actor() == att
&& att->get_attack_mode() != Actor::manual;
Shape_info& info = ShapeID::get_info(weapon);
const Weapon_info *winf = weapon >= 0 ? info.get_weapon_info() : 0;
int reach;
int family = -1; // Ammo, is needed, is the weapon itself.
int proj = -1; // This is what we will use as projectile sprite.
if (!winf)
{
Monster_info *minf = attacker->get_info().get_monster_info();
reach = minf ? minf->get_reach() : Monster_info::get_default()->get_reach();
}
else
{
reach = winf->get_range();
proj = winf->get_projectile();
family = winf->get_ammo_consumed();
}
int dist = target ? attacker->distance(target) : attacker->distance(tile);
bool ranged = not_in_melee_range(winf, dist, reach);
// Out of range?
if (attacker->get_effective_range(winf, reach) < dist)
{
// We are out of range.
if (flash_mouse)
Mouse::mouse->flash_shape(Mouse::outofrange);
return false;
}
// See if we need ammo.
Game_object *ammo = 0;
int need_ammo = attacker->get_weapon_ammo(weapon, family,
proj, ranged, &ammo);
if (need_ammo && !ammo)
{
if (flash_mouse)
Mouse::mouse->flash_shape(Mouse::outofammo);
// We don't have ammo, so bail out.
return false;
}
// proj == -3 means use weapon shape for projectile sprite.
if (proj == -3)
proj = weapon;
const Ammo_info *ainf;
int basesprite;
if (need_ammo && family >= 0)
{
// ammo should be nonzero here.
ainf = ammo->get_info().get_ammo_info();
basesprite = ammo->get_shapenum();
}
else
{
ainf = info.get_ammo_info();
basesprite = weapon;
}
if (ainf)
{
int sprite = ainf->get_sprite_shape();
if (sprite == -3)
proj = basesprite;
else if (sprite != -1 && sprite != ainf->get_family_shape())
proj = sprite;
}
else