-
Notifications
You must be signed in to change notification settings - Fork 4
/
enemy.cpp
1658 lines (1494 loc) · 53 KB
/
enemy.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 "moyai/client.h"
#include "ids.h"
#include "dimension.h"
#include "util.h"
#include "atlas.h"
#include "conf.h"
#include "item.h"
#include "net.h"
#include "field.h"
#include "char.h"
#include "hud.h"
#include "mapview.h"
#include "effect.h"
#include "enemy.h"
#include "pc.h"
#include "globals.h"
////////////////
Enemy::Enemy( Vec2 lc, TileDeck *dk, bool flying, bool swimming, float fly_height, int client_id, int internal_id ) : Char( CAT_ENEMY, lc, dk, g_char_layer, client_id, internal_id ), v(0,0), hp(1), maxhp(1), fly_height(fly_height), flying(flying), swimming(swimming), shadow_prop(NULL), timeout(ENEMY_DEFAULT_TIMEOUT), beam_hits(true), machine(true), reward_itt(ITT_EMPTY), explosion_type(EXT_MACHINE), bt_can_enter(BT_AIR), body_size(8), hit_category(CAT_INVAL), push_pc(false), hyper_until(0), enable_hyper_effect(false), fly_on_kill_num(0), egg_on_kill_num(0), enable_durbar(true), enable_move(true), enemy_type(ET_INVAL), net_auto_sync_move_interval_sec(2), base_score(0) {
tex_epsilon = 1.0/1024.0/2.0;
setFragmentShader(g_eye_col_replacer);
render_children_first = true; // Shadow is always under main sprite
}
Enemy::~Enemy() {
if( enemy_type != ET_INVAL ) realtimeCharDeleteSend(this);
}
void Enemy::checkLocalSyncMoveSend() {
if( isLocal() ) {
realtimeEnemyMoveSend(this);
}
}
void Enemy::ensureShadow() {
if( !shadow_prop ) {
shadow_prop = createShadow(loc);
addChild(shadow_prop);
}
}
bool Enemy::charPoll( double dt ) {
if( enemyPoll(dt) == false ) {
return false;
}
Vec2 nloc;
if( enable_move ) {
nloc = loc + v * dt;
} else {
nloc = loc;
}
// Enemy hits enemy. used only by Builder. Builder can't go on to other Builder.
if( hit_category != CAT_INVAL ) {
Char *nearest = getNearestByCategory( loc, hit_category, this );
if(nearest ) {
float l = nearest->loc.len(loc);
if( l < body_size*2 ) {
Vec2 relv = nearest->loc.to(loc).normalize(PPC*8);
nloc += relv * dt;
}
}
}
// Enemy pushes PC (used only by Builder now)
if( push_pc ) {
Char *cur = (Char*) g_char_layer->prop_top;
while(cur) {
if( cur->category == CAT_PC ) {
float l = cur->loc.len(loc);
if( l < body_size ) {
PC *pc = (PC*)cur;
pc->onPushed( GROUNDER_PUSH_PC_DAMAGE, this);
onPushPC(pc);
}
}
cur = (Char*) cur->next;
}
}
Vec2 finalcand = calcNextLoc( loc, nloc, body_size, true, flying, swimming, bt_can_enter );
if( finalcand == loc ) {
// can't move..
}
loc = finalcand;
//
draw_offset.y = fly_height;
if( flying && shadow_prop ) {
shadow_prop->setLoc(loc);
}
// no flickering
if( timeout > 0 && accum_time > timeout ) {
onTimeout();
return false;
}
// Hyper effect
if( accum_time < hyper_until && enable_hyper_effect ){
if( updateInterval( INTVL_IND_HYPER, 0.5 ) && range(0,10) < 10 ) {
Particle *p = createHyperSpark(loc.randomize(PPC/2) + Vec2(0,fly_height));
if( enable_move) p->v = v;
}
}
// Getting hyper
Cell *c = g_fld->get(loc);
if( c && c->hyper_count > 0 ) {
hyper_until = accum_time + HYPER_TIME_SEC;
}
// Auto HP recovery by hyper
if( hyper_until > accum_time ) {
if( updateInterval(INTVL_IND_HYPER,0.5) ) {
hp ++;
if( hp>maxhp )hp = maxhp;
}
}
// regular syncing
if( net_auto_sync_move_interval_sec > 0 ) {
if( enemy_type != ET_INVAL && updateInterval( INTVL_IND_AUTOSYNC, net_auto_sync_move_interval_sec ) && isLocal() ) {
realtimeEnemyMoveSend(this);
}
}
return true;
}
void Enemy::notifyHitBeam( Beam *b, int dmg ) {
if( dmg <= 0 ) return;
onBeam(b,dmg);
realtimeEnemyDamageSend(this,dmg); // Send damage packet even when this instance is not generated locally (assume no cheating)
bool killed = applyDamage(dmg);
if(killed) {
// Destroyed!
bool hyp = hyper_until > accum_time;
int bonus_rate = hyp ? 2 : 1;
g_pc->modScore( base_score * bonus_rate );
}
}
// Returns true when destroyed
bool Enemy::applyDamage( int dmg ) {
hp -= dmg ;
if( enable_durbar ) {
DurBar *db = DurBar::ensure(this, hp, maxhp, g_durbar_deck );
db->draw_offset.y = fly_height;
}
if( isRemote() ) return false;
if( hp<= 0 ) {
switch(explosion_type) {
case EXT_NONE: break;
case EXT_CREATURE:
createDigSmoke(loc + Vec2(0,fly_height), 1);
soundPlayAt(g_killcreature_sound,loc,1);
break;
case EXT_MACHINE:
createExplosion( loc + Vec2(0,fly_height), 2,3 );
soundPlayAt(g_killmachine_sound,loc,1);
if(hyper_until > accum_time ){
Vec2 tgt;
if( g_fld->findEnemyAttackTarget(loc,&tgt, MACHINE_SHOOT_DISTANCE ) ) {
Bullet::shootFanAt( BLT_SPARIO, loc, tgt, 1, M_PI/8.0 );
}
}
break;
case EXT_BOSS:
createBossExplosion( loc + Vec2(0,fly_height) );
soundPlayAt(g_destroy_building_sound,loc,1);
break;
}
if( fly_on_kill_num > 0 && hyper_until > accum_time ) {
for(int i=0;i<fly_on_kill_num;i++){
new Fly( loc );
}
}
if( egg_on_kill_num > 0 && hyper_until > accum_time ) {
for(int i=0;i<egg_on_kill_num;i++){
Egg *e = new Egg( loc );
e->v = Vec2(0,0).randomize(PPC*3);
}
}
to_clean = true;
if(enable_durbar) DurBar::clear(this);
switch( reward_itt ) {
case ITT_ARTIFACT:
new Debris( loc, B_ATLAS_ITEM_ARTIFACT );
break;
default:
break;
}
if( machine && maxhp > 5 ) {
if( range(0,100) < 10 ) {
new Debris(loc, B_ATLAS_ITEM_ENERGY_PARTICLE );
}
}
onKill();
return true;
} else {
if(machine) {
soundPlayAt(g_beamhithard_sound,loc,1);
} else {
soundPlayAt(g_wormdamage_sound,loc,1);
}
return false;
}
}
Enemy *Enemy::getNearestShootable( Vec2 from ) {
float minl = 9999999999;
Enemy *out = NULL;
Char *cur = (Char*) g_char_layer->prop_top;
while(cur) {
if( cur->category == CAT_ENEMY ){
Enemy *e = (Enemy*)cur;
if( e->beam_hits ) {
float l = from.len(e->loc);
if( l < minl ) {
minl = l;
out = e;
}
}
}
cur = (Char*)cur->next;
}
return out;
}
/////////////////////////////
Worm::Worm( Vec2 lc, int client_id, int internal_id ) : Enemy( lc, g_base_deck, NOT_FLYING, NOT_SWIMMING, 0, client_id, internal_id ), shooter(false), shoot_at(2), last_hit_at(0), rest_until(0), turn_at(0) {
machine = false;
hp = maxhp = 2;
egg_on_kill_num = irange(1,3);
setIndex( B_ATLAS_WORM_NORMAL_BASE );
setScl( range(PPC,PPC*1.5) );
explosion_type = EXT_CREATURE;
enable_hyper_effect = true;
bt_can_enter = BT_CELL;
enemy_type = ET_WORM;
net_auto_sync_move_interval_sec = 1;
base_score = 50;
if(isLocal()) realtimeNewEnemySend( this );
}
void Worm::onBeam( Beam *b, int dmg ) {
// knockback
v = b->v;
turn_at = accum_time + KNOCKBACK_DELAY;
realtimeEnemyKnockbackSend(this);
}
bool Worm::enemyPoll( double dt ) {
// Get stronger when walk on ENHANCER
Cell *c = g_fld->get(loc);
if(!c)return false;
Cell *uc = g_fld->get(loc+Vec2(0,PPC));
Cell *dc = g_fld->get(loc+Vec2(0,-PPC));
Cell *lc = g_fld->get(loc+Vec2(PPC,0));
Cell *rc = g_fld->get(loc+Vec2(-PPC,0));
if( shooter == false &&
(c->gt == GT_ENHANCER || (uc&&uc->gt == GT_ENHANCER) || (dc&&dc->gt == GT_ENHANCER) || (rc&&rc->gt == GT_ENHANCER) || (lc&&lc->gt == GT_ENHANCER)) ) {
hp = maxhp = 10;
setIndex( B_ATLAS_WORM_SHOOTER_BASE );
egg_on_kill_num = irange(4,10);
base_score = 100;
shooter = true;
}
if( accum_time < rest_until ) {
v *= 0;
} else {
if( c->gt == GT_GRASS ) {
if( accum_time > rest_until + 1 ) {
rest_until = accum_time + 0.5;
}
}
if( accum_time > turn_at ) {
Vec2 tgt;
bool hastgt = false;
if( g_fld->findEnemyAttackTarget(loc, &tgt, WORM_SHOOT_DISTANCE ) ) {
v = loc.to(tgt).normalize(PPC*3);
hastgt = true;
} else {
v = Vec2(0,0).randomize(1).normalize(PPC*2);
}
turn_at = accum_time + ( hastgt ? 1 : 3 );
}
}
// Shoot
if( accum_time > shoot_at ) {
if( shooter && range(0,100) < 20 ) {
if(isLocal()) new Bullet( BLT_POISONBALL, loc, g_pc->loc );
v *= 0;
shoot_at = accum_time + range(1,3);
}
}
updateDefaultRightFacedRot(v);
// Direct damage on touching PC
if( hit(g_pc,PPC/4) && last_hit_at < accum_time - 0.5 ) {
last_hit_at = accum_time;
turn_at = accum_time + KNOCKBACK_DELAY;
int dmg = WORM_DAMAGE;
if( shooter ) dmg *= 2;
g_pc->onAttacked(dmg, this);
v = loc.to(g_pc->loc).normalize( -PPC * 10 );
soundPlayAt( g_wormbite_sound, loc,1 );
realtimeEnemyKnockbackSend(this);
}
return true;
}
//////////////////////////
Egg::Egg(Vec2 lc, int client_id, int internal_id ) : Enemy( lc, g_base_deck, NOT_FLYING, NOT_SWIMMING, 0, client_id, internal_id ), hatch_at(7) {
setIndex( B_ATLAS_WORM_EGG );
hp = maxhp = 1;
setScl(PPC);
explosion_type = EXT_CREATURE;
v = Vec2(0,0);
timeout = 999; // Must hatch before timeout
setRot( irange(0,M_PI*2));
enemy_type = ET_EGG;
net_auto_sync_move_interval_sec = 2;
base_score = 100;
if(isLocal()) realtimeNewEnemySend(this);
}
bool Egg::enemyPoll(double dt) {
v *= 0.95;
if( accum_time > hatch_at ) {
if(isLocal()) new Worm( loc );
return false;
}
return true;
}
/////////////////////////
Fly::Fly( Vec2 lc, int client_id, int internal_id ) : Enemy( lc, g_base_deck, FLYING, NOT_SWIMMING, DEFAULT_FLY_HEIGHT, client_id, internal_id ), turn_at(1) {
hp = maxhp = 1;
setScl(PPC*1.5);
explosion_type = EXT_CREATURE;
v = Vec2(0,0);
timeout = ENEMY_DEFAULT_TIMEOUT / 2;
enable_hyper_effect = true;
ensureShadow();
v = Vec2(0,0).randomize(PPC*2);
enemy_type = ET_FLY;
net_auto_sync_move_interval_sec = 1;
base_score = 20;
if(isLocal()) realtimeNewEnemySend( this);
}
bool Fly::enemyPoll(double dt) {
setIndex( B_ATLAS_FLY_BASE + ((int)(accum_time * 10) % 2) );
if(accum_time > turn_at ) {
turn_at = accum_time + range(1,2);
Vec2 tgt;
if( g_fld->findEnemyAttackTarget(loc,&tgt,WORM_SHOOT_DISTANCE)) {
target = tgt;
} else {
target = loc.randomize(PPC*10);
}
checkLocalSyncMoveSend();
}
//
float vel = (hyper_until > accum_time) ? PPC*20 : PPC * 5;
Vec2 vv = loc.to(target).normalize(vel);
v += vv * dt;
v -= v * dt * 0.5;
if( loc.len(g_pc->loc) < PPC/2 ) {
to_clean = true;
g_pc->onPushed(FLY_DAMAGE,this);
}
return true;
}
/////////////////////
Bullet::Bullet( BULLETTYPE blt, Vec2 at, Vec2 to, int client_id, int internal_id ) : Enemy( at, g_base_deck, FLYING, NOT_SWIMMING, 0, client_id, internal_id ), hit_pc_damage(1), hit_wall_damage(1), bullet_type(blt), round_vel(0), round_dia(0), hit_beam_size(0), friction(0), rot_speed(0) {
clean_at = 20;
setIndex(0);
float vel = PPC;
timeout = 10;
net_auto_sync_move_interval_sec = 0; // no regular interval sync, because going straight with constant velocity.
bool to_rot = false;
bool to_quantize = true;
float hyper1_5x=1, hyper2x = 1, hyper3x = 1, hyper4x = 1;
Cell *c = g_fld->get(at);
if( c && c->hyper_count > 0 ) {
hyper1_5x = 1.5;
hyper2x = 2;
hyper3x = 3;
hyper4x = 4;
hyper_until = timeout;
}
switch(blt) {
case BLT_POISONBALL:
vel = PPC * range(3,6) * hyper2x;
timeout /= hyper2x;
setIndex( B_ATLAS_POISONBALL_BASE );
setScl( range( PPC, PPC*2));
hit_pc_damage = POISONBALL_DAMAGE;
hit_wall_damage = 0;
clean_at = 3;
beam_hits = false;
break;
case BLT_SPARIO:
vel = PPC * 4 * hyper2x;
setIndex( (hyper2x>1) ? B_ATLAS_HYPER_SPARIO : B_ATLAS_SPARIO );
setScl( PPC*1.5, PPC*1.5 );
hit_pc_damage = SPARIO_DAMAGE * hyper2x;
hit_wall_damage = 0;
beam_hits = false;
explosion_type = EXT_MACHINE;
timeout /= hyper2x;
break;
case BLT_SIG:
category = CAT_ENEMY; // This bullet can be destroyed by beam
to_rot = true;
hp = 1;
vel = PPC * 8 * hyper2x;
timeout /= hyper2x;
setIndex( B_ATLAS_SIG );
setScl( PPC*1.5, PPC*1.5 );
hit_pc_damage = SPARIO_DAMAGE * hyper2x;
hit_wall_damage = ( hyper2x > 1 ) ? 4 : 1;
hit_beam_size = PPC/3;
soundPlayAt( g_shoot_sig_sound, at,1 );
explosion_type = EXT_MACHINE;
enable_hyper_effect = true;
base_score = 30;
break;
case BLT_BELO:
category = CAT_ENEMY; // This bullet can be destroyed by beam
hp = 5 * hyper4x;
vel = PPC * hyper2x;
timeout /= hyper2x;
setIndex( B_ATLAS_BELO_BASE );
setScl( PPC*2, PPC*2 );
hit_pc_damage = 20 * hyper2x;
hit_wall_damage = ( hyper2x > 1 ) ? 8 : 2;
round_vel = 5 * hyper4x;
round_dia = PPC*5 * hyper4x;
hit_beam_size = PPC/2;
explosion_type = EXT_MACHINE;
base_score = 100;
break;
case BLT_FIREBALL: // undestroyable
case BLT_FIRE:
case BLT_ROTFIRE:
if( blt == BLT_FIREBALL) {
vel = PPC * range(3,25);
timeout = range(7,10);
setIndex( B_ATLAS_STRAIGHT_FIREBALL );
setScl( PPC* range(1,2));
to_rot = true;
friction = 0.98;
} else if( blt == BLT_FIRE ){
vel = PPC;
timeout = range(3,5);
setIndex( B_ATLAS_FIRE );
to_rot = false;
setScl( PPC );
friction = 0.98;
} else if( blt == BLT_ROTFIRE ) {
vel = PPC*4 * hyper2x;
setIndex( B_ATLAS_ROUND_FIRE );
to_rot = false;
rot_speed = 20;
setScl( PPC );
}
hit_wall_damage = 0;
hit_pc_damage = FIREBALL_DAMAGE;
beam_hits = false;
to_quantize = false;
break;
case BLT_SNOWBALL:
vel = PPC * 6 * hyper2x;
timeout = 10 / hyper2x;
setIndex( B_ATLAS_SNOWBALL );
setScl(PPC);
to_rot = true;
hit_pc_damage = 10;
hp = 1;
explosion_type = EXT_CREATURE;
beam_hits = true;
base_score = 20;
break;
case BLT_TORNADO:
vel = PPC * 4 * hyper2x;
timeout = 15/hyper2x;
setIndex(0); // Animated in polling func
setScl(PPC);
enable_hyper_effect = true;
hit_pc_damage = 20 * hyper2x;
beam_hits = false;
hit_wall_damage = 2 * hyper2x;
bt_can_enter = BT_SNOW; // Goes over the snow
break;
case BLT_SHRIMP_DEBRIS:
setIndex( B_ATLAS_SHRIMP_DEBRIS );
vel = PPC * 10;
timeout = 1;
setScl(PPC);
hit_pc_damage = 20;
beam_hits = true;
hp = 2;
hit_wall_damage = 1;
rot_speed = 40;
explosion_type = EXT_CREATURE;
base_score = 10;
break;
case BLT_BOUNCER:
setIndex( B_ATLAS_BOUNCER );
vel = PPC * 6 * hyper1_5x;
timeout = 15/hyper2x;
setScl(PPC);
hit_pc_damage = 20;
beam_hits = true;
hp = 3 * hyper3x;
hit_wall_damage = 0;
explosion_type = EXT_MACHINE;
body_size = 4;
base_score = 50;
break;
case BLT_CRYSTAL_BEAM:
setIndex( B_ATLAS_STRAIGHT_BEAM );
vel = PPC * 10 * hyper1_5x;
timeout = 8/hyper1_5x;
setScl(PPC);
hit_pc_damage = 40;
beam_hits = false;
hp = 0;
hit_wall_damage = 10;
body_size = 2;
to_rot = true;
break;
default:
assertmsg( false, "invalid bullettype:%d", blt);
}
v = at.to(to).normalize(vel);
if( to_quantize ) v = quantizeAngle(v, 32 );
if( to_rot ) updateDefaultRightFacedRot(v);
if( hit_wall_damage == 0 && c && c->isPlayerBuilding() == false && c->isEnterable(true,true,false,BT_AIR) == false ) {
to_clean = true;
// TODO: Now erasing bullet just after appearing. But ideally this shold't appear. This causes unnecessary network traffic.
} else {
if( isLocal() ) realtimeNewBulletSend(this);
}
}
Bullet::~Bullet() {
if(isLocal()) realtimeCharDeleteSend(this);
}
bool Bullet::enemyPoll( double dt ) {
if( friction > 0 ) {
v *= friction;
}
if( round_vel > 0 ) {
loc += Vec2( cos( accum_time*round_vel), sin(accum_time*round_vel) ) * round_dia * dt;
}
if( rot_speed > 0 ) {
setRot( accum_time * rot_speed );
}
if( hit_pc_damage > 0 ) {
if( loc.len(g_pc->loc) < PPC/3 ) {
g_pc->onAttacked(hit_pc_damage, this);
createDigSmoke(loc,1);
return false;
}
}
if( bullet_type == BLT_BELO ) {
setIndex( B_ATLAS_BELO_BASE + (int)( accum_time * 3 ) % 2 );
}
if( (bullet_type == BLT_FIREBALL || (bullet_type == BLT_FIRE ) ) && v.len() < PPC ) {
setIndex( B_ATLAS_FIRE );
setXFlip( ((int)(accum_time*32) % 2) );
setRot(0);
}
if( bullet_type == BLT_FIRE || bullet_type == BLT_FIREBALL || bullet_type == BLT_ROTFIRE ) {
g_fld->meltSnow(loc);
}
if( bullet_type == BLT_TORNADO ) {
float hyrate = hyper_until > accum_time ? 3 : 1;
setIndex( B_ATLAS_TORNADO_BASE + ( (int)(accum_time*15) % 4) );
float maxvel = PPC*4*hyrate;
Vec2 dv = loc.to(g_pc->loc).normalize(maxvel);
v += dv * dt;
if( v.len() > maxvel) v = v.normalize(maxvel);
}
if( bullet_type == BLT_POISONBALL ) {
float s = 1 + sin( accum_time * 20 ) * 0.4;
setScl(s*PPC*2);
}
return true;
}
void Bullet::shootAt( BULLETTYPE blt, Vec2 from, Vec2 at ) {
new Bullet(blt,from,at);
}
void Bullet::shootFanAt( BULLETTYPE blt, Vec2 from, Vec2 at, int side_n, float pitch ) {
Bullet *center = new Bullet(blt, from,at);
float rad = atan2( center->v.y, center->v.x );
for(int i=0;i<side_n;i++) {
Vec2 sidev( cos(rad + pitch*(i+1)), sin(rad + pitch*(i+1)) );
Bullet *b = new Bullet(blt,from, from + sidev.normalize( center->v.len() ) );
sidev = Vec2( cos(rad - pitch*(i+1)), sin(rad - pitch*(i+1)) );
b = new Bullet(blt, from, from + sidev.normalize( center->v.len() ) );
}
}
Beam *Bullet::checkHitBeam() {
Char *cur = (Char*) g_char_layer->prop_top;
while(cur) {
if( cur->category == CAT_BEAM ) {
if( cur->hit(this,hit_beam_size) ) {
return (Beam*)cur;
}
}
cur = (Char*) cur->next;
}
return NULL;
}
void Bullet::onTouchWall( Vec2 nextloc, int hitbits, bool nxok, bool nyok ) {
Vec2 rt_at, rb_at, lt_at, lb_at;
Cell *rtc = (hitbits & Field::WALKABLE_BIT_HIT_RT) ? g_fld->get( rt_at = nextloc+Vec2(body_size,body_size)) : NULL;
Cell *ltc = (hitbits & Field::WALKABLE_BIT_HIT_LT) ? g_fld->get( lt_at = nextloc+Vec2(-body_size,body_size)) : NULL;
Cell *rbc = (hitbits & Field::WALKABLE_BIT_HIT_RB) ? g_fld->get( rb_at = nextloc+Vec2(body_size,-body_size)) : NULL;
Cell *lbc = (hitbits & Field::WALKABLE_BIT_HIT_LB) ? g_fld->get( lb_at = nextloc+Vec2(-body_size,-body_size)) : NULL;
Cell *tgtc = NULL;
// Find the nearest cell
float minl = 999999,l;
Vec2 at;
if( (l=loc.len(rt_at)) < minl ) { tgtc = rtc; minl = l; at = rt_at; }
if( (l=loc.len(lt_at)) < minl ) { tgtc = ltc; minl = l; at = lt_at; }
if( (l=loc.len(rb_at)) < minl ) { tgtc = rbc; minl = l; at = rb_at; }
if( (l=loc.len(lb_at)) < minl ) { tgtc = lbc; at = lb_at; }
if( tgtc == NULL ) return;
if( bullet_type == BLT_SNOWBALL ) {
g_fld->tryPutBlockNearestOnAir(loc, BT_SNOW );
to_clean = true;
} else if( bullet_type == BLT_BOUNCER && tgtc->isPlayerBuilding() == false ) { // bounding bullet
bool xrev = (!nxok), yrev = (!nyok);
if( hitbits & Field::WALKABLE_BIT_HIT_RT ) {
if( hitbits & Field::WALKABLE_BIT_HIT_LT ) { // Up
yrev = true;
} else if( hitbits & Field::WALKABLE_BIT_HIT_RB ){ // Right
xrev = true;
} else { // Right Up
}
} else if( hitbits & Field::WALKABLE_BIT_HIT_LT ) {
if( hitbits & Field::WALKABLE_BIT_HIT_LB ) { // Left
xrev = true;
} else { // Left Up
}
} else if( hitbits & Field::WALKABLE_BIT_HIT_LB ) {
if( hitbits & Field::WALKABLE_BIT_HIT_RB ) { // Down
yrev = true;
} else { // Left Down
}
} else if( hitbits & Field::WALKABLE_BIT_HIT_RB ) { // Right Down
}
if(xrev) v.x *= -1;
if(yrev) v.y *= -1;
if(nxok==false && nyok == false) to_clean = true;
} else {
int damage = hit_wall_damage;
if( tgtc->isPlayerBuilding() ) {
damage += 1;
}
if( tgtc->bt == BT_SNOW && (bullet_type== BLT_FIREBALL || bullet_type == BLT_ROTFIRE || bullet_type == BLT_FIRE ) ) {
damage += 1;
}
if( damage > 0 ) {
g_fld->damage( at, damage, NULL, this );
}
createExplosion( loc, 1, 1 );
if( hit_wall_damage > 0 ) soundPlayAt( g_hitspario_sound, loc,1);
to_clean = true;
}
}
//////////////
Takwashi::Takwashi( Vec2 lc, int client_id, int internal_id ) : Enemy( lc, g_base_deck, FLYING, NOT_SWIMMING, DEFAULT_FLY_HEIGHT, client_id, internal_id ), shoot_start(0), shoot_end(0), last_shoot_at(0), to_shoot(false) {
setIndex( B_ATLAS_TAKWASHI );
v = Vec2(0,0);
hp = maxhp = 25;
explosion_type = EXT_MACHINE;
setFragmentShader(g_eye_col_replacer);
enable_hyper_effect = true;
ensureShadow();
enemy_type = ET_TAKWASHI;
net_auto_sync_move_interval_sec = 1;
base_score = 800;
if(isLocal()) realtimeNewEnemySend(this);
}
bool Takwashi::enemyPoll(double dt) {
if( shoot_start == 0 ) {
shoot_start = accum_time + 2;
shoot_end = accum_time + 4;
} else {
if( accum_time > shoot_end ) {
shoot_start = shoot_end = 0;
}
}
if( accum_time > shoot_start && accum_time < shoot_end ) {
if( v.len() > 0 ) {
Vec2 tgt;
if( (to_shoot = g_fld->findEnemyAttackTarget(loc, &tgt, TAKWASHI_SHOOT_DISTANCE)) ) {
shoot_tgt = tgt;
updateDefaultRightFacedRot(loc.to(tgt));
}
}
float intvl = 0.1;
if( hyper_until > accum_time ) intvl /= 2;
if( accum_time > last_shoot_at + intvl) {
last_shoot_at = accum_time;
if( to_shoot && isLocal() ) Bullet::shootAt( BLT_SIG, loc + Vec2(0,fly_height), shoot_tgt );
}
v = Vec2(0,0);
} else {
if( v.len() == 0 ) {
Vec2 tgt;
if( g_fld->findEnemyAttackTarget( loc, &tgt, TAKWASHI_SHOOT_DISTANCE ) ) {
float vel = PPC;
if( hyper_until > accum_time ) vel *= 3;
v = loc.to(tgt).normalize(vel);
updateDefaultRightFacedRot(v);
}
}
}
return true;
}
///////////////////////
Girev::Girev( Vec2 lc, int client_id, int internal_id ) :Enemy( lc, g_girev_deck, FLYING, NOT_SWIMMING, DEFAULT_FLY_HEIGHT, client_id, internal_id ), shoot_at(0), turn_at(0), repairer_at(3) {
setIndex(0);
v = Vec2(0,0);
hp = maxhp = 500;
setScl(64,64);
reward_itt = ITT_ARTIFACT;
explosion_type = EXT_BOSS;
enable_hyper_effect = true;
timeout = ENEMY_DEFAULT_TIMEOUT * 100;
ensureShadow();
enemy_type = ET_GIREV;
net_auto_sync_move_interval_sec = 1;
base_score = 4000;
if(isLocal()) realtimeNewEnemySend(this);
}
bool Girev::enemyPoll(double dt) {
if( isLocal() ) {
if( accum_time > turn_at) {
float intvl = 2;
if( hyper_until > accum_time ) intvl = 0.8;
turn_at = accum_time + intvl;
Vec2 tgt;
if( g_fld->findEnemyAttackTarget( loc, &tgt, MACHINE_SHOOT_DISTANCE) ) {
float vel = PPC;
if( hyper_until > accum_time ) vel *= 2;
v = loc.to(tgt).normalize(vel);
if(isLocal()) Bullet::shootFanAt( BLT_SPARIO, loc + Vec2(0,fly_height), tgt, 8, M_PI/8.0 );
}
checkLocalSyncMoveSend();
}
if( accum_time > repairer_at ) {
float intvl = 2;
if( hyper_until > accum_time ) intvl /= 4;
repairer_at = accum_time + intvl;
new Repairer(loc);
}
}
return true;
}
///////////////////////
Repairer::Repairer( Vec2 lc, int client_id, int internal_id ) : Enemy(lc, g_base_deck, FLYING, NOT_SWIMMING, DEFAULT_FLY_HEIGHT, client_id, internal_id ), turn_at(0), spark_at(0) {
setIndex( B_ATLAS_ZOSHI_BASE );
v = Vec2(0,0);
hp = maxhp = 20;
setScl(24*1.5);
timeout = ENEMY_DEFAULT_TIMEOUT * 2;
explosion_type = EXT_MACHINE;
enable_hyper_effect = true;
ensureShadow();
enemy_type = ET_REPAIRER;
net_auto_sync_move_interval_sec = 1;
base_score = 1000;
if(isLocal()) realtimeNewEnemySend(this);
}
bool Repairer::enemyPoll(double dt) {
setIndex( B_ATLAS_ZOSHI_BASE + (((int)(accum_time*30) % 4)));
if( accum_time > turn_at ) {
float intvl = 3;
float vel = PPC * 2;
if( hyper_until > accum_time ) {
intvl /= 2;
vel *= 2;
}
turn_at = accum_time + intvl;
if( isLocal() ) {
v = Vec2(0,0).randomize(1).normalize(vel);
checkLocalSyncMoveSend();
}
}
// Where to repair?
int sight = 15;
Pos2 from( loc.x/PPC, loc.y/PPC);
DIR dir_to_go = DIR_NONE;
Cell *to_fix = NULL;
for(int i=1;i<sight;i++) {
Cell *c = g_fld->get( from + Pos2(i,0) );
if(c && c->isToRepair() ) { dir_to_go = DIR_RIGHT; to_fix = c; break; }
c = g_fld->get( from + Pos2(-i,0) );
if(c && c->isToRepair() ) { dir_to_go = DIR_LEFT; to_fix = c; break; }
c = g_fld->get( from + Pos2(0,i) );
if(c && c->isToRepair() ) { dir_to_go = DIR_UP; to_fix = c; break; }
c = g_fld->get( from + Pos2(0,-i) );
if(c && c->isToRepair() ) { dir_to_go = DIR_DOWN; to_fix = c; break; }
}
if( dir_to_go != DIR_NONE ) {
int dx,dy;
dirToDXDY(dir_to_go,&dx,&dy);
v = Vec2(dx,dy) * PPC*2;
if( accum_time > spark_at ) {
spark_at = accum_time + 0.5;
int x,y;
g_fld->getCellXY(to_fix, &x,&y);
if( from.len(Pos2(x,y)) < 2 ) {
Vec2 spark_at = toCellCenter(x,y);
createSpark(spark_at, range(0.7,1.5));
to_fix->content ++;
soundPlayAt(g_eye_repair_sound,loc,1);
if( to_fix->content > 10 ) {
if( to_fix->bt == BT_ENEMY_EYE_DESTROYED ) {
BLOCKTYPE cands[] = { BT_MONO_EYE, BT_COMBI_EYE, BT_PANEL_EYE, BT_SHIELD_EYE, BT_ROUND_EYE };
to_fix->bt = choose( cands );
to_fix->content = 0;
g_fld->notifyChanged(to_fix);
assert( to_fix->fortress_id > 0 );
// Fortress *ft = g_fld->searchFortress( to_fix->fortress_id );
// assert(ft);
// ft->setEyeRepaired( Pos2(x,y), to_fix->bt );
} else if( to_fix->st == ST_GROUNDWORK && to_fix->bt == BT_AIR ) {
Pos2 at(x,y);
if( g_fld->checkBarrierGen(at, DIR_UP, BARRIER_INTERVAL) >0 ||
g_fld->checkBarrierGen(at, DIR_DOWN, BARRIER_INTERVAL) >0 ||
g_fld->checkBarrierGen(at, DIR_RIGHT, BARRIER_INTERVAL) >0 ||
g_fld->checkBarrierGen(at, DIR_LEFT, BARRIER_INTERVAL) >0 ) {
to_fix->st = ST_NONE;
to_fix->bt = BT_BARRIERGEN;
g_fld->notifyChanged(to_fix);
} else {
to_fix->st = ST_NONE;
g_fld->buildRandomGrounder(at);
}
}
soundPlayAt( g_repair_done_sound,loc,1);
}
}
}
}
return true;
}
////////////////////
Chaser::Chaser( Vec2 lc, int client_id, int internal_id ) :Enemy(lc, g_base_deck, FLYING, NOT_SWIMMING, 0.1, client_id, internal_id ), reset_target_at(0), shoot_at(2), accel_at(1) {
hp = maxhp = 1;
setScl(24*1.5);
explosion_type = EXT_MACHINE;
v = Vec2(0,0).randomize(1).normalize( range(3,5)*PPC );
thres = range(8,16) * PPC;
enable_hyper_effect = true;
ensureShadow();
enemy_type = ET_CHASER;
net_auto_sync_move_interval_sec = 0.2; // Quicker, because of frequent turning
base_score = 400;
if(isLocal()) realtimeNewEnemySend(this);
}
bool Chaser::enemyPoll(double dt) {
setIndex( B_ATLAS_TOROID_BASE + (( (int)(accum_time*12) % 7 )));
if( fly_height < DEFAULT_FLY_HEIGHT ) {
fly_height += PPC * dt;
}
if( isLocal() ) {
if( accum_time > reset_target_at ) {
reset_target_at = accum_time + 2;
Vec2 tgt;
if( g_fld->findEnemyAttackTarget( loc, &tgt, MACHINE_SHOOT_DISTANCE ) ) {
target = tgt;
}
}
// Rest at the beginning and then start to keep modest distance to target.
if( accum_time > accel_at ) {
float l = loc.len(target);
float vel = PPC*8;
Vec2 dv = loc.to(target).normalize(vel);
if( l < thres ) {
v += -dv * dt;
} else {
v += dv * dt;
}
if( v.len() > vel) v = v.normalize(vel);
}
//
if( accum_time > shoot_at ) {
float intvl = range(1,3);
if( hyper_until > accum_time ) intvl /= 8;
shoot_at = accum_time + intvl;
if(isLocal()) Bullet::shootAt( BLT_SPARIO, loc + Vec2(0,fly_height), target );
}
}
return true;
}
//////////////////////////
Builder::Builder( Vec2 lc, int client_id, int internal_id ) : Enemy( lc, g_base_deck, NOT_FLYING, NOT_SWIMMING, 0, client_id, internal_id ), turn_at(0), building(false), build_dir(DIR_NONE), build_cnt(0), build_after_at(8) {
category = CAT_BUILDER;
hit_category = CAT_BUILDER;
body_size = 10;
hp = maxhp = 10;
setScl(24*1.5); // because pixel art is 16x16
explosion_type = EXT_MACHINE;
v = Vec2(0,0);
setIndex( B_ATLAS_BUILDER_BASE );
timeout = ENEMY_DEFAULT_TIMEOUT * 3;
push_pc = true;
enable_hyper_effect = true;
enemy_type = ET_BUILDER;
net_auto_sync_move_interval_sec = 1;
base_score = 1500;
if( isLocal() ) realtimeNewEnemySend(this);
}
bool Builder::enemyPoll(double dt) {
if( isLocal() ) {