[WIP] Treballant en el jefe.

This commit is contained in:
2026-04-10 20:41:00 +02:00
parent 87e2a134a8
commit 568b531d4b
2 changed files with 306 additions and 141 deletions

View File

@@ -346,3 +346,10 @@ function debug.write_tile(x, y, yplus, print_type, align )
end end
draw.text(msg,scr_x+txt_offset+1,scr_y+1+yplus,2) draw.text(msg,scr_x+txt_offset+1,scr_y+1+yplus,2)
end end
function empty_table(t)
for _ in pairs(t) do
return false
end
return true
end

View File

@@ -67,11 +67,12 @@ function imp.new(_hab, _x, _y)
scene_object=false, scene_object=false,
fight_modes=_fight_modes, fight_modes=_fight_modes,
mode="stop", mode="stop",
mode_cooldown=1000, mode_cooldown=500,
super_cooldown=2000, super_cooldown=2000,
shot_cooldown=500, shot_cooldown=500,
action="", action="",
movement="", movement="",
movement_type="",
enabled=true, enabled=true,
disable_reason="", disable_reason="",
can_warp=false, can_warp=false,
@@ -81,6 +82,11 @@ function imp.new(_hab, _x, _y)
hot_points={}, hot_points={},
target={}, target={},
old_target={}, old_target={},
path={},
paths={},
action_event="",
path_curr_action=1,
analisis={},
invencible=false, invencible=false,
-- direccio=imp.direccio, -- direccio=imp.direccio,
do_jump=imp.do_jump, do_jump=imp.do_jump,
@@ -95,7 +101,7 @@ function imp.new(_hab, _x, _y)
update=imp.state_normal, update=imp.state_normal,
land=imp.land, land=imp.land,
mode_controller=imp.mode_controller, mode_controller=imp.mode_controller,
movement=imp.movement, --movement=imp.movement,
advance=imp.advance, advance=imp.advance,
reduce_cooldown=imp.reduce_cooldown, reduce_cooldown=imp.reduce_cooldown,
set_fight_mode=imp.set_fight_mode, set_fight_mode=imp.set_fight_mode,
@@ -103,11 +109,102 @@ function imp.new(_hab, _x, _y)
controller_input=imp.controller_input, controller_input=imp.controller_input,
ia=imp.ia, ia=imp.ia,
analyze_env=imp.analyze_env, analyze_env=imp.analyze_env,
next_pattern_movement=imp.next_pattern_movement,
reset_mode_cooldown=imp.reset_mode_cooldown,
free_movement=imp.free_movement,
free_move=imp.free_move,
reset_pattern_movement=imp.reset_pattern_movement
-- imp.enabled=false -- imp.enabled=false
-- imp.counter=500 -- imp.counter=500
} }
end end
function imp:reset_mode_cooldown( value )
value = value or 500
self.mode_cooldown = value
return value
end
function imp:reset_pattern_movement()
self.path={}
self.path_curr_action=1;
end
function imp:next_pattern_movement( from )
self.movement_type = "pattern"
self:reset_mode_cooldown()
from = from or self.path.next
-- obtindre uno dels possibles camins des del punt "from"
local next_path_idx = math.random(#self.paths[from])
-- Activar següent target
local next_path = self.paths[from][next_path_idx]
self.target = self.hot_points[next_path.next]
print("TARGET ID= "..self.target.id.." HOT POINT= "..next_path.next)
self.path = next_path
self.path_curr_action = 1
action = self.path.actions[1].action
self.action_event=""
return action
end
function imp:free_move()
-- Moviment horitzontal
if self.target.x+self.target.bb.x>=self.x+self.bb.x then
action="right"
elseif self.target.x+self.target.bb.x<self.x+self.bb.x then
action="left"
end
-- Els dos punts de baix pero un poc mes alt que el piso
local x1_check = self.x+self.bb.x
local x2_check = self.x+self.bb.x+self.bb.w
local y_check = self.y+self.bb.h-4
-- Tiles on esta
local tile_type1, tile_code1= arc_check_tile(x1_check,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check,y_check)
-- debug
local scr_x1, scr_y = viewp:screen_coords( x1_check, y_check )
local scr_x2, scr_y = viewp:screen_coords( x2_check, y_check )
draw.rect(scr_x1,scr_y,scr_x2-scr_x1,8,2)
-- /debug
--Comprovar si está en target
if collision(self, self.target) then
-- print("Next target "..tile_type1.." "..tile_code1)
-- print("Next target "..tile_type2.." "..tile_code2)
self:reset_mode_cooldown()
-- Buscar quin es el target
local idx = 1
for i=1,#self.hot_points do
if self.target==self.hot_points[i] then
idx= i
print("TARGET ID= "..self.target.id.." HOT POINT IDX= "..i.." Reached")
end
end
-- Canviar a moviment per patro
action = self:next_pattern_movement(idx)
elseif half_collision(self, self.target)
and tile_type1==tiletype.void
and tile_type2==tiletype.void then -- and (self.y>self.target.y or self.y<self.target.y) ) then -- afegir que no hi haja tile per a escalar
-- print("Next Target 2 "..tile_type1.." "..tile_code1)
elseif tile_type1~=tiletype.void
or tile_type2~=tiletype.void then
action="jump"
end
return action
end
function imp:free_movement()
self.movement_type = "free"
print("Free")
end
function imp:fight() function imp:fight()
print("FIGHTING MODE") print("FIGHTING MODE")
-- table.insert(self.hot_points, point.new(54,5,4,12,8,"P1")) -- table.insert(self.hot_points, point.new(54,5,4,12,8,"P1"))
@@ -117,14 +214,58 @@ function imp:fight()
-- table.insert(self.hot_points, point.new(44,12,5,12,8,"P5")) -- table.insert(self.hot_points, point.new(44,12,5,12,8,"P5"))
-- table.insert(self.hot_points, point.new(54,12,4,12,8,"P6")) -- table.insert(self.hot_points, point.new(54,12,4,12,8,"P6"))
table.insert(self.hot_points, point.new(54, 5, 4, 0, 0,"P1")) self.hot_points[1] = point.new(44, 5, 1, 0, 0,"P1")
table.insert(self.hot_points, point.new(44, 5, 3, 0, 0,"P2")) self.hot_points[2] = point.new(45, 8, 1, 0, 0,"P2")
table.insert(self.hot_points, point.new(55, 8, 4, 0, 0,"P3")) self.hot_points[3] = point.new(44,12, 5, 0, 0,"P3")
table.insert(self.hot_points, point.new(45, 8, 3, 0, 0,"P4")) self.hot_points[4] = point.new(54, 5, 4, -8, 0,"P4")
table.insert(self.hot_points, point.new(44,12, 5, 0, 0,"P5")) self.hot_points[5] = point.new(54,12, 4, 8, 0,"P5")
table.insert(self.hot_points, point.new(54,12, 4, 0, 0,"P6")) self.hot_points[6] = point.new(55, 8, 4, 8, 0,"P6")
table.insert(self.hot_points, abad); self.hot_points[7] = abad
self.target = self.hot_points[3]
self.target = self.hot_points[6]
self.path = {next=6, actions={{action="right",event="target"}}}
self.paths[1] = {
{next=3, actions= {{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{next=4, actions= {{action="right" , event="land"},
{action="left" , event="target"}}},
{next=5, actions= {{action="right" , event="target"}}}
}
self.paths[2] = {
{next=3, actions={{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{next=6, actions={{action="left" , event="land"},
{action="right" , event="target"}}},
{next=5, actions={{action="left" , event="target"}}}
}
self.paths[3] = {
{next=1, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="jump" , event="target"}}},
{next=2, actions={{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="jump" , event="target"}}},
{next=4, actions={{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{next=5, actions={{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{next=6, actions={{action="left" , event="land"},
{action="right" , event="target"}}}
}
self.paths[4] = {
{next=1, actions={{action="jump" , event="target"}}},
{next=5, actions={{action="right", event="target"}}},
{next=6, actions={{action="right", event="target"}}}
}
self.paths[5] = {
{next=4, actions={{action="left" , event="target"}}},
{next=6, actions={{action="right", event="target"}}}
}
self.paths[6] = {
{next=2, actions={{action="jump", event="target"}}},
{next=5, actions={{action="left", event="target"}}}
}
self.mode="chase" self.mode="chase"
end end
@@ -143,6 +284,15 @@ function imp:draw()
end end
end end
end end
-- print("DRAW path= "..#self.path)
if #self.path>0 then
for i=1,#self.path do
local world_x, world_y = coords.room_to_world(self.path[i][1], self.path[i][2], self.path[i][3])
local scr_x, scr_y = viewp:screen_coords( world_x, world_y )
draw.rect(scr_x, scr_y, 16, 16, 14)
end
end
end end
function imp:hit() function imp:hit()
@@ -180,10 +330,10 @@ function imp:analyze_env()
dy = (self.y-abad.y) dy = (self.y-abad.y)
r = math.sqrt(dx*dx+dy*dy) r = math.sqrt(dx*dx+dy*dy)
-- msg_print(10,20,r,true) -- msg_print(10,20,r,true)
local can_chase_abad = false self.analisis.can_chase_abad = false
if r<=100 then if r<=100 then
-- draw.circ(scr_x+self.bb.w/2,scr_y+self.bb.h/2,r,2) -- draw.circ(scr_x+self.bb.w/2,scr_y+self.bb.h/2,r,2)
can_chase_abad=true self.analisis.can_chase_abad=true
end end
-- Els dos punts de cintura per a saber si es pot escalar -- Els dos punts de cintura per a saber si es pot escalar
@@ -192,48 +342,131 @@ function imp:analyze_env()
local y_check = self.y+self.bb.h-4 local y_check = self.y+self.bb.h-4
local tile_type1, tile_code1= arc_check_tile(x1_check,y_check) local tile_type1, tile_code1= arc_check_tile(x1_check,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check,y_check) local tile_type2, tile_code2= arc_check_tile(x2_check,y_check)
local can_climb = false self.analisis.can_climb = false
if tile_type1~=tiletype.void or tile_type2~=tiletype.void then if tile_type1~=tiletype.void or tile_type2~=tiletype.void then
can_climb = true self.analisis.can_climb = true
end end
-- Abad a tir -- Abad a tir
local can_shot = false self.analisis.can_shot = false
if h_collision(self,abad) then can_shot=true end if h_collision(self,abad) then self.analisis.can_shot=true end
-- Super preparat -- Super preparat
local can_super = false self.analisis.can_super = false
if self.super_cooldown<=0 then can_super=true end if self.super_cooldown<=0 then self.analisis.can_super=true end
-- Acces a la zona central -- Acces a la zona central
x1_check = self.x+self.bb.x x1_check = self.x+self.bb.x
x2_check = self.x+self.bb.x+self.bb.w x2_check = self.x+self.bb.x+self.bb.w
y_check = self.y+self.bb.h y_check = self.y+self.bb.h
local can_go_altar = false self.analisis.can_go_altar = false
local hab1, tx1, ty1 = coords.world_to_tile(x1_check, y_check) local hab1, tx1, ty1 = coords.world_to_tile(x1_check, y_check)
local hab2, tx2, ty2 = coords.world_to_tile(x2_check, y_check) local hab2, tx2, ty2 = coords.world_to_tile(x2_check, y_check)
if (hab1==44 and tx1==6 and ty1==4) or (hab2==44 and tx2==7 and ty2==4) if (hab1==44 and tx1==6 and ty1==4) or (hab2==44 and tx2==7 and ty2==4)
or (hab1==45 and tx1==7 and ty1==3) or (hab2==45 and tx2==8 and ty2==3)then or (hab1==45 and tx1==7 and ty1==3) or (hab2==45 and tx2==8 and ty2==3)then
can_go_altar = true self.analisis.can_go_altar = true
end end
-- Cau al següent moviment -- Cau al següent moviment
local going_to_fall = false self.analisis.going_to_fall = false
local tile_type1, tile_code1= arc_check_tile(x1_check,y_check) local tile_type1, tile_code1= arc_check_tile(x1_check,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check,y_check) local tile_type2, tile_code2= arc_check_tile(x2_check,y_check)
if tile_type1==tiletype.void and tile_type2==tiletype.void then if tile_type1==tiletype.void and tile_type2==tiletype.void then
going_to_fall = true self.analisis.going_to_fall = true
end end
local target_reached = false self.analisis.target_reached = false
local target_aligned = false -- local target_aligned = false
if #self.target>0 then -- self.path= {}
-- -- print("ANALYZE => "..#self.target)
if not empty_table(self.target) then
-- Target reached -- Target reached
if collision(self, self.target) then target_reached=true end if collision(self, self.target) then self.analisis.target_reached=true end
--
-- Target aligned -- -- Target aligned
if half_collision(self, self.target) then target_aligned=true end -- if half_collision(self, self.target) then target_aligned=true end
--
-- local hab1, tx1, ty1 = coords.world_to_tile(self.x+self.bb.x, self.y+self.bb.y+self.h/2)
-- local hab2, tx2, ty2 = coords.world_to_tile(self.target.x, self.target.y)
-- -- print(hab1..", "..tx1..", "..ty1)
-- -- print(hab2..", "..tx2..", "..ty2)
-- local floorImp = math.floor(hab1 / 10)
-- local floorTarget = math.floor(hab2 / 10)
-- local roomImp = hab1 % 10
-- local roomTarget = hab2 % 10
--
-- local sentit_y = 1
-- if floorImp>floorTarget then
-- sentit_y = -1
-- elseif floorImp==floorTarget then
-- sentit_y = 1
-- if ty1>ty2 then
-- sentit_y= -1
-- elseif ty1==ty2 then
-- sentit_y = 0
-- end
-- end
--
-- local sentit_x = 1
-- if roomImp>roomTarget then
-- sentit_x = -1
-- elseif roomImp==roomTarget then
-- sentit_x = 1
-- if tx1>tx2 then
-- sentit_x = -1
-- elseif tx1==tx2 then
-- sentit_x = 0
-- end
-- end
--
-- local max_steps = 50
-- local found = false
-- while not found and max_steps>0 do
-- max_steps = max_steps -1
-- end
--
-- if hab1==hab2 then
-- local tx = 0
-- local ty = 0
-- if tx2<tx1 then
-- for i=tx2,tx1 do
-- tx = i
-- self.path[#self.path+1] = {hab1, tx, ty1}
-- end
-- if ty2<ty1 then
-- for j=ty2,ty1 do
-- ty = j
-- self.path[#self.path+1] = {hab1, tx, ty}
-- end
-- else
-- for j=ty1,ty2 do
-- ty = j
-- self.path[#self.path+1] = {hab1, tx, ty}
-- end
-- end
-- elseif tx1<tx2 then
-- for i=tx1,tx2 do
-- tx = i
-- self.path[#self.path+1] = {hab1, tx, ty1}
-- end
-- if ty2<ty1 then
-- for j=ty2,ty1 do
-- ty = j
-- self.path[#self.path+1] = {hab1, tx, ty}
-- end
-- else
-- for j=ty1,ty2 do
-- ty = j
-- self.path[#self.path+1] = {hab1, tx, ty}
-- end
-- end
-- end
-- end
--
-- self.path[#self.path+1] = {hab1, tx1, ty1}
-- self.path[#self.path+1] = {hab2, tx2, ty2}
end end
-- if can_climb then print("Climb!") end -- if can_climb then print("Climb!") end
-- if can_shot then print("Shot!") end -- if can_shot then print("Shot!") end
-- if can_super then print("Super!") end -- if can_super then print("Super!") end
@@ -367,6 +600,7 @@ function imp:state_falling()
-- Si toca terra canviar el mode -- Si toca terra canviar el mode
if self:land() then if self:land() then
self.update=imp.state_normal self.update=imp.state_normal
self.action_event = "land"
return return
end end
@@ -517,83 +751,26 @@ function imp:think( )
if self.mode=="stop" then return action end if self.mode=="stop" then return action end
if self.target.x+self.target.bb.x>=self.x+self.bb.x then if self.analisis.going_to_fall then self.action_event="prefall" end
action="right" if self.analisis.target_reached then self.action_event="target" end
elseif self.target.x+self.target.bb.x<self.x+self.bb.x then
action="left" if self.movement_type=="free" then
msg_print(10,20,"FREE",true)
action = self:free_move()
else
msg_print(10,20,"PATTERN "..self.action_event,true)
-- print("Pattern")
if self.action_event~=self.path.actions[self.path_curr_action].event then
action = self.path.actions[self.path_curr_action].action
else
if self.path_curr_action<#self.path then
self.path_curr_action = self.path_curr_action + 1
else
action = self:next_pattern_movement()
end
end end
-- Els dos punts de baix de l'abad
local x1_check = self.x+self.bb.x
local x2_check = self.x+self.bb.x+self.bb.w
local y_check = self.y+self.bb.h-4
local tile_type1, tile_code1= arc_check_tile(x1_check,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check,y_check)
local scr_x1, scr_y = viewp:screen_coords( x1_check, y_check )
local scr_x2, scr_y = viewp:screen_coords( x2_check, y_check )
draw.rect(scr_x1,scr_y,scr_x2-scr_x1,8,2)
if collision(self, self.target) then
print("Next target "..tile_type1.." "..tile_code1)
print("Next target "..tile_type2.." "..tile_code2)
self.mode_cooldown = 0
elseif half_collision(self, self.target) and tile_type1==tiletype.void and tile_type2==tiletype.void then -- and (self.y>self.target.y or self.y<self.target.y) ) then -- afegir que no hi haja tile per a escalar
print("Next Target 2 "..tile_type1.." "..tile_code1)
-- self.mode_cooldown = 0
-- self:set_fight_mode()
elseif tile_type1~=tiletype.void or tile_type2~=tiletype.void then
-- print("jump "..tile_type1..", "..tile_type2)
action="jump"
end end
-- local scr_x, scr_y = viewp:screen_coords( self.x, self.y )
-- draw.line(0,scr_y,256,scr_y,2)
-- draw.line(scr_x,0,scr_x,192,2)
-- if self.y+self.bb.y>=self.target.y+self.target.bb.y then
-- local tile_type, tile_code = arc_check_tile(self.x+self.bb.x,self.y+self.bb.h, true)
-- local tile_type2, tile_code2 = arc_check_tile(self.x+self.bb.x+self.bb.w,self.y+self.bb.h, true)
-- msg_print(50, 50, tile_type..", "..tile_code, true )
-- if tile_type ~=tiletype.void or tile_type2 ~=tiletype.void then
-- print("JUMP")
-- action = "jump"
-- else
-- if self.target.x+self.target.bb.x>=self.x+self.bb.x then
-- print("sense eixida")
-- -- self.mode_cooldown = 0
-- -- self:set_fight_mode()
-- end
-- end
-- -- elseif self.y==self.target.y and math.abs(self.x-self.target.x)<=8 then
-- -- action = "jumpfwd"
-- end
-- local ix, iy = viewp:screen_coords( self.x, self.y )
-- draw.rect(ix+(self.bb.x+(self.bb.w/2)), iy+(self.h/2), 2,2, 2)
-- if self.fight_modes[self.mode]=="away" or self.fight_modes[self.mode]=="chase" then
-- if self.y>=self.target.y
-- and arc_check_tile(self.x+(self.bb.x+(self.bb.w/2)),(self.y+(self.h/2)))~=tiletype.void then
-- action = "jump"
-- elseif self.y==self.target.y and math.abs(self.x-self.target.x)<=8 then
-- action = "jumpfwd"
-- else
-- if self.fight_modes[self.mode]=="away" then
-- if self.target.x<=self.x then
-- action="right"
-- else
-- action="left"
-- end
-- elseif self.fight_modes[self.mode]=="chase" then
-- if self.target.x>=self.x then
-- action="right"
-- else
-- action="left"
-- end
-- end
-- end
-- elseif self.fight_modes[self.mode]=="super" then
-- action="super"
-- elseif self.fight_modes[self.mode]=="shot" then
-- action="shot"
-- end
self.action = action self.action = action
return action return action
@@ -617,10 +794,12 @@ function imp:set_fight_mode()
if self.mode=="stop" then return end if self.mode=="stop" then return end
if self.mode_cooldown <= 0 then if self.mode_cooldown <= 0 then
self:free_movement()
-- seleccionar mode d'acció -- seleccionar mode d'acció
local last_mode=self.mode local last_mode=self.mode
-- self.mode_cooldown = 150+math.random(50)-1 -- self.mode_cooldown = 150+math.random(50)-1
self.mode_cooldown = 1000 self:reset_mode_cooldown()
self:reset_pattern_movement()
-- self.mode=math.random(#self.fight_modes) -- self.mode=math.random(#self.fight_modes)
-- --
-- if self.mode==self.fight_modes["super"] and self.super_cooldown>0 then self.mode=self.fight_modes["shot"] end -- if self.mode==self.fight_modes["super"] and self.super_cooldown>0 then self.mode=self.fight_modes["shot"] end
@@ -629,12 +808,19 @@ function imp:set_fight_mode()
self.mode=self.fight_modes["chase"] self.mode=self.fight_modes["chase"]
-- print("cambiar modo => "..self.fight_modes[self.mode]) -- print("cambiar modo => "..self.fight_modes[self.mode])
-- if self.movement_type == "free" then
local new_point = math.random(#self.hot_points) local new_point = math.random(#self.hot_points)
if self.target == self.hot_points[new_point] then new_point = ((new_point+1)%#self.hot_points)+1 end if self.target == self.hot_points[new_point] then
new_point = ((new_point+1)%#self.hot_points)+1
end
self.old_target = self.target self.old_target = self.target
self.target=self.hot_points[new_point] self.target=self.hot_points[new_point]
print("target => "..self.target.id) print("Next random target => "..self.target.id)
-- else
--if self.action_event=="target" then
-- print(self.old_target.id)
--end
-- end
end end
-- -- if self.mode==last_mode then self.mode=((last_mode+1)%#self.fight_modes)+1 end -- -- if self.mode==last_mode then self.mode=((last_mode+1)%#self.fight_modes)+1 end
-- elseif self.mode_cooldown == 0 then -- elseif self.mode_cooldown == 0 then
@@ -690,31 +876,3 @@ function imp:collision()
end end
end end
end end
-- function imp:direccio( actor )
-- -- msg_print(0,0,"direccio",true)
-- local check_side = "L"
-- -- definir el punts per a comprobar en el mapa
-- if self.fight_modes[self.mode]=="away" then
-- if actor.x>self.x then
-- -- caminar cap "enrere", check costat esq
-- check_side = "L"
-- else
-- -- caminar cap "endavant", check costat dret
-- check_side = "R"
-- end
-- elseif self.fight_modes[self.mode]=="chase" then
-- if actor.x>self.x then
-- -- caminar cap "endavant", check costat dret
-- check_side = "R"
-- else
-- -- caminar cap "enrere", check costat esq
-- check_side = "L"
-- end
-- end
--
-- local step = 1
-- if check_side=="L" then step = -step end
--
-- return step
-- end