- [WIP] Batman AI

- [WIP] EL_ALIEN AI
- Multi-camera
- Random starting places
This commit is contained in:
2022-10-27 14:59:47 +02:00
parent 6ee51f42a5
commit a710317c0a
6 changed files with 288 additions and 21 deletions

248
batman.lua Normal file
View File

@@ -0,0 +1,248 @@
batman={hab=5,x=24,y=24,flip=true,goup=true,frame=8,stairscooldown=0,stepscooldown=0,wait=0,step=0,anim={24,25,24,26},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}
function batman.init()
batman.hit=batman.hit
batman.update=batman.update_normal
batman.draw=batman.draw
local habs={5,46,36,39}
batman.hab=habs[1+rnd(4)]
end
function batman.draw()
local flip=batman.flip
if batman.update==batman.update_stairs then
flip=(((batman.x>>1)+(batman.y>>1))%2)==0
end
sspr((batman.frame&7)*16,(batman.frame>>3)*16,16,16,batman.x,batman.y,16,16,flip)
end
function batman.hit()
end
function batman.update_normal()
batman.wait=batman.wait+1
if batman.wait>=6 then
batman.wait=0
if batman.stairscooldown>0 then batman.stairscooldown=batman.stairscooldown-1 end
batman.step=(batman.step+1)%4
if batman.stepscooldown>0 then batman.stepscooldown=batman.stepscooldown-1 end
batman.frame=batman.anim[batman.step+1]
local inc=12 if batman.flip then inc=2 end
if not batman.flip and batman.x==84 then
batman.hab=batman.hab+1
batman.x=-4
elseif check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.nonpc then
--if rnd(10)<8 then
batman.update=batman.update_jumping
batman.step=0
--else
-- batman.flip=not batman.flip
--end
elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
if batman.flip then
batman.x=batman.x-2
else
batman.x=batman.x+2
end
if batman.x<-4 then
batman.hab=batman.hab-1
batman.x=84
end
elseif check_tile(batman.hab,batman.x+inc,batman.y+6)<tiletype.half then
local r=rnd(2)
if check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.block then r=0 end
if batman.stepscooldown>0 and check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.half then r=1 end
if r==0 then
batman.update=batman.update_jumping
batman.step=0
else
batman.stepscooldown=4
if batman.flip then
batman.x=batman.x-2
else
batman.x=batman.x+2
end
if batman.x<-4 then
batman.hab=batman.hab-1
batman.x=84
end
end
else
--if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.void and ((batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.void) then
--batman.update=batman.update_falling
--else
batman.flip=not batman.flip
--end
end
if batman.stairscooldown==0 and
check_tile(batman.hab,batman.x+6,batman.y+8)==tiletype.stair and
check_tile(batman.hab,batman.x+14,batman.y+8)==tiletype.stair then
if rnd(10)<8 then
batman.update=batman.update_stairs
if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.block then
batman.goup=true
else
batman.goup=false
end
--batman.goup=rnd(2)==0 and true or false
else
batman.stairscooldown=50
end
elseif batman.update==batman.update_stairs then
batman.stairscooldown=50
batman.update=batman.update_normal
batman.frame=24
end
if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.void and ((batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.void) then
local option=rnd(2)
if batman.stairscooldown>0 then option=0 end
if option==0 then
batman.update=batman.update_falling
else
batman.update=batman.update_jumping
batman.step=0
batman.wait=0
--else
-- batman.flip=not batman.flip
end
end
if batman.hab==abad.hab then
if aabb(abad,batman) then
if not batman.scene_intro then
start_scene(scenes.batman_intro)
batman.scene_intro=true
elseif not batman.scene_object and abad.objects.gorro~=nil then
start_scene(scenes.batman_gorro)
batman.scene_object=true
end
end
end
end
end
function batman.update_jumping()
batman.frame=25
batman.wait=batman.wait+1
if batman.wait>=6 then
batman.wait=0
local inc=12 if batman.flip then inc=2 end
if not batman.flip and batman.x==84 then
batman.hab=batman.hab+1
batman.x=-4
elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
if batman.flip then
batman.x=batman.x-2
else
batman.x=batman.x+2
end
if batman.x<-4 then
batman.hab=batman.hab-1
batman.x=84
end
end
if batman.step<6 then
if batman.y>0 then
if check_tile(batman.hab,batman.x+4,batman.y-2)~=tiletype.block then
if (batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y-2)~=tiletype.block then
batman.y=batman.y-2
end
end
else
batman.hab=batman.hab-10
batman.y=32
end
elseif batman.step>6 then
batman.update=batman.update_falling
end
batman.step=batman.step+1
end
end
function batman.update_falling()
batman.frame=25
batman.wait=batman.wait+1
if batman.wait>=6 then
batman.wait=0
local inc=12 if batman.flip then inc=2 end
if not batman.flip and batman.x==84 then
batman.hab=batman.hab+1
batman.x=-4
elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
if batman.flip then
batman.x=batman.x-2
else
batman.x=batman.x+2
end
if batman.x<-4 then
batman.hab=batman.hab-1
batman.x=84
end
end
if batman.y<32 then
if (batman.y+16)&7==0 and (check_tile(batman.hab,batman.x+4,batman.y+16)>=tiletype.half or ((batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+16)>=tiletype.half)) then
batman.update=batman.update_normal
return
end
batman.y=batman.y+2
else
batman.hab=batman.hab+10
batman.y=0
end
end
end
function batman.update_stairs()
batman.frame=27
batman.wait=batman.wait+1
if batman.wait>=6 then
batman.wait=0
--if btn(KEY_RIGHT) then
-- batman.flip=false
-- batman_advance()
--elseif btn(KEY_LEFT) then
-- batman.flip=true
-- batman_advance()
if batman.goup then
if batman.y>0 then
if check_tile(batman.hab,batman.x+4,batman.y+8)==tiletype.stair or (batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+8)==tiletype.stair then
batman.y=batman.y-2
else
batman.update=batman.update_normal
if rnd(3)>0 then batman.flip=not batman.flip end
batman.stairscooldown=50
end
else
batman.hab=batman.hab-10
batman.y=32
end
else
if batman.y<32 then
if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.stair or (batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.stair then
batman.y=batman.y+2
else
batman.update=batman.update_normal
if rnd(3)>0 then batman.flip=not batman.flip end
batman.stairscooldown=50
end
else
batman.hab=batman.hab+10
batman.y=0
end
end
end
end

View File

@@ -1,9 +1,11 @@
elalien={hab=38,x=24,y=24,flip=true,goup=true,frame=8,stairscooldown=0,wait=0,step=0,anim={8,9,8,10},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}
elalien={hab=66,x=24,y=24,flip=true,goup=true,frame=8,stairscooldown=0,wait=0,step=0,anim={8,9,8,10},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}
function elalien.init()
elalien.hit=elalien.hit
elalien.update=elalien.update_normal
elalien.draw=elalien.draw
local habs={66,56,59,53}
premiere.hab=habs[1+rnd(4)]
end
function elalien.draw()
@@ -31,12 +33,12 @@ function elalien.update_normal()
elalien.hab=elalien.hab+1
elalien.x=-4
elseif check_tile(elalien.hab,elalien.x+inc,elalien.y+14)==tiletype.nonpc then
if rnd(10)<8 then
--if rnd(10)<8 then
elalien.update=elalien.update_jumping
elalien.step=0
else
elalien.flip=not elalien.flip
end
--else
-- elalien.flip=not elalien.flip
--end
elseif check_tile(elalien.hab,elalien.x+inc,elalien.y+14)<tiletype.half then
if elalien.flip then
elalien.x=elalien.x-2
@@ -75,7 +77,7 @@ function elalien.update_normal()
if elalien.stairscooldown==0 and
check_tile(elalien.hab,elalien.x+6,elalien.y+8)==tiletype.stair and
check_tile(elalien.hab,elalien.x+14,elalien.y+8)==tiletype.stair then
if rnd(10)<6 then
if rnd(10)<8 then
elalien.update=elalien.update_stairs
if check_tile(elalien.hab,elalien.x+4,elalien.y+16)==tiletype.block then
elalien.goup=true
@@ -93,16 +95,16 @@ function elalien.update_normal()
end
if check_tile(elalien.hab,elalien.x+4,elalien.y+16)==tiletype.void and ((elalien.x+4)&7==0 or check_tile(elalien.hab,elalien.x+12,elalien.y+16)==tiletype.void) then
local option=rnd(10)
local option=rnd(2)
if elalien.stairscooldown>0 then option=0 end
if option<5 then
if option==0 then
elalien.update=elalien.update_falling
elseif option<8 then
else
elalien.update=elalien.update_jumping
elalien.step=0
elalien.wait=0
else
elalien.flip=not elalien.flip
--else
-- elalien.flip=not elalien.flip
end
end
@@ -174,7 +176,7 @@ function elalien.update_falling()
if not elalien.flip and elalien.x==84 then
elalien.hab=elalien.hab+1
elalien.x=-4
elseif check_tile(elalien.hab,elalien.x+inc,elalien.y+14)<tiletype.half and check_tile(elalien.hab,elalien.x+inc,elalien.y+16)~=tiletype.void then
elseif check_tile(elalien.hab,elalien.x+inc,elalien.y+14)<tiletype.half then
if elalien.flip then
elalien.x=elalien.x-2
else
@@ -186,9 +188,6 @@ function elalien.update_falling()
end
end
local xx=flr((elalien.x+4)/8)
local yy=flr((elalien.y+16)/8)
if elalien.y<32 then
if (elalien.y+16)&7==0 and (check_tile(elalien.hab,elalien.x+4,elalien.y+16)>=tiletype.half or ((elalien.x+4)&7~=0 and check_tile(elalien.hab,elalien.x+12,elalien.y+16)>=tiletype.half)) then
elalien.update=elalien.update_normal
@@ -221,6 +220,7 @@ function elalien.update_stairs()
elalien.y=elalien.y-2
else
elalien.update=elalien.update_normal
if rnd(3)>0 then elalien.flip=not elalien.flip end
elalien.stairscooldown=50
end
else
@@ -233,6 +233,7 @@ function elalien.update_stairs()
elalien.y=elalien.y+2
else
elalien.update=elalien.update_normal
if rnd(3)>0 then elalien.flip=not elalien.flip end
elalien.stairscooldown=50
end
else

View File

@@ -2,4 +2,4 @@ title=Cacaus
width=128
height=96
zoom=4
files=scenes.lua,starter.lua,score.lua,switches.lua,map.lua,mapa.lua,cacau.lua,elalien.lua,premiere.lua,caco.lua,zombie.lua,abad.lua,main.lua
files=scenes.lua,starter.lua,score.lua,switches.lua,map.lua,mapa.lua,cacau.lua,batman.lua,elalien.lua,premiere.lua,caco.lua,zombie.lua,abad.lua,main.lua

View File

@@ -6,6 +6,9 @@ mode=modes.editing
seltile = 0
actors={}
cameras={}
camera_names={"Premiere","EL_ALIEN","BatMan"}
current_camera=1
function remove_actor(actor)
for index, value in pairs(actors) do
@@ -28,6 +31,9 @@ function _init()
table.insert(actors,premiere)
elalien.init()
table.insert(actors,elalien)
batman.init()
table.insert(actors,batman)
z = zombie.new()
table.insert(actors,z)
c=caco.new(13,24,16,true)
@@ -37,6 +43,10 @@ function _init()
table.insert(actors,starter.new(11,16,32,scenes.abad_corfes))
score.create()
table.insert(cameras,premiere)
table.insert(cameras,elalien)
table.insert(cameras,batman)
_update=update_game
end
@@ -147,15 +157,15 @@ function update_game()
else
draw_hab(abad.hab,0,0)
--text(abad.hab,1,1,2)
draw_hab(elalien.hab,0,48)
text("EL_ALIEN",2,49,8)
draw_hab(cameras[current_camera].hab,0,48)
text(camera_names[current_camera],2,49,8)
score.draw()
setsource(tiles)
prnt("x"..abad.vides,114,13,2)
rectfill(102+(abad.energia>>1),30,122,37,16)
mapa_update(abad.hab,elalien.hab)
mapa_update(abad.hab,cameras[current_camera].hab)
for key,actor in pairs(actors) do
actor:update()
@@ -174,6 +184,12 @@ function update_game()
if btnp(KEY_RETURN) then
mode=modes.editing
mapa_restore_backup()
elseif btnp(KEY_1) then
current_camera=1
elseif btnp(KEY_2) then
current_camera=2
elseif btnp(KEY_3) then
current_camera=3
end
end

View File

@@ -44,7 +44,7 @@ mapa={
66,256,256,67,256,65,256,64,256,256,256,66,
256,256,64,256,256,256,256,256,256,68,256,67,
18,19,18,19,18,19,18,19,256,256,65,256,
256,256,256,256,256,256,256,256,256,256,256,256,
256,256,256,256,256,256,256,16,256,256,256,256,
256,256,256,256,256,256,256,16,256,64,256,66,
20,21,20,21,20,21,20,21,256,256,66,256,

View File

@@ -1,9 +1,11 @@
premiere={hab=44,x=24,y=24,flip=true,frame=11,wait=0,step=0,anim={12,13,12,14},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}
premiere={hab=75,x=24,y=24,flip=true,frame=12,wait=0,step=0,anim={12,13,12,14},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}
function premiere.init()
premiere.hit=premiere.hit
premiere.update=premiere.update_normal
premiere.draw=premiere.draw
local habs={75,44,20,0}
premiere.hab=habs[1+rnd(4)]
end
function premiere.draw()