-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtower.p8
1941 lines (1824 loc) · 75.5 KB
/
tower.p8
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
pico-8 cartridge // http://www.pico-8.com
version 34
__lua__
-- tower noire
-- by cow
function _init()
log = {{0,"you wake up in a large room."},{0,"a old man and items are here."}}
-- entity descriptions:
-- 1: type
-- 0 block
-- 1 creature
-- 2 corpses and effects
-- 3 items
-- 2: name
-- 3: sprite
-- 4: x
-- 5: y
-- 6: health
-- 7: status effect
entities = {{3,"tride",116,64,64},{3,"snack",99,72,64},{0,"wizard",35,64,72,0,0},{0,"fire",31,72,72}}
cursor = ">"
show_menu = false
show_prompt = false
game_started = false
game_won = false
camera_x = 0
camera_y = 0
room = 0
player_can_move = true
player_is_visible = true
player_health = 28
player_max_health = 28
player_x = 40
player_x_previous = 40
player_y = 56
player_y_previous = 56
player_steps = 0
player_kills = 0
player_image = 0
player_inventory = {{"hand","sword",115},{"body","tunic",112},{"legs","",46},{"pocket","flask",101},{"pocket","snack",99},{"pocket","",62}}
player_actions = {{cursor,"equip"},{"","throw"},{"","eat"},{"","drop"},{"","exit"}}
player_stats = {str=7, def=1}
player_effects = {"",""}
wearable = {{"tunic",2,1},{"poncho",2,2},{"jokki",2,3},{"exomis",2,4},{"sword",1,0,7},{"sword+3",1,0,10},{"gavel",1,0,6,"bash"},{"estoc",1,0,5,"parry"},{"tride",1,0,3,"sticky"},{"boots",3,1},{"socks",3,1}} --name, equip slot, def amount, str amount, other effect
breakable = {{"snack",1,"",0.5},{"flask",-2,"",0.95}} --name, damage, other effect, probability of breaking
edible = {{"snack",2,"",0,4},{"flask",10,"",0,2}} --name, heal amount, other effect, other amount, fixed heal
music(4)
-- unused items:
-- {3,"gavel",117,0,0}
-- {3,"jokki",114,0,0}
-- {3,"exomis",102,0,0}
-- {3,"estoc",115,0,0}
-- {3,"quest",100,0,0}
-- {1,"wailer",36,0,0,12}
-- {1,"homunc",39,0,0,12,0}
-- {1,"groza",40,0,0,12}
function dig_tunnels()
-- set x and y for straight tunnels
local x = flr(rnd(room_width-2))+2
local y = flr(rnd(room_height-2))+2
-- identify areas where tunnels are needed
for i=1, #grid do
for j=1, #grid[i] do
-- horizontal
local horizontal_count = 0
if j==2 then
if grid[i][j-1]!=0 then horizontal_count += 1 end
if grid[i][j+1]!=0 then horizontal_count += 1 end
end
if grid[i][j] == 0 and j==2 and horizontal_count==cell_width-1 then
for x=1,level_width-2 do
-- add wall tiles north
if mget((x-1)+((j-1)),(y-2)+((i-1)*room_height))==2 then
mset((x-1)+((j-1)),(y-2)+((i-1)*room_height),1)
end
-- add wall tiles south
if mget((x-1)+((j-1)),(y)+((i-1)*room_height))==2 then
mset((x-1)+((j-1)),(y)+((i-1)*room_height),1)
end
-- delete everything
mset((x-1)+((j-1)),(y-1)+((i-1)*room_height),0)
end
end
-- vertical
local vertical_count = 0
if i==2 then
if grid[i-1][j]!=0 then vertical_count += 1 end
if grid[i+1][j]!=0 then vertical_count += 1 end
end
if grid[i][j] == 0 and i==2 and vertical_count==cell_height-1 then
for y=1,level_height-2 do
-- add wall tiles west
if mget((x-2)+((j-1)*room_width),(y-1)+(i-1))==2 then
mset((x-2)+((j-1)*room_width),(y-1)+(i-1),1)
end
-- add wall tiles east
if mget(x+((j-1)*room_width),(y-1)+(i-1))==2 then
mset(x+((j-1)*room_width),(y-1)+(i-1),1)
end
-- delete everything
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)),0)
end
end
end
end
end
function populate_tiles()
local difficulty_scale = flr(room / 5)
if difficulty_scale > 5 then
difficulty_scale = 5
end
level_difficulty = flr(rnd(6)) + difficulty_scale
-- level_difficulty = 1
if level_difficulty >= 8 then
add(log,{player_steps,"you get a bad feeling."})
end
entities = {}
-- every floor do
place_entity_randomly({1,"slime",32,0,0,12})
-- every floor randomly place
for i=1,flr(level_difficulty/2)+1+flr(rnd(2)) do
if rnd(1) > .75 then
place_entity_randomly({1,"slime",32,0,0,12})
else
place_entity_randomly({1,"rat",33,0,0,12})
end
end
-- specific enemies per floor
if level_difficulty <= 2 then
if rnd(1) > .75 then
place_entity_randomly({3,"snack",99,0,0})
end
end
if level_difficulty > 2 and level_difficulty <= 4 then
for j=1,3 do
if rnd(1) > .25 then
place_entity_randomly({1,"dancer",37,0,0,32})
end
end
for j=1,2 do
if rnd(1) > .25 then
place_entity_randomly({1,"flame",38,0,0,64,""})
end
end
elseif level_difficulty <= 6 then
for j=1,2 do
if rnd(1) > .25 then
place_entity_randomly({1,"flame",38,0,0,64,""})
end
end
for j=1,1 do
if rnd(1) > .5 then
place_entity_randomly({1,"homunc",39,0,0,48,0})
end
end
for j=1,2 do
if rnd(1) > .5 then
place_entity_randomly({1,"dancer",37,0,0,32})
end
end
else
for j=1,2 do
if rnd(1) > .25 then
place_entity_randomly({1,"flame",38,0,0,64,""})
end
end
for j=1,2 do
if rnd(1) > .5 then
place_entity_randomly({1,"homunc",39,0,0,48,0})
end
end
for j=1,2 do
if rnd(1) > .25 then
place_entity_randomly({1,"dancer",37,0,0,48})
end
end
end
-- lategame items
if level_difficulty >= 6 then
if rnd(1) > .9 then
place_entity_randomly({3,"gavel",117,0,0})
end
if rnd(1) > .9 then
place_entity_randomly({3,"jokki",114,0,0})
end
end
if level_difficulty >= 8 then
if rnd(1) > .8 then
place_entity_randomly({3,"sword+3",115,0,0})
end
if rnd(1) > .8 then
place_entity_randomly({3,"exomis",114,0,0})
end
for j=1,2 do
if rnd(1) > .5 then
place_entity_randomly({3,"snack",99,0,0})
end
end
if rnd(1) > .5 then
place_entity_randomly({3,"flask",101,0,0})
end
end
if room == 24 then
place_entity_randomly({1,"groza",40,0,0,56})
end
end
function get_tile_count(tile)
local count = 0
for x=1,level_width do
for y=1,level_height do
if mget(level_width-x,level_height-y) == tile then
count += 1
end
end
end
return count
end
function remove_tiles(tile)
for x=1,level_width do
for y=1,level_height do
if mget(x,y) == tile then
mset(x,y,0)
return false
end
end
end
end
function place_entity_randomly(entity)
-- places the entity randomly on the grid
-- give an entity dict, but set x and y to arbitrary values
local rnd_x = flr(rnd(level_width-1))+1
local rnd_y = flr(rnd(level_height-1))+1
while contains_impassible_tiles(mget(rnd_x, rnd_y), true) or entity_there(rnd_x*8, rnd_y*8, true) do
rnd_x = flr(rnd(level_width))
rnd_y = flr(rnd(level_height))
end
add(entities,{entity[1],entity[2],entity[3],rnd_x*8,rnd_y*8,entity[6]})
end
function place_tile_randomly(tile)
local rnd_x = flr(rnd(level_width-1))+1
local rnd_y = flr(rnd(level_height-1))+1
while mget(rnd_x, rnd_y) != 0 do
rnd_x = flr(rnd(level_width))
rnd_y = flr(rnd(level_height))
end
mset(rnd_x, rnd_y, tile)
end
function tile_rooms()
for x=1,level_width do
for y=1,level_height do
mset(x-1,y-1,2)
end
end
for i=1, #grid do
for j=1, #grid[i] do
-- get width
if grid[i][j] != 0 then
for y=1,room_height do
for x=1,room_width do
-- add wall tiles
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),1)
-- add inner tiles (to be fleshed out)
if x!=1 and x!=room_width and y!=1 and y!=room_height then
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),0)
-- make stairs
if grid[i][j] == 2 then
-- make stairs everywhere you can
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),15)
end
end
-- fix up neighbouring rooms
-- west and east
if x==1 and j>1 and grid[i][j-1]!=0 and y!=1 and y!=room_height and mget((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height)) != 15 then
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),0)
elseif x==room_width and j<#grid[i] and grid[i][j+1]!=0 and y!=1 and y!=room_height then
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),0)
end
-- north and south
if y==1 and i>1 and grid[i-1][j]!=0 and x!=1 and x!=room_width and mget((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height)) != 15 then
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),0)
elseif y==room_height and i<#grid and grid[i+1][j]!=0 and x!=1 and x!=room_width then
mset((x-1)+((j-1)*room_width),(y-1)+((i-1)*room_height),0)
end
end
end
end
end
end
end
function generate_level()
-- tweakable parameters
level_width = 12
level_height = 12
cell_width = 3
cell_height = 3
room_width = level_width / cell_width
room_height = level_height / cell_height
room_count = 6
-- generate grid based on cell width and height
grid = {}
for y=1,cell_height do
add(grid,{})
for x=1,cell_width do
add(grid[y],0)
end
end
-- generate tile grid for fov and decoration
level_grid = {}
for y=1,level_height do
add(level_grid,{})
for x=1,level_width do
add(level_grid[y],flr(rnd(2)))
end
end
-- dynamically add to the grid showing the rooms
local current_rooms = 1
-- add player's current room
local player_room_x = flr(player_x / room_width / 8) + 1
local player_room_y = flr(player_y / room_height / 8) + 1
grid[player_room_y][player_room_x] = 1
while current_rooms < room_count do
local y = flr(rnd(cell_height)) + 1
local x = flr(rnd(cell_width)) + 1
if grid[y][x] != 1 then
-- ...make it a room
grid[y][x] = 1
-- count the rooms to make sure it's not over the total count
current_rooms = 0
for rows in all(grid) do
for room in all(rows) do
if room != 0 then
current_rooms += 1
end
if current_rooms == room_count then
grid[y][x] = 2
end
end
end
end
end
tile_rooms()
dig_tunnels()
-- remove extra stairs
while get_tile_count(15) > 1 do
remove_tiles(15)
end
if get_tile_count(15) < 1 then
place_tile_randomly(15)
end
if rnd(1) > .75 then
place_tile_randomly(42)
end
if room == 24 then
-- final floor, remove exits
remove_tiles(15)
remove_tiles(42)
add(log,{player_steps,"this should be the place."})
end
populate_tiles()
room += 1
end
function point_is_in_grid(x,y,grid)
for e in all(grid) do
if e[1] == x and e[2] == y then
return e[3]
end
end
return false
end
function update_enemy_movement_grid()
enemy_movement_grid = {}
add(enemy_movement_grid,{player_x/8, player_y/8, 0})
for i=0,2 do
for e in all(enemy_movement_grid) do
if e[3] == i then
local xto = e[1]-1
local yto = e[2]
if not contains_impassible_tiles(mget(xto,yto),true) and not point_is_in_grid(xto,yto,enemy_movement_grid) then
add(enemy_movement_grid,{xto,yto,i+1})
end
local xto = e[1]+1
local yto = e[2]
if not contains_impassible_tiles(mget(xto,yto),true) and not point_is_in_grid(xto,yto,enemy_movement_grid) then
add(enemy_movement_grid,{xto,yto,i+1})
end
local xto = e[1]
local yto = e[2]-1
if not contains_impassible_tiles(mget(xto,yto),true) and not point_is_in_grid(xto,yto,enemy_movement_grid) then
add(enemy_movement_grid,{xto,yto,i+1})
end
local xto = e[1]
local yto = e[2]+1
if not contains_impassible_tiles(mget(xto,yto),true) and not point_is_in_grid(xto,yto,enemy_movement_grid) then
add(enemy_movement_grid,{xto,yto,i+1})
end
end
end
end
end
function debug_enemy_movement_grid()
-- for i in all(enemy_movement_grid) do
-- print(i[3],i[1]*8,i[2]*8,7)
-- end
end
function new_input()
local list_of_input = {btnp(0),btnp(1),btnp(2),btnp(3),btnp(4),btnp(5)}
return list_of_input
end
function update_fov()
fov_grid = level_grid
local player_grid_x = player_x / 8
local player_grid_y = player_y / 8
player_fov={
{player_grid_x-1,player_grid_y-2,12},
{player_grid_x,player_grid_y-2,12},
{player_grid_x+1,player_grid_y-2,12},
{player_grid_x-2,player_grid_y-1,12},
{player_grid_x-1,player_grid_y-1,10},
{player_grid_x,player_grid_y-1,10},
{player_grid_x+1,player_grid_y-1,10},
{player_grid_x+2,player_grid_y-1,12},
{player_grid_x-2,player_grid_y,12},
{player_grid_x-1,player_grid_y,10},
{player_grid_x,player_grid_y,10},
{player_grid_x+1,player_grid_y,10},
{player_grid_x+2,player_grid_y,12},
{player_grid_x-2,player_grid_y+1,12},
{player_grid_x-1,player_grid_y+1,10},
{player_grid_x,player_grid_y+1,10},
{player_grid_x+1,player_grid_y+1,10},
{player_grid_x+2,player_grid_y+1,12},
{player_grid_x-1,player_grid_y+2,12},
{player_grid_x,player_grid_y+2,12},
{player_grid_x+1,player_grid_y+2,12},
}
local tile_count=1
for tile in all(player_fov) do
if contains_impassible_tiles(mget(tile[1],tile[2]),false) or mget(tile[1],tile[2]) == 42 then
del(player_fov, tile)
end
tile_count+=1
end
end
function draw_fov()
update_fov()
for i in all(player_fov) do
if level_grid[i[1]] != nil and level_grid[i[2]] != nil then
spr(i[3]+level_grid[i[1]][i[2]],i[1]*8,i[2]*8)
else
spr(i[3],i[1]*8,i[2]*8)
end
end
debug_enemy_movement_grid()
end
function is_visible(x, y)
-- gets visibility by x and y
x = flr(x/8)
y = flr(y/8)
for i in all(player_fov) do
if i[1] == x and i[2] == y then
return true
end
end
-- return true -- debug only, todo: set to false
end
function map_draw()
if room == 0 and game_started then
mapdraw(0,0,0,0,128,148)
return
end
if not game_started then
mapdraw(16,0,0,0,128,148)
return
end
draw_fov()
-- custom control over what is lit
-- it's lit fam
for i=0,level_height do
for j=0,level_width do
if mget(i,j) == 15 and is_visible(i*8, j*8) then
-- mapdraw(i,j,i*8,j*8,1,1)
mapdraw(i-1,j-1,(i-1)*8,(j-1)*8,3,3)
elseif mget(i,j) != 15 and is_visible(i*8, j*8) then
mapdraw(i-1,j-1,(i-1)*8,(j-1)*8,3,3)
elseif mget(i,j) == 2 then
mapdraw(i,j,i*8,j*8,1,1)
end
end
end
end
function change_room(relative)
dpal={0,1,1, 2,1,13,13, 4,4,9,3, 13,1,13,14}
for i=0,40 do
for j=1,15 do
col = j
for k=1,((i+(j%5))/4) do
col=dpal[col]
end
pal(j,col,1)
end
for l=1,2 do
flip()
end
end
generate_level()
end
function game_draw()
print(log[count(log)-1][2],4,camera_y+107,1)
print(log[count(log)-1][2],4,camera_y+106,5)
print(log[count(log)][2],4,camera_y+115,1)
print(log[count(log)][2],4,camera_y+114,13)
spr(63,camera_x+98,camera_y+0)
print(player_health .. "/" .. player_max_health,camera_x+108,camera_y+1,7)
for i=1,6 do
spr(player_inventory[i][3],camera_x+98,camera_y+(i*8))
if player_inventory[i][2] != "" then
if i <= 3 then
print(player_inventory[i][2],camera_x+108,camera_y+1+(i*8),7)
else
print(player_inventory[i][2],camera_x+108,camera_y+1+(i*8),13)
end
else
print("none",camera_x+108,camera_y+1+(i*8),1)
end
end
print(player_effects[1],camera_x+100,camera_y+1+(7*8),1)
print(player_effects[2],camera_x+100,camera_y+1+(8*8),1)
print("flr " .. room,camera_x+100,camera_y+2+(9*8),1)
print("flr " .. room,camera_x+100,camera_y+1+(9*8),13)
print("str " .. player_stats.str,camera_x+100,camera_y+2+(10*8),1)
print("str " .. player_stats.str,camera_x+100,camera_y+1+(10*8),13)
print("def " .. player_stats.def,camera_x+100,camera_y+2+(11*8),1)
print("def " .. player_stats.def,camera_x+100,camera_y+1+(11*8),13)
-- print("poison",camera_x+100,camera_y+1+(11*8),3)
if show_menu then
menu()
end
if show_prompt then
if game_won == true then
win_prompt()
return
end
prompt()
end
if input[5] and not show_prompt then
show_menu = true
end
end
function menu()
if menu_selection == nil then
menu_selection = 0
end
if btnp(2) then
if menu_selection == 0 then menu_selection = 5 else menu_selection -= 1 end
end
if btnp(3) then
if menu_selection == 5 then menu_selection = 0 else menu_selection += 1 end
end
if input[5] then
input[5] = false
if player_inventory[menu_selection+1][2] != "" then
show_menu = false
show_prompt = true
end
end
if btnp(5) then
show_menu = false
show_prompt = false
end
rect(camera_x+98,camera_y+7+(8*menu_selection),camera_x+127,camera_y+15+(8*menu_selection),7)
end
function prompt()
rect(camera_x+16,camera_y+32,camera_x+111,camera_y+79,13)
rectfill(camera_x+17,camera_y+33,camera_x+110,camera_y+78,0)
line(camera_x+16,camera_y+80,camera_x+111,camera_y+80,1)
print("what do you want to",camera_x+24,camera_y+40,7)
print("do with the "..player_inventory[menu_selection+1][2].."?",camera_x+24,camera_y+48,7)
for i=1,5 do
if i <= 3 then
print(player_actions[i][1]..player_actions[i][2],camera_x+(24+(i-1)*28),camera_y+58,7)
else
print(player_actions[i][1]..player_actions[i][2],camera_x+(24+(i-4)*28),camera_y+66,7)
end
end
if btnp(0) then
for i=1,5 do
if player_actions[i][1] == cursor then
player_actions[i][1] = ""
if i==1 then
player_actions[5][1] = cursor
break
else
player_actions[i-1][1] = cursor
break
end
end
end
end
if btnp(1) then
for i=1,5 do
if player_actions[i][1] == cursor then
player_actions[i][1] = ""
if i==5 then
player_actions[1][1] = cursor
break
else
player_actions[i+1][1] = cursor
break
end
end
end
end
if input[5] then
for i=1,5 do
if player_actions[i][1] == cursor then
if i == 1 then
wear_selected_item()
show_prompt = false
end
if i == 2 then
throw_selected_item()
sfx(4)
show_prompt = false
end
if i == 3 then
eat_selected_item()
sfx(4)
show_prompt = false
end
if i == 4 then
drop_selected_item()
sfx(4)
show_prompt = false
end
if i == 5 then
show_menu = false
show_prompt = false
end
end
end
end
if btnp(5) then
show_menu = true
show_prompt = false
end
end
function win_prompt()
rect(camera_x+16,camera_y+32,camera_x+111,camera_y+79,13)
rectfill(camera_x+17,camera_y+33,camera_x+110,camera_y+78,0)
line(camera_x+16,camera_y+80,camera_x+111,camera_y+80,1)
print("congratulations, the",camera_x+24,camera_y+40,7)
print("challenge is over.",camera_x+24,camera_y+48,7)
print("steps taken: "..player_steps,camera_x+24,camera_y+58,7)
print("kills: "..player_kills,camera_x+24,camera_y+66,7)
end
function entity_there(x_check,y_check,count_player)
local result = false
for i=1,count(entities) do
if entities[i][4] == x_check and entities[i][5] == y_check then
if entities[i][1] != 2 and entities[i][1] != 3 then
result = true
end
else
if count_player or count_player == nil then
if player_x == x_check and player_y == y_check then
result = true
end
end
end
end
return result
end
function item_there(x_check,y_check)
for i=1,count(entities) do
if entities[i][4] == x_check and entities[i][5] == y_check and entities[i][1] == 3 then
return entities[i]
end
end
return false
end
function stash_item(item)
for i=4,6 do
if player_inventory[i][2] == "" then
add(log,{player_steps,"you stash the "..item[2].."."})
player_inventory[i][2] = item[2]
player_inventory[i][3] = item[3]
del(entities,item)
return
end
end
add(log,{player_steps,"no room in your pockets."})
end
function wear_selected_item()
local can_wear = false
for item in all(wearable) do
if item[1] == player_inventory[menu_selection+1][2] then
can_wear = true
if item[2] == menu_selection+1 then
add(log,{player_steps,"you've already equipped this."})
break
end
if player_inventory[item[2]][2] != "" then
add(log,{player_steps,"unequip your "..player_inventory[item[2]][2].." first."})
break
end
-- move it to the correct slot
player_inventory[item[2]][3] = player_inventory[menu_selection+1][3]
player_inventory[item[2]][2] = player_inventory[menu_selection+1][2]
player_inventory[menu_selection+1][3] = 62
player_inventory[menu_selection+1][2] = ""
add(log,{player_steps,"you equip the "..item[1].."."})
sfx(4)
-- apply stats and status effects
if item[3] != nil and item[3] != 0 then
player_stats.def = item[3]
if item[5] != nil and item[5] != "" then
player_effects[1] = item[5]
end
end
if item[4] != nil and item[4] != 0 then
player_stats.str = item[4]
if item[5] != nil and item[5] != "" then
player_effects[2] = item[5]
end
end
break
else
add(log,{player_steps,"you can't find a way to wear"})
add(log,{player_steps,"the "..player_inventory[menu_selection+1][2].."."})
end
end
end
function throw_selected_item()
local distance = -flr(-rnd(3)) + 1
local direction = flr(player_image/2) -- 0 = left, 1 = right, 2 = up, 3 = down
local to_x = 0
local to_y = 0
add(log,{player_steps,"you throw the "..player_inventory[menu_selection+1][2].."."})
for d=1,distance do
-- get player direction
local direction = flr(player_image/2) -- 0 = left, 1 = right, 2 = up, 3 = down
if direction <= 1 then
to_x = player_x + 8*d*sgn(direction-1)
to_y = player_y
to = mget(player_x/8 + d*sgn(direction-1), player_y/8)
else
to_x = player_x
to_y = player_y + 8*d*sgn(direction-3)
to = mget(player_x/8, player_y/8 + d*sgn(direction-3))
end
-- if contains entity then hit that entity
if entity_there(to_x,to_y,false) then
for e in all(entities) do
if e[4] == to_x and e[5] == to_y then
for item in all(breakable) do
if player_inventory[menu_selection+1][2] == item[1] and rnd(1) < item[4] then
add(log,{player_steps,"the "..player_inventory[menu_selection+1][2].." breaks!"})
add(entities,{
2,
"liquid",
74+flr(rnd(4)),
e[4],
e[5]
})
player_inventory[menu_selection+1][2] = ""
if menu_selection > 2 then
-- todo: debug this
player_inventory[menu_selection+1][3] = 62
end
return
elseif e[1] == 1 then
local roll = nil
for i in all(wearable) do
if i[1] == player_inventory[menu_selection+1][2] then
if i[4] != nil then
roll = flr(rnd(i[4]))
end
end
end
if roll == nil then
roll = flr(rnd(3))
end
add(log,{player_steps,"the "..player_inventory[menu_selection+1][2].." hits the "..e[2].." for "..roll})
add(log,{player_steps,"damage!"})
e[6] -= roll
if e[6] <= 0 then
entity_die(e)
d = d+1
end
end
end
end
end
distance = d-1
player_step(0,0)
break
end
-- if contains a wall, bounce off or break
if contains_impassible_tiles(to,true) then
add(log,{player_steps,"the "..player_inventory[menu_selection+1][2].." bounces and falls."})
distance = d-1
player_step(0,0)
break
end
end
if direction <= 1 then
add(entities,{
3,
player_inventory[menu_selection+1][2],
player_inventory[menu_selection+1][3],
player_x+(distance*8)*sgn(direction-1),
player_y}
)
else
add(entities,{
3,
player_inventory[menu_selection+1][2],
player_inventory[menu_selection+1][3],
player_x,
player_y+(distance*8)*sgn(direction-3)}
)
end
player_inventory[menu_selection+1][2] = ""
if menu_selection > 2 then
player_inventory[menu_selection+1][3] = 62
end
if menu_selection + 1 == 1 then
player_stats.str = 0
for e in all(entities) do
if player_effects[2] == "sticky" and e[7] != nil and e[7] == "stuck" then
del(e,e[7])
end
end
player_effects[2] = ""
end
if menu_selection + 1 == 2 then
player_stats.def = 0
player_effects[1] = ""
end
end
function eat_selected_item()
local can_eat = false
for i=1,count(edible) do
if player_inventory[menu_selection+1][2] == edible[i][1] then
can_eat = true
-- eat the cake, anime
player_heal(edible[i][1],edible[i][2],edible[i][5])
-- remove the item from inventory
player_inventory[menu_selection+1][2] = ""
if menu_selection > 2 then
player_inventory[menu_selection+1][3] = 62
end
end
end
if can_eat == false then
add(log,{player_steps,"eating this would be difficult."})
end
end
function drop_selected_item()
add(log,{player_steps,"you drop the "..player_inventory[menu_selection+1][2].."."})
add(entities,{
3,
player_inventory[menu_selection+1][2],
player_inventory[menu_selection+1][3],
player_x,
player_y})
player_inventory[menu_selection+1][2] = ""
-- update stats if it's an equipped item
if menu_selection + 1 == 1 then
player_stats.str = 0
for e in all(entities) do
if player_effects[2] == "sticky" and e[7] != nil and e[7] == "stuck" then
del(e,e[7])
end
end
player_effects[2] = ""
end
if menu_selection + 1 == 2 then
player_stats.def = 0
player_effects[1] = ""
end
if menu_selection > 2 then
player_inventory[menu_selection+1][3] = 62
end
end
function entity_draw()
for e in all(entities) do
if e[1] == 2 or e[1] == 3 then
if room == 0 and not entity_there(e[4],e[5],false) then
spr(e[3], e[4], e[5])
end
if not entity_there(e[4],e[5],false) and is_visible(e[4],e[5]) then
-- static objects (items and splatter)
spr(e[3], e[4], e[5])
end
else
-- living objects
if room == 0 then
spr(e[3]+((player_steps % 2)*16), e[4], e[5])
end
if is_visible(e[4],e[5]) then
spr(e[3]+((player_steps % 2)*16), e[4], e[5])
end
end
end
end
function entity_update()
for e in all(entities) do
-- movement patterns
if e[2] == "slime" then -- check if they're stuck or frozen or something
roll = rnd(2)-1
if roll >= 0.67 then
etox = (sgn(rnd(2)-1)*8)/8
etoy = 0
else
if roll <= -0.67 then
etox = 0
etoy = (sgn(rnd(2)-1)*8)/8
else
etox = 0
etoy = 0
end
end
end
if e[2] == "rat" then
roll = rnd(2)-1
if roll >= 0.2 then
etox = (sgn(rnd(2)-1)*8)/8
etoy = 0
else
if roll <= -0.2 then
etox = 0
etoy = (sgn(rnd(2)-1)*8)/8
else
etox = 0
etoy = 0
end
end
end
if e[2] == "dancer" then
-- have to reset etox and etoy otherwise they'll use the global values
-- use tiles for when you're detected, then follow the direction of the player
etox = 0
etoy = 0