forked from exult/exult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
effects.cc
1875 lines (1695 loc) · 49.5 KB
/
effects.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
/*
* effects.cc - Special effects.
*
* 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 "effects.h"
#include "Audio.h"
#include "AudioMixer.h"
#include "Gump.h"
#include "Gump_manager.h"
#include "Zombie.h"
#include "actors.h"
#include "ammoinf.h"
#include "chunks.h"
#include "dir.h"
#include "egg.h"
#include "game.h"
#include "gameclk.h"
#include "gamemap.h"
#include "gamewin.h"
#include "ignore_unused_variable_warning.h"
#include "shapeinf.h"
#include "ucmachine.h"
#include "weaponinf.h"
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif // __GNUC__
#include <SDL.h>
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif // __GNUC__
using namespace Pentagram;
using std::rand;
using std::string;
using std::strlen;
using std::vector;
int Cloud::randcnt = 0;
int Lightning_effect::active = 0;
/**
* Clean up.
*/
Effects_manager::~Effects_manager() {
remove_all_effects(false);
}
/**
* Add text over a given item.
* @param msg text to add
* @param item Item text ID's, or null.
*/
void Effects_manager::add_text(const char* msg, Game_object* item) {
if (!msg) { // Happens with edited games.
return;
}
// Don't duplicate for item.
if (std::find_if(
texts.cbegin(), texts.cend(),
[item](const auto& el) {
return el->is_text(item);
})
!= texts.cend()) {
return; // Already have text on this.
}
// txt->paint(this); // Draw it.
// painted = 1;
texts.emplace_front(std::make_unique<Text_effect>(msg, item, gwin));
}
/**
* Add a text object at a given spot.
* @param msg text to add
* @param x x coord. on screen.
* @param y y coord. on screen.
*/
void Effects_manager::add_text(const char* msg, int x, int y) {
texts.emplace_front(std::make_unique<Text_effect>(
msg, gwin->get_scrolltx() + x / c_tilesize,
gwin->get_scrollty() + y / c_tilesize, gwin));
}
/**
* Add a text object in the center of the screen
*/
void Effects_manager::center_text(const char* msg) {
remove_text_effects();
Shape_manager* sman = Shape_manager::get_instance();
add_text(
msg, (gwin->get_width() - sman->get_text_width(0, msg)) / 2,
gwin->get_height() / 2);
}
/**
* Add an effect at the start of the chain.
*/
void Effects_manager::add_effect(std::unique_ptr<Special_effect> effect) {
effects.emplace_front(std::move(effect));
}
/**
* Remove a given object's text effect.
*/
void Effects_manager::remove_text_effect(
Game_object* item // Item text was added for.
) {
auto effectToDelete
= std::find_if(texts.begin(), texts.end(), [item](const auto& el) {
return el->is_text(item);
});
if (effectToDelete == texts.end()) {
return;
} else {
texts.erase(effectToDelete);
gwin->paint();
}
}
/**
* Remove a sprite from the chain and return it to caller
*/
std::unique_ptr<Special_effect> Effects_manager::remove_effect(
Special_effect* effect) {
auto toDelete = std::find_if(
std::begin(effects), std::end(effects), [effect](const auto& ef) {
return ef.get() == effect;
});
decltype(effects)::value_type result;
if (toDelete != effects.end()) {
result = std::move(*toDelete);
effects.erase(toDelete);
}
return result;
}
/**
* Remove text from the chain and delete it.
*/
void Effects_manager::remove_text_effect(Text_effect* txt) {
texts.remove_if([txt](const auto& el) {
return el.get() == txt;
});
}
/**
* Remove all text items.
*/
void Effects_manager::remove_all_effects(bool repaint) {
if (effects.empty() && texts.empty()) {
return;
}
effects.clear();
texts.clear();
if (repaint) {
gwin->paint(); // Just paint whole screen.
}
}
/**
* Remove text effects.
*/
void Effects_manager::remove_text_effects() {
texts.clear();
gwin->set_all_dirty();
}
/**
* Remove weather effects.
* @param dist Only remove those from eggs at least this far away.
*/
void Effects_manager::remove_weather_effects(int dist) {
Actor* main_actor = gwin->get_main_actor();
Tile_coord apos
= main_actor ? main_actor->get_tile() : Tile_coord(-1, -1, -1);
effects.remove_if([&](const auto& ef) {
return ef->is_weather()
&& (!dist
|| static_cast<Weather_effect*>(ef.get())->out_of_range(
apos, dist));
});
gwin->set_all_dirty();
}
/**
* Remove lightning effects.
*/
void Effects_manager::remove_usecode_lightning() {
effects.remove_if([](const auto& ef) {
return ef->is_usecode_lightning();
});
gwin->set_all_dirty();
}
/**
* Find last numbered weather effect added.
*/
int Effects_manager::get_weather() {
auto found = std::find_if(
effects.cbegin(), effects.cend(), [](const auto& ef) {
return ef->is_weather()
&& (static_cast<Weather_effect*>(ef.get())->get_num()
>= 0);
});
return found != effects.cend()
? static_cast<Weather_effect*>(found->get())->get_num()
: 0;
}
/**
* Construct and store game window singleton
* TODO check if passing it from other scope
* won't be better
*/
Special_effect::Special_effect() : gwin(Game_window::get_instance()) {}
/**
* Destructor
* Takes advantage of the fact that gwin
* is stored.
*/
Special_effect::~Special_effect() {
if (in_queue()) {
gwin->get_tqueue()->remove(this);
}
}
/**
* Paint them all.
*/
void Effects_manager::paint() {
for (auto& effect : effects) {
effect->paint();
}
}
/**
* Paint all text.
*/
void Effects_manager::paint_text() {
for (auto& txt : texts) {
txt->paint();
}
}
/**
* Paint all text.
*/
void Effects_manager::update_dirty_text() {
for (auto& txt : texts) {
txt->update_dirty();
}
}
/**
* Some special effects may not need painting.
*/
void Special_effect::paint() {}
/**
* Create an animation from the 'sprites.vga' file.
*/
Sprites_effect::Sprites_effect(
int num, // Index.
const Tile_coord& p, // Position within world.
int dx, int dy, // Add to offset for each frame.
int delay, // Delay (msecs) before starting.
int frm, // Starting frame.
int rps // Reps, or <0 to go through frames.
)
: sprite(num, frm, SF_SPRITES_VGA), pos(p), xoff(0), yoff(0),
deltax(dx), deltay(dy), reps(rps) {
frames = sprite.get_num_frames();
// Start.
gwin->get_tqueue()->add(Game::get_ticks() + delay, this);
}
/**
* Create an animation on an object.
*/
Sprites_effect::Sprites_effect(
int num, // Index.
Game_object* it, // Item to put effect by.
int xf, int yf, // Offset from actor in pixels.
int dx, int dy, // Add to offset on each frame.
int frm, // Starting frame.
int rps // Reps, or <0 to go through frames.
)
: sprite(num, frm, SF_SPRITES_VGA), item(weak_from_obj(it)), xoff(xf),
yoff(yf), deltax(dx), deltay(dy), reps(rps) {
pos = it->get_tile();
frames = sprite.get_num_frames();
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks(), this);
}
/**
* Add a dirty rectangle for the current position and frame.
*/
inline void Sprites_effect::add_dirty(int frnum) {
if (pos.tx == -1 || frnum == -1) {
return; // Already at destination.
}
Shape_frame* shape = sprite.get_shape();
const int lp = pos.tz / 2;
gwin->add_dirty(gwin->clip_to_win(
gwin->get_shape_rect(
shape,
xoff
+ (pos.tx - lp - gwin->get_scrolltx())
* c_tilesize,
yoff
+ (pos.ty - lp - gwin->get_scrollty())
* c_tilesize)
.enlarge((3 * c_tilesize) / 2)));
}
/**
* Animation.
*/
void Sprites_effect::handle_event(
unsigned long curtime, // Current time of day.
uintptr udata) {
int frame_num = sprite.get_framenum();
const int delay
= gwin->get_std_delay(); // Delay between frames. Needs to
// match usecode animations.
if (!reps || (reps < 0 && frame_num == frames)) { // At end?
// Remove & delete this.
gwin->set_all_dirty();
auto ownHandle = eman->remove_effect(this);
return;
}
add_dirty(frame_num); // Clear out old.
gwin->set_painted();
const Game_object_shared item_obj = item.lock();
if (item_obj) { // Following actor?
pos = item_obj->get_tile();
}
xoff += deltax; // Add deltas.
yoff += deltay;
frame_num++; // Next frame.
if (reps > 0) { // Given a count?
--reps;
frame_num %= frames;
}
add_dirty(frame_num); // Want to paint new frame.
sprite.set_frame(frame_num);
// Add back to queue for next time.
gwin->get_tqueue()->add(curtime + delay, this, udata);
}
/**
* Render.
*/
void Sprites_effect::paint() {
if (sprite.get_framenum() >= frames) {
return;
}
const int lp = pos.tz / 2; // Account for lift.
sprite.paint_shape(
xoff + (pos.tx - lp - gwin->get_scrolltx()) * c_tilesize
- gwin->get_scrolltx_lo(),
yoff + (pos.ty - lp - gwin->get_scrollty()) * c_tilesize
- gwin->get_scrolltx_lo());
}
static inline int get_explosion_shape(int weap, int proj) {
const int shp = proj >= 0 ? proj : (weap >= 0 ? weap : 704);
return ShapeID::get_info(shp).get_explosion_sprite();
}
static inline int get_explosion_sfx(int weap, int proj) {
const int shp = proj >= 0 ? proj : (weap >= 0 ? weap : 704);
return ShapeID::get_info(shp).get_explosion_sfx();
}
/**
* Start explosion.
*/
Explosion_effect::Explosion_effect(
const Tile_coord& p, Game_object* exp,
int delay, // Delay before starting (msecs).
int weap, // Weapon to use for damage calcs., or
// -1 for default(704 = powder keg).
int proj, // Projectile for e.g., burst arrows, 0 otherwise
Game_object* att // who is responsible for the explosion
// or nullptr for default
// Different sprites for different explosion types
)
: Sprites_effect(get_explosion_shape(weap, proj), p, 0, 0, delay),
explode(weak_from_obj(exp)),
weapon(weap >= 0 ? weap : (proj >= 0 ? proj : 704)), projectile(proj),
exp_sfx(get_explosion_sfx(weap, proj)), attacker(weak_from_obj(att)) {
// if (exp && exp->get_shapenum() == 704) // powderkeg
if (exp && exp->get_info().is_explosive()) { // powderkeg
exp->set_quality(1); // mark as detonating
}
if (!att || !att->as_actor()) {
// Blame avatar: if we have no living attacker.
attacker = weak_from_obj(gwin->get_main_actor());
}
}
void Explosion_effect::handle_event(
unsigned long curtime, // Current time of day.
uintptr udata) {
const int frnum = sprite.get_framenum();
if (!frnum) { // Max. volume, with stereo position.
Audio::get_ptr()->play_sound_effect(exp_sfx, pos, AUDIO_MAX_VOLUME);
}
if (frnum == frames / 4) {
// this was in ~Explosion_effect before
const Game_object_shared exp_obj = explode.lock();
if (exp_obj && !exp_obj->is_pos_invalid()) {
Game_window::get_instance()->add_dirty(exp_obj.get());
exp_obj->remove_this();
explode = Game_object_weak();
}
Shape_frame* shape = sprite.get_shape();
const int width = shape->get_width(); // Get the sprite's width
Game_object_vector vec; // Find objects near explosion.
Game_object::find_nearby(
vec, pos, c_any_shapenum, width / (2 * c_tilesize), 0);
// Objects could disappear due to Avatar being killed and teleported.
vector<Game_object_weak> cvec(vec.size());
Game_object::obj_vec_to_weak(cvec, vec);
for (auto& it : cvec) {
const Game_object_shared obj = it.lock();
if (obj) {
const Game_object_shared att_obj = attacker.lock();
obj->attacked(att_obj.get(), weapon, projectile, true);
}
}
}
Sprites_effect::handle_event(curtime, udata);
}
/**
* Get direction in 1/16's starting from North.
*/
inline int Get_dir16(Tile_coord& t1, Tile_coord& t2) {
// Treat as cartesian coords.
return Get_direction16(t1.ty - t2.ty, t2.tx - t1.tx);
}
/**
* Initialize path & add to time queue.
*/
void Projectile_effect::init(
const Tile_coord& s, // Source,
const Tile_coord& d // Destination.
) {
no_blocking = false; // We'll check the ammo & weapon.
const Weapon_info* winfo = ShapeID::get_info(weapon).get_weapon_info();
if (winfo) {
no_blocking = no_blocking || winfo->no_blocking();
if (speed < 0) {
speed = winfo->get_missile_speed();
}
autohit = winfo->autohits();
}
if (speed < 0) {
speed = 4;
}
const Ammo_info* ainfo
= ShapeID::get_info(projectile_shape).get_ammo_info();
if (ainfo) {
no_blocking = no_blocking || ainfo->no_blocking();
autohit = autohit || ainfo->autohits();
}
const Game_object_shared att_obj = attacker.lock();
const Game_object_shared tgt_obj = target.lock();
if (att_obj) { // Try to set start better.
const int dir = tgt_obj ? att_obj->get_direction(tgt_obj.get())
: att_obj->get_direction(d);
pos = att_obj->get_missile_tile(dir);
} else {
pos = s; // Get starting position.
}
Tile_coord dst = d;
if (tgt_obj) { // Try to set end better.
dst = tgt_obj->get_center_tile();
} else {
dst.tz = pos.tz;
}
path = new Zombie(); // Create simple pathfinder.
// Find path. Should never fail.
const bool explodes
= (winfo && winfo->explodes()) || (ainfo && ainfo->explodes());
if (explodes && ainfo && ainfo->is_homing()) {
path->NewPath(pos, pos, nullptr); // A bit of a hack, I know...
} else {
path->NewPath(pos, dst, nullptr);
if (att_obj) {
// Getprojectile out of shooter's volume.
const Block vol = att_obj->get_block();
Tile_coord t;
bool done;
while (path->GetNextStep(t, done)) {
if (!vol.has_world_point(t.tx, t.ty, t.tz)) {
break;
}
}
pos = t;
}
}
const int sprite_shape = sprite.get_shapenum();
set_sprite_shape(sprite_shape);
// Start after a slight delay.
gwin->get_tqueue()->add(Game::get_ticks(), this, gwin->get_std_delay() / 2);
}
void Projectile_effect::set_sprite_shape(int s) {
if (s < 0) {
skip_render = true;
sprite.set_shape(s);
sprite.set_frame(0);
return;
}
sprite.set_shape(s);
frames = sprite.get_num_frames();
if (frames >= 24) { // Use frames 8-23, for direction
// going clockwise from North.
Tile_coord src = path->get_src();
Tile_coord dest = path->get_dest();
const int dir = Get_dir16(src, dest);
sprite.set_frame(8 + dir);
}
// else if (frames == 1 && sprite.get_shapenum() != 704)
else if (frames == 1 && sprite.get_info().is_explosive()) {
sprite.set_frame(0); // (Don't show powder keg!)
} else {
skip_render = true; // We just won't show it.
}
add_dirty(); // Paint immediately.
}
/**
* Create a projectile animation.
*/
Projectile_effect::Projectile_effect(
Game_object* att, // Source of spell/attack.
Game_object* to, // End here, 'attack' it with shape.
int weap, // Weapon (bow, gun, etc.) shape.
int proj, // Projectile shape # in 'shapes.vga'.
int spr, // Shape to render on-screen or -1 for none.
int attpts, // Attack points of projectile.
int spd // Projectile speed, or -1 to use default.
)
: attacker(weak_from_obj(att)), target(weak_from_obj(to)), weapon(weap),
projectile_shape(proj), sprite(spr, 0), return_path(false),
skip_render(spr < 0), speed(spd), attval(attpts), autohit(false) {
init(att->get_tile(), to->get_tile());
}
/**
* Constructor used by missile eggs & fire_projectile intrinsic.
*/
Projectile_effect::Projectile_effect(
Game_object* att, // Source of spell/attack.
const Tile_coord& d, // End here.
int weap, // Weapon (bow, gun, etc.) shape.
int proj, // Projectile shape # in 'shapes.vga'.
int spr, // Shape to render on-screen or -1 for none.
int attpts, // Attack points of projectile.
int spd, // Projectile speed, or -1 to use default.
bool retpath // Return of a boomerang.
)
: attacker(weak_from_obj(att)), weapon(weap), projectile_shape(proj),
sprite(spr, 0), return_path(retpath), skip_render(spr < 0),
speed(spd), attval(attpts), autohit(false) {
init(att->get_tile(), d);
}
/*
* Another used by missile eggs and for 'boomerangs'.
*/
Projectile_effect::Projectile_effect(
const Tile_coord& s, // Start here.
Game_object* to, // End here, 'attack' it with shape.
int weap, // Weapon (bow, gun, etc.) shape.
int proj, // Projectile shape # in 'shapes.vga'.
int spr, // Shape to render on-screen or -1 for none.
int attpts, // Attack points of projectile.
int spd, // Projectile speed, or -1 to use default.
bool retpath // Return of a boomerang.
)
: target(weak_from_obj(to)), weapon(weap), projectile_shape(proj),
sprite(spr, 0), return_path(retpath), skip_render(spr < 0),
speed(spd), attval(attpts), autohit(false) {
init(s, to->get_tile());
}
/**
* Delete.
*/
Projectile_effect::~Projectile_effect() {
delete path;
}
/**
* Add a dirty rectangle for the current position and frame.
*/
inline void Projectile_effect::add_dirty() {
if (skip_render) {
return;
}
Shape_frame* shape = sprite.get_shape();
// Force repaint of prev. position.
const int liftpix = pos.tz * c_tilesize / 2;
gwin->add_dirty(gwin->clip_to_win(
gwin->get_shape_rect(
shape,
(pos.tx - gwin->get_scrolltx()) * c_tilesize - liftpix,
(pos.ty - gwin->get_scrollty()) * c_tilesize - liftpix)
.enlarge(c_tilesize / 2)));
}
/**
* See if something was hit.
*
* @return target hit, or 0.
*/
inline Game_object_shared Find_target(Game_window* gwin, Tile_coord pos) {
ignore_unused_variable_warning(gwin);
if (pos.tz % 5 == 0) { // On floor?
pos.tz++; // Look up 1 tile.
}
const Tile_coord dest = pos; // This gets modified.
if (!Map_chunk::is_blocked(pos, 1, MOVE_FLY, 0) && dest == pos) {
return nullptr;
}
Game_object* ptr = Game_object::find_blocking(pos);
return shared_from_obj(ptr);
}
/**
* Animation.
*/
void Projectile_effect::handle_event(
unsigned long curtime, // Current time of day.
uintptr udata) {
const int delay = gwin->get_std_delay() / 2;
add_dirty(); // Force repaint of old pos.
const Weapon_info* winf = ShapeID::get_info(weapon).get_weapon_info();
if (winf && winf->get_rotation_speed()) {
// The missile rotates (such as axes/boomerangs)
const int new_frame
= sprite.get_framenum() + winf->get_rotation_speed();
sprite.set_frame(
new_frame > 23 ? ((new_frame - 8) % 16) + 8 : new_frame);
}
bool path_finished = false;
const Game_object_shared att_obj = attacker.lock();
Game_object_shared tgt_obj = target.lock();
for (int i = 0; i < speed; i++) {
// This speeds up the missile.
path_finished = !path->GetNextStep(pos)
|| // Get next spot.
// If missile egg, detect target.
(!tgt_obj && !no_blocking
&& (tgt_obj = Find_target(gwin, pos)) != nullptr);
if (path_finished) {
target = Game_object_weak(tgt_obj);
break;
}
}
const Ammo_info* ainf = ShapeID::get_info(projectile_shape).get_ammo_info();
if (path_finished) {
// Done?
const bool explodes
= (winf && winf->explodes()) || (ainf && ainf->explodes());
if (return_path) { // Returned a boomerang?
const Ireg_game_object_shared obj
= gmap->create_ireg_object(sprite.get_shapenum(), 0);
if (!tgt_obj || !tgt_obj->add(obj.get())) {
obj->set_flag(Obj_flags::okay_to_take);
obj->set_flag(Obj_flags::is_temporary);
obj->move(pos.tx, pos.ty, pos.tz, -1);
}
} else if (explodes) { // Do this here (don't want to explode
// returning weapon).
Tile_coord offset;
if (tgt_obj) {
offset = Tile_coord(
0, 0, tgt_obj->get_info().get_3d_height() / 2);
} else {
offset = Tile_coord(0, 0, 0);
}
if (ainf && ainf->is_homing()) {
eman->add_effect(std::make_unique<Homing_projectile>(
weapon, att_obj.get(), tgt_obj.get(), pos,
pos + offset));
} else {
eman->add_effect(std::make_unique<Explosion_effect>(
pos + offset, nullptr, 0, weapon, projectile_shape,
att_obj.get()));
}
target = Game_object_weak(); // Takes care of attack.
} else {
// Not teleported away ?
const bool returns
= (winf && winf->returns()) || (ainf && ainf->returns());
bool hit = false;
if (tgt_obj && att_obj != tgt_obj &&
// Aims for center tile, so check center tile.
tgt_obj->get_center_tile().distance(pos) < 3) {
hit = autohit || tgt_obj->try_to_hit(att_obj.get(), attval);
if (hit) {
tgt_obj->play_hit_sfx(weapon, true);
tgt_obj->attacked(
att_obj.get(), weapon, projectile_shape, false);
}
} else {
// Hack warning: this exists solely to make Mind Blast (SI)
// work as it does in the original when you target the
// avatar with the spell.
if (winf && winf->get_usecode() > 0) {
ucmachine->call_usecode(
winf->get_usecode(), nullptr,
Usecode_machine::weapon);
}
}
if (returns && att_obj && // boomerangs
att_obj->distance(pos) < 50) {
// not teleported away
auto proj = std::make_unique<Projectile_effect>(
pos, att_obj.get(), weapon, projectile_shape,
sprite.get_shapenum(), attval, speed, true);
proj->set_speed(speed);
proj->set_sprite_shape(sprite.get_shapenum());
eman->add_effect(std::move(proj));
} else {
// See if we should drop projectile.
bool drop = false;
// Seems to match originals quite well.
if (!winf) {
drop = true;
} else if (ainf) {
const int ammo = winf->get_ammo_consumed();
const int type = ainf->get_drop_type();
drop = (ammo >= 0 || ammo == -3)
&& (type == Ammo_info::always_drop
|| (!hit && type != Ammo_info::never_drop));
}
if (drop) {
const Tile_coord dpos = Map_chunk::find_spot(
pos, 3, sprite.get_shapenum(), 0, 1);
if (dpos.tx != -1) {
const Game_object_shared aobj
= gmap->create_ireg_object(
sprite.get_shapenum(), 0);
if (!att_obj
|| att_obj->get_flag(Obj_flags::is_temporary)) {
aobj->set_flag(Obj_flags::is_temporary);
}
aobj->set_flag(Obj_flags::okay_to_take);
aobj->move(dpos);
}
}
}
}
add_dirty();
skip_render = true;
auto ownHandle = eman->remove_effect(this);
return;
}
add_dirty(); // Paint new spot/frame.
// Add back to queue for next time.
gwin->get_tqueue()->add(curtime + delay, this, udata);
}
/**
* Render.
*/
void Projectile_effect::paint() {
if (skip_render) {
return;
}
const int liftpix = pos.tz * c_tilesize / 2;
sprite.paint_shape(
(pos.tx - gwin->get_scrolltx()) * c_tilesize - liftpix
- gwin->get_scrolltx_lo(),
(pos.ty - gwin->get_scrollty()) * c_tilesize - liftpix
- gwin->get_scrollty_lo());
}
/**
* Create a 'death vortex' or an 'energy mist'.
*/
Homing_projectile::Homing_projectile( // A better name is welcome...
int shnum, // The projectile shape.
Game_object* att, // Who cast the spell.
Game_object* trg, // What to aim for.
const Tile_coord& sp, // Where to start.
const Tile_coord& tp // Target pos, if trg isn't an actor.
)
: sprite(ShapeID::get_info(shnum).get_explosion_sprite(), 0,
SF_SPRITES_VGA),
next_damage_time(0),
sfx(ShapeID::get_info(shnum).get_explosion_sfx()), channel(-1) {
weapon = shnum;
attacker = weak_from_obj(att);
pos = sp;
dest = tp;
target = trg ? trg->as_actor() : nullptr;
stationary = target == nullptr; // If true, the sprite will 'park' at
// dest
frames = sprite.get_num_frames();
// Go for 20 seconds.
stop_time = Game::get_ticks() + 20 * 1000;
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks(), this);
channel = Audio::get_ptr()->play_sound_effect(sfx, pos, -1);
}
/**
* Add a dirty rectangle for the current position and frame.
*
* @return Width in pixels.
*/
inline int Homing_projectile::add_dirty() {
Shape_frame* shape = sprite.get_shape();
const int liftpix = pos.tz * c_tilesize / 2;
gwin->add_dirty(gwin->clip_to_win(
gwin->get_shape_rect(
shape,
(pos.tx - gwin->get_scrolltx()) * c_tilesize - liftpix,
(pos.ty - gwin->get_scrollty()) * c_tilesize - liftpix)
.enlarge(c_tilesize / 2)));
return shape->get_width();
}
/**
* Animation.
*/
void Homing_projectile::handle_event(
unsigned long curtime, // Current time of day.
uintptr udata) {
const int width = add_dirty(); // Repaint old area.
if ((target && !target->is_dead()) || stationary) {
// Move to target/destination if needed
const Tile_coord tpos = stationary ? dest : target->get_tile();
const int deltax = tpos.tx - pos.tx;
const int deltay = tpos.ty - pos.ty;
const int deltaz
= tpos.tz
+ (stationary ? 0 : target->get_info().get_3d_height() / 2)
- pos.tz;
const int absx = deltax >= 0 ? deltax : -deltax;
const int absy = deltay >= 0 ? deltay : -deltay;
const int absz = deltaz >= 0 ? deltaz : -deltaz;
const uint32 dist = absx * absx + absy * absy + absz * absz;
if (dist > 1) {
if (deltax) {
pos.tx += deltax / absx;
}
if (deltay) {
pos.ty += deltay / absy;
}
if (deltaz) {
pos.tz += deltaz / absz;
}
}
} else {
// The target has been killed; find another one
Actor_vector npcs; // Find NPC's.
Game_object::find_nearby_actors(npcs, pos, -1, 30);
Actor* nearest = nullptr;
uint32 bestdist = 100000;
for (auto* npc : npcs) {
if (!npc->is_in_party() && !npc->is_dead()
&& (npc->get_effective_alignment() >= Actor::evil)) {
const Tile_coord npos = npc->get_tile();
const int dx = npos.tx - pos.tx;
const int dy = npos.ty - pos.ty;
const int dz = npos.tz - pos.tz;
const uint32 dist = dx * dx + dy * dy + dz * dz;
if (dist < bestdist) {
bestdist = dist;
nearest = npc;
}
}
}
target = nearest;
}
if (curtime > next_damage_time) { // Time to cause damage.
// Do it every second.
next_damage_time = curtime + 1000;
Actor_vector npcs; // Find NPC's.
Game_object::find_nearby_actors(
npcs, pos, -1, width / (2 * c_tilesize));
for (auto* npc : npcs) {
if (!npc->is_in_party()) {
// Still powerful, but no longer overkill...
// also makes the enemy react, which is good
const Game_object_shared att_obj = attacker.lock();
npc->attacked(att_obj.get(), weapon, weapon, true);
}
}
}
sprite.set_frame((sprite.get_framenum() + 1) % frames);
add_dirty(); // Paint new.
if (curtime < stop_time) { // Keep going?
gwin->get_tqueue()->add(curtime + 100, this, udata);
if (channel < 0) {
return;
}
channel = Audio::get_ptr()->update_sound_effect(channel, pos);
} else {
if (channel >= 0) {
AudioMixer::get_instance()->stopSample(channel);
channel = -1;
}
gwin->set_all_dirty();
auto ownHandle = eman->remove_effect(this);
return;
}
}
/**
* Render.
*/
void Homing_projectile::paint() {
const int liftpix = pos.tz * c_tilesize / 2;
sprite.paint_shape(
(pos.tx - gwin->get_scrolltx()) * c_tilesize - liftpix
- gwin->get_scrolltx_lo(),
(pos.ty - gwin->get_scrollty()) * c_tilesize - liftpix
- gwin->get_scrollty_lo());
}
/**