[WIP] Treballant en el jefe. Implementant chase i avoid

This commit is contained in:
2026-04-19 15:56:33 +02:00
parent dab08a0f17
commit 5622392f41
6 changed files with 94 additions and 20 deletions

View File

@@ -133,7 +133,7 @@ end
function abad_hurt(howmuch) function abad_hurt(howmuch)
-- howmuch = 0 howmuch = 0
if abad.hurting == 0 and abad.respawning==0 then if abad.hurting == 0 and abad.respawning==0 then
sound.play(audio_abad_hit) sound.play(audio_abad_hit)
abad.energia=abad.energia-howmuch abad.energia=abad.energia-howmuch

View File

@@ -15,7 +15,7 @@ end
-- --
------------------------------- -------------------------------
function imp:reduce_timers() function imp:reduce_timers()
if self.mood==self.moods.stop then return end if self.mood=="stop" then return end
if DEBUG_FN_NAME then print_dbg("reduce_timers") end if DEBUG_FN_NAME then print_dbg("reduce_timers") end
for key, val in pairs(self.timers) do for key, val in pairs(self.timers) do
@@ -37,13 +37,13 @@ end
-- --
------------------------------- -------------------------------
function imp:reset_timer( timer ) function imp:reset_timer( timer )
if self.mood == self.moods.stop then return end if self.mood == "stop" then return end
if DEBUG_FN_NAME then print_dbg("reduce_timers") end if DEBUG_FN_NAME then print_dbg("reduce_timers") end
local time = 0 local time = 0
if timer=="mood" then time = 150 if timer=="mood" then time = 150
elseif timer=="target" then time = 350 elseif timer=="target" then time = 350
elseif timer=="shot" then time = 100 elseif timer=="shot" then time = 100
elseif timer=="super" then time = 200 elseif timer=="super" then time = 600
elseif timer=="super_wait" then time = 200 elseif timer=="super_wait" then time = 200
elseif timer=="flip_wait" then time = 50 elseif timer=="flip_wait" then time = 50
end end
@@ -55,7 +55,7 @@ end
-- --
------------------------------- -------------------------------
function imp:analyze_env() --OK function imp:analyze_env() --OK
if self.mood==self.moods.stop then return end if self.mood=="stop" then return end
if DEBUG_FN_NAME then print_dbg("analyze_env") end if DEBUG_FN_NAME then print_dbg("analyze_env") end
-- Distancia fins a l'abad -- Distancia fins a l'abad
local r = distancia(self,abad) local r = distancia(self,abad)
@@ -180,10 +180,10 @@ end
-- --
------------------------------- -------------------------------
function imp:choose_action() -- antic imp:move function imp:choose_action() -- antic imp:move
if DEBUG_FN_NAME then print_dbg("choose_action "..self.mood.." / "..self.moods.stop) end if DEBUG_FN_NAME then print_dbg("choose_action "..self.mood.." / ".."stop") end
-- if self.mood==self.moods.stop then return self.mood end -- if self.mood=="stop" then return self.mood end
-- Si no te ganes de fer res, no moure -- Si no te ganes de fer res, no moure
if self.mood==self.moods.stop then return self.actions.no_action end if self.mood=="stop" then return self.actions.no_action end
local next_action = self:next_action() local next_action = self:next_action()
@@ -513,6 +513,14 @@ function imp:next_action()
next_action = "shot" next_action = "shot"
end end
-- anar a fer el super
if self.timers.super<=0 then
self.pattern:set_target_mode(PATTERN_CHASE,self.pattern.node[1])
else
self.pattern:set_target_mode(self.mood,abad)
end
-- fer el super
if self.timers.super<=0 and self:check_action_event("super_ready") then if self.timers.super<=0 and self:check_action_event("super_ready") then
if self.x<self.pattern.node[3].x then if self.x<self.pattern.node[3].x then
local node = 3 local node = 3
@@ -567,7 +575,7 @@ if DEBUG_FN_NAME then print_dbg("") end
end end
function imp:state_jumping() function imp:state_jumping()
if self.mood==self.moods.stop then return self.mood end if self.mood=="stop" then return self.mood end
if DEBUG_FN_NAME then print_dbg("state_jumping") end if DEBUG_FN_NAME then print_dbg("state_jumping") end
-- ?? -- ??
self.wait=self.wait+1 self.wait=self.wait+1
@@ -729,7 +737,7 @@ if DEBUG_FN_NAME then print_dbg("land") end
end end
function imp:state_walking() function imp:state_walking()
if self.mood==self.moods.stop then return end if self.mood=="stop" then return end
if DEBUG_FN_NAME then print("state_walking") end if DEBUG_FN_NAME then print("state_walking") end
-- Limitar la velocitat de moviment -- Limitar la velocitat de moviment
@@ -859,3 +867,13 @@ function imp:super_movement()
if DEBUG_FN_NAME then print("super_movement") end if DEBUG_FN_NAME then print("super_movement") end
self.move_type = self.move_types.super self.move_type = self.move_types.super
end end
function imp:choose_mood()
if self.mood=="stop" then return end
if self.timers.mood<=0 then
self.mood=self.moods[math.random(2+(math.random(2)-1))+1] -- chase o avoid o aleatoriament neutral
self:reset_timer("mood")
print(self.mood)
end
end

View File

@@ -32,9 +32,9 @@ function imp.new(_hab,_x,_y)
anim={28,29,28,30}, -- seqüencia de frames anim={28,29,28,30}, -- seqüencia de frames
wait=0, wait=0,
step=0, step=0,
moods={stop=0, chase=1, avoid=2, neutral=4}, moods={"stop",PATTERN_CHASE, PATTERN_AVOID, PATTERN_RANDOM }, -- deu coincidir en els modes de pattern
mood=0, --anterior fight_mode mood="stop", --anterior fight_mode
timers={mood=150,target=350, shot=100, super=200, super_wait=200, flip_wait=50, stucked_locked=50}, --anterior *_cooldown timers={mood=150,target=350, shot=100, super=2000, super_wait=200, flip_wait=50, stucked_locked=50}, --anterior *_cooldown
move_types={free=0, pattern=1, super=2}, move_types={free=0, pattern=1, super=2},
move_type=1, -- anterior movement_type move_type=1, -- anterior movement_type
actions={no_action=0, up=1, down=2, left=4, right=8, jump=16, shot=32, super=64, jumpfwd=128}, actions={no_action=0, up=1, down=2, left=4, right=8, jump=16, shot=32, super=64, jumpfwd=128},
@@ -88,6 +88,7 @@ function imp.new(_hab,_x,_y)
super_movement = imp.super_movement, super_movement = imp.super_movement,
hit = imp.hit, hit = imp.hit,
warp_update = imp.warp_update, warp_update = imp.warp_update,
choose_mood = imp.choose_mood,
-- debug -- debug
_moure = imp._moure, _moure = imp._moure,
_pause = false, _pause = false,
@@ -231,13 +232,13 @@ function imp:update_normal()
if not self._pause then if not self._pause then
-- print("-------------------------------------------------") -- print("-------------------------------------------------")
-- if self.mood~=self.moods.stop then print("TARGET= "..self.pattern:target()) end -- if self.mood~="stop" then print("TARGET= "..self.pattern:target()) end
-- if self.mood~=self.moods.stop then print("ACT/EV= "..self.pattern:action().." / "..self.pattern:event()) end -- if self.mood~="stop" then print("ACT/EV= "..self.pattern:action().." / "..self.pattern:event()) end
-- if self.mood~=self.moods.stop then print("ACTIONS= "..#self.pattern:actions()) end -- if self.mood~="stop" then print("ACTIONS= "..#self.pattern:actions()) end
self:reduce_timers() self:reduce_timers()
-- Ajustar mood (emocions) -- Ajustar mood (emocions)
-- self:choose_mood() self:choose_mood()
-- self:choose_target() -- self:choose_target()
@@ -251,7 +252,7 @@ function imp:update_normal()
self:choose_action() self:choose_action()
-- local str_aev = "" -- local str_aev = ""
-- for k,v in pairs(self.action_event) do str_aev=str_aev..", "..v end -- for k,v in pairs(self.action_event) do str_aev=str_aev..", "..v end
-- if self.mood~=self.moods.stop then print("TARGET= "..self.pattern:target().." ACTION= "..self.pattern:action().." ACTION EVENT= "..self.pattern:event().." / "..str_aev) end -- if self.mood~="stop" then print("TARGET= "..self.pattern:target().." ACTION= "..self.pattern:action().." ACTION EVENT= "..self.pattern:event().." / "..str_aev) end
-- moviment -- moviment
self:controller_input() self:controller_input()
self:moure() self:moure()

View File

@@ -78,6 +78,7 @@ end
function mini.init() function mini.init()
-- back_sf=surf.new(256,192) -- back_sf=surf.new(256,192)
math.randomseed(os.time())
fonts_init() fonts_init()
images_init() images_init()
mapa_init() mapa_init()

View File

@@ -1,5 +1,9 @@
pattern={} pattern={}
PATTERN_RANDOM = "random"
PATTERN_CHASE = "chase"
PATTERN_AVOID = "avoid"
function pattern.new() function pattern.new()
return { return {
node = {}, -- array de punts ( node[1] = punt1,... node[n] = puntN ) node = {}, -- array de punts ( node[1] = punt1,... node[n] = puntN )
@@ -9,6 +13,9 @@ function pattern.new()
action_idx = 1, -- punter a l'acció actual action_idx = 1, -- punter a l'acció actual
manual_target = {}, manual_target = {},
manual_path = {}, manual_path = {},
poi = {}, -- points of interest
-- target_modes = { PATTERN_RANDOM, PATTERN_CHASE, PATTERN_AVOID },
target_mode = PATTERN_RANDOM,
next_action = pattern.next_action, next_action = pattern.next_action,
next_target = pattern.next_target, next_target = pattern.next_target,
in_target = pattern.in_target, in_target = pattern.in_target,
@@ -20,10 +27,16 @@ function pattern.new()
-- recovery = pattern.noop, -- recovery = pattern.noop,
draw = pattern.draw, draw = pattern.draw,
collision = pattern.collision, collision = pattern.collision,
add_manual_target = pattern.add_manual_target add_manual_target = pattern.add_manual_target,
set_target_mode = pattern.set_target_mode,
} }
end end
function pattern:set_target_mode ( mode, poi )
self.poi = poi or {}
self.target_mode = mode
end
function pattern:add_manual_target ( node, path ) function pattern:add_manual_target ( node, path )
self.manual_target = self.node[node] self.manual_target = self.node[node]
self.manual_path = path self.manual_path = path
@@ -171,8 +184,46 @@ function pattern:next_target( target )
-- target id -- target id
self.path_idx = target self.path_idx = target
-- tindre en compte l'estrategia per a triar el següent target des del path actual per ara aleatori -- tindre en compte l'estrategia per a triar el següent target des del path actual per ara aleatori
if self.poi.x == nil or self.poi.y == nil then
self.target_mode = PATTERN_RANDOM
end
if #self.path[self.path_idx]==1 then
self.target_idx = 1
elseif self.target_mode == PATTERN_RANDOM then
self.target_idx = math.random(#self.path[self.path_idx]) self.target_idx = math.random(#self.path[self.path_idx])
else
-- calcular distancies
local next_target_min = 0
local next_target_max = 0
local min_distance = -1
local max_distance = 0
local d = 0
for k, v in pairs(self.path[self.path_idx]) do
d = distancia( self.node[v.target], self.poi )
if d>max_distance then
max_distance = d
next_target_max = k
end
if min_distance==-1 and d>=0 then min_distance=d+1 end
if d<min_distance and d>=0 then
min_distance = d
next_target_min = k
end
end
-- asignar target segons el mode
if self.target_mode==PATTERN_CHASE then
self.target_idx = next_target_min
elseif self.target_mode==PATTERN_AVOID then
self.target_idx = next_target_max
else
self.target_idx = math.random(#self.path[self.path_idx])
end
end
self.action_idx = 1 self.action_idx = 1
end end

View File

@@ -66,6 +66,9 @@ function stages.stage1_init()
table.insert( actors, caco.new(61,3,2,false) ) table.insert( actors, caco.new(61,3,2,false) )
table.insert( actors, zombie.new(2, 3, 3,false) ) table.insert( actors, zombie.new(2, 3, 3,false) )
table.insert( actors, zombie.new(2, 4, 3,true) )
table.insert( actors, zombie.new(2, 5, 3,false) )
table.insert( actors, zombie.new(3, 3, 3,true) )
table.insert( actors, zombie.new(28, 3, 3,false) ) table.insert( actors, zombie.new(28, 3, 3,false) )
table.insert( actors, zombie.new(32, 3, 3,false) ) table.insert( actors, zombie.new(32, 3, 3,false) )
table.insert( actors, zombie.new(44, 3, 3,false) ) table.insert( actors, zombie.new(44, 3, 3,false) )