-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar_dungeon.lua
2456 lines (2222 loc) · 154 KB
/
lidar_dungeon.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
-- title: LiDAR Dungeon
-- author: Wojciech Graj
-- desc: A Bullet-Hell Roguelike made for 7DRL2023
-- site: https://github.com/wojciech-graj/LiDAR-Dungeon
-- license: AGPL-3.0-or-later
-- version: 1.0
-- script: lua
--[[
LiDAR Dungeon
Copyright (C) 2023 Wojciech Graj
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--]]
--- Conventions
-- VARIABLE delta: time since last frame (ms)
-- VARIABLE g_*: global variable
-- VARIABLE dir_*: normalized direction
-- VARIABLE c_*: constant variable
-- VARIABLE room: {start_x, start_x, end_x, end_y}
-- FUNCTION Class:process(delta): executed every frame. May return boolean,
-- where true signifies that object should be deleted
--- Interfaces
-- Entity:
-- VARIABLES: pos_x, pos_y, angle, health
-- FUNCTIONS: move_abs, move_rel, damage, draw_sprite
--- Map Data
-- 0: Empty
-- &0x7: Enemy count
-- &0x38: Tile:
-- 8: Wall
-- 16: Item
-- 24: Inactive Enemy
-- 32: Exit
-- 40: Boss
-- Temporary values:
-- 128: Map filler
-- 1: Map air filler
----------------------------------------
-- utility functions -------------------
----------------------------------------
--- Calculate a ray-map intersection
-- Uses Digital Differential Analyzer (DDA) voxel traversal to find closest wall
-- intersection.
-- @return dist number, side int
function g_ray_isect(pos_x, pos_y, dir_x, dir_y)
local math_floor = math.floor
local math_abs = math.abs
local map_x = math_floor(pos_x)
local map_y = math_floor(pos_y)
local delta_dist_x = math_abs(1 / dir_x)
local delta_dist_y = math_abs(1 / dir_y)
local step_x
local side_dist_x
if dir_x < 0 then
step_x = -1
side_dist_x = (pos_x - map_x) * delta_dist_x
else
step_x = 1
side_dist_x = (map_x + 1.0 - pos_x) * delta_dist_x
end
local step_y
local side_dist_y
if dir_y < 0 then
step_y = -1
side_dist_y = (pos_y - map_y) * delta_dist_y
else
step_y = 1
side_dist_y = (map_y + 1.0 - pos_y) * delta_dist_y
end
-- DDA
local side
local iters = 0
while iters < 96 do
if side_dist_x < side_dist_y then
side_dist_x = side_dist_x + delta_dist_x
map_x = map_x + step_x
side = 0
else
side_dist_y = side_dist_y + delta_dist_y
map_y = map_y + step_y
side = 1
end
iters = iters + 1
tile_data = mget(map_x, map_y)
if tile_data & 0x38 == 8 then
break
end
end
local dist
if side == 0 then
dist = side_dist_x - delta_dist_x
else -- side == 1
dist = side_dist_y - delta_dist_y
end
return dist, side
end
--- Get ray-circle collision
-- @return squared distance along ray if colliding, 1e9 otherwise
function g_ray_circ_collides(rel_pos_x, rel_pos_y, dir_x, dir_y, rad)
if (rel_pos_x * dir_x + rel_pos_y * dir_y) / math.sqrt(rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y) > 0 then -- in front
local dist_perp = math.abs(dir_x * rel_pos_y - dir_y * rel_pos_x)
if dist_perp < rad then -- within radius
return rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y - dist_perp * dist_perp
end
end
return 1e9
end
--- Spawn explosion projectiles
function g_explode(pos_x, pos_y)
local table_insert = table.insert
local projs = g_projs
local math_cos = math.cos
local math_sin = math.sin
for theta = 0, 6.28, .55 do
table_insert(projs, Proj.new(pos_x, pos_y, theta, .005, .5, 0, 3))
end
end
--- Pick up item
function g_item_pickup(pos_x, pos_y)
mset(pos_x, pos_y, mget(pos_x, pos_y) & 0x7)
g_state = 2
sfx(21)
local math_random = math.random
-- select 2 different item types
local item_1 = Item.new()
local item_2
repeat
item_2 = Item.new()
until (item_1.type_idx ~= item_2.type_idx)
g_items = {
item_1,
item_2,
}
end
-- Draw UI
function g_ui_render()
-- Draw borders
map(210, 34, 5, 17, 200, 0)
local player = g_player
-- Draw bars
map(217 - player.health, 51, 1, 3, 208, 8)
map(210 + 7 * player.ping_cooldown / player.ping_cooldown_max, 57, 1, 3, 216, 8)
map(210 + 7 * player.weapon.cooldown / player.weapon.cooldown_max, 54, 1, 3, 224, 8)
-- Draw compass
local comp_pos_x = g_comp_pos_x
local comp_pos_y = g_comp_pos_y
local dir_x_comp
local dir_y_comp
if comp_pos_x == -1 and comp_pos_y == -1 then
local theta = g_t / 300 + math.random() * .5
dir_x_comp = math.cos(theta) * 4
dir_y_comp = math.sin(theta) * 4
else
dir_x_comp = comp_pos_x - player.pos_x
dir_y_comp = comp_pos_y - player.pos_y
local dir_comp_mul = 4 / math.sqrt(dir_y_comp * dir_y_comp + dir_x_comp * dir_x_comp)
dir_x_comp = dir_x_comp * dir_comp_mul
dir_y_comp = dir_y_comp * dir_comp_mul
end
line(220, 4, 220 + dir_x_comp, 4 + dir_y_comp, 2)
local string_format = string.format
local math_floor = math.floor
-- Print self stats
print("SELF", 208, 34, 2, true)
print(string_format("SPD:%.1f", player.speed * 1000), 209, 42, 2, false, 1, true)
-- Print scanner stats
print("SCAN", 208, 50, 8, true)
print(string_format("RNG:%d", math_floor(player.ping_range)), 209, 58, 8, false, 1, true)
print(string_format("REL:%.1f", player.ping_cooldown_max * .001), 209, 66, 8, false, 1, true)
print(string_format("SPR:%d", math_floor(player.ping_spread * 57.296)), 209, 74, 8, false, 1, true)
-- Print weapon stats
local weapon = player.weapon
if weapon.proj_type == 0 then
print("GUN", 212, 82, 7, true)
else -- weapon.proj_type == 1
print("RCKT", 208, 82, 7, true)
end
print(string_format("DMG:%.1f", weapon.damage), 209, 90, 7, false, 1, true)
print(string_format("REL:%.1f", weapon.cooldown_max * .001), 209, 98, 7, false, 1, true)
print(string_format("BUL:%d", weapon.proj_cnt), 209, 106, 7, false, 1, true)
print(string_format("SPR:%d", math_floor(weapon.spread * 57.296)), 209, 114, 7, false, 1, true)
print(string_format("RNG:%d", math_floor(weapon.range)), 209, 122, 7, false, 1, true)
end
--- pix, but with absolute position and checks to verify if on screen
function g_pix_bounded(pos_x, pos_y, color)
local player = g_player
if pos_x > player.pos_x_scr * 25 and pos_x < player.pos_x_scr * 25 + 25
and pos_y > player.pos_y_scr * 17 and pos_y < player.pos_y_scr * 17 + 17 then
pix((pos_x % 25) * 8, (pos_y % 17) * 8, color)
end
end
--- Print rainbow text that bobs up and down
-- @param text table: table of characters to print
-- @param start int: color on which to start
-- @param range int: no. colors to include in rainbow
function g_print_rainbow(text, pos_x, pos_y, start, range)
local t = g_t
local color_start = t // 70
local offset_y = t / 100
local math_sin = math.sin
for k, v in ipairs(text) do
print(v, pos_x + 6 * k, pos_y + math_sin(offset_y + k * .7), (color_start + k) % range + start, true)
end
end
--- Print stats
-- @param floors int or nil
function g_print_stats(stats, pos_x, pos_y, floors)
local string_format = string.format
local math_floor = math.floor
if floors then
print(string_format("Floors Cleared.....%d", floors), pos_x, pos_y, 13, true, 1, true)
pos_y = pos_y + 8
end
local format_values = {
stats.damage_taken,
stats.damage_dealt,
stats.bullets_fired,
math_floor(stats.time_total / 1000),
stats.bullets_taken,
stats.pings,
stats.items_collected,
stats.enemies_destroyed,
math_floor(stats.distance_travelled),
}
local formats = gc_stats_format_strings
for i = 1, #formats do
print(string_format(formats[i], format_values[i]), pos_x, pos_y + i * 8 - 8, 13, true, 1, true)
end
end
gc_stats_format_strings = {
"Damage Taken.......%.1f",
"Damage Dealt.......%.1f",
"Bullets Fired......%d",
"Seconds Elapsed....%d",
"Bullets Taken......%d",
"Scans Performed....%d",
"Upgrades Obtained..%d",
"Enemies Destroyed..%d",
"Distance Travelled.%d",
}
--- Place an exit on the map
function g_exit_spawn(tile_x, tile_y)
g_comp_pos_x = tile_x + .5
g_comp_pos_y = tile_y + .5
mset(tile_x, tile_y, (mget(tile_x, tile_y) & 0x7) + 32)
end
--- pix, but for the title screen
function g_pix_title_screen(pos_x, pos_y, color)
pix((pos_x - 208) * 8, (pos_y - 102) * 8, color)
end
----------------------------------------
-- map generation ----------------------
----------------------------------------
--- Naming:
-- s*: (start) top left corner
-- e*: (end) bottom right corner
-- l*: (length) width/height including walls
-- r*: (room) relating to current room
-- t*: screen index
--- Adds area to free areas if suitable
function g_map_gen_add_area(tab, sx, sy, ex, ey, lx, ly)
if lx >= 6 and ly >= 6 then
table.insert(tab, {sx, sy, ex, ey, lx, ly})
end
end
--- Digs a corridor out of room
function g_dig_corridor(x, y, dx, dy)
local math_random = math.random
-- Don't always dig tunnel
if math_random() < .3 then
return
end
while mget(x, y) ~= 1 do -- haven't reached open space
-- Dig
mset(x, y, 1)
if mget(x + dy, y + dx) ~= 1 then
mset(x + dy, y + dx, 8)
end
if mget(x - dy, y - dx) ~= 1 then
mset(x - dy, y - dx, 8)
end
-- Check edge of map collision, exit if so
if x >= 199 then
mset(199, y, 8)
return
elseif x <= 0 then
mset(0, y, 8)
return
end
if y >= 118 then
mset(x, 118, 8)
return
elseif y <= 0 then
mset(x, 0, 8)
return
end
-- Meander sometimes
if math_random() < .1 then
x = x + dy
y = y + dx
else
x = x + dx
y = y + dy
end
end
end
--- Places a tile cnt times in a room
function g_map_place_rand(room, tile, cnt)
local math_random = math.random
for _ = 1, cnt do
mset(
math_random(room[1] + 2, room[3] - 2),
math_random(room[2] + 2, room[4] - 2),
tile
)
end
end
--- Generates map
function g_map_gen()
-- Clear map
for x = 0, 199 do
for y = 0, 118 do
mset(x, y, 128)
end
end
local math_min = math.min
local math_max = math.max
local table_insert = table.insert
local table_remove = table.remove
local table_unpack = table.unpack
local math_random = math.random
local math_floor = math.floor
local map_gen_add_area = g_map_gen_add_area
local rooms = {}
-- Place rooms
for tx = 0, 7 do -- Generate each screen separately to limit rooms to single screen
for ty = 0, 6 do
local free_area = {{25 * tx, 17 * ty, 25 * tx + 24, 17 * ty + 16, 24, 16}}
while #free_area > 0 do
local sx, sy, ex, ey, lx, ly = table_unpack(table_remove(free_area))
-- Place room
local rlx = math_random(6, math_min(lx, 11))
local rly = math_random(6, math_min(ly, 9))
local rsx = (ex - rlx ~= sx) and math_random(sx, ex - rlx) or sx
local rsy = (ey - rly ~= sy) and math_random(sy, ey - rly) or sy
local rex = rsx + rlx
local rey = rsy + rly
table_insert(rooms, {rsx, rsy, rex, rey})
-- Draw to map
for x = rsx, rex do
mset(x, rsy, 8)
mset(x, rey, 8)
end
for y = rsy, rey do
mset(rsx, y, 8)
mset(rex, y, 8)
end
for y = rsy + 1, rey - 1 do
for x = rsx + 1, rex - 1 do
mset(x, y, 1)
end
end
local right = ex - rex
local left = rsx - sx
local top = rsy - sy
local bottom = ey - rey
-- Split remaining space into 4 areas
if math_max(left, right) > math_max(top, bottom) then
map_gen_add_area(free_area, sx, sy, rsx, ey, left, ly)
map_gen_add_area(free_area, rex, sy, ex, ey, right, ly)
map_gen_add_area(free_area, rsx, sy, rex, rsy, rlx, top)
map_gen_add_area(free_area, rsx, rey, rex, ey, rlx, bottom)
else
map_gen_add_area(free_area, sx, sy, ex, rsy, lx, top)
map_gen_add_area(free_area, sx, rey, ex, ey, lx, bottom)
map_gen_add_area(free_area, sx, rsy, rsx, rey, left, rly)
map_gen_add_area(free_area, rex, rsy, ex, rey, right, rly)
end
end
end
end
local dig_corridor = g_dig_corridor
-- Dig corridors
for _, room in pairs(rooms) do
local sx, sy, ex, ey = table_unpack(room)
dig_corridor(ex, sy + math_random(ey - sy - 2), 1, 0)
dig_corridor(sx, sy + math_random(ey - sy - 2), -1, 0)
dig_corridor(sx + math_random(ex - sx - 2), ey, 0, 1)
dig_corridor(sx + math_random(ex - sx - 2), sy, 0, -1)
end
-- Flood-fill air placeholder with air
local room = rooms[1]
local x = room[1] + 1
local y = room[2] + 1
local stack = {{x, y}}
while #stack > 0 do
local x, y = table_unpack(table_remove(stack))
if mget(x, y) == 1 then
mset(x, y, 0)
table_insert(stack, {x - 1, y})
table_insert(stack, {x + 1, y})
table_insert(stack, {x, y - 1})
table_insert(stack, {x, y + 1})
end
end
-- Count disconnected tiles and fill
local discon_cnt = 0
for x = 0, 199 do
for y = 0, 118 do
local tile_data = mget(x, y)
if tile_data == 128 then
mset(x, y, 8)
elseif tile_data == 1 then
discon_cnt = discon_cnt + 1
mset(x, y, 8)
end
end
end
-- Re-do generation if too many disconnected tiles
if discon_cnt > 363 then
return g_map_gen()
end
-- Cull invalid rooms
if discon_cnt > 0 then
for k, v in pairs(rooms) do
if mget(v[1], v[2]) ~= 1 then
table_remove(rooms, k)
end
end
end
local map_place_rand = g_map_place_rand
local start_room = table_remove(rooms, math_random(#rooms))
map_place_rand(start_room, 16, 3) -- Place 3 upgrades in start room
-- Place items, enemies
for _, room in pairs(rooms) do
map_place_rand(room, 8, math_random(3) - 1)
map_place_rand(room, 16, math_random() < .15 and 1 or 0)
map_place_rand(room, 24, math.floor(math_random(2, 8) ^ .7 - 1))
end
-- Place exit
local exit_room
repeat
exit_room = rooms[math_random(#rooms)]
until ((start_room[1] - exit_room[1]) ^ 2 + (start_room[2] - exit_room[2]) ^ 2 > 70)
g_exit_spawn(math_random(exit_room[1] + 2, exit_room[3] - 2),
math_random(exit_room[2] + 2, exit_room[4] - 2)
)
return start_room
end
----------------------------------------
-- Item --------------------------------
----------------------------------------
--- type_idx:
-- 1: speed
-- 2: ping
-- 3: gun
-- 4: rocket
-- 5: repair
--- subtype_idx:
-- ping:
-- 1: range
-- 2: spread
-- 3: cooldown
-- 4: bounce
-- gun/rocket:
-- 1: new weapon
-- 2: bullet_cnt
-- 3: damage
-- 4: spread
-- 5: cooldown
-- 6: range
-- 7: bounce
Item = {
type_idx = 0,
data = nil,
subtype_idx = 1,
desc = nil,
c_data = {
[1] = {
name = "THRUSTER",
color = 2,
},
[2] = {
name = " SCANNER",
color = 8,
},
[3] = {
name = " GUN",
color = 7,
},
[4] = {
name = " ROCKETS",
color = 7,
},
[5] = {
name = " REPAIR",
color = 2,
},
},
}
Item.__index = Item
function Item.new()
local self = setmetatable({}, Item)
local math_random = math.random
self.type_idx = math_random(5)
if self.type_idx == 1 then
self.data = math_random() * .0002 + .0001
self.desc = string.format("+%.1f\nSPEED", self.data * 1000)
elseif self.type_idx == 2 then
local roll = math_random()
if roll < .31 then
self.subtype_idx = 1
self.data = math_random(3)
self.desc = string.format("+%d\nRANGE", self.data)
elseif roll < .62 then
self.subtype_idx = 2
self.data = math_random(10)
self.desc = string.format("+%d DEG\nSPREAD", self.data)
self.data = self.data * .0175
elseif roll < .93 then
self.subtype_idx = 3
self.data = math.random() * .06 + .01
self.desc = string.format("-%.2f\nCOOLDOWN", self.data)
else
self.subtype_idx = 4
self.data = math_random() * .3 + .2
self.desc = string.format("+1BOUNCE\n-%d%%RNG", math.floor(self.data * 100))
end
elseif self.type_idx == 3 or self.type_idx == 4 then
local player = g_player
local roll = math_random()
if roll < .05 then
self.subtype_idx = 1
local bullet_cnt = player.weapon.proj_cnt
local damage = player.weapon.damage + (math_random() - .5)
local spread = math_random(1000) / 1000
local cooldown = math_random(500, 1000)
local speed = (cooldown - 500) // 250 + 1
self.data = Weapon.new(self.type_idx - 3, 1, bullet_cnt, spread, .1, cooldown, 20, -1, damage, .005)
self.desc = string.format("%dBUL %dDMG\n%sSPR %sSPD", bullet_cnt, math.max(1, math.floor(damage)), math.floor(spread * 9), speed)
else
self.type_idx = player.weapon.proj_type + 3
if roll < .1 then
self.subtype_idx = 2
self.data = math_random() * .2 + .2
self.desc = string.format("+1BULLET\n-%d%%DMG", math.floor(self.data * 100))
elseif roll < .36 then
self.subtype_idx = 3
self.data = math_random() * .2 + .1
self.desc = string.format("+%.1f\nDAMAGE", self.data)
elseif roll < .51 then
self.subtype_idx = 4
self.data = math_random(20) - 10
self.desc = string.format("%s%d DEG\nSPREAD", (self.data < 0) and "" or "+", self.data)
self.data = self.data * .0175
elseif roll < .66 then
self.subtype_idx = 5
self.data = math_random() * .06 + .01
self.desc = string.format("-%.2f\nCOOLDOWN", self.data)
elseif roll < .95 then
self.subtype_idx = 6
self.data = math.random(2)
self.desc = string.format("+%d\nRANGE", self.data)
else
self.subtype_idx = 7
self.data = math_random() * .3 + .2
self.desc = string.format("+1BOUNCE\n-%d%%RNG", math.floor(self.data * 100))
end
end
else -- self.type_idx == 5
self.data = math_random(3) + 1
self.desc = string.format("+%d\nHEALTH", self.data)
end
return self
end
function Item:render(pos_x, pos_y)
map(206 + 4 * self.type_idx, 17, 4, 4, pos_x, pos_y)
local data = self.c_data[self.type_idx]
print(data.name, pos_x, 34 + pos_y, data.color, false, 1, true)
print(self.desc, pos_x, 42 + pos_y, data.color, false, 1, true)
end
function Item:apply()
local player = g_player
if self.type_idx == 1 then
player.speed = player.speed + self.data
elseif self.type_idx == 2 then
if self.subtype_idx == 1 then
player.ping_range = player.ping_range + self.data
elseif self.subtype_idx == 2 then
player.ping_spread = player.ping_spread + self.data
elseif self.subtype_idx == 3 then
player.ping_cooldown_max = math.max(player.ping_cooldown_max - self.data, 100)
else -- self.subtype_idx == 4
player.ping_bounces = player.ping_bounces + 1
player.ping_range = player.ping_range * (1 - self.data)
end
elseif self.type_idx == 3 or self.type_idx == 4 then
if self.subtype_idx == 1 then
player.weapon = self.data
elseif self.subtype_idx == 2 then
player.weapon.proj_cnt = player.weapon.proj_cnt + 1
player.weapon.damage = player.weapon.damage * (1 - self.data)
elseif self.subtype_idx == 3 then
player.weapon.damage = player.weapon.damage + self.data
elseif self.subtype_idx == 4 then
player.weapon.spread = player.weapon.spread + self.data
elseif self.subtype_idx == 5 then
player.weapon.cooldown_max = math.max(player.weapon.cooldown_max - self.data, 100)
elseif self.subtype_idx == 6 then
player.weapon.range = player.weapon.range + self.data
else -- self.subtype_idx == 7
player.weapon.bounces = player.weapon.bounces + 1
player.weapon.range = player.weapon.range * (1 - self.data)
end
else -- self.type_idx == 5
player:heal(self.data)
end
end
----------------------------------------
-- Entity ------------------------------
----------------------------------------
Entity = {}
Entity.__index = Entity
--- Move an entity relative to its angle
function Entity:move_rel(dist_front, dist_side)
local dir_x_self = math.cos(self.angle)
local dir_y_self = math.sin(self.angle)
return self:move_abs(dist_front * dir_x_self + dist_side * dir_y_self, dist_front * dir_y_self - dist_side * dir_x_self)
end
--- Move an entity relative to the XY axes
function Entity:move_abs(dist_x, dist_y)
local dist_mag = math.sqrt(dist_x * dist_x + dist_y * dist_y)
local dist_invmag = 1 / dist_mag
local dir_x = dist_x * dist_invmag
local dir_y = dist_y * dist_invmag
local isect_dist = g_ray_isect(self.pos_x, self.pos_y, dir_x, dir_y)
if isect_dist > .4 then -- not approaching wall
local dist_min = math.min(dist_mag, math.max(0, isect_dist - .4))
self.pos_x = self.pos_x + dir_x * dist_min
self.pos_y = self.pos_y + dir_y * dist_min
return dist_min
end
return 0
end
--- Draw a triangular sprite
function Entity:draw_sprite(color)
local player = g_player
if self.pos_x > player.pos_x_scr * 25 and self.pos_x < player.pos_x_scr * 25 + 25
and self.pos_y > player.pos_y_scr * 17 and self.pos_y < player.pos_y_scr * 17 + 17 then
local math_cos = math.cos
local math_sin = math.sin
local pos_x_scl = (self.pos_x % 25) * 8
local pos_y_scl = (self.pos_y % 17) * 8
tri(
pos_x_scl + 4 * math_cos(self.angle),
pos_y_scl + 4 * math_sin(self.angle),
pos_x_scl + 4 * math_cos(self.angle + 2.7),
pos_y_scl + 4 * math_sin(self.angle + 2.7),
pos_x_scl + 4 * math_cos(self.angle - 2.7),
pos_y_scl + 4 * math_sin(self.angle - 2.7),
color
)
end
end
----------------------------------------
-- Hitmark -----------------------------
----------------------------------------
-- type_idx:
-- 1: wall
-- 2: item
-- 3: exit
--- Map-Proj hit indicator
Hitmark = {
pos_x = 0,
pos_y = 0,
age = 0,
type_idx = 0,
}
Hitmark.__index = Hitmark
function Hitmark.new(pos_x, pos_y, type_idx)
local self = setmetatable({}, Hitmark)
self.pos_x = pos_x
self.pos_y = pos_y
self.type_idx = type_idx
return self
end
function Hitmark:process(delta)
self.age = self.age + delta
local color
if self.type_idx == 1 then
color = 12 + self.age * .002
elseif self.type_idx == 2 then
color = 4
else -- self.type_idx == 3
color = 11
end
if g_state == 6 then
g_pix_title_screen(self.pos_x, self.pos_y, color)
else
g_pix_bounded(self.pos_x, self.pos_y, color)
end
return self.age > 1000
end
----------------------------------------
-- Weapon ------------------------------
----------------------------------------
--- proj_type:
-- 0: bullet
-- 1: missile
--- target:
-- 0: player
-- 1: enemy
Weapon = {
proj_type = 0,
damage = 0,
target = 0,
proj_cnt = 0,
spread = 0,
accuracy = 0,
cooldown = 0,
cooldown_max = 0,
range = 0,
ammo = 0,
speed = 0,
bounces = 0,
}
Weapon.__index = Weapon
function Weapon.new(proj_type, target, proj_cnt, spread, accuracy, cooldown, range, ammo, damage, speed, bounces)
local self = setmetatable({}, Weapon)
self.proj_type = proj_type
self.target = target
self.proj_cnt = proj_cnt
self.spread = spread
self.accuracy = accuracy
self.cooldown_max = cooldown
self.range = range
self.ammo = ammo
self.damage = damage
self.speed = speed
self.bounces = bounces or 0
return self
end
function Weapon.new_random(target)
local math_random = math.random
local wpn_type = math_random(4)
if wpn_type == 1 then -- gun
return Weapon.new(0,
target,
1,
0,
math_random() * .5,
math_random(800, 1300),
math_random(10, 15),
-1,
math_random() + .5,
math_random() * .002 + .004,
math_random() < .5 and 2 or 0
)
elseif wpn_type == 2 then -- shotgun
return Weapon.new(0,
target,
math_random(2, 3),
math_random() * .9,
math_random() * .5,
math_random(1000, 1500),
math_random(6, 9),
-1,
1,
math_random() * .002 + .003,
math_random() < .1 and 3 or ((math_random() < .5) and 1 or 0)
)
elseif wpn_type == 3 then -- circle
return Weapon.new(0,
target,
math_random(4, 6),
3.14,
.3,
math_random(1000, 1200),
math_random(6, 9),
-1,
1,
math_random() * .002 + .0015,
math_random() < .1 and 3 or 0
)
else -- wpn_type == 4 -- rapid-fire
return Weapon.new(1,
target,
1,
0,
.4,
math_random(300, 700),
math_random(7, 11),
-1,
.5,
math_random() * .001 + .0032,
math_random() < .1 and 1 or 0
)
end
end
function Weapon:process(delta)
self.cooldown = math.max(self.cooldown - delta, 0)
end
function Weapon:fire(pos_x, pos_y, angle)
if self.cooldown == 0 and self.ammo ~= 0 then
self.cooldown = self.cooldown_max
self.ammo = self.ammo - 1
local math_random = math.random
local dangle = (self.proj_cnt == 1 and 1 or 2) * self.spread / self.proj_cnt
local projs = g_projs
local table_insert = table.insert
for i = 1, self.proj_cnt do
table_insert(projs, Proj.new(pos_x, pos_y, angle - self.spread + dangle * i + (2 * math.random() - 1) * self.accuracy, self.speed, self.range, self.bounces, self.target + 1 + 3 * self.proj_type, self.damage))
end
if self.target == 1 then
local player = g_player
player.stats_flr.bullets_fired = player.stats_flr.bullets_fired + self.proj_cnt
end
end
end
----------------------------------------
-- Enemy -------------------------------
----------------------------------------
--- Naming:
-- Enemy.b_*: (boss) boss-specific variables
--- ai_idx:
-- 1: follow
-- 2: encircle
-- 3: erratic
-- 4: RESERVED
-- 5: boss
Enemy = {
pos_x = 0,
pos_y = 0,
angle = 0,
display_timer = 0,
move_abs = Entity.move_abs,
move_rel = Entity.move_rel,
draw_sprite = Entity.draw_sprite,
speed = 0,
health = 0,
weapon = nil,
ai_idx = 1,
pos_x_player_last = 0,
pos_y_player_last = 0,
vel_side = 0,
loot_chance = 0,
-- b_ai_timer
-- b_weapons
}
Enemy.__index = Enemy
function Enemy.new(pos_x, pos_y, speed, health, weapon, ai_idx, loot_chance)
local self = setmetatable({}, Enemy)
self.pos_x = pos_x
self.pos_y = pos_y
self.speed = speed
self.health = health
self.weapon = weapon
self.ai_idx = ai_idx
self.loot_chance = loot_chance or .33
mset(pos_x, pos_y, mget(pos_x, pos_y) & 0x7)
self:mark_area(true)
return self
end
function Enemy.new_random(pos_x, pos_y)
local math_random = math.random
return Enemy.new(
pos_x,
pos_y,
math_random() * .025 + .005,
math_random(5 + g_player.floor * 2),
Weapon.new_random(0),
math_random(3)
)
end
function Enemy.new_boss(pos_x, pos_y)
local math_random = math.random
local self = Enemy.new(
pos_x,
pos_y,
math_random() * .005 + .04,
15 + g_player.floor * 8,
nil,
5
)
self.b_ai_timer = 0
self.b_weapons = {
Weapon.new(0, 0, 6, 3.14, .4, 900, 17, -1, 1, .005, 2),
Weapon.new(0, 0, 9, 1.5, .3, 750, 12, -1, 1, .006),
Weapon.new(1, 0, 1, 0, .8, 350, 15, -1, 1, .008),