forked from nuvie/nuvie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEffect.cpp
2077 lines (1774 loc) · 57 KB
/
Effect.cpp
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
#include <cstring>
#include "nuvieDefs.h"
#include "Game.h"
#include "Actor.h"
#include "Map.h"
#include "Party.h"
#include "Script.h"
#include "AnimManager.h"
#include "MapWindow.h"
#include "TileManager.h"
#include "GameClock.h"
#include "EffectManager.h"
#include "UseCode.h"
#include "MsgScroll.h"
#include "ActorManager.h"
#include "SoundManager.h"
#include "U6objects.h"
#include "Effect.h"
#include "Player.h"
#include <cassert>
#define MESG_ANIM_HIT_WORLD ANIM_CB_HIT_WORLD
#define MESG_ANIM_HIT ANIM_CB_HIT
#define MESG_ANIM_DONE ANIM_CB_DONE
#define MESG_EFFECT_COMPLETE EFFECT_CB_COMPLETE
//#define MESG_INPUT_READY EVENT_CB_INPUT_READY
#define MESG_INPUT_READY MSGSCROLL_CB_TEXT_READY
#define TRANSPARENT_COLOR 0xFF /* transparent pixel color */
#define EXP_EFFECT_TILE_NUM 382
QuakeEffect *QuakeEffect::current_quake = NULL;
FadeEffect *FadeEffect::current_fade = NULL;
/* Add self to effect list (for future deletion).
*/
Effect::Effect() : defunct(false)
{
retain_count = 0;
game = Game::get_game();
effect_manager = game->get_effect_manager();
effect_manager->add_effect(this);
}
Effect::~Effect()
{
// FIXME: should we remove self from Callbacks' default targets?
}
/* Start managing new animation. (AnimMgr will do that actually, but we point to
* it and can stop it when necessary)
*/
void Effect::add_anim(NuvieAnim *anim)
{
anim->set_target(this); // add self as callback target for anim
game->get_map_window()->get_anim_manager()->new_anim(anim);
}
/* Fire from a cannon in direction: 0=north, 1=east, 2=south, 3=west,
* -1=use cannon frame
*/
CannonballEffect::CannonballEffect(Obj *src_obj, sint8 direction)
: target_loc()
{
usecode = game->get_usecode();
obj = src_obj;
MapCoord obj_loc(obj->x, obj->y, obj->z);
target_loc = obj_loc;
if(direction == -1)
direction = obj->frame_n;
uint8 target_dist = 5; // distance that cannonball will fly
if(direction == NUVIE_DIR_N)
target_loc.y -= target_dist;
else if(direction == NUVIE_DIR_E)
target_loc.x += target_dist;
else if(direction == NUVIE_DIR_S)
target_loc.y += target_dist;
else if(direction == NUVIE_DIR_W)
target_loc.x -= target_dist;
start_anim();
}
/* Pause world & start animation. */
void CannonballEffect::start_anim()
{
MapCoord obj_loc(obj->x, obj->y, obj->z);
game->pause_world();
game->pause_anims();
game->pause_user();
anim = new TossAnim(game->get_tile_manager()->get_tile(399),
obj_loc, target_loc, CANNON_SPEED, TOSS_TO_BLOCKING|TOSS_TO_ACTOR|TOSS_TO_OBJECT);
add_anim(anim);
}
/* Handle messages from animation. Hit actors & walls. */
uint16 CannonballEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
bool stop_effect = false;
Actor *hit_actor = NULL;
switch(msg)
{
case MESG_ANIM_HIT_WORLD:
{
MapCoord *hit_loc = static_cast<MapCoord *>(msg_data);
Tile *obj_tile = game->get_obj_manager()->get_obj_tile(hit_loc->x, hit_loc->y, hit_loc->z);
Tile *tile = game->get_game_map()->get_tile(hit_loc->x,hit_loc->y,
hit_loc->z);
if((tile->flags2 & TILEFLAG_MISSILE_BOUNDARY)
|| (obj_tile && (obj_tile->flags2 & TILEFLAG_MISSILE_BOUNDARY)))
{
//new ExplosiveEffect(hit_loc->x, hit_loc->y, 2);
new ExpEffect(EXP_EFFECT_TILE_NUM, MapCoord(hit_loc->x, hit_loc->y, hit_loc->z));
stop_effect = true;
}
break;
}
case MESG_ANIM_HIT:
{
MapEntity *hit_ent = static_cast<MapEntity *>(msg_data);
if(hit_ent->entity_type == ENT_ACTOR)
{
//hit_ent->actor->hit(32);
hit_actor = hit_ent->actor;
stop_effect = true;
}
if(hit_ent->entity_type == ENT_OBJ)
{
DEBUG(0,LEVEL_DEBUGGING,"hit object %d at %x,%x,%x\n", hit_ent->obj->obj_n, hit_ent->obj->x, hit_ent->obj->y, hit_ent->obj->z);
// FIX: U6 specific
// FIX: hit any part of ship, and reduce qty of center
if(hit_ent->obj->obj_n == 412)
{
uint8 f = hit_ent->obj->frame_n;
if(f == 9 || f == 15 || f == 11 || f == 13) // directions
{
if(hit_ent->obj->qty < 20) hit_ent->obj->qty = 0;
else hit_ent->obj->qty -= 20;
if(hit_ent->obj->qty == 0)
game->get_scroll()->display_string("Ship broke!\n");
stop_effect = true;
}
}
}
break;
}
case MESG_ANIM_DONE:
//new ExplosiveEffect(target_loc.x, target_loc.y, 3);
new ExpEffect(EXP_EFFECT_TILE_NUM, MapCoord(target_loc.x, target_loc.y, target_loc.z));
stop_effect = true;
break;
}
if(stop_effect)
{
if(hit_actor)
{
anim->pause(); //pause to avoid recursive problems when animations are called from actor_hit() lua script.
Game::get_game()->get_script()->call_actor_hit(hit_actor, 32, true);
}
if(msg != MESG_ANIM_DONE) // this msg means anim stopped itself
anim->stop();
game->unpause_all();
usecode->message_obj(obj, MESG_EFFECT_COMPLETE, this);
delete_self();
}
return(0);
}
#define EXP_EFFECT_SPEED 3
ExpEffect::ExpEffect(uint16 tileNum, MapCoord location)
{
start_loc = location;
finished_tiles = 0;
exp_tile_num = tileNum;
usecode = NULL;
obj = NULL;
start_anim();
}
/* Pause world & start animation. */
void ExpEffect::start_anim()
{
game->pause_world();
game->pause_anims();
game->pause_user();
targets.resize(16);
targets[0] = MapCoord(start_loc.x+2,start_loc.y-1,start_loc.z);
targets[1] = MapCoord(start_loc.x+1,start_loc.y+2,start_loc.z);
targets[2] = MapCoord(start_loc.x,start_loc.y-2,start_loc.z);
targets[3] = MapCoord(start_loc.x+1,start_loc.y-1,start_loc.z);
targets[4] = MapCoord(start_loc.x-1,start_loc.y+2,start_loc.z);
targets[5] = MapCoord(start_loc.x-1,start_loc.y-1,start_loc.z);
targets[6] = MapCoord(start_loc.x-2,start_loc.y,start_loc.z);
targets[7] = MapCoord(start_loc.x-1,start_loc.y+1,start_loc.z);
targets[8] = MapCoord(start_loc.x,start_loc.y+2,start_loc.z);
targets[9] = MapCoord(start_loc.x-1,start_loc.y-2,start_loc.z);
targets[10] = MapCoord(start_loc.x-2,start_loc.y-1,start_loc.z);
targets[11] = MapCoord(start_loc.x-2,start_loc.y+1,start_loc.z);
targets[12] = MapCoord(start_loc.x+2,start_loc.y+1,start_loc.z);
targets[13] = MapCoord(start_loc.x+2,start_loc.y,start_loc.z);
targets[14] = MapCoord(start_loc.x+1,start_loc.y+1,start_loc.z);
targets[15] = MapCoord(start_loc.x+1,start_loc.y-2,start_loc.z);
anim = new ProjectileAnim(exp_tile_num, &start_loc, targets, EXP_EFFECT_SPEED, true);
add_anim(anim);
}
ProjectileEffect::ProjectileEffect(uint16 tileNum, MapCoord start, MapCoord target, uint8 speed, bool trailFlag, uint16 initialTileRotation, uint16 rotationAmount, uint8 src_y_offset)
{
vector<MapCoord> t;
t.push_back(target);
init(tileNum, start, t, speed, trailFlag, initialTileRotation, rotationAmount, src_y_offset);
}
ProjectileEffect::ProjectileEffect(uint16 tileNum, MapCoord start, vector<MapCoord> t, uint8 speed, bool trailFlag, uint16 initialTileRotation)
{
init(tileNum, start, t, speed, trailFlag, initialTileRotation, 0, 0);
}
void ProjectileEffect::init(uint16 tileNum, MapCoord start, vector<MapCoord> t, uint8 speed, bool trailFlag, uint16 initialTileRotation, uint16 rotationAmount, uint8 src_y_offset)
{
finished_tiles = 0;
tile_num = tileNum;
start_loc = start;
anim_speed = speed;
trail = trailFlag;
initial_tile_rotation = initialTileRotation;
rotation_amount = rotationAmount;
src_tile_y_offset = src_y_offset;
targets = t;
start_anim();
}
/* Pause world & start animation. */
void ProjectileEffect::start_anim()
{
game->pause_world();
//game->pause_anims();
game->pause_user();
add_anim(new ProjectileAnim(tile_num, &start_loc, targets, anim_speed, trail, initial_tile_rotation, rotation_amount, src_tile_y_offset));
}
/* Handle messages from animation. Hit actors & walls. */
uint16 ProjectileEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
bool stop_effect = false;
switch(msg)
{
case MESG_ANIM_HIT_WORLD:
{
MapCoord *hit_loc = static_cast<MapCoord *>(msg_data);
Tile *tile = game->get_game_map()->get_tile(hit_loc->x,hit_loc->y,
hit_loc->z);
if(tile->flags1 & TILEFLAG_WALL)
{
//new ExplosiveEffect(hit_loc->x, hit_loc->y, 2);
stop_effect = true;
}
break;
}
case MESG_ANIM_HIT:
{
MapEntity *hit_ent = static_cast<MapEntity *>(msg_data);
hit_entities.push_back(*hit_ent);
break;
}
case MESG_ANIM_DONE:
//new ExplosiveEffect(target_loc.x, target_loc.y, 3);
stop_effect = true;
break;
}
if(stop_effect)
{
//finished_tiles++;
if(msg != MESG_ANIM_DONE) // this msg means anim stopped itself
((NuvieAnim *)caller)->stop();
//if(finished_tiles == 16)
// {
game->unpause_world();
game->unpause_user();
game->unpause_anims();
//usecode->message_obj(obj, MESG_EFFECT_COMPLETE, this);
delete_self();
// }
}
return(0);
}
/*** TimedEffect ***/
void TimedEffect::start_timer(uint32 delay)
{
if(!timer)
timer = new TimedCallback(this, NULL, delay, true);
}
void TimedEffect::stop_timer()
{
if(timer)
{
timer->clear_target();
timer = NULL;
}
}
/*** QuakeEffect ***/
/* Shake the visible play area around for `duration' milliseconds. Magnitude
* determines the speed of movement. An actor may be selected to keep the
* MapWindow centered on after the Quake.
*/
QuakeEffect::QuakeEffect(uint8 magnitude, uint32 duration, Actor *keep_on)
{
// single use only, so MapWindow doesn't keep moving away from center
// ...and do nothing if magnitude isn't usable
if(current_quake || magnitude == 0)
{
delete_self();
return;
}
current_quake = this; // cleared in timer function
map_window = game->get_map_window();
stop_time = game->get_clock()->get_ticks() + duration;
strength = magnitude;
// get random direction (always move left-right more than up-down)
init_directions();
map_window->get_pos(&orig.x, &orig.y);
map_window->get_level(&orig.z);
orig_actor = keep_on;
map_window->set_freeze_blacking_location(true);
start_timer(strength * 5);
}
QuakeEffect::~QuakeEffect()
{
}
/* On TIMED: Move map.
*/
uint16 QuakeEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
// uint8 twice_strength = strength * 2;
if(msg != MESG_TIMED)
return(0);
if(game->get_clock()->get_ticks() >= stop_time)
{
stop_quake();
return(0);
}
recenter_map();
map_window->shiftMapRelative(sx, sy);
// move in opposite direction on next call
if(sx == -(4*strength) || sx == (4*strength))
sx = (sx == -(4*strength)) ? (2*strength) : -(2*strength);
else if(sx == -(2*strength) || sx == (2*strength))
sx = 0;
if(sy == -(2*strength) || sy == (2*strength))
sy = 0;
if(sx == 0 && sy == 0)
init_directions();
return(0);
}
/* Finish effect. Move map back to initial position.
*/
void QuakeEffect::stop_quake()
{
current_quake = NULL;
map_window->set_freeze_blacking_location(false);
recenter_map();
delete_self();
}
/* Set sx,sy to a random direction. (always move left-right more than up-down)
*/
void QuakeEffect::init_directions()
{
uint8 dir = NUVIE_RAND() % 8;
sx = 0; sy = 0;
switch(dir)
{
case NUVIE_DIR_N : sy = -(strength*2); break;
case NUVIE_DIR_NE : sx = (strength*4); sy = -(strength*2); break;
case NUVIE_DIR_E : sx = (strength*4); break;
case NUVIE_DIR_SE : sx = (strength*4); sy = (strength*2); break;
case NUVIE_DIR_S : sy = (strength*2); break;
case NUVIE_DIR_SW : sx = -(strength*4); sy = (strength*2); break;
case NUVIE_DIR_W : sx = -(strength*4); break;
case NUVIE_DIR_NW : sx = -(strength*4); sy = -(strength*2); break;
}
}
/* Center map on original actor or move to original location.
*/
void QuakeEffect::recenter_map()
{
if(orig_actor)
map_window->centerMapOnActor(orig_actor);
else
map_window->moveMap(orig.x, orig.y, orig.z);
}
/*** HitEffect ***/
/* Hit target actor. FIXME: implement duration and hitting a location
*/
HitEffect::HitEffect(Actor *target, uint32 duration)
{
game->pause_user();
add_anim(new HitAnim(target));
Game::get_game()->get_sound_manager()->playSfx(NUVIE_SFX_HIT); //FIXME use NUVIE_SFX_SAMPLE defines here.
}
HitEffect::HitEffect(MapCoord location)
{
game->pause_user();
add_anim(new HitAnim(&location));
Game::get_game()->get_sound_manager()->playSfx(NUVIE_SFX_HIT); //FIXME use NUVIE_SFX_SAMPLE defines here.
}
/* On ANIM_DONE: end
*/
uint16 HitEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
if(msg == MESG_ANIM_DONE)
{
game->unpause_user();
delete_self();
}
return(0);
}
TextEffect::TextEffect(std::string text) // default somewhat centered on player for cheat messages
{
MapWindow *map_window = game->get_map_window();
if(!map_window || map_window->Status() != WIDGET_VISIBLE) // scripted sequence like intro and intro menu
return;
MapCoord loc = game->get_player()->get_actor()->get_location();
loc.x = (loc.x- map_window->get_cur_x() -2)*16;
loc.y = (loc.y- map_window->get_cur_y() -1)*16;
add_anim(new TextAnim(text, loc, 1500));
}
/*** TextEffect ***/
/* Print Text to MapWindow for duration
*/
TextEffect::TextEffect(std::string text, MapCoord location)
{
add_anim(new TextAnim(text, location, 1500));
}
/* On ANIM_DONE: end
*/
uint16 TextEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
if(msg == MESG_ANIM_DONE)
{
delete_self();
}
return(0);
}
/*** ExplosiveEffect ***/
ExplosiveEffect::ExplosiveEffect(uint16 x, uint16 y, uint32 size, uint16 dmg)
: start_at()
{
start_at.x = x; start_at.y = y;
radius = size;
hit_damage = dmg;
start_anim();
}
/* Pause world & start animation.
*/
void ExplosiveEffect::start_anim()
{
game->pause_world();
game->pause_user();
add_anim(new ExplosiveAnim(&start_at, radius));
}
/* Handle messages from animation. Hit actors & objects.
*/
uint16 ExplosiveEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
bool stop_effect = false;
switch(msg)
{
case MESG_ANIM_HIT:
{
MapEntity *hit_ent = static_cast<MapEntity *>(msg_data);
if(hit_ent->entity_type == ENT_ACTOR)
{
if(hit_damage != 0) // hit actor if effect causes damage
hit_ent->actor->hit(hit_damage);
}
else if(hit_ent->entity_type == ENT_OBJ)
{
DEBUG(0,LEVEL_DEBUGGING,"Explosion hit object %d (%x,%x)\n", hit_ent->obj->obj_n, hit_ent->obj->x, hit_ent->obj->y);
stop_effect = hit_object(hit_ent->obj);
}
break;
}
case MESG_ANIM_DONE:
stop_effect = true;
break;
}
if(stop_effect)
{
if(msg != MESG_ANIM_DONE)
anim->stop();
game->unpause_world();
game->unpause_user();
delete_self();
}
return(0);
}
/* UseCodeExplosiveEffect: before deleting send message to source object
*/
void UseCodeExplosiveEffect::delete_self()
{
if(obj)
game->get_usecode()->message_obj(obj, MESG_EFFECT_COMPLETE, this);
Effect::delete_self();
}
/* The explosion hit an object.
* Returns true if the effect should end, false to continue.
*/
bool UseCodeExplosiveEffect::hit_object(Obj *hit_obj)
{
// ignite & destroy powder kegs (U6)
if(hit_obj->obj_n == 223 && hit_obj != original_obj)
{
// FIXME: this doesn't belong here (U6/obj specific)
uint16 x = hit_obj->x, y = hit_obj->y;
game->get_obj_manager()->remove_obj_from_map(hit_obj);
delete_obj(hit_obj);
if(obj) // pass our source obj on to next effect as original_obj
new UseCodeExplosiveEffect(NULL, x, y, 2, hit_damage, obj);
else // pass original_obj on to next effect
new UseCodeExplosiveEffect(NULL, x, y, 2, hit_damage, original_obj);
}
return(false);
}
/*** ThrowObjectEffect ***/
ThrowObjectEffect::ThrowObjectEffect()
{
obj_manager = game->get_obj_manager();
anim = NULL;
throw_obj = NULL;
throw_tile = 0;
throw_speed = 0;
degrees = 0;
stop_flags = 0;
}
void ThrowObjectEffect::start_anim()
{
game->pause_anims();
game->pause_world();
game->pause_user();
assert(throw_tile || throw_obj); // make sure it was properly initialized
assert(throw_speed != 0);
if(throw_obj)
anim = new TossAnim(throw_obj, degrees, start_at, stop_at, throw_speed, stop_flags);
else
anim = new TossAnim(throw_tile, start_at, stop_at, throw_speed, stop_flags);
add_anim(anim);
}
/* Object has stopped. */
void ThrowObjectEffect::hit_target()
{
if(anim)
anim->stop();
game->unpause_all();
delete_self();
}
/* The animation will travel from original object location to drop location if
* NULL actor is specified.
*/
DropEffect::DropEffect(Obj *obj, uint16 qty, Actor *actor, MapCoord *drop_loc)
{
drop_from_actor = actor;
start_at = drop_from_actor ? drop_from_actor->get_location() : MapCoord(obj->x, obj->y, obj->z);
stop_at = *drop_loc;
degrees = 90;
get_obj(obj, qty); // remove from actor, set throw_obj
if(start_at != stop_at)
{
throw_speed = 192; // animation speed
start_anim();
}
else
hit_target(); // done already? why bother calling DropEffect? :p
}
/* Take `qty' objects of a stack if necessary, and remove from the actor's
* inventory. Set `throw_obj'.
*/
void DropEffect::get_obj(Obj *obj, uint16 qty)
{
throw_obj = obj_manager->get_obj_from_stack(obj, qty);
if(drop_from_actor)
drop_from_actor->inventory_remove_obj(throw_obj);
}
/* On ANIM_HIT_WORLD: end at hit location
* On ANIM_DONE: end
*/
uint16 DropEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
// if throw_obj is NULL, object already hit target
if(!throw_obj || (msg != MESG_ANIM_DONE && msg != MESG_ANIM_HIT_WORLD))
return(0);
if(msg == MESG_ANIM_HIT_WORLD && stop_at == *(MapCoord *)msg_data
&& anim)
anim->stop();
hit_target();
return(0);
}
/* Add object to map. (call before completing effect) */
void DropEffect::hit_target()
{
throw_obj->x = stop_at.x;
throw_obj->y = stop_at.y;
throw_obj->z = stop_at.z;
//FIXME drop logic should probably be in lua script.
if(drop_from_actor && obj_manager->is_breakable(throw_obj) && start_at.distance(stop_at) > 1)
{
nuvie_game_t game_type = game->get_game_type();
if(game_type == NUVIE_GAME_U6 && throw_obj->obj_n == OBJ_U6_DRAGON_EGG)
{
throw_obj->frame_n = 1; //brake egg.
obj_manager->add_obj(throw_obj, OBJ_ADD_TOP);
}
else if(game_type == NUVIE_GAME_U6 && throw_obj->obj_n == OBJ_U6_MIRROR)
{
throw_obj->frame_n = 2; //break mirror.
obj_manager->add_obj(throw_obj, OBJ_ADD_TOP);
}
else
{ // remove items from container if there is one
if(game->get_usecode()->is_container(throw_obj))
{
U6Link *link = throw_obj->container->start();
for(;link != NULL; link = throw_obj->container->start())
{
Obj *obj = (Obj *)link->data;
obj_manager->moveto_map(obj, stop_at);
}
}
obj_manager->unlink_from_engine(throw_obj);
delete_obj(throw_obj);
}
Game::get_game()->get_scroll()->display_string("\nIt broke!\n");
Game::get_game()->get_sound_manager()->playSfx(NUVIE_SFX_BROKEN_GLASS);
}
else
{
Obj *dest_obj = obj_manager->get_obj(stop_at.x, stop_at.y, stop_at.z);
if(obj_manager->can_store_obj(dest_obj, throw_obj))
obj_manager->moveto_container(throw_obj, dest_obj);
else
obj_manager->add_obj(throw_obj, OBJ_ADD_TOP);
}
throw_obj = NULL; // set as dropped
// not appropriate to do "Event::endAction(true)" from here to display
// prompt, as we MUST unpause_user() in ThrowObjectEffect::hit_target, and
// that would be redundant and may not unpause everything if wait mode was
// already cancelled... so just prompt
game->get_scroll()->display_string("\n");
game->get_scroll()->display_prompt();
game->get_map_window()->updateBlacking();
ThrowObjectEffect::hit_target(); // calls delete_self()
}
/*** MissileEffect ***/
MissileEffect::MissileEffect(uint16 tile_num, uint16 obj_n, const MapCoord &source,
const MapCoord &target, uint8 dmg,
uint8 intercept, uint16 speed)
{
actor_manager = game->get_actor_manager();
hit_actor = 0;
hit_obj = 0;
init(tile_num, obj_n, source, target, dmg, intercept, speed);
}
/* Start effect. If target is unset then the actor is the target. */
void MissileEffect::init(uint16 tile_num, uint16 obj_n,
const MapCoord &source, const MapCoord &target,
uint32 dmg, uint8 intercept, uint32 speed)
{
assert(tile_num || obj_n); // at least obj_n must be set
// (although it might work if throw_obj is already set)
assert(speed != 0);
assert(intercept != 0); // must hit target
if(obj_n != 0)
throw_obj = new_obj(obj_n, 0, 0,0,0);
if(tile_num != 0)
throw_tile = game->get_tile_manager()->get_tile(tile_num);
else if(throw_obj != 0)
throw_tile = obj_manager->get_obj_tile(throw_obj->obj_n,0);
throw_speed = speed;
hit_damage = dmg;
start_at = source;
stop_at = target;
stop_flags = intercept;
assert(stop_at != start_at); // Hmm, can't attack self with boomerang then
// if (stop_at != start_at) {
// start_at.x=WRAPPED_COORD(start_at.x+1,start_at.z);
// start_at.y=WRAPPED_COORD(start_at.y-1,start_at.z);
// }
// set tile rotation here based on obj_num
if(throw_obj != 0)
{
if(throw_obj->obj_n == OBJ_U6_SPEAR)
degrees = 315;
if(throw_obj->obj_n == OBJ_U6_THROWING_AXE)
degrees = 0;
if(throw_obj->obj_n == OBJ_U6_DAGGER)
degrees = 315;
if(throw_obj->obj_n == OBJ_U6_ARROW)
degrees = 270;
if(throw_obj->obj_n == OBJ_U6_BOLT)
degrees = 270;
}
start_anim();
}
/* On HIT: hit Actor or Obj and end
* On HIT_WORLD: end at hit location, hit Actor or Obj, else place obj
* On DONE: end
*/
uint16 MissileEffect::callback(uint16 msg, CallBack *caller, void *msg_data)
{
if(msg != MESG_ANIM_DONE && msg != MESG_ANIM_HIT_WORLD && msg != MESG_ANIM_HIT)
return 0;
if(msg == MESG_ANIM_DONE)
{
// will always hit anything at the target
// FIXME: only hit breakable objects like doors
// hit_obj = obj_manager->get_obj(stop_at.x,stop_at.y,stop_at.z);
hit_actor = actor_manager->get_actor(stop_at.x,stop_at.y,stop_at.z);
hit_target();
}
else if(msg == MESG_ANIM_HIT && ((MapEntity *)msg_data)->entity_type == ENT_ACTOR)
{
if(hit_damage != 0)
hit_actor = ((MapEntity*)msg_data)->actor;
hit_target();
}
else if(msg == MESG_ANIM_HIT && ((MapEntity *)msg_data)->entity_type == ENT_OBJ)
{
// FIXME: only hit breakable objects like doors
/* if(hit_damage != 0)
hit_obj = ((MapEntity*)msg_data)->obj;
hit_target();*/
}
// MESG_ANIM_HIT_WORLD
hit_blocking();
return 0;
}
/* Hit target or add object to map. (call before completing effect) */
void MissileEffect::hit_target()
{
if(hit_actor)
{
hit_actor->hit(hit_damage, ACTOR_FORCE_HIT);
delete_obj(throw_obj); throw_obj = 0; // don't drop
}
else if(hit_obj)
{
if(hit_obj->qty < hit_damage)
hit_obj->qty = 0;
else hit_obj->qty -= hit_damage;
delete_obj(throw_obj); throw_obj = 0; // don't drop
}
if(throw_obj != 0)
{
throw_obj->x = stop_at.x; throw_obj->y = stop_at.y;
throw_obj->z = stop_at.z;
throw_obj->status |= OBJ_STATUS_OK_TO_TAKE | OBJ_STATUS_TEMPORARY;
if(obj_manager->is_stackable(throw_obj))
throw_obj->qty = 1; // stackable objects must have a quantity
obj_manager->add_obj(throw_obj, OBJ_ADD_TOP);
throw_obj = 0;
}
ThrowObjectEffect::hit_target(); // calls delete_self()
}
void MissileEffect::hit_blocking()
{
delete_obj(throw_obj);
ThrowObjectEffect::hit_target();
}
/*** SleepEffect ***/
/* The TimedAdvance is started after the fade-out completes. */
SleepEffect::SleepEffect(std::string until)
: timer(NULL),
stop_hour(0),
stop_minute(0),
stop_time("")
{
stop_time = until;
game->pause_user();
effect_manager->watch_effect(this, new FadeEffect(FADE_PIXELATED, FADE_OUT));
}
SleepEffect::SleepEffect(uint8 to_hour)
: timer(NULL),
stop_hour(to_hour),
stop_minute(0),
stop_time("")
{
game->pause_user();
effect_manager->watch_effect(this, new FadeEffect(FADE_PIXELATED, FADE_OUT));
}
SleepEffect::~SleepEffect()
{
//if(timer) // make sure it doesn't try to call us again
// timer->clear_target();
}
/* As with TimedEffect, make sure the timer doesn't try to use callback again.
*/
void SleepEffect::delete_self()
{
//timer->clear_target(); // this will also stop/delete the TimedAdvance
//timer = NULL;
Effect::delete_self();
}
/* Resume normal play when requested time has been reached.
*/
//FIXME: need to handle TimedAdvance() errors and fade-in
uint16 SleepEffect::callback(uint16 msg, CallBack *caller, void *data)
{
uint8 hour = Game::get_game()->get_clock()->get_hour();
uint8 minute = Game::get_game()->get_clock()->get_minute();
// waited for FadeEffect
if(msg == MESG_EFFECT_COMPLETE)
{
if(timer == NULL) // starting
{
if(stop_time != "") // advance to start time
{
timer = new TimedAdvance(stop_time, 360); // 6 hours per second FIXME: it isnt going anywhere near that fast
timer->set_target(this);
timer->get_time_from_string(stop_hour, stop_minute, stop_time);
// stop_hour & stop_minute are checked each hour
}
else // advance a number of hours
{
uint16 advance_h = (hour == stop_hour) ? 24
: (hour < stop_hour) ? (stop_hour-hour)
: (24-(hour-stop_hour));
timer = new TimedAdvance(advance_h, 360);
timer->set_target(this);
stop_minute = minute;
}
}
else // stopping
{
Party *party = game->get_party();
for(int s=0; s<party->get_party_size(); s++)
{
Actor *actor = party->get_actor(s);
//heal actors.
uint8 hp_diff = actor->get_maxhp() - actor->get_hp();
if(hp_diff > 0)
{
if(hp_diff == 1)
hp_diff = 2;
actor->set_hp(actor->get_hp() + NUVIE_RAND()%(hp_diff/2) + hp_diff/2);
}
}
game->unpause_user();
delete_self();
}
return(0);
}
// assume msg == MESG_TIMED; will stop after effect completes
if(hour == stop_hour && minute >= stop_minute)
effect_manager->watch_effect(this, new FadeEffect(FADE_PIXELATED, FADE_IN));
return(0);
}
/*** FadeEffect ***/
FadeEffect::FadeEffect(FadeType fade, FadeDirection dir, uint32 color, uint32 speed)
{
speed = speed ? speed : game->get_map_window()->get_win_area()*2116; // was 256000
init(fade, dir, color, NULL, 0, 0, speed);
}
/* Takes an image to fade from/to. */
FadeEffect::FadeEffect(FadeType fade, FadeDirection dir, SDL_Surface *capture, uint32 speed)
{
speed = speed ? speed : game->get_map_window()->get_win_area()*1620; // was 196000
init(fade, dir, 0, capture, 0, 0, speed); // color=black
}