diff --git a/data/helpers.lua b/data/helpers.lua index 46a9903..60712ce 100644 --- a/data/helpers.lua +++ b/data/helpers.lua @@ -346,3 +346,10 @@ function debug.write_tile(x, y, yplus, print_type, align ) end draw.text(msg,scr_x+txt_offset+1,scr_y+1+yplus,2) end + +function empty_table(t) + for _ in pairs(t) do + return false + end + return true +end \ No newline at end of file diff --git a/data/imp.lua b/data/imp.lua index 045707a..570712f 100644 --- a/data/imp.lua +++ b/data/imp.lua @@ -67,11 +67,12 @@ function imp.new(_hab, _x, _y) scene_object=false, fight_modes=_fight_modes, mode="stop", - mode_cooldown=1000, + mode_cooldown=500, super_cooldown=2000, shot_cooldown=500, action="", movement="", + movement_type="", enabled=true, disable_reason="", can_warp=false, @@ -81,6 +82,11 @@ function imp.new(_hab, _x, _y) hot_points={}, target={}, old_target={}, + path={}, + paths={}, + action_event="", + path_curr_action=1, + analisis={}, invencible=false, -- direccio=imp.direccio, do_jump=imp.do_jump, @@ -95,7 +101,7 @@ function imp.new(_hab, _x, _y) update=imp.state_normal, land=imp.land, mode_controller=imp.mode_controller, - movement=imp.movement, + --movement=imp.movement, advance=imp.advance, reduce_cooldown=imp.reduce_cooldown, set_fight_mode=imp.set_fight_mode, @@ -103,11 +109,102 @@ function imp.new(_hab, _x, _y) controller_input=imp.controller_input, ia=imp.ia, 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.counter=500 } 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.xself.target.y or self.y "..#self.target) + if not empty_table(self.target) then -- Target reached - if collision(self, self.target) then target_reached=true end - - -- Target aligned - if half_collision(self, self.target) then target_aligned=true end + if collision(self, self.target) then self.analisis.target_reached=true end + -- + -- -- Target aligned + -- 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=self.x+self.bb.x then - action="right" - elseif self.target.x+self.target.bb.xself.target.y or self.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 return action @@ -617,10 +794,12 @@ function imp:set_fight_mode() if self.mode=="stop" then return end if self.mode_cooldown <= 0 then + self:free_movement() -- seleccionar mode d'acció local last_mode=self.mode -- 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) -- -- 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"] -- print("cambiar modo => "..self.fight_modes[self.mode]) - - 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 - self.old_target = self.target - self.target=self.hot_points[new_point] - print("target => "..self.target.id) + -- if self.movement_type == "free" then + 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 + self.old_target = self.target + self.target=self.hot_points[new_point] + print("Next random target => "..self.target.id) + -- else + --if self.action_event=="target" then + -- print(self.old_target.id) + --end + -- end end -- -- if self.mode==last_mode then self.mode=((last_mode+1)%#self.fight_modes)+1 end -- elseif self.mode_cooldown == 0 then @@ -690,31 +876,3 @@ function imp:collision() 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 \ No newline at end of file