-
Notifications
You must be signed in to change notification settings - Fork 1
/
ships.lua
1461 lines (1184 loc) · 29.6 KB
/
ships.lua
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
-- BLAST FLOCK source files
-- by TRASEVOL_DOG (https://trasevol.dog/)
require("drawing")
require("maths")
require("table")
require("object")
require("sprite")
require("fx")
ship_list = {}
ship_types = {"smol", "medium", "biggie", "huge", "helix"}
ship_base_n = {6, 4, 2, 1}
-- ^^ number of ships per type needed for upgrade to next type
function init_ship_stats()
ship_infos={
smol={
anim = "ship",
hlen = 6,
btyp = "shell",
w = 12,
value = 10,
spawnval = 0,
lives = 3
},
medium={
anim = "mediumship",
hlen = 8,
btyp = "shell",
w = 16,
value = 25,
spawnval = 0,
lives = 4
},
biggie={
anim = "bigship",
hlen = 11,
btyp = "shell",
w = 20,
value = 50,
spawnval = 0,
lives = 5
},
huge={
anim = "hugeship",
hlen = 13,
btyp = "shell",
w = 24,
value = 100,
spawnval = 0,
lives = 6
},
helix={
anim = "helixship",
hlen = 24,
btyp = "shell",
w = 28,
value = 2000,
spawnval = 200
}
}
ship_stats={
--ship={
-- stat = {base, rnd, friend_bonus}
--}
smol={
acc = {0.2, 0.2, 0.2},
dec = {0.95, 0, -0.05},
spdcap = {2, 0.5, 0.5},
acca = {0.0005, 0.001, 0.001},
deca = {0.9, 0, 0},
vacap = {0.002, 0.01, 0.008},
grv = {0.05, 0.02, 0.02},
cldwn = {1.5, 0, -1.3},
attack = {0, 0.2, 0},
kick = {1, 0, 0},
bltspd = {3, 1, 7},
revive = {75, 0, -25},
maxhp = {3.5, 0, 0.5}
},
medium={ -- NOT DEFINED - ONLY COPIED FROM BIGGIE
acc = {0.2, 0.2, 0.2},
dec = {0.95, 0, -0.05},
spdcap = {2, 0.5, 0.5},
acca = {0.0005, 0.001, 0.001},
deca = {0.92, 0, 0},
vacap = {0.002, 0.005, 0.007},
grv = {0.05, 0.02, 0.02},
cldwn = {0.65, 0, -0.5},
attack = {0, 0.15, 0},
kick = {1, 0, 0},
bltspd = {3, 1, 7},
revive = {80, 0, -15},
maxhp = {6, 0, 2}
},
biggie={
acc = {0.2, 0.2, 0.2},
dec = {0.95, 0, -0.05},
spdcap = {2, 0.5, 0.5},
acca = {0.0005, 0.001, 0.001},
deca = {0.94, 0, 0},
vacap = {0.002, 0.005, 0.006},
grv = {0.05, 0.02, 0.02},
cldwn = {0.6, 0, -0.5},
attack = {0, 0.1, 0},
kick = {1, 0, 0},
bltspd = {3, 1, 7},
revive = {75, 0, 0},
maxhp = {8, 0, 2}
},
huge={ -- NOT DEFINED - ONLY COPIED FROM BIGGIE
acc = {0.15, 0.2, 0.2},
dec = {0.95, 0, -0.05},
spdcap = {2, 0.5, 0.5},
acca = {0.0005, 0.001, 0.001},
deca = {0.96, 0, 0},
vacap = {0.002, 0.004, 0.005},
grv = {0.05, 0.02, 0.02},
cldwn = {0.55, 0, -0.5},
attack = {0, 0.05, 0},
kick = {0.5, 0, 0},
bltspd = {3, 1, 7},
revive = {85, 0, 0},
maxhp = {11, 0, 2}
},
helix={
--handled in helix update
}
}
end
function update_ship(s)
s.t=s.t+0.01*dt30f
lsrand(s.id)
load_shipstats(s,ship_types[s.typ_id % 8], not s.gang)
local p = s.gang or players[s.player]
-- if not p then
-- print("WARNING: Playerless ships!!")
-- p = {x = s.x, y = s.y}
-- end
if p and p.boosting then
s.boost=4
else
s.boost=max(s.boost-0.5,0)
end
local adif=update_ship_movement(s)
update_ship_shooting(s,adif)
local xx,yy
if not (server and server_only) then
--s.dx = lerp(s.dx, 0, 0.075*dt30f)
--s.dy = lerp(s.dy, 0, 0.075*dt30f)
s.dx = s.dx - sgn(s.dx)*min(abs(s.dx), (1+abs(s.dx/16))*dt30f)
s.dy = s.dy - sgn(s.dy)*min(abs(s.dy), (1+abs(s.dy/16))*dt30f)
xx = s.x + s.dx
yy = s.y + s.dy
else
xx = s.x
yy = s.y
end
s.fxt = s.fxt - delta_time
if s.fxt <= 0 then
local l = s.info.hlen+4
local sxx = xx-l*s.co
local syy = yy-l*s.si
if s.dead then
if rnd(3)<1 then
create_smoke(sxx,syy,1,1+rnd(3))
elseif rnd(2)<1 then
create_smoke(sxx,syy,1,1+rnd(3),25)
end
elseif s.gang then
if (rnd(10)<1) then
create_smoke(sxx,syy,2,rnd(3),pick{21, s.color},s.aim+0.5)
end
if s.hp<s.stats.maxhp/3 and rnd(2)<1 then
create_smoke(sxx,syy,1,rnd(3),25,rnd(1))
end
else
if (rnd(64)>group_size("ship_player"..s.player)) then
create_smoke(sxx,syy,2,rnd(3),pick{21, s.color},s.aim+0.5)
end
if s.hp<s.stats.maxhp/3 and rnd(2)<1 then
create_smoke(sxx,syy,1,rnd(3),25,rnd(1))
end
end
s.fxt = 0.033
end
if not s.dead and not s.gang then
-- local col -- vvv code for scrapping vvv
-- for id,p in pairs(players) do
-- if id ~= s.player then
-- col = col or collide_objgroup(s, "ship_player"..id)
-- end
-- end
--
-- if col then
-- damage_ship(col,0.1,s)
-- damage_ship(s,0.1,col)
--
-- sfx("scrap")
-- create_smoke((s.x+col.x)/2,(s.y+col.y)/2,0.5,3,0)
-- end
if p and not p.shooting then
local typ = s.typ_id % 8
if typ < 4 and #p.typs[typ] > ship_base_n[typ] + #p.typs[typ+1]*2 then
local col = collide_objgroup(s,"ship_player"..s.player)
if col and (col.typ_id % 8) == (s.typ_id % 8) then
del(p.typs[typ], s)
del(p.typs[typ], col)
upgrade_ship(s, col)
end
end
local col=collide_objgroup(s,"ship_player-2")
if col and not (col.dead and col.t>0.5) then
befriend_ship(col, s.player)
end
end
end
end
function update_falling_ship(s)
s.t=s.t-0.01*dt30f
if s.t<-2 then
destroy_ship(s)
return
end
s.aim=s.aim+delta_time --s.va
s.dead = true
--s.stats.spdcap = 6
s.boost = 6
local adif=update_ship_movement(s)
local xx,yy
if client then
s.dx = s.dx - sgn(s.dx)*min(abs(s.dx), (1+abs(s.dx/16))*dt30f)
s.dy = s.dy - sgn(s.dy)*min(abs(s.dy), (1+abs(s.dy/16))*dt30f)
xx = s.x + s.dx
yy = s.y + s.dy
else
xx = s.x
yy = s.y
end
s.fxt = s.fxt - delta_time
if s.fxt <= 0 then
if rnd(3)<1 then
create_smoke(xx, yy, 1,1+rnd(3),23)
elseif rnd(2)<1 then
local l = s.info.hlen
create_smoke(xx, yy, 1,1+rnd(3),25)
end
s.fxt = 0.033
end
end
function update_ship_movement(s)
local stt=s.stats
local adif=0
if not s.dead then
local targ
if s.gang then
local p = players[s.gang.target]
targ = {x = p.mx, y = p.my}
else
targ = players[s.player]
end
if not targ then targ = {x = s.x, y = s.y} end
local tax = rel_wrap(targ.x, s.x)
local taim = atan2(tax-s.x,targ.y-s.y)
adif = angle_diff(s.aim,taim)
s.va = s.va+sgn(adif)*min(abs(adif),stt.acca)+rnd(0.008)-0.004
s.va = sgn(s.va)*min(abs(s.va),stt.vacap)
s.aim = s.aim + s.va*dt30f
s.va = lerp(s.va, 0, (1-stt.deca)*dt30f)
local acc = stt.acc+s.boost*0.05
s.co = cos(s.aim)
s.si = sin(s.aim)
s.vx = s.vx+acc*s.co*dt30f
s.vy = s.vy+acc*s.si*dt30f
end
s.vy = s.vy+stt.grv*dt30f
local spd=dist(s.vx,s.vy)
local nspd=min(spd,stt.spdcap+s.boost)
s.vx = s.vx/spd*nspd
s.vy = s.vy/spd*nspd
s.x = s.x+s.vx*dt30f
s.y = s.y+s.vy*dt30f
s.vx = lerp(s.vx, 0, (1-stt.dec)*dt30f)
s.vy = lerp(s.vy, 0, (1-stt.dec)*dt30f)
return adif
end
function update_ship_shooting(s,adif)
local stt=s.stats
local inf=s.info
s.curcld=max(s.curcld-0.01*dt30f,0)
if s.dead then return end
local p = players[s.player]
local shootdir
if s.gang then
local targ = players[s.gang.target]
if abs(adif)<0.2 and dist(s.x,s.y,targ.mx,targ.my)<400 then
if not s.shootin then
s.shootin=true
s.curcld=max(stt.attack,s.curcld)
end
adif = 0
else
s.shootin=false
end
elseif p then
if p.shooting and not s.shootin then
s.shootin=true
s.curcld=max(stt.attack,s.curcld)
end
s.shootin = p.shooting
end
if s.shootin and s.curcld<=0 then
local shootdir = s.aim + 0.25*adif
local d=inf.hlen+8
local x,y
if client then
x,y=s.x+s.dx+d*cos(shootdir),s.y+s.dy+d*sin(shootdir)
else
x,y=s.x+d*cos(shootdir),s.y+d*sin(shootdir)
end
create_bullet(x, y, shootdir+rnd(0.01)-0.005, stt.bltspd, s.color, s.player, s.gang ~= nil)
s.shots=s.shots+1
-- if s.shots%3==0 and s.typ=="biggie" and not p then
-- s.curcld=2*stt.cldwn
-- else
-- s.curcld=stt.cldwn
-- end
s.curcld=stt.cldwn
shootshake=shootshake+1
s.justfired=2
s.x=s.x-stt.kick*cos(shootdir)
s.y=s.y-stt.kick*sin(shootdir)
s.vx=s.vx-stt.kick*cos(shootdir)
s.vy=s.vy-stt.kick*sin(shootdir)
if s.player_id == my_id then
sfx("shoot",s.x,s.y)
else
sfx("enemyshoot",s.x,s.y)
end
end
end
function update_helixship(s)
s.t=s.t+0.01*dt30f
--sfx("helix",s.x,s.y)
--^sounds terrible
s.fxt = s.fxt - delta_time
if s.dead then
s.vy=s.vy+0.02*dt30f
local k=sgn(s.vx)*0.0002
s.tilt=s.tilt+k*dt30f
for i=1,3 do
s.aim[i]=s.aim[i]+k*dt30f
end
s.x=s.x+s.vx*dt30f
s.y=s.y+s.vy*dt30f
if t%0.02<0.01*dt30f then
create_explosion(s.x+rnd(64)-32,s.y+rnd(64)-32,16,21)
add_shake(4)
end
s.t=s.t-0.02*dt30f
if s.fxt <= 0 then
if s.t<=0.5 then
create_explosion(s.x+rnd(48)-24,s.y+rnd(48)-24,56,22)
boomsfx()
for i=1,32 do
create_smoke(s.x+rnd(64)-32,s.y+rnd(64)-32,2+rnd(6),nil,23)
create_smoke(s.x+rnd(64)-32,s.y+rnd(64)-32,2+rnd(6),nil,24)
end
deregister_object(s)
add_shake(64)
love.timer.sleep(0.1)
end
if rnd(2)<1 then
create_smoke(s.x,s.y,6,6)
else
create_smoke(s.x,s.y,6,6,0)
end
end
return
end
if s.fxt <= 0 then
if s.hp<200 and rnd(2)<1 then
create_smoke(s.x+rnd(96)-48,s.y,1,4+rnd(3),0)
end
s.fxt = 0.33
end
local dir=atan2(massx-s.x,massy-s.y)
if gameover then
s.vx=s.vx*0.02*dt30f
s.vy=s.vy*0.02*dt30f
else
s.vx=s.vx+s.acc*cos(dir)*dt30f
s.vy=s.vy+s.acc*sin(dir)*dt30f
end
local a=atan2(s.vx,s.vy)
local l=dist(s.vx,s.vy)
l=min(l,s.spdcap)
s.vx=l*cos(a)
s.vy=l*sin(a)
s.x=s.x+s.vx*dt30f
s.y=s.y+s.vy*dt30f
s.tilt=(s.vx/s.spdcap)*0.03
s.curcld=max(s.curcld-0.01*dt30f,0)
if s.shootin==0 then
local aimed=true
for i=1,3 do
local adif=angle_diff(s.aim[i],dir)
s.aim[i]=s.aim[i]+sgn(adif)*0.008*dt30f
aimed=aimed and (abs(adif)<0.05)
end
if aimed and s.curcld<=0 and dist(s.x,s.y,massx,massy)<600 then
s.shootin=1
end
else
s.shootin=max(s.shootin-0.01*dt30f,0)
if s.shootin%0.02<0.01*dt30f and not gameover then
local k=flr(s.shootin*50)%3-1
local a=s.aim[k+2]
local x=s.x+k*32*cos(s.tilt)+24*cos(a)
local y=s.y+k*32*sin(s.tilt)+24*sin(a)
create_bullet(x,y,a+rnd(0.02)-0.01,6,false)
sfx("enemyshoot",x,y)
add_shake(1)
end
if s.shootin==0 then
s.curcld=s.cldwn
end
end
end
function update_bullet(s,dt)
s.x=s.x+s.vx*dt30f
s.y=s.y+s.vy*dt30f
local col = nil
for id,p in pairs(players) do
if id ~= s.player then
col = col or collide_objgroup(s, "ship_player"..id)
end
end
if col and not (col.dead and col.t>0.5) then
damage_ship(col, s.is_ai and 0.5 or 2, s)
create_explosion(s.x,s.y,4,s.color)
deregister_object(s)
return
end
s.t=s.t-delta_time
if s.t<0 then
deregister_object(s)
end
end
function damage_ship(s,dmg,o)
s.hp=s.hp-dmg
s.gothit=2
if s.hp<=0 then
if s.dead then
destroy_ship(s)
else
s.lives=s.lives-1
if s.typ=="helix" then
add_shake(5)
create_explosion(s.x,s.y,24,s.c)
boomsfx(s.x,s.y)
s.dead=true
s.hp=1000
group_del("enemy_ship",s)
s.friend=false
s.t=3
elseif s.lives>0 and s.retrievable then
neutralize_ship(s,o)
boomsfx(s.x,s.y)
else
destroy_ship(s)
end
end
end
end
function neutralize_ship(s,o)
add_shake(3)
create_explosion(s.x,s.y,16,s.c)
s.dead=true
pass_to_player(s, -2)
local a
if o then
a=atan2(s.x-o.x,s.y-o.y)
else
a=rnd(1)
end
local spd=4+rnd(1)
s.vx=spd*cos(a)
s.vy=spd*sin(a)
s.t=1
end
function befriend_ship(s, player)
-- s.stats.spdcap=s.stats.spdcap/3
sfx("save")
pass_to_player(s, player)
-- local acur=rnd(1)--atan2(player.x-s.x,player.y-s.y)
-- s.aim=s.aim+0.2*angle_diff(s.aim,acur)
end
function pass_to_player(s, player)
group_del("ship_player"..s.player, s)
group_add("ship_player"..player,s)
local op = s.gang or players[s.player]
if server and server_only then
del(op.ships, s)
add(players[player].ships, s)
else
if players[player].ships[s.id] then
destroy_ship(s)
return
end
op.ships[s.id] = nil
players[player].ships[s.id] = s
end
s.player = player
if s.gang then s.gang = nil end
if s.player == -2 then
s.dead = true
--s.stats.spdcap=s.stats.spdcap*3
s.hp=3
s.w=s.w+8
s.h=s.h+8
s.shootin=false
s.va=sgn(irnd(2)-1.5)*(0.005+rnd(0.02))
else
s.dead = false
load_shipinfo(s,ship_types[s.typ_id], true)
s.hp=s.stats.maxhp
--create_convertring(s)
s.t = 0
end
s.typ_id = s.typ_id % 8
s.color = pick(players[player].colors)
s.plt = ship_plts[s.color]
ship_list[s.id] = s -- should already be the case, just tryna stay safe
end
function upgrade_ship(s, s2)
-- if (s.typ_id % 8) >= 4 then
-- return
-- end
s.typ_id = (s.typ_id % 8) + 9
s.typ = ship_types[s.typ_id % 8]
if s2 then
deregister_object(s2)
if server and server_only then
del(players[s2.player].ships, s2)
else
players[s2.player].ships[s2.id] = nil
end
ship_list[s2.id] = nil
end
load_shipinfo(s,ship_types[s.typ_id % 8], true)
s.t = 0
s.hp=s.stats.maxhp
sfx("save")
end
function update_gangs()
if server and server_only then
server_update_gangs()
else
client_update_gangs()
end
-- if server and server_only then
-- for id,p in pairs(players) do
-- gang_relevance[id] = {}
-- end
-- end
--
-- function search_new_target(gang)
-- local d = sqr(gang_safe_dist)
-- local target
-- for p_id, p in pairs(players) do
-- if p_id >= 0 then
-- local dx = ((gang.x-p.mx+areaw/2)%areaw)-areaw/2
-- local dy = gang.y-p.my
-- local nd = sqrdist(dx, dy)
-- if nd < d then
-- target = p_id
-- d = nd
-- end
-- end
-- end
--
-- if target then
-- if gang.target then
-- gang.target = target
-- else
-- activate_gang(gang, target)
-- end
-- return true
-- end
--
-- return false
-- end
--
-- local active_search_new_target = server and server_only and function(gang)
-- local mind = sqr(gang_safe_dist)
-- local d = mind
-- local target
-- for p_id, p in pairs(players) do
-- if p_id >= 0 then
-- local dx = ((gang.x-p.mx+areaw/2)%areaw)-areaw/2
-- local dy = gang.y-p.my
-- local nd = sqrdist(dx, dy)
-- if nd < mind then
-- gang_relevance[p_id][gang.id] = true
--
-- if nd < d then
-- target = p_id
-- d = nd
-- end
-- end
-- end
-- end
--
-- if target then
-- if gang.target then
-- gang.target = target
-- else
-- activate_gang(gang, target)
-- end
-- return true
-- end
--
-- return false
-- end or search_new_target
--
-- for id, gang in pairs(gang_grid) do
-- if gang.target then
-- local nships = 0
-- for _,_ in pairs(gang.ships) do
-- nships = nships + 1
-- end
--
-- if nships <= 0 then
-- if server and server_only then
-- delete_gang(gang)
-- end
-- else
-- calculate_gang_pos(gang)
--
-- if active_search_new_target(gang) then
-- local p = players[gang.target]
-- if p then
--
-- for _,sh in pairs(gang.ships) do
-- update_ship(sh)
-- end
--
-- end
-- else
-- delete_gang(gang)
-- end
-- end
-- else
-- if server and server_only then
-- search_new_target(gang)
-- else
-- delete_gang(gang)
-- end
-- end
-- end
end
function server_update_gangs()
update_gang_sys()
for id,p in pairs(players) do
gang_relevance[id] = {}
end
function find_target_and_relevance(gang)
local mind = sqr(gang_safe_dist)
local d = mind
local target
for p_id, p in pairs(players) do
if p_id >= 0 then
local dx = ((gang.x-p.mx+areaw/2)%areaw)-areaw/2
local dy = gang.y-p.my
local nd = sqrdist(dx, dy)
if nd < mind then
gang_relevance[p_id][gang.id] = true
if nd < d then
target = p_id
d = nd
end
end
end
end
if target then
if gang.target then
gang.target = target
else
activate_gang(gang, target)
end
return true
end
return false
end
for id,gang in pairs(gang_grid) do
if gang.target then
if calculate_gang_pos(gang)==0 then
delete_gang(gang)
else
if players[gang.target] then
for _,sh in pairs(gang.ships) do
update_ship(sh)
end
end
end
if not find_target_and_relevance(gang) then
delete_gang(gang)
end
else
find_target_and_relevance(gang)
end
end
end
function client_update_gangs()
for id,gang in pairs(gang_grid) do
calculate_gang_pos(gang)
if players[gang.target] then
for _,sh in pairs(gang.ships) do
update_ship(sh)
end
end
end
end
function update_gang_sys()
if not (server and server_only) then
return
end
gang_grid_t = gang_grid_t - delta_time
if gang_grid_t < 0 then
local x,y = rnd(areaw), rnd(areah)
local id = get_gang_id(x,y)
if id and not gang_grid[id] then
local d = sqr(gang_safe_dist)
for p_id, p in pairs(players) do
if p_id >= 0 then
local dy = y-p.my
local dx = ((x-p.mx+areaw/2)%areaw)-areaw/2
d = min(d, sqrdist(dx, dy))
end
end
if d>=sqr(gang_safe_dist) then
create_gang(x, y, id)
end
end
gang_grid_t = 1
end
end
function calculate_gang_pos(gang)
local x,y,k=0,0,0
for _,sh in pairs(gang.ships) do
x = x + sh.x
y = y + sh.y
k = k + 1
end
gang.size = k
if k==0 then
return 0
end
gang.x = x/k
gang.y = y/k
return k
end
function sync_gang(gang, ships, target, delay)
for s_id, sh in pairs(gang.ships) do
if not ships[s_id] then
destroy_ship(sh)
end
end
for s_id, d in pairs(ships) do
local s = gang.ships[s_id]
if s then
local ox,oy = s.x,s.y
s.x = d[1]+ delay*30*s.vx
s.y = d[2]+ delay*30*s.vy
s.dx = (((ox-s.x+areaw/2)%areaw)-areaw/2)
s.dy = oy-s.y
-- local dx = ((s.x-d[1]+areaw/2)%areaw)-areaw/2
-- local dy = s.y-d[2]
-- s.dx = s.dx+ dx
-- s.dy = s.dy+ dy
-- s.x = s.x - dx + delay*30*s.vx
-- s.y = s.y - dy + delay*30*s.vy
end
end
calculate_gang_pos(gang)
gang.target = target
end
function clear_gangs(x,y)
local id = get_gang_id(x,y)
local gang = gang_grid[id]
if gang and not gang.target then
delete_gang(gang)
end
end
function draw_ship(s)
local inf=s.info
local xx,yy
if client and debug_mode~=1 then
xx, yy = s.x+s.dx, s.y+s.dy
else
xx, yy = s.x, s.y
end
if xx+s.w*2<xmod or xx-s.w*2>xmod+screen_width or yy+s.h*2<ymod or yy-s.h*2>ymod+screen_height then
return
end
local ofx,ofy=0,0
if s.boost==4 then ofx,ofy=ofx+rnd(2)-1,ofy+rnd(2)-1 end
if debug_mode==2 then
all_colors_to(21)
draw_anim(s.x+ofx,s.y+ofy,inf.anim,"rotate",s.aim,s.aim,false,(s.aim+0.25)%1>0.5)
end
if s.player == my_id then
draw_anim_outline(xx+ofx,yy+ofy,inf.anim,"rotate",s.aim,ship_outline_col,s.aim,false,(s.aim+0.25)%1>0.5)
else
draw_anim_outline(xx+ofx,yy+ofy,inf.anim,"rotate",s.aim,25,s.aim,false,(s.aim+0.25)%1>0.5)
end
if s.gothit>0 or (s.dead and s.t>0.5) then
all_colors_to(21)
s.gothit=max(s.gothit-1,0)
else