forked from KirbyKid256/mari0_aces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clearpipe.lua
1223 lines (1171 loc) · 39 KB
/
clearpipe.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
clearpipe = class:new()
local addsegment
function clearpipe:init(x, y, r, t)
--path
self.x = x
self.y = y
self.t = t or "clearpipe"
self.r = {unpack(r)}
table.remove(self.r, 1)
table.remove(self.r, 1)
self.path = {{self.x, self.y}}
if self.t == "pneumatictube" then
self.dusttimer = 0
self.dusttable = {}
self.suck = true
self.suckspeed = 14 --owo
self.sucklen = 4
self.sucktable = {"player","box","core","turret"}
self.suckentranceportalx = false
self.suckentranceportaly = false
self.suckentranceportalfacing = false
self.suckexitportalx = false
self.suckexitportaly = false
self.suckexitportalfacing = false
end
if #self.r > 0 and self.r[1] ~= "link" then
--rightclick things
--if self.r[1]:find("|") then
--n1:2`0:2`1:2`1:1
local v = convertr(self.r[1], {"string","bool","bool"})
--path
local s = v[1]:gsub("n", "-")
local s2 = s:split("`")
local s3
for i = 1, #s2 do
s3 = s2[i]:split(":")
local x, y = tonumber(s3[1]), tonumber(s3[2])
if x and y then
table.insert(self.path, {self.x+x, self.y+y})
else
break
end
end
self.intersection = v[2]
if self.t == "pneumatictube" then
self.suck = v[3]
end
--end
table.remove(self.r, 1)
end
self.quadi = 1
self.graphic = clearpipeimg
if self.t == "pneumatictube" then
self.graphic = pneumatictubeimg
end
self.checktable = {"player", "goomba", "koopa", "mole", "enemy", "box", "smallspring", "blocktogglebutton", "powblock", "thwomp"} --can enter pipe
if not self.intersection then
--so here's this giant tumor
self.child = {}
local dir = "right"
local exitdir, enterdir = false, false
for i = 1, #self.path do
local tx, ty = self.path[i][1], self.path[i][2]
local quadi = 2
local ignore, turn = false, false
--get direction
if self.path[i+1] then
if self.path[i+1][2] < ty then
dir = "up"
elseif self.path[i+1][2] > ty then
dir = "down"
end
if self.path[i+1][1] < tx then
dir = "left"
elseif self.path[i+1][1] > tx then
dir = "right"
end
end
self.path[i].dir = dir
if (self.path[i-1] and self.path[i+1] and i ~= #self.path) and not
((self.path[i+1][1] ~= self.path[i-1][1] and self.path[i+1][2] == self.path[i-1][2])
or (self.path[i+1][2] ~= self.path[i-1][2] and self.path[i+1][1] == self.path[i-1][1])) then
if (self.path[i+1][1] == tx+1 and self.path[i+1][2] == ty and self.path[i-1][1] == tx and self.path[i-1][2] == ty+1)
or (self.path[i+1][1] == tx and self.path[i+1][2] == ty+1 and self.path[i-1][1] == tx+1 and self.path[i-1][2] == ty) then
turn = "c1"
elseif (self.path[i+1][1] == tx-1 and self.path[i+1][2] == ty and self.path[i-1][1] == tx and self.path[i-1][2] == ty+1)
or (self.path[i+1][1] == tx and self.path[i+1][2] == ty+1 and self.path[i-1][1] == tx-1 and self.path[i-1][2] == ty) then
turn = "c2"
elseif (self.path[i+1][1] == tx and self.path[i+1][2] == ty-1 and self.path[i-1][1] == tx+1 and self.path[i-1][2] == ty)
or (self.path[i+1][1] == tx+1 and self.path[i+1][2] == ty and self.path[i-1][1] == tx and self.path[i-1][2] == ty-1) then
turn = "c3"
else
turn = "c4"
end
elseif (self.path[i-1] and self.path[i-2] and i ~= #self.path) and
((dir == "right" and self.path[i][2] ~= self.path[i-1][2])
or (dir == "left" and self.path[i][2] == self.path[i-1][2] and self.path[i][2] ~= self.path[i-2][2])
or (dir == "down" and self.path[i][1] ~= self.path[i-1][1])
or (dir == "up" and self.path[i][1] == self.path[i-1][1] and self.path[i][1] ~= self.path[i-2][1])) then
--is the path 2 tiles ago in a different direction?
ignore = true
elseif (self.path[i+1] and self.path[i+2] and i ~= #self.path) and
((dir == "left" and self.path[i][2] ~= self.path[i+1][2])
or (dir == "right" and self.path[i][2] == self.path[i+1][2] and self.path[i][2] ~= self.path[i+2][2])
or (dir == "up" and self.path[i][1] ~= self.path[i+1][1])
or (dir == "down" and self.path[i][1] == self.path[i+1][1] and self.path[i][1] ~= self.path[i+2][1])) then
--is the path 2 tiles ahead in a different direction?
ignore = true
end
--create segment
if not ignore then
if not turn then
local verquad = {{1,4},{2,4}}
local horquad = {{2,1},{2,2}}
if self.t == "pneumatictube" then
if i/2 ~= math.floor(i/2) then
verquad = {{3,5},{4,5}}
horquad = {{4,1},{4,2}}
end
end
if dir == "right" then
if i == 1 then
addsegment(tx, ty-1, self, {1,1}, i, dir, dir, false, self.graphic)
addsegment(tx, ty, self, {1,2}, i, dir, dir, false, self.graphic)
enterdir = "right"
elseif i == #self.path then
addsegment(tx, ty-1, self, {3,1}, i, dir, false, dir, self.graphic)
addsegment(tx, ty, self, {3,2}, i, dir, false, dir, self.graphic)
exitdir = "right"
else
addsegment(tx, ty-1, self, horquad[1], i, dir, false, false, self.graphic)
addsegment(tx, ty, self, horquad[2], i, dir, false, false, self.graphic)
end
elseif dir == "up" then
if i == 1 then
addsegment(tx-1, ty, self, {1,5}, i, dir, dir, false, self.graphic)
addsegment(tx, ty, self, {2,5}, i, dir, dir, false, self.graphic)
enterdir = "up"
elseif i == #self.path then
addsegment(tx-1, ty, self, {1,3}, i, dir, false, dir, self.graphic)
addsegment(tx, ty, self, {2,3}, i, dir, false, dir, self.graphic)
exitdir = "up"
else
addsegment(tx-1, ty, self, verquad[1], i, dir, false, false, self.graphic)
addsegment(tx, ty, self, verquad[2], i, dir, false, false, self.graphic)
end
elseif dir == "left" then
if i == 1 then
addsegment(tx, ty-1, self, {3,1}, i, dir, dir, false, self.graphic)
addsegment(tx, ty, self, {3,2}, i, dir, dir, false, self.graphic)
enterdir = "left"
elseif i == #self.path then
addsegment(tx, ty-1, self, {1,1}, i, dir, false, dir, self.graphic)
addsegment(tx, ty, self, {1,2}, i, dir, false, dir, self.graphic)
exitdir = "left"
else
addsegment(tx, ty-1, self, horquad[1], i, dir, false, false, self.graphic)
addsegment(tx, ty, self, horquad[2], i, dir, false, false, self.graphic)
end
elseif dir == "down" then
if i == 1 then
addsegment(tx-1, ty, self, {1,3}, i, dir, dir, false, self.graphic)
addsegment(tx, ty, self, {2,3}, i, dir, dir, false, self.graphic)
enterdir = "down"
elseif i == #self.path then
addsegment(tx-1, ty, self, {1,5}, i, dir, false, dir, self.graphic)
addsegment(tx, ty, self, {2,5}, i, dir, false, dir, self.graphic)
exitdir = "down"
else
addsegment(tx-1, ty, self, verquad[1], i, dir, false, false, self.graphic)
addsegment(tx, ty, self, verquad[2], i, dir, false, false, self.graphic)
end
end
elseif turn == "c1" then
addsegment(tx, ty, self, {6,2}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty, self, {5,2}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty-1, self, {5,1}, i, dir, false, false, self.graphic)
addsegment(tx, ty-1, self, {6,1}, i, dir, false, false, self.graphic)
elseif turn == "c2" then
addsegment(tx, ty, self, {8,2}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty, self, {7,2}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty-1, self, {7,1}, i, dir, false, false, self.graphic)
addsegment(tx, ty-1, self, {8,1}, i, dir, false, false, self.graphic)
elseif turn == "c3" then
addsegment(tx, ty, self, {6,4}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty, self, {5,4}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty-1, self, {5,3}, i, dir, false, false, self.graphic)
addsegment(tx, ty-1, self, {6,3}, i, dir, false, false, self.graphic)
elseif turn == "c4" then
addsegment(tx, ty, self, {8,4}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty, self, {7,4}, i, dir, false, false, self.graphic)
addsegment(tx-1, ty-1, self, {7,3}, i, dir, false, false, self.graphic)
addsegment(tx, ty-1, self, {8,3}, i, dir, false, false, self.graphic)
end
end
end
--extend path because actual movementpath is offset by -.5,-.5 to make up for the 2 tile width of pipes
if #self.path == 1 then
exitdir = "right"
end
self.pathoffset = 0
if exitdir == "right" then
table.insert(self.path, {self.path[#self.path][1]+1, self.path[#self.path][2], dir="right"})
elseif exitdir == "down" then
table.insert(self.path, {self.path[#self.path][1], self.path[#self.path][2]+1, dir="down"})
end
if enterdir == "left" then
self.pathoffset = 1 --path actually starts a little ahead
table.insert(self.path, 1, {self.path[1][1]+1, self.path[1][2], dir="left"})
elseif enterdir == "up" then
self.pathoffset = 1
table.insert(self.path, 1, {self.path[1][1], self.path[1][2]+1, dir="up"})
end
self.entrance = enterdir
self.exit = exitdir
end
self.updatetable = {}
end
function clearpipe:update(dt)
if self.delete then
return true
end
if self.t == "pneumatictube" then
local delete = {}
for i = 1, #self.dusttable do
local t = self.dusttable[i]
t[2] = t[2] + self.suckspeed*dt
t[3] = t[3] + t[4]*9*dt--rotation
if t[2] > self.sucklen then
table.insert(delete, i)
end
end
table.sort(delete, function(a,b) return a>b end)
for i, v in pairs(delete) do
table.remove(self.dusttable, v) --remove
end
self.dusttimer = self.dusttimer - dt
if self.dusttimer < 0 then
self.dusttimer = math.random(1,4)/20
table.insert(self.dusttable, {math.random()*2, 0, 0, math.random()*2-1, math.random(1,2)})
end
if self.suck then
--Search portals
if not self.intersectentrance then
self.suckentranceportalx = false
self.suckentranceportaly = false
self.suckentranceportalfacing = false
local dir = self.entrance
local cx, cy = self.path[1][1]-1, self.path[1][2]-1
local cx, cy, cw, ch, dx, dy = self:getsuck(dir,"entrance",cx,cy)
for x = cx+1, cx+cw do
for y = cy+1, cy+ch do
local portalx, portaly, portalfacing, infacing = getPortal(x, y, dir)
if portalx then
if portalfacing == "left" then
portalfacing = "right"
if infacing == "up" then
portaly = portaly
else portaly = portaly+1
end
elseif portalfacing == "right" then
portalfacing = "left"
if infacing == "down" then
portalx,portaly = portalx+1,portaly
else portalx,portaly = portalx+1,portaly+1
end
elseif portalfacing == "up" then
portalfacing = "down"
if infacing == "left" then
portalx,portaly = portalx+1,portaly
end
else
portalfacing = "up"
if infacing == "right" then
portalx,portaly = portalx+1,portaly+1
else portalx,portaly = portalx,portaly+1
end
end
self.suckentranceportalx = portalx
self.suckentranceportaly = portaly
self.suckentranceportalfacing = portalfacing
break
end
end
end
end
if not self.intersectexit then
self.suckexitportalx = false
self.suckexitportaly = false
self.suckexitportalfacing = false
local dir = self.exit
local cx, cy = self.path[#self.path][1]-1, self.path[#self.path][2]-1
local cx, cy, cw, ch, dx, dy = self:getsuck(dir,"exit",cx,cy)
if dir == "left" then dir = "right"
elseif dir == "right" then dir = "left"
elseif dir == "up" then dir = "down"
else dir = "up" end
local portalx, portaly, portalfacing, infacing
for x = cx+1, cx+cw do
for y = cy+1, cy+ch do
portalx, portaly, portalfacing, infacing = getPortal(x, y, dir)
if portalx then
if portalfacing == "left" then
if infacing == "up" then
portaly = portaly
else portaly = portaly+1
end
elseif portalfacing == "right" then
if infacing == "down" then
portalx,portaly = portalx+1,portaly
else portalx,portaly = portalx+1,portaly+1
end
elseif portalfacing == "up" then
if infacing == "left" then
portalx,portaly = portalx+1,portaly
end
else
if infacing == "right" then
portalx,portaly = portalx+1,portaly+1
else portalx,portaly = portalx,portaly+1
end
end
self.suckexitportalx = portalx
self.suckexitportaly = portaly
self.suckexitportalfacing = portalfacing
break
end
end
end
end
--Suck in entities
for j, w in pairs(self.sucktable) do
for i, v in pairs(objects[w]) do
if v.active then
if not self.intersectentrance then
local dir = self.entrance
local cx, cy = self.path[1][1]-1, self.path[1][2]-1
self:suckrange(dir,"entrance",cx,cy,v,dt)
if self.suckentranceportalx then
local cx, cy = self.suckentranceportalx-1, self.suckentranceportaly-1
self:suckrange(self.suckentranceportalfacing,"entrance",cx,cy,v,dt)
end
end
if not self.intersectexit then
local dir = self.exit
local cx, cy = self.path[#self.path][1]-1, self.path[#self.path][2]-1
self:suckrange(dir,"exit",cx,cy,v,dt)
if self.suckexitportalx then
local cx, cy = self.suckexitportalx-1, self.suckexitportaly-1
self:suckrange(self.suckentranceportalfacing,"exit",cx,cy,v,dt)
end
end
end
end
end
end
end
local delete = {}
for j, w in pairs(self.updatetable) do
local a = w[1]
local b = w[2]
local dir
local pass = true
if not b.clearpipe then
--if for some reason the clearpipe table dissapeared
if self:release(a,b,"forward","forcefinish") then
table.insert(delete, j)
end
pass = false
end
if pass then
if self.intersection then
dir = b.clearpipe.travel
else
dir = self.path[self:getstage(b.clearpipe.stage)].dir
end
--rotation
if not b.noclearpiperotation then
if dir == "left" then
if b.clearpipe.dir == "forward" then
b.rotation = math.pi*1.5
else
b.rotation = math.pi*.5
end
elseif dir == "right" then
if b.clearpipe.dir == "forward" then
b.rotation = math.pi*.5
else
b.rotation = math.pi*1.5
end
elseif dir == "up" then
if b.clearpipe.dir == "backwards" then
b.rotation = math.pi
end
elseif dir == "down" then
if b.clearpipe.dir == "forward" then
b.rotation = math.pi
end
end
end
--movement handling
local finished, forcefinish = false, false
if self.intersection then --mutliple paths
local oldstage = b.clearpipe.stage
b.clearpipe.stage = b.clearpipe.stage + (b.clearpipespeed or clearpipespeed)*dt
--change direction
if oldstage < 2 and b.clearpipe.stage > 2 then
local uphold = (a == "player" and upkey(b.playernumber)) and self.crossup
local downhold = (a == "player" and downkey(b.playernumber)) and self.crossdown
local lefthold = (a == "player" and leftkey(b.playernumber)) and self.crossleft
local righthold = (a == "player" and rightkey(b.playernumber)) and self.crossright
if dir == "right" then
if self.crossright and (not (uphold)) and (not (downhold)) then
elseif self.crossup and not (downhold) then
b.clearpipe.travel = "up"
elseif self.crossdown then
b.clearpipe.travel = "down"
else
b.clearpipe.travel = "left"
end
elseif dir == "left" then
if self.crossleft and (not (uphold)) and (not (downhold)) then
elseif self.crossup and not (downhold) then
b.clearpipe.travel = "up"
elseif self.crossdown then
b.clearpipe.travel = "down"
else
b.clearpipe.travel = "right"
end
elseif dir == "down" then
if self.crossdown and (not (lefthold)) and (not (righthold)) then
elseif self.crossleft and not (righthold) then
b.clearpipe.travel = "left"
elseif self.crossright then
b.clearpipe.travel = "right"
else
b.clearpipe.travel = "up"
end
elseif dir == "up" then
if self.crossup and (not (lefthold)) and (not (righthold)) then
elseif self.crossleft and not (righthold) then
b.clearpipe.travel = "left"
elseif self.crossright then
b.clearpipe.travel = "right"
else
b.clearpipe.travel = "down"
end
end
dir = b.clearpipe.travel
end
--keep object in position
self:hold(a, b)
--check if finished with path
if b.clearpipe.stage > 3 then
finished = true
end
else --singular path
--next stage
if b.clearpipe.dir == "forward" then
b.clearpipe.stage = b.clearpipe.stage + (b.clearpipespeed or clearpipespeed)*dt
else
b.clearpipe.stage = b.clearpipe.stage - (b.clearpipespeed or clearpipespeed)*dt
end
--keep object in position
self:hold(a, b)
--check if finished with path
if b.clearpipe.dir == "forward" then
if b.clearpipe.stage >= #self.path then
finished = true
end
elseif b.clearpipe.dir == "backwards" then
if b.clearpipe.stage <= 1 then
finished = true
end
end
end
if b.transformkill and (not b.dontpassclearpipe) then
--if then enemy transformed switch to the new one
local clearpipedata = shallowcopy(b.clearpipe)
w[2] = b.transformedinto
if (not w[2]) then
finished = true
forcefinish = true
else
self:initialhold(a, w[2], clearpipedata)
finished = true
forcefinish = true
end
elseif b.shot or b.delete then
finished = true
end
--push object outside pipe
if finished then
if self:release(a,b,dir,forcefinish) then
table.insert(delete, j)
end
end
end
end
table.sort(delete, function(a,b) return a>b end)
for i, v in pairs(delete) do
table.remove(self.updatetable, v)
end
end
function clearpipe:grab(a, b, dir, stage, pipedir, entrance, exit)
--reject big enemies
if b.width > 2 or b.height > 2 or b.dontenterclearpipes then
return false
end
if self.intersection then
local ox, oy = math.max(self.x-1, math.min(self.x+1, b.x+1+b.width/2)), math.max(self.y-1, math.min(self.y+1, b.y+1+b.height/2))
--reject enemies in wrong intersection direction
local travel = nil
if ox >= self.x and oy >= self.y-(ox%1) and oy <= self.y+(ox%1) then --to left
if self.crossright then
travel = "left"
else
return false
end
elseif ox < self.x and oy >= self.y-(1-(ox%1)) and oy <= self.y+(1-(ox%1))then --to right
if self.crossleft then
travel = "right"
else
return false
end
elseif oy >= self.y and ox >= self.x-(oy%1) and ox <= self.x+(oy%1) then --to up
if self.crossdown then
travel = "up"
else
return false
end
elseif oy < self.y and ox >= self.x-(1-(oy%1)) and oy <= self.x+(1-(oy%1)) then --to down
if self.crossup then
travel = "down"
else
return false
end
end
b.clearpipe = {stage = 1, dir = "forward", travel = travel}
--get offset
local addx, addy = 0, 0
if travel == "right" then
addx = -1+(b.clearpipe.stage-1)
b.clearpipe.speed = math.abs(b.speedx)
elseif travel == "left" then
addx = 1-(b.clearpipe.stage-1)
b.clearpipe.speed = math.abs(b.speedx)
elseif travel == "down" then
addy = -1+(b.clearpipe.stage-1)
b.clearpipe.speed = math.abs(b.speedy)
elseif travel == "up" then
addy = 1-(b.clearpipe.stage-1)
b.clearpipe.speed = math.abs(b.speedy)
end
--keep object in position
self:hold(a,b)
else
b.clearpipe = {stage = 1, dir = dir}
if dir == "backwards" then
if self.t == "pneumatictube" and not self.intersectexit then
--can't go in the wrong way on a pnematic tube
b.clearpipe = nil
return false
end
b.clearpipe.stage = #self.path
end
if not ((dir == "forward" and self.intersectentrance and (stage and stage <= 2)) or (dir == "backwards" and self.intersectexit and (stage and stage+self.pathoffset >= #self.path-1))) then
if stage then --start midpipe
if pipedir == "left" or pipedir == "up" then
b.clearpipe.stage = stage - self.pathoffset + 1.5--if starting from the middle of the pipe, go to the middle of the stage
else
b.clearpipe.stage = stage + self.pathoffset + .5-- + 1.5 --if starting from the middle of the pipe, go to the middle of the stage
end
end
if self.t ~= "pneumatictube" and (not b.noclearpipesound) then
playsound(clearpipesound)
end
end
if pipedir == "up" or pipedir == "down" then
b.clearpipe.speed = math.abs(b.speedy)
else
b.clearpipe.speed = math.abs(b.speedx)
end
end
table.insert(self.updatetable, {a, b})
self:initialhold(a, b)
return true
end
function clearpipe:initialhold(a, b, clearpipedata)
if clearpipedata then
b.clearpipe = clearpipedata
table.insert(self.updatetable, {a, b})
end
if a == "player" and not b.invincible then
b.invincible = true
b.clearpipeexitinvincibility = 2
end
b.clearpipe.gravity = b.gravity
b.clearpipe.static = b.static
b.clearpipe.speedx = b.speedx
b.clearpipe.speedy = b.speedy
b.gravity = 0
b.static = true
if a == "player" then
b.falling = true
end
if b.doclearpipe then
b:doclearpipe(true)
end
end
function clearpipe:hold(a, b)
local addx, addy = 0, 0
if self.intersection then
--get offset
if b.clearpipe.travel == "right" then
addx = -1+(b.clearpipe.stage-1)
elseif b.clearpipe.travel == "left" then
addx = 1-(b.clearpipe.stage-1)
elseif b.clearpipe.travel == "down" then
addy = -1+(b.clearpipe.stage-1)
elseif b.clearpipe.travel == "up" then
addy = 1-(b.clearpipe.stage-1)
end
--keep object in position
b.x = self.x-1-b.width/2 + addx
b.y = self.y-1-b.height/2 + addy
else
local nextstage
if b.clearpipe.dir == "forward" then
nextstage = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)+1]
else
nextstage = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)-1]
end
local v = (b.clearpipe.stage%1)
--get offset
if nextstage and self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)] then
if b.clearpipe.dir == "backwards" then
addx = nextstage[1]-self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][1]
addy = nextstage[2]-self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][2]
v = 1-v
else
addx = nextstage[1]-self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][1]
addy = nextstage[2]-self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][2]
end
end
--keep object in position
b.x = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][1]-1-b.width/2 + addx*v
b.y = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][2]-1-b.height/2 + addy*v
end
end
function clearpipe:release(a, b, dir, force)
if not b then
return false
elseif not b.clearpipe then
elseif (dir == "up" and b.clearpipe.dir == "forward") or (dir == "down" and b.clearpipe.dir == "backwards") then
if self.intersection then
b.y = self.y-2-b.height
else
b.y = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][2]-1-b.height
end
if a ~= "player" then
if (b.gravity and b.gravity == 0) then
b.speedx = b.releasespeedx or 0
else
b.speedx = b.clearpipe.speedx
end
end
b.speedy = -math.max(clearpipereleasespeed, math.abs(b.clearpipe.speed or b.speedy))
elseif (dir == "down" and b.clearpipe.dir == "forward") or (dir == "up" and b.clearpipe.dir == "backwards") then
if self.intersection then
b.y = self.y
else
b.y = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][2]-1
end
if a ~= "player" then
if (b.gravity and b.gravity == 0) then
b.speedx = b.releasespeedx or 0
else
b.speedx = b.clearpipe.speedx
end
end
b.speedy = math.max(clearpipereleasespeed, math.abs(b.clearpipe.speed or b.speedy))
elseif (dir == "left" and b.clearpipe.dir == "forward") or (dir == "right" and b.clearpipe.dir == "backwards") then
if self.intersection then
b.x = self.x-2-b.width
else
b.x = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][1]-1-b.width
end
b.speedy = 0
if a ~= "player" then
b.speedx = -math.max(b.releasespeedx or clearpipereleasespeed, math.abs(b.clearpipe.speed or b.speedx))
else
b.speedx = -0.1
end
elseif (dir == "right" and b.clearpipe.dir == "forward") or (dir == "left" and b.clearpipe.dir == "backwards") then
if self.intersection then
b.x = self.x
else
b.x = self.path[self:getstage(b.clearpipe.stage,b.clearpipe.dir)][1]-1
end
b.speedy = 0
if a ~= "player" then
b.speedx = math.max(b.releasespeedx or clearpipereleasespeed, math.abs(b.clearpipe.speed or b.speedx))
else
b.speedx = 0.1
end
end
if (not force) and checkintile(b.x+0.1, b.y+0.1, b.width-0.2, b.height-0.2, {}, b, "clearpipe") then
if b.clearpipe.dir == "forward" then
b.clearpipe.dir = "backwards"
else
b.clearpipe.dir = "forward"
end
return false
end
if b.clearpipe then
if not (self.intersection or (b.clearpipe.dir == "backwards" and self.intersectentrance) or (b and b.clearpipe and b.clearpipe.dir == "forward" and self.intersectexit)) then
if self.t ~= "pneumatictube" and (not b.noclearpipesound) and (not b.shot) and (not b.delete) then
playsound(clearpipesound)
end
end
b.gravity = b.clearpipe.gravity
b.static = b.clearpipe.static
end
b.clearpipe = false
b.static = false
if b.doclearpipe then
b:doclearpipe(false)
end
return true
end
function clearpipe:draw()
--Dust
if self.t == "pneumatictube" and (not self.intersection) and self.suck then
for i = 1, #self.dusttable do
local t = self.dusttable[i]
if not self.intersectentrance then
local dir = self.entrance
local cx, cy = self.path[1][1]-1, self.path[1][2]-1
local x, y, cw, ch, dx, dy = self:getsuck(dir,"entrance",cx,cy)
local ox, oy = t[1], t[2]*dy+self.sucklen*math.max(0,-dy)
if dir == "left" or dir == "right" then
ox, oy = t[2]*dx+self.sucklen*math.max(0,-dx), t[1]
end
local v = (t[2]/self.sucklen)
love.graphics.setColor(1, 1, 1, v)
love.graphics.draw(dustimg, dustquad[t[5]], (x+ox-xscroll)*16*scale, (y+oy-.5-yscroll)*16*scale, t[3], scale, scale, 8, 8)
if self.suckentranceportalx then
local dir = self.suckentranceportalfacing
local cx, cy = self.suckentranceportalx-1, self.suckentranceportaly-1
local x, y, cw, ch, dx, dy = self:getsuck(dir,"entrance",cx,cy)
local ox, oy = t[1], t[2]*dy+self.sucklen*math.max(0,-dy)
if dir == "left" or dir == "right" then
ox, oy = t[2]*dx+self.sucklen*math.max(0,-dx), t[1]
end
local v = (t[2]/self.sucklen)
love.graphics.setColor(1, 1, 1, v)
love.graphics.draw(dustimg, dustquad[t[5]], (x+ox-xscroll)*16*scale, (y+oy-.5-yscroll)*16*scale, t[3], scale, scale, 8, 8)
end
end
if not self.intersectexit then
local dir = self.exit
local cx, cy = self.path[#self.path][1]-1, self.path[#self.path][2]-1
local x, y, cw, ch, dx, dy = self:getsuck(dir,"exit",cx,cy)
local ox, oy = t[1], t[2]*dy+self.sucklen*math.max(0,-dy)
if dir == "left" or dir == "right" then
ox, oy = t[2]*dx+self.sucklen*math.max(0,-dx), t[1]
end
local v = 1-(t[2]/self.sucklen)
love.graphics.setColor(1, 1, 1, v)
love.graphics.draw(dustimg, dustquad[t[5]], (x+ox-xscroll)*16*scale, (y+oy-.5-yscroll)*16*scale, t[3], scale, scale, 8, 8)
if self.suckexitportalx then
local dir = self.suckexitportalfacing
local cx, cy = self.suckexitportalx-1, self.suckexitportaly-1
local x, y, cw, ch, dx, dy = self:getsuck(dir,"exit",cx,cy)
local ox, oy = t[1], t[2]*dy+self.sucklen*math.max(0,-dy)
if dir == "left" or dir == "right" then
ox, oy = t[2]*dx+self.sucklen*math.max(0,-dx), t[1]
end
local v = 1-(t[2]/self.sucklen)
love.graphics.setColor(1, 1, 1, v)
love.graphics.draw(dustimg, dustquad[t[5]], (x+ox-xscroll)*16*scale, (y+oy-.5-yscroll)*16*scale, t[3], scale, scale, 8, 8)
end
end
end
--[[SUCK DEBUG
love.graphics.setLineWidth(2*scale)
local dir = self.entrance
local cx, cy = self.path[1][1]-1, self.path[1][2]-1
local cx, cy, cw, ch, dx, dy = self:getsuck(dir,"entrance",cx,cy)
love.graphics.setColor(1,1,1,180/255) if self.suckentranceportalx then love.graphics.setColor(1,0,0,180/255) end
love.graphics.rectangle("line",(cx-xscroll)*16*scale, (cy-0.5-yscroll)*16*scale, cw*16*scale, ch*16*scale)
if self.suckentranceportalx then
local cx, cy = self.suckentranceportalx-1, self.suckentranceportaly-1
local cx, cy, cw, ch, dx, dy = self:getsuck(self.suckentranceportalfacing,"entrance",cx,cy)
love.graphics.rectangle("line",(cx-xscroll)*16*scale, (cy-0.5-yscroll)*16*scale, cw*16*scale, ch*16*scale)
end
local dir = self.exit
local cx, cy = self.path[#self.path][1]-1, self.path[#self.path][2]-1
local cx, cy, cw, ch, dx, dy = self:getsuck(dir,"exit",cx,cy)
love.graphics.setColor(1,1,1,180/255) if self.suckexitportalx then love.graphics.setColor(1,0,0,180/255) end
love.graphics.rectangle("line",(cx-xscroll)*16*scale, (cy-0.5-yscroll)*16*scale, cw*16*scale, ch*16*scale)
if self.suckexitportalx then
local cx, cy = self.suckexitportalx-1, self.suckexitportaly-1
local cx, cy, cw, ch, dx, dy = self:getsuck(self.suckexitportalfacing,"exit",cx,cy)
love.graphics.rectangle("line",(cx-xscroll)*16*scale, (cy-0.5-yscroll)*16*scale, cw*16*scale, ch*16*scale)
end]]
end
--[[DEBUG
for i = 1, #self.path do
love.graphics.setColor(1, 0, 0, 100/255)
love.graphics.rectangle("fill", (self.path[i][1]-1-xscroll)*16*scale, (self.path[i][2]-1.5-yscroll)*16*scale, 16*scale, 16*scale)
love.graphics.setColor(0, 0, 0, 210/255)
love.graphics.circle("fill", (self.path[i][1]-1-xscroll)*16*scale, (self.path[i][2]-1.5-yscroll)*16*scale, 2*scale)
end]]
end
addsegment = function(x, y, parent, quad, stage, dir, entrance, exit, graphic)
if not objects["clearpipesegment"][tilemap(x, y)] then
objects["clearpipesegment"][tilemap(x, y)] = clearpipesegment:new(x, y, parent, quad, stage, dir, entrance, exit, graphic)
else
--just add the sprite if a segment already exists
table.insert(objects["clearpipesegment"][tilemap(x, y)].quad, clearpipequad[quad[1]][quad[2]])
table.insert(objects["clearpipesegment"][tilemap(x, y)].graphic, graphic)
end
end
function clearpipe:getstage(v,dir)
if dir == "forward" then
return math.max(1, math.min(#self.path, math.floor(v)))
else
return math.max(1, math.min(#self.path, math.ceil(v)))
end
end
function clearpipe:createintersection()
if self.intersection then
--intersection
local tx, ty = self.x, self.y
local tiles = {
{5,1},
{8,1},
{5,4},
{8,4}
}
self.crossleft = false
self.crossright = false
self.crossup = false
self.crossleft = false
self.suck = false
--up
local s = objects["clearpipesegment"][tilemap(tx-1, ty-2)] --segment
local s2 = objects["clearpipesegment"][tilemap(tx, ty-2)]
if s and s2 and (s.dir == "down" or s.dir == "up") then
if self.crossleft then
tiles[1] = {3,3}
else
tiles[1] = {1,4}
end
if self.crossright then
tiles[2] = {4,3}
else
tiles[2] = {2,4}
end
if s.entrance then
s.parent.intersectentrance = true
s2.parent.intersectentrance = true
elseif s.exit then
s.parent.intersectexit = true
s2.parent.intersectexit = true
end
s.quad = {clearpipequad[1][4]}
s2.quad = {clearpipequad[2][4]}
self.crossup = true
end
--down
local s = objects["clearpipesegment"][tilemap(tx-1, ty+1)] --segment
local s2 = objects["clearpipesegment"][tilemap(tx, ty+1)]
if s and s2 and (s.dir == "up" or s.dir == "down") then
if self.crossleft then
tiles[3] = {3,4}
else
tiles[3] = {1,4}
end
if self.crossright then
tiles[4] = {4,4}
else
tiles[4] = {2,4}
end
if s.entrance then
s.parent.intersectentrance = true
s2.parent.intersectentrance = true
elseif s.exit then
s.parent.intersectexit = true
s2.parent.intersectexit = true
end
s.quad = {clearpipequad[1][4]}
s2.quad = {clearpipequad[2][4]}
self.crossdown = true
end
--left
local s = objects["clearpipesegment"][tilemap(tx-2, ty)] --segment
local s2 = objects["clearpipesegment"][tilemap(tx-2, ty-1)]
if s and s2 and (s.dir == "right" or s.dir == "left") then
if self.crossup then
tiles[1] = {3,3}
else
tiles[1] = {2,1}
end
if self.crossdown then
tiles[3] = {3,4}
else
tiles[3] = {2,2}
end
if s.entrance then
s.parent.intersectentrance = true
s2.parent.intersectentrance = true
elseif s.exit then
s.parent.intersectexit = true
s2.parent.intersectexit = true
end
s.quad = {clearpipequad[2][2]}
s2.quad = {clearpipequad[2][1]}
self.crossleft = true
end
--right
local s = objects["clearpipesegment"][tilemap(tx+1, ty)] --segment
local s2 = objects["clearpipesegment"][tilemap(tx+1, ty-1)]
if s and s2 and (s.dir == "left" or s.dir == "right") then
if self.crossup then
tiles[2] = {4,3}
else
tiles[2] = {2,1}
end
if self.crossdown then
tiles[4] = {4,4}
else
tiles[4] = {2,2}
end
if s.entrance then
s.parent.intersectentrance = true
s2.parent.intersectentrance = true
elseif s.exit then
s.parent.intersectexit = true
s2.parent.intersectexit = true
end
s.quad = {clearpipequad[2][2]}
s2.quad = {clearpipequad[2][1]}
self.crossright = true
end
addsegment(tx-1, ty-1, self, tiles[1], i, "intersection", false, false, self.graphic)
addsegment(tx, ty-1, self, tiles[2], i, "intersection", false, false, self.graphic)
addsegment(tx-1, ty, self, tiles[3], i, "intersection", false, false, self.graphic)
addsegment(tx, ty, self, tiles[4], i, "intersection", false, false, self.graphic)
self.path = {{self.x, self.y, dir = "right"}}
end
end
function clearpipe:getsuck(dir,tubeend,cx,cy)
local dx, dy = 0, 0
local cw, ch = 2, 2
if dir == "right" then
dx = 1
cw = self.sucklen
cy = cy-1
elseif dir == "left" then
dx = -1
cw = self.sucklen