[WIP] Treballant en el jefe. Separat el 'motor' de moviment per patró

This commit is contained in:
2026-04-14 23:41:34 +02:00
parent d477988d10
commit 0c9c31dca2
4 changed files with 234 additions and 56 deletions

View File

@@ -1,3 +1,5 @@
require "pattern"
imp = {} imp = {}
local DEBUG_FN_NAME = false local DEBUG_FN_NAME = false
@@ -163,19 +165,23 @@ function imp.new(_hab,_x,_y)
timers={mood=150,target=150, shot=2500, super=20000, flip_wait=50}, --anterior *_cooldown timers={mood=150,target=150, shot=2500, super=20000, flip_wait=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
paths={}, -- Llista de "camins" a fer des d'un hot_point
-- paths_active = 0, -- Número de path seleccionat -- hot_points={}, -- llista de punts del mapa on anar
pattern={}, -- Instruccions per a moviment per patró, anterior path -- paths={}, -- Llista de "camins" a fer des d'un hot_point
pattern_point=1, -- anterior path_curr_action -- -- paths_active = 0, -- Número de path seleccionat
target={}, -- Punt al que anar -- pattern={}, -- Instruccions per a moviment per patró, anterior path
-- pattern_point=1, -- anterior path_curr_action
-- target={}, -- Punt al que anar
shot_target=nil, -- a qui disparar shot_target=nil, -- a qui disparar
analisis = {}, -- memoria per a guardar el resultat de l'analisis analisis = {}, -- memoria per a guardar el resultat de l'analisis
hot_points={}, -- llista de punts del mapa on anar
step_length=1, step_length=1,
falling = 0, falling = 0,
action_event="", -- Event d'acció que ha passat action_event="", -- Event d'acció que ha passat
jump_height=0, jump_height=0,
max_jump_height=24, max_jump_height=24,
pattern= pattern.new(),
vmove_space = 1,
-- funcions -- funcions
next_frame = imp.staying_next_frame, -- funcio de seleccio del frame next_frame = imp.staying_next_frame, -- funcio de seleccio del frame
choose_mood = imp.choose_mood, choose_mood = imp.choose_mood,
@@ -207,6 +213,7 @@ function imp.new(_hab,_x,_y)
land = imp.land, land = imp.land,
shot = imp.shot, shot = imp.shot,
advance=imp.advance, advance=imp.advance,
jump=imp.jump,
_moure=imp._moure, -- temporal debug _moure=imp._moure, -- temporal debug
} }
@@ -280,12 +287,12 @@ end
function imp:update_normal() function imp:update_normal()
print("-------------------------------------------------") print("-------------------------------------------------")
print("") print("")
if self.pattern.target then print("TARGET= "..self.pattern.target) end if self.mood~=self.moods.stop then print("TARGET= "..self.pattern:target()) end
if self.pattern.target then print("ACTIONS= "..#self.pattern.actions) end if self.mood~=self.moods.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()
-- analisis -- analisis
self:analyze_env() self:analyze_env()
@@ -435,9 +442,11 @@ if DEBUG_FN_NAME then print("analyze_env") end
-- Ha arribat a destí -- Ha arribat a destí
self.analisis.target_reached = false self.analisis.target_reached = false
if not empty_table(self.target) then print("TARGET= "..self.pattern:target())
if not empty_table(self.pattern:target_node()) then
-- Target reached -- Target reached
if collision(self, self.target) then self.analisis.target_reached=true end if self.pattern:in_target(self) then self.analisis.target_reached=true end
-- if collision(self, self.pattern:target_node()) then self.analisis.target_reached=true end
end end
end end
@@ -468,23 +477,23 @@ if not DEBUG_FN_NAME then print("pattern_get_next_path") end
end end
function imp:create_hot_points() --OK function imp:create_hot_points() --OK
self.hot_points[1] = point.new(44, 5, 1, 0, 8,"P1") self.pattern.node[1] = point.new(44, 5, 1, 0, 8,"P1")
self.hot_points[2] = point.new(45, 8, 1, 0, 8,"P2") self.pattern.node[2] = point.new(45, 8, 1, 0, 8,"P2")
self.hot_points[3] = point.new(44,12, 5, 8, 0,"P3") self.pattern.node[3] = point.new(44,12, 5, 8, 0,"P3")
self.hot_points[4] = point.new(54, 5, 4, -8, 0,"P4") self.pattern.node[4] = point.new(54, 5, 4, -8, 0,"P4")
self.hot_points[5] = point.new(54,12, 4, 8, 0,"P5") self.pattern.node[5] = point.new(54,12, 4, 8, 0,"P5")
self.hot_points[6] = point.new(55, 8, 4, 8, 0,"P6") self.pattern.node[6] = point.new(55, 8, 4, 8, 0,"P6")
self.hot_points[7] = point.new(55, 8, 0, 0, 0,"P7") self.pattern.node[7] = point.new(55, 8, 0, 0, 0,"P7")
self.hot_points[8] = point.new(54, 5, 0, 0, 0,"P8") self.pattern.node[8] = point.new(54, 5, 0, 0, 0,"P8")
self.hot_points[9] = point.new(54, 9, 1, 0, 0,"P9") self.pattern.node[9] = point.new(54, 9, 1, 0, 0,"P9")
self.hot_points[10] = point.new(55,4, 1, 0, 0,"P10") self.pattern.node[10] = point.new(55, 4, 1, 0, 0,"P10")
-- self.hot_points[11] = abad -- self.hot_points[11] = abad
-- self.hot_points[11] = point.new(45, 1, 5, 8, 0,"altar-R") -- self.hot_points[11] = point.new(45, 1, 5, 8, 0,"altar-R")
-- self.hot_points[12] = point.new(44,11, 5, 0, 0,"altar-L") -- self.hot_points[12] = point.new(44,11, 5, 0, 0,"altar-L")
end end
function imp:load_pattern_paths() --OK function imp:load_pattern_paths() --OK
self.paths[1] = { self.pattern.path[1] = {
{target=3, actions= {{action="right" , event="prefall"}, {target=3, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"}, {action="jumpfwd", event="land"},
{action="right" , event="target"}}}, {action="right" , event="target"}}},
@@ -505,7 +514,7 @@ function imp:load_pattern_paths() --OK
{action="left" , event="target"}}}, {action="left" , event="target"}}},
{target=5, actions= {{action="right" , event="target"}}} {target=5, actions= {{action="right" , event="target"}}}
} }
self.paths[2] = { self.pattern.path[2] = {
{target=3, actions= {{action="left" , event="prefall"}, {target=3, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"}, {action="jumpfwd", event="land"},
{action="left" , event="target"}}}, {action="left" , event="target"}}},
@@ -526,7 +535,7 @@ function imp:load_pattern_paths() --OK
{action="right" , event="target"}}}, {action="right" , event="target"}}},
{target=5, actions= {{action="left" , event="target"}}} {target=5, actions= {{action="left" , event="target"}}}
} }
self.paths[3] = { self.pattern.path[3] = {
{target=1, actions={{action="left" , event="prefall"}, {target=1, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"}, {action="jumpfwd", event="land"},
{action="left" , event="prefall"}, {action="left" , event="prefall"},
@@ -544,23 +553,23 @@ function imp:load_pattern_paths() --OK
{target=6, actions={{action="left" , event="land"}, {target=6, actions={{action="left" , event="land"},
{action="right" , event="target"}}} {action="right" , event="target"}}}
} }
self.paths[4] = { self.pattern.path[4] = {
{target=1, actions={{action="jump" , event="target"}}}, {target=1, actions={{action="jump" , event="target"}}},
{target=5, actions={{action="right", event="target"}}}, {target=5, actions={{action="right", event="target"}}},
{target=6, actions={{action="right", event="target"}}}, {target=6, actions={{action="right", event="target"}}},
{target=8, actions={{action="jump" , event="target"}}}, {target=8, actions={{action="jump" , event="target"}}},
} }
self.paths[5] = { self.pattern.path[5] = {
{target=4, actions={{action="left" , event="target"}}}, {target=4, actions={{action="left" , event="target"}}},
{target=6, actions={{action="right", event="target"}}} {target=6, actions={{action="right", event="target"}}}
} }
self.paths[6] = { self.pattern.path[6] = {
{target=2, actions={{action="jump", event="target"}}}, {target=2, actions={{action="jump", event="target"}}},
{target=5, actions={{action="left", event="target"}}}, {target=5, actions={{action="left", event="target"}}},
{target=7, actions={{action="jump", event="target"}}}, {target=7, actions={{action="jump", event="target"}}},
{target=4, actions={{action="left", event="target"}}} {target=4, actions={{action="left", event="target"}}}
} }
self.paths[7] = { self.pattern.path[7] = {
{target=5, actions={{action="left" , event="prefall"}, {target=5, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"}, {action="jumpfwd", event="land"},
{action="left" , event="target"}}}, {action="left" , event="target"}}},
@@ -569,7 +578,7 @@ function imp:load_pattern_paths() --OK
{action="left" , event="target"}}}, {action="left" , event="target"}}},
{target=2, actions={{action="jump" , event="target"}}} {target=2, actions={{action="jump" , event="target"}}}
} }
self.paths[8] = { self.pattern.path[8] = {
{target=5, actions={{action="right" , event="prefall"}, {target=5, actions={{action="right" , event="prefall"},
{action="jumpfwd" , event="land"}, {action="jumpfwd" , event="land"},
{action="right" , event="target"}}}, {action="right" , event="target"}}},
@@ -578,7 +587,7 @@ function imp:load_pattern_paths() --OK
{action="right" , event="target"}}}, {action="right" , event="target"}}},
{target=1, actions={{action="jump" , event="target"}}} {target=1, actions={{action="jump" , event="target"}}}
} }
self.paths[9] = { self.pattern.path[9] = {
{target=5, actions={{action="right" , event="target"}}}, {target=5, actions={{action="right" , event="target"}}},
{target=1, actions={{action="left" , event="prefall"}, {target=1, actions={{action="left" , event="prefall"},
{action="jumpfwd" , event="land"}, {action="jumpfwd" , event="land"},
@@ -586,7 +595,7 @@ function imp:load_pattern_paths() --OK
{action="jump" , event="target"}}}, {action="jump" , event="target"}}},
{target=4, actions={{action="left" , event="target"}}} {target=4, actions={{action="left" , event="target"}}}
} }
self.paths[10] = { self.pattern.path[10] = {
{target=5, actions={{action="left" , event="target"}}}, {target=5, actions={{action="left" , event="target"}}},
{target=2, actions={{action="right" , event="prefall"}, {target=2, actions={{action="right" , event="prefall"},
{action="jumpfwd" , event="land"}, {action="jumpfwd" , event="land"},
@@ -594,10 +603,10 @@ function imp:load_pattern_paths() --OK
{action="jump" , event="target"}}}, {action="jump" , event="target"}}},
{target=6, actions={{action="right" , event="target"}}} {target=6, actions={{action="right" , event="target"}}}
} }
self.paths[11] = { self.pattern.path[11] = {
{target=4, actions={{action="left", event="target"}}} {target=4, actions={{action="left", event="target"}}}
} }
self.paths[12] = { self.pattern.path[12] = {
{target=6, actions={{action="right", event="target"}}} {target=6, actions={{action="right", event="target"}}}
} }
@@ -608,9 +617,9 @@ if DEBUG_FN_NAME then print("fight") end
self:create_hot_points() self:create_hot_points()
self:load_pattern_paths() self:load_pattern_paths()
self.target = self.hot_points[6] self.pattern.manual_target = self.pattern.node[6]
self.pattern = {target=6, actions={{action="right",event="target"}}} self.pattern.manual_path = {target=6, actions={{action="right",event="target"}}}
print("imp2.lua -> #self.pattern.actions= "..#self.pattern.actions)
self.shot_target = abad self.shot_target = abad
self.mood=self.moods.chase self.mood=self.moods.chase

View File

@@ -110,29 +110,44 @@ if DEBUG_FN_NAME then print("free_next_action") end
return action return action
end end
-- function imp:pattern_next_action()
-- if DEBUG_FN_NAME then print("pattern_next_action") end
-- print("self.pattern_point= ".. self.pattern_point)
-- local pattern_event = self.pattern.actions[self.pattern_point].event
-- print("imp_action.lua -> pattern_next_action -> "..self.action_event.."~="..pattern_event)
-- -- Si no s'ha donat l'event mantindre el moviment
-- if self.action_event~=pattern_event then
-- action = self.actions[self.pattern.actions[self.pattern_point].action]
-- else
-- -- Si s'ha donat l'event avançar el punter del path
-- -- En el path
-- self.pattern_point = self.pattern_point + 1
-- -- Si no queden accions carregar el següent path
-- print(self.pattern_point..">#"..#self.pattern.actions)
-- if self.pattern_point>#self.pattern.actions then
-- self:pattern_get_next_path()
-- end
-- print("-self.pattern_point= ".. self.pattern_point)
-- action = self:pattern_action()
-- end
-- self.action_event = ""; -- Action_event processat
-- return action
-- end
function imp:pattern_next_action() function imp:pattern_next_action()
if DEBUG_FN_NAME then print("pattern_next_action") end if DEBUG_FN_NAME then print("pattern_next_action") end
print("self.pattern_point= ".. self.pattern_point) local pattern_event = self.pattern:event()
local pattern_event = self.pattern.actions[self.pattern_point].event print("imp_action.lua -> pattern_next_action -> "..self.action_event.."~="..self.pattern:event())
print("imp_action.lua -> pattern_next_action -> "..self.action_event.."~="..pattern_event)
-- Si no s'ha donat l'event mantindre el moviment -- Si no s'ha donat l'event mantindre el moviment
if self.action_event~=pattern_event then if self.action_event~=self.pattern:event() then
action = self.actions[self.pattern.actions[self.pattern_point].action] action = self.pattern:action()
else else
-- Si s'ha donat l'event avançar el punter del path -- Següent acció
-- En el path action = self.pattern:next_action()
self.pattern_point = self.pattern_point + 1
-- Si no queden accions carregar el següent path
print(self.pattern_point..">#"..#self.pattern.actions)
if self.pattern_point>#self.pattern.actions then
self:pattern_get_next_path()
end
print("-self.pattern_point= ".. self.pattern_point)
action = self:pattern_action()
end end
self.action_event = ""; -- Action_event processat self.action_event = ""; -- Action_event processat
return action return self.actions[action]
end end
function imp:pattern_action() function imp:pattern_action()

View File

@@ -74,6 +74,8 @@ end
function imp:controller_input() function imp:controller_input()
if DEBUG_FN_NAME then print("controller_input") end if DEBUG_FN_NAME then print("controller_input") end
print(self.action)
--To Do: JumpFWD --To Do: JumpFWD
if self.action == self.actions.right or self.action == self.actions.left then if self.action == self.actions.right or self.action == self.actions.left then
self.movement = self.action self.movement = self.action

152
data/pattern.lua Normal file
View File

@@ -0,0 +1,152 @@
pattern={}
function pattern.new()
return {
node = {}, -- array de punts ( node[1] = punt1,... node[n] = puntN )
path = {}, -- array de camins entre nodes ( path[1] = { {target=2, actions={ {action="right", until="land"} ...} } ...} )
path_idx = 0, -- punter a path actiu (coincidix en el node)
target_idx = 1, -- punter a target actiu, no al número de target si no a l'index dins del path
action_idx = 1, -- punter a l'acció actual
manual_target = {},
manual_path = {},
next_action = pattern.next_action,
next_target = pattern.next_target,
in_target = pattern.in_target,
target = pattern.target,
target_node = pattern.target_node,
action = pattern.action,
event = pattern.event,
actions = pattern.actions,
recovery = pattern.recovery
}
end
-- Necessite saber quina estrategia aplicar abans de moure al següent target
-- ara aleatori
function pattern:origin()
return self.node[self.path_idx]
end
function pattern:origin_num ()
return self.path_idx
end
function pattern:set_path ( num )
self.path_idx = num
end
function pattern:get_path ( num )
return self.path[num]
end
-- [OK] Torna el id del target actual
-- function pattern:get_target ( )
-- return self.path[self.path_idx][target_idx].target
-- end
function pattern:target()
print("pattern:target 1")
if not empty_table(self.manual_path) then
print("pattern:target 2 a")
return self.manual_path.target
else
print("pattern:target 2 b")
return self.path[self.path_idx][self.target_idx].target
end
end
-- [OK] Torna la llista d'accions actual
-- function pattern:get_actions ( )
-- return self.path[self.path_idx][target_idx].actions
-- end
function pattern:actions()
if not empty_table(self.manual_path) then
return self.manual_path.actions
else
return self.path[self.path_idx][self.target_idx].actions
end
end
-- [OK] Torna l'acció actual
-- function pattern:get_action ( )
-- return self.path[self.path_idx][target_idx].actions[self.action_idx]
-- end
function pattern:action()
if not empty_table(self.manual_path) then
return self.manual_path.actions[self.action_idx].action
else
return self.path[self.path_idx][self.target_idx].actions[self.action_idx].action
end
end
-- [OK] Torna l'event actual
function pattern:event()
if not empty_table(self.manual_path) then
return self.manual_path.actions[self.action_idx].event
else
return self.path[self.path_idx][self.target_idx].actions[self.action_idx].event
end
end
-- [OK] Obtindre la següent acció a fer
-- function pattern:next()
-- self.action_idx = self.action_idx+1
-- if self.action_idx>#self.path[self.path_idx][self.target_idx].actions then
-- pattern:next_target() -- pasar per paràmetre o no
-- end
-- return pattern:action()
-- end
function pattern:next_action ( )
print("pattern:next_action 1")
local num_actions = 0
-- index a la següent acció
self.action_idx = self.action_idx+1
-- comprovar si no queden accions disponibles
if not empty_table(self.manual_path) then
num_actions = #self.manual_path.actions
else
num_actions = #self.path[self.path_idx][self.target_idx].actions
end
print("pattern:next_action 2")
if self.action_idx>num_actions then
-- avançar el target
self:next_target() -- pasar per paràmetre o no
end
print("pattern:next_action 3")
-- Tornar l'acció
return self:action()
end
-- [OK] Canvia al següent target segons el path o al que se li pase
function pattern:next_target( target )
print("pattern:next_target 1")
target = target or self:target()
print("pattern:next_target 2")
-- Si hi ha una entrada manual eliminar-la
if not empty_table(self.manual_path) then
self.manual_path = {}
self.manual_target = {}
end
print("pattern:next_target 3")
-- target id
self.path_idx = target
-- tindre en compte l'estrategia per a triar el següent target des del path actual per ara aleatori
self.target_idx = math.random(#self.path[self.path_idx])
self.action_idx = 1
print("pattern:next_target 4")
end
-- [OK] Torna el node del target actual
function pattern:target_node()
return self.node[self:target()]
end
-- Comprova si point ha arribat al target (Afegir precissio per si volem que estiga nomes tocar, dins o abans d'eixir?)
function pattern:in_target( area )
return collision( area, self:target_node() )
end
function pattern:recovery( point, target)
end