-
Notifications
You must be signed in to change notification settings - Fork 1
/
picohell.p8
1614 lines (1498 loc) · 68.9 KB
/
picohell.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 18
__lua__
-- pico hell
-- by cpiod
-- this code is licensed gpl3 at https://github.com/pfgimenez/picohell
-- state:
-- 0: player turn
-- 1: player aim
-- 2: enemy turn
-- 100: title screen
-- 101: controls
-- 102: game over screen
-- flag: 0: non-walkable
-- 1: bullet-opaque
-- sfx: 0: pistol
-- 1: shotgun
-- 2: rifle
-- 3: explosion
-- 4: player hurt
-- 5: get ammo
-- 6: reload
-- 7: medkit used
-- 8: error
-- 9: level up
function _init()
level=0
color_mode=1
-- poke(0x5f2d, 1) -- mouse debug
unset_unseen_color()
-- menuitem(2,"show controls",show_ctrl)
o_pressed=nil
visibility_radius=5
-- light direction
light_x,light_y=1,1
-- player sprite direction
facing=1
maxhp=100
maxarm=100
warn_low=false
mesgs={}
cam_x=0
cam_y=0
cam_xc=0
cam_yc=0
cam_dx=0
cam_dy=0
title_cam_y=0
state=100
wait=0
anim=false
xp_goal={10,50,200,500}
player={hp=maxhp,ent=9,deltatime=rnd(),wpn=make_weapon(3),arm=0,lvl=1,xp=0,def=0}
ammo={12,0,0}
max_ammo={200,50}
end
function restart()
printh("reload!")
reload()
_init()
end
function show_ctrl()
title_cam_y=128
old_state=state
state=101
end
-->8
-- draw
function _draw()
if(state<100) _draw_game()
if(state==100) _draw_title()
if(state==102) _draw_gameover()
if state==101 then
title_cam_y+=1
_draw_title()
if title_cam_y>=128 then
state=1
show_ctrl()
end
end
end
pentacle={{"o",0,0,47},
{"o",0,0,39},
{"l",38,-8,-24,31},
{"l",-24,31,3,-39},
{"l",3,-39,20,32},
{"l",20,32,-34,-16},
{"l",-34,-16,38,-8},
{"o",38,-8,8},
{"o",3,-39,4},
{"o",-34,-16,6},
{"o",-24,31,4},
{"o",20,32,6},
{"l",35,-17,10,-11},
{"l",35,-17,26,-9},
{"o",35,-17,3},
{"o",110-64,55-64,1},
{"l",34,19,-36,16},
{"o",-36,16,3},
{"p",-36,16},
{"l",34,19,19,5},
{"o",34,19,5},
{"p",35,-17},
{"l",-39,-2,-47,-3},
{"l",-39,-2,-22,-5},
{"l",-39,-2,-14,6},
{"o",-39,-2,2},
{"o",-47,-3,2},
{"l",-25,-30,-30,-36},
{"o",-30,-36,3},
{"p",-30,-36},
{"l",-25,-30,-18,-14},
{"l",-25,-30,7,-4},
{"o",7,-4,2},
{"l",-15,-22,-12,-30},
{"o",-15,-22,3},
{"o",-12,-30,3},
{"l",12,45,10,38},
{"o",12,45,2},
{"l",10,38,1,17},
{"l",10,38,10,23},
{"o",1,17,2},
{"o",-27,-38,1},
{"l",12,-45,11,-37},
{"o",12,-45,2},
{"l",11,-37,5,-30},
{"l",11,-37,8,-17}
}
function draw_pentacle(c)
for v in all(pentacle) do
-- local c=2
if v[1]=="l" then
local x,y=rotate(v[2],v[3])
local x2,y2=rotate(v[4],v[5])
if(v[6]!=nil) c=v[6]
line(x,y,x2,y2,c)
elseif v[1]=="o" then
local x,y=rotate(v[2],v[3])
circfill(x,y,v[4]*mul,0)
circ(x,y,v[4]*mul,c)
elseif v[1]=="p" then
local x,y=rotate(v[2],v[3])
pset(x,y)
end
end
end
function rotate(x,y)
return mul*(x*cosa-y*sina)+x0,mul*(x*sina+y*cosa)+y0
end
function _draw_gameover()
cls()
anim=false
print("oups...")
end
function _draw_title()
cls()
camera()
local a=t()/300
--local a=0
cosa,sina=cos(a),sin(a)
mul=min(t()/50+0.8,1.5)
x0=64
y0=64
draw_pentacle(9)
camera(0, title_cam_y)
--spr(64,40,45,7,4)
pal(8,2)
for i=0,1 do
t1x=13+1-i
ty=55+1-i
-- p
sspr(0,33,10,14,t1x,ty)
-- i
sspr(10,33,3,14,t1x+13,ty)
-- c
sspr(10,33,10,14,t1x+19,ty)
-- o
sspr(20,33,10,14,t1x+32,ty)
-- h
sspr(0,49,10,14,t1x+49,ty)
-- e
sspr(10,49,10,14,t1x+62,ty)
-- l
sspr(20,49,10,14,t1x+75,ty)
-- l
sspr(20,49,10,14,t1x+88,ty)
pal(8,8)
end
print_center("a jupiter hell demake by cpiod",15*8+1,6,0)
local y,d=168,7
print_center("press 🅾️ to start",80,7,1)
print_center("controls",y-2*d,7,0)
print_center("press ⬅️⬆️⬇️➡️ to move",y,6,4)
print_center("press 🅾️ to shoot",y+2*d,6,1)
print_center("hold 🅾️ to aim",y+3*d,6,1)
print_center("press ❎ to reload",y+5*d,6,1)
print_center("hold ❎ to pick up",y+6*d,6,1)
print_center("kill all the demons!",y+9*d,8,0)
--print((stat(32)-64).." "..(stat(33)-64),0,0,11)
--pset(stat(32),stat(33),11)
end
function print_center(s,y,c,d)
-- d because there are "double" symbols (such as ❎)
local x=64-(#s+d)*2
rectfill(x-1,y-1,x+(#s+d)*4-1,y+5,0)
print(s,x,y,c)
end
-- x0,y0,mul,a0
pentacles_pos={{64,64,1.5,0}}
--{rnd()*128,rnd()*128,rnd(0.5)+0.5,rnd()}}
function _draw_game()
printh("draw")
cls()
camera()
-- clip(0,8,128,112)
for p in all(pentacles_pos) do
local a=t()/300+p[4]
cosa,sina=cos(a),sin(a)
mul=p[3]
x0=p[1]
y0=p[2]
draw_pentacle(2)
end
camera(cam_x+cam_dx+player.ox-64,cam_y+cam_dy+player.oy-64)
-- unseen map
set_unseen_color()
local x,y=ceil((cam_x+cam_dx+player.ox-72)/8),ceil((cam_y+cam_dy+player.oy-72)/8)
map(x,y,8*x,8*y,min(32-x,17),min(32-y,17))
unset_unseen_color()
-- camera()
-- print(stat(0).."kb "..(stat(1)*100).."%",1,20,9)
-- camera(cam_x+cam_dx+player.ox-64,cam_y+cam_dy+player.oy-64)
for x=max(0,player.x-visibility_radius),min(player.x+visibility_radius,31) do
for y=max(0,player.y-visibility_radius),min(player.y+visibility_radius,31) do
if(dist(player.x,player.y,x,y)<=visibility_radius and is_seen({x=x,y=y})) map(x,y,8*x,8*y,1,1)
end
end
-- for d in all(soot) do
-- if(is_seen(d)) set_color(d) spr(d.sprnb,8*d.x,8*d.y-2)
-- end
for d in all(decor) do
if(is_seen(d)) set_color(d) spr(d.sprnb,8*d.x,8*d.y,1,1,d.mh,d.mv)
end
for e in all(entities) do --shadow
if(is_seen(e)) set_color(e) spr(2,e.ox,e.oy,1,1,e.x<=player.x)
end
for e in all(floor_weapons) do
if(is_seen(e)) set_color(e) spr(e.sprnb,8*e.x,8*e.y-2)
end
for e in all(entities) do
if(is_seen(e)) set_color(e) spr(e.sprnb+get_sprite_delta(e),e.ox,e.oy-2,1,1,e.x<=player.x)
end
for b in all(barrels) do
if(is_seen(b)) set_color(b) spr(b.sprnb,8*b.x,8*b.y-2)
end
unset_unseen_color()
-- player
d=get_sprite_delta(player)
local s=32
spr(s+d,player.ox,player.oy-2,1,1,facing>0)
-- aim
if state==1 then
local s=17
-- aim on player
if(a_x==player.x and a_y==player.y) s=19
-- no los
if(not is_visible(a_x,a_y,true)) s=19
spr(s,a_x*8,a_y*8-2)
end
animate()
-- clip()
camera()
-- health bar
-- rectfill(2,2,39,16,1)
-- rect(2,2,39,16,6)
local c=player.hp>20 and 9 or 8
print("♥"..lpad(tostr(player.hp),3).."/"..maxhp,3,4,c)
print("웃"..lpad(tostr(player.arm),3).."/"..maxarm,3,10,9)
-- rectfill(93,2,80+43,16,0)
-- rect(93,2,80+43,16,6)
print("level "..player.lvl,95,4,9)
print("depth "..level,95,10,9)
local s=weapon_name[player.wpn.typ].." "
..player.wpn.amm
.."/"..player.wpn.mag
.." ("..ammo[player.wpn.ammtyp]..")"
local x=64-#s*2
local y=15*8+1
-- rectfill(x-2,y-2,x+#s*4,y+6,0)
-- rect(x-2,y-2,x+#s*4,y+6,6)
print(s,x,y,9)
print((stat(1)*100).."%",1,17,3)
print(stat(7).."fps",1,23,3)
end
function lpad(s,l)
if(#s<l) return lpad(" "..s,l)
return s
end
function animate()
anim=false
cam_dx,cam_dy=0,0
for bul in all(bullets) do
anim=draw_bullets(bul) or anim
end
for e in all(explosion) do
anim=explode(e) or anim
end
anim=animate_camera() or anim
for e in all(entities) do
anim=animate_ent(e) or anim
end
anim=animate_ent(player) or anim
for tim in all(medkits_used) do
animate_medkit(tim) -- non-blocking
end
for e in all(blood) do
anim_blood(e) -- non-blocking
end
for e in all(mesgs) do
print_float(e)
end
if(wait>0) wait-=1 anim=true
end
function animate_medkit(med)
if t()-med.tim<=0.25 then
r=300/4-(t()-med.tim)*300
for i=r,r+med.lvl/25 do
circ(player.ox+4,player.oy+2,i,med.col)
end
return true
end
del(medkit_used,med)
return false
end
function animate_camera()
if(cam_x<cam_xc) cam_x+=ceil((cam_xc-cam_x)/8)
if(cam_x>cam_xc) cam_x-=ceil((cam_x-cam_xc)/8)
if(cam_y<cam_yc) cam_y+=ceil((cam_yc-cam_y)/8)
if(cam_y>cam_yc) cam_y-=ceil((cam_y-cam_yc)/8)
if(cam_x!=cam_xc or cam_y!=cam_yc) printh("animate camera")
return cam_x!=cam_xc or cam_y!=cam_yc
end
function animate_ent(e)
local x=e.x*8
local y=e.y*8
local d=(e==player) and 2 or 1
if e.ox!=x or e.oy!=y then
if(x-1>e.ox) e.ox+=d
if(x+1<e.ox) e.ox-=d
if(y-1>e.oy) e.oy+=d
if(y+1<e.oy) e.oy-=d
if(abs(x-e.ox)==1) e.ox=x
if(abs(y-e.oy)==1) e.oy=y
printh("animate ent")
return true
end
return false
end
function draw_bullets(bul)
for b in all(bul[1]) do
printh("animate bullets")
if b.delay>0 then
b.delay-=1
else
if b.bulspr then
spr(b.bulspr,b.x0-4,b.y0-4,1,1,flr(shr(b.dur,1)%2)==0,flr(shr(b.dur,2)%2)==0)
else
pset(b.x0,b.y0,6)
end
b.x0+=b.vx
b.y0+=b.vy
b.dur-=1
if(b.dur<=0) del(bul[1],b)
end
end
if #bul[1]==0 then
for param in all(bul[2]) do
damage(param[1],param[2])
end
del(bullets,bul)
end
return #bullets>0
end
function get_sprite_delta(e)
local d=0
local t=t()--+e.deltatime
if 8*e.x==e.ox and 8*e.y==e.oy then
-- still
if(t%(1.4)>0.7) d=1
else
-- moving
d=2
if(flr(t*10)%2==0) d=3
end
return d
end
function anim_blood(e)
if e[5]<t() then
del(blood,e)
return false
else
e[4]+=0.1
e[1]+=e[3]
e[2]+=e[4]
pset(e[1],e[2],2)
return true
end
end
function explode(ex)
cam_dx=rnd(6)-3
cam_dy=rnd(6)-3
for e in all(ex[1]) do
circfill(e.x,e.y,min(e.rad,100*(t()-e.t)),8)
circfill(e.x,e.y,min(e.rad,100*(t()-e.t-0.1)),9)
circfill(e.x,e.y,min(e.rad,100*(t()-e.t-0.3)),0)
if 100*(t()-e.t-0.3)>e.rad then
del(ex[1],e)
end
end
if #ex[1]==0 then
for param in all(ex[2]) do
damage(param[1],param[2])
end
del(explosion,ex)
return false
end
printh("animate explosion")
return true
end
function set_unseen_color()
if color_mode==0 then
for i=0,1 do
pal(i,0)
end
for i=2,15 do
pal(i,5)
end
color_mode=1
end
end
function unset_unseen_color()
if color_mode==1 then
pal()
palt(0, false)
palt(14, true)
color_mode=0
end
end
function add_msg(msg,c,c2,x,y,v)
if(c==nil) c=7
if(x==nil) x=player.ox
if(y==nil) y=player.oy
if(v==nil) v=0.5
if(c2==nil) c2=1
add(mesgs,{msg,16,c,x,y,v,c2})
end
function print_float(msg)
if msg[2]<=0 then
del(mesgs,msg)
else
local x,y=msg[4],msg[5]
msg[2]-=msg[6]
for i=1,0,-1 do
for j=1,0,-1 do
local c=msg[7]
if(i+j==0) c=msg[3]
print(msg[1],x+4-#msg[1]*2+i,y-10+msg[2]/2+j,c)
end
end
end
end
function set_color(e)
if e.vis then
unset_unseen_color()
else
set_unseen_color()
end
end
-->8
-- entity
-- entity type:
-- 0: weapon
-- 1: enemy
-- 2: barrel
-- 3: decoration
-- 4: wall
-- 5: ammo
-- 6: medkit
-- 7: armor
-- 9: player
-- weapons type:
-- 0: pistol
-- 1: shotgun
-- 2: rifle
ammo_name={"bullets","shells"}
weapon_name={"pistol","combat shotgun","assault rifle"}
-- weapon struct:
-- x,y: position (if on floor)
-- amm: current ammo in magazine
-- mag: magazine size
-- ammtyp: ammo type
-- bul: bullet per shot
-- used: ammo per shot
-- rng: max range
-- dmg: damage
-- disp: dispersion
-- sprnb: sprite number
-- delay: delay between to bullets in the same attack
function make_weapon(typ)
if(typ==1) return {typ=1,ammtyp=1,mag=6,amm=6,bul=1,rng=5,dmg=3,disp=1,ent=0,sprnb=71,used=1,maxrng=9,delay=0,drp=true}
if(typ==2) return {typ=2,ammtyp=2,mag=1,amm=1,bul=5,rng=3,dmg=5,disp=5,ent=0,sprnb=72,used=1,maxrng=4,delay=0,drp=true}
if(typ==3) return {typ=3,ammtyp=1,mag=24,amm=24,bul=4,rng=5,dmg=3,disp=2,ent=0,sprnb=73,used=4,maxrng=9,delay=3,drp=true}
if(typ==4) return {typ=4,ammtyp=0,mag=100,amm=100,bul=1,rng=5,dmg=10,disp=1,ent=0,used=1,maxrng=9,delay=0,bulspr=10}
assert(false,typ)
end
function make_floor_weapon(x,y,typ)
w=make_weapon(typ-70)
w.x=x
w.y=y
w.vis=is_visible(x,y)
add(floor_weapons,w)
end
function make_floor_ammo(x,y,typ)
typ-=73
add(floor_weapons,make_ammo(x,y,typ))
end
function make_ammo(x,y,typ)
if(typ==1) return {typ=1,ammo=12,sprnb=74,ent=5,x=x,y=y,vis=is_visible(x,y)}
if(typ==2) return {typ=2,ammo=4,sprnb=75,ent=5,x=x,y=y,vis=is_visible(x,y)}
end
-- enemy struct:
-- sprnb: sprite number
-- x,y: pos
-- facing: sprite direction
-- ox,oy: temporary pos (for anim)
-- hp: health point
-- wpn: weapon struct
-- rng: preferred range
function make_enemy(x,y,typ)
typ-=12
typ/=16
if(typ==0) add(entities,{facing=1,sprnb=12+16*typ,x=x,y=y,ox=8*x,oy=8*y,hp=10,wpn=make_weapon(1),ent=1,rng=3,deltatime=rnd(),xp=3})
if(typ==1) add(entities,{facing=1,sprnb=12+16*typ,x=x,y=y,ox=8*x,oy=8*y,hp=10,wpn=make_weapon(2),ent=1,rng=3,deltatime=rnd(),xp=20})
if(typ==2) add(entities,{facing=1,sprnb=12+16*typ,x=x,y=y,ox=8*x,oy=8*y,hp=10,wpn=make_weapon(4),ent=1,rng=3,deltatime=rnd(),xp=50})
end
-- barrel struct:
-- x,y: pos
-- dmg: damage
function make_barrel(x,y)
add(barrels,{x=x,y=y,hp=1,dmg=50,ent=2,sprnb=8+flr(rnd(2))})
end
-- x,y: pos
-- hp: hp gain
function make_medkit(x,y,n)
if(n==103) add(floor_weapons,{x=x,y=y,hp=50,sprnb=n,ent=6})
if(n==104) add(floor_weapons,{x=x,y=y,hp=5,sprnb=n,ent=6})
end
-- x,y: pos
-- arm: arm gain
function make_armor(x,y,n)
if(n==3) add(floor_weapons,{x=x,y=y,sprnb=n,ent=7,arm=50})
if(n==4) add(floor_weapons,{x=x,y=y,sprnb=n,ent=7,arm=5})
end
function make_blood(x,y,n)
add(decor,{x=x,y=y,sprnb=n,ent=3,mv=rnd()<0.5,mh=rnd()<0.5,vis=is_visible(x,y)})
end
-- decorative struct:
-- x,y: pos
-- sprnb: sprite number
function add_blood(x,y)
local found=false
for e in all(decor) do
if(e.x==x and e.y==y) e.sprnb=min(90,e.sprnb+1) found=true break
end
if not found then
make_blood(x,y,88)
end
end
function add_soot(x,y)
add(soot,{x=x,y=y,sprnb=24+flr(rnd(4)),ent=3})
end
function add_enemy(i,j,n)
local typ
if(n==12) typ=0
if(n==28) typ=1
if(n==44) typ=2
add(entities,make_enemy(typ,i,j))
end
-->8
-- gameloop
dep={{-1,0},{1,0},nil,{0,-1},
nil,nil,nil,{0,1}}
function _update60()
-- no update during animation
--printh("anim="..tostr(anim))
printh("new frame")
if state==100 then
if(btnp()!=0) state=101 return
elseif state==101 then
if btnp()!=0 then
make_level()
state=0
return
end
elseif state==102 then
if(btnp()!=0) restart() return
end
-- check movment (except during aim)
if(state!=1 and not depl) depl=dep[btnp()]
if(anim) return
-- player turn
if state==0 then
if not warn_low and player.hp<20 then
add_msg("low hp!",8,9)
warn_low=true
end
player_turn()
-- player aim
elseif state==1 then
player_aim()
-- enemy turn
elseif state==2 then
enemy_turn()
end
end
function player_move(d)
local next_x=player.x+d[1]
local next_y=player.y+d[2]
update_facing(d[1],d[2])
-- check collision
if can_go(next_x,next_y) then
-- update coordinates
player.x=next_x
player.y=next_y
state=2 -- end of turn
-- update camera
cam_xc=d[1]*16
cam_yc=d[2]*16
-- pick up medkits
use_medkit()
-- pick up armor
pickup_armor()
-- pick up ammo
pickup_ammo()
update_seen()
update_visibility()
if mget(player.x,player.y)==5 then
--stairs
make_level()
state=0
return
end
else
-- bump animation
player.ox+=4*d[1]
player.oy+=4*d[2]
end
end
function pickup_armor()
for e in all(floor_weapons) do
if e.ent==7 and e.x==player.x and e.y==player.y and player.arm<maxarm then
del(floor_weapons,e)
add(medkits_used,{col=11,lvl=e.arm,tim=t()})
sfx(5)
local old=player.arm
player.arm=min(maxarm,player.arm+e.arm)
add_msg("+"..tostr(player.arm-old).." armor",11,3)
end
end
end
function pickup_ammo()
local a=get_floor_weapon(player.x,player.y)
if a and a.ent==5 and ammo[a.typ]<max_ammo[a.typ] then
sfx(5)
del(floor_weapons,a)
local old=ammo[a.typ]
ammo[a.typ]=min(max_ammo[a.typ],ammo[a.typ]+a.ammo)
add_msg("+"..tostr(ammo[a.typ]-old).." "..ammo_name[a.typ])
end
end
function use_medkit()
for e in all(floor_weapons) do
if e.ent==6 and e.x==player.x and e.y==player.y and player.hp<maxhp then
del(floor_weapons,e)
add(medkits_used,{col=12,lvl=e.hp,tim=t()})
sfx(7)
local old=player.hp
player.hp=min(maxhp,player.hp+e.hp)
add_msg("+"..tostr(player.hp-old).." hp",12,1)
if(player.hp>=20) warn_low=false
end
end
end
function player_start_aim()
if player.wpn.amm<player.wpn.used then
-- no ammo !
add_msg("reload!")
sfx(8)
else
state=1 -- start aim
local e=closest_enemy(player.x,player.y)
if(e==nil or dist(player.x,player.y,e.x,e.y)>10) e=player
a_x,a_y=e.x,e.y
update_facing(a_x-player.x,a_y-player.y)
end
end
function player_turn()
local d=depl or dep[btnp()]
depl=nil
-- move
if d!=nil then
player_move(d)
-- start aim
elseif btnp(❎) then
player_start_aim()
elseif btnp(🅾️) and not o_pressed then
o_pressed=t()
elseif o_pressed and t()-o_pressed>0.4 then
local w=player.wpn
local w2=get_floor_weapon(player.x,player.y)
if w2==nil or w2.ent!=0 then
add_msg("no weapon!")
else
w.x=player.x
w.y=player.y
add(floor_weapons,w)
player.wpn=w2
del(floor_weapons,w2)
w3=get_floor_weapon(player.x,player.y)
add_msg("got "..weapon_name[player.wpn.typ])
end
o_pressed=nil
elseif o_pressed and not btn(🅾️) then
-- reload
local w=player.wpn
if w.mag!=w.amm and ammo[player.wpn.ammtyp]>0 then
local amount=min(ammo[player.wpn.ammtyp],w.mag-w.amm)
ammo[player.wpn.ammtyp]-=amount
w.amm+=amount
pickup_ammo()
state=2 -- end of turn
wait=15
sfx(6)
elseif w.mag==w.amm then
add_msg("full!")
sfx(8)
else
add_msg("no ammo!")
sfx(8)
end
o_pressed=nil
end
end
function player_aim()
local d=dep[band(btnp(),15)]
-- aim
if d!=nil then
a_x+=d[1]
a_y+=d[2]
-- orient lamp according to aim
update_facing(a_x-player.x,a_y-player.y)
end
-- no more hold
if not btn(❎) then
-- no self-shot
if a_x==player.x and a_y==player.y then
add_msg("no target")
sfx(8)
state=0
else
-- successful shot
local w=player.wpn
w.amm-=w.used
shoot(player.x,player.y,a_x,a_y,player.wpn)
state=2 -- end of turn
sfx(player.wpn.typ-1)
end
end
end
function enemy_turn()
-- printh("enemy turn")
state=0-- end turn
for e in all(entities) do
local moved=false
local d=dist(player.x,player.y,e.x,e.y)
if not e.vis then
-- do nothing if player not seen
moved=true
elseif d>e.rng then
moved=enemy_move(e)
end
if not moved and d<=e.rng+3 then
if e.wpn.amm>=e.wpn.used then
e.wpn.amm-=e.wpn.used
shoot(e.x,e.y,player.x,player.y,e.wpn) sfx(e.wpn.typ-1)
else
-- reload (infinite ammo)
e.wpn.amm=e.wpn.mag
end
end
end
end
function enemy_move(e)
local dx,dy
if player.x>e.x then
dx=1
elseif player.x<e.x then
dx=-1
end
if player.y>e.y then
dy=1
elseif player.y<e.y then
dy=-1
end
local next_x,next_y=e.x,e.y
if dx!=nil and abs(player.x-e.x)>=d/2 and can_go(e.x+dx,e.y) then
next_x+=dx
elseif dy!=nil and abs(player.y-e.y)>=d/2 and can_go(e.x,e.y+dy) then
next_y+=dy
end
if can_go(next_x,next_y) then
e.x=next_x
e.y=next_y
return true
end
return false
end
function euc_dist(x1,y1,x2,y2)
printh("euc_dist")
return sqrt((x2-x1)^2+(y2-y1)^2)
end
-- shoot from x1,y1 to x2,y2
function shoot(x1,y1,x2,y2,w)
local d=euc_dist(x1,y1,x2,y2)
cosa=(x2-x1)/d
sina=(y2-y1)/d
x2=flr(x1+cosa*w.maxrng+0.5)
y2=flr(y1+sina*w.maxrng+0.5)
for i=0,w.bul-1 do
local dmg=w.dmg
if(w.rng<d) dmg=ceil(dmg/3)
local dx,dy=0,0
if(rnd(w.disp*w.maxrng)>7) dx=1
if(rnd()>0.5) dx*=-1
if(rnd(w.disp*w.maxrng)>7) dy=1
if(rnd()>0.5) dy*=-1
x3=x2+dx
y3=y2+dy
local b={{},{}}
e=los_line(x1,y1,x3,y3,chk_ent_and_wall,false)
if e then
x3=e.x
y3=e.y
add(b[2],{e,dmg})
end
local speed=5
x3+=(rnd(6)-3)/8
y3+=(rnd(6)-3)/8
local d=sqrt((x3-x1)^2+(y3-y1)^2)
local vx=speed*(x3-x1)/d
local vy=speed*(y3-y1)/d
add(b[1],{x0=8*x1+4,y0=8*y1+4,vx=vx,vy=vy,dur=d*8/speed,delay=w.delay*i,bulspr=w.bulspr})
add(bullets,b)
end
end
function damage(e,dmg)
-- can't die (wall) or already dead
if(not e.hp or e.hp<=0) return
if(e==player) dmg=max(1,dmg-player.def)
if e.ent==1 or e==player then
add_msg("-"..tostr(dmg).." hp",9,1,e.ox,e.oy,1)
add_blood(e.x,e.y)
end
if e.arm then -- armor
local arm_dmg=min(e.arm,dmg)
e.arm-=arm_dmg
dmg-=arm_dmg
end
e.hp-=dmg
-- may be saved by medkit or armor
if e==player then
if(dmg>20) cls(8) -- red flash when serious damage
use_medkit()
pickup_armor()
end
if e.hp<=0 then
-- if dead, project blood
if e==player or e.ent==1 then
for i=1,5 do
local v=0.5+rnd(0.5)
local a=rnd(0.5)
add(blood,{e.ox+4+rnd(2)-1,e.oy+rnd(2)-1,
cos(a)*v,sin(a)*v,t()+0.3+rnd(0.1)})
end
end
if e==player then
state=102 --game over
-- wait=120
elseif(e.ent==1) then
add_xp(e.xp)
del(entities,e)
if e.wpn.drp then
local x,y=get_empty_tile(e.x,e.y)
e.wpn.x=x
e.wpn.y=y
e.wpn.vis=is_visible(x,y)
add(floor_weapons,e.wpn)
x,y=get_empty_tile(e.x,e.y)
add(floor_weapons,make_ammo(x,y,e.wpn.ammtyp))
end
elseif e.ent==2 then -- barrel
local ex={{},{}}
add(explosion,ex)
sfx(3)
for i=1,8 do
add(ex[1],{x=8*e.x+rnd(16)-8,y=8*e.y+rnd(16)-8,rad=5+rnd(20),t=t()+rnd(0.3)})
end
del(barrels,e)
for i=-3,3 do
for j=-3,3 do
local dmg=flr(e.dmg/(abs(i)+abs(j)+1))
local x3,y3=e.x+i,e.y+j
if(in_map(x3,y3) and not chk_wall(x3,y3) and rnd()>0.4) add_soot(x3,y3)
e2=chk_ent(x3,y3)
if(e2) add(ex[2],{e2,dmg})--damage(e2,dmg)
end