Skip to content
Snippets Groups Projects
Commit ff384776 authored by dg's avatar dg
Browse files

create home path

AIs base
simple AI
fix core GameEnergyBased class to allow actors with too muych energy to act anyway


git-svn-id: http://svn.net-core.org/repos/t-engine4@81 51575b47-30f0-44d4-a5cc-537603b46e54
parent 0790de43
No related branches found
No related tags found
No related merge requests found
......@@ -24,9 +24,9 @@ function _M:tick()
for uid, e in pairs(self.level.entities) do
if e.energy and e.energy.value < self.energy_to_act then
e.energy.value = (e.energy.value or 0) + self.energy_per_tick * (e.energy.mod or 1)
if e.energy.value >= self.energy_to_act and e.act then
e:act(self)
end
end
if e.energy.value >= self.energy_to_act and e.act then
e:act(self)
end
end
end
......
......@@ -82,7 +82,12 @@ end
--- Setup write dir for a module
-- Static
function _M:setupWrite(mod)
local base = fs.getHomePath() .. fs.getPathSeparator() .. mod.short_name .. fs.getPathSeparator()
-- Create module directory
fs.setWritePath(engine.homepath)
fs.mkdir(mod.short_name)
-- Enter module directory
local base = engine.homepath .. fs.getPathSeparator() .. mod.short_name
fs.setWritePath(base)
fs.mount(base, "/", false)
end
......
-- Defines a simple AI building blocks
-- Target nearest and move/attack it
newAI("move_simple", function(self)
if self.ai_target.actor then
local l = line.new(self.x, self.y, self.ai_target.actor.x, self.ai_target.actor.y)
local lx, ly = l()
self:move(lx, ly)
elseif self.ai_target.x and self.ai_target.y then
local l = line.new(self.x, self.y, self.ai_target.x, self.ai_target.y)
local lx, ly = l()
self:move(lx, ly)
end
end)
newAI("target_simple", function(self)
-- if self.ai_state.target_decay and self.ai_state.target_decay > 0 then
-- if game.turn % 100 ~= 0 then return end
-- Find closer ennemy and target it
self.ai_target.actor = nil
core.fov.calc_circle(self.x, self.y, self.sight, function(self, lx, ly)
if game.level.map:checkEntity(lx, ly, Map.TERRAIN, "block_sight") then return true end
-- get and test the actor, if we are neutral or friendly, ignore it
local act = game.level.map(lx, ly, Map.ACTOR)
if not act then return end
if self:reactionToward(act) >= 0 then return end
-- If it is closer to the current target, target it
if not self.ai_target.actor then
self.ai_target.actor = act
elseif core.fov.distance(self.x, self.y, act.x, act.y) < core.fov.distance(self.x, self.y, self.ai_target.actor.x, self.ai_target.actor.y) then
self.ai_target.actor = act
end
end, function()end, self)
end)
newAI("simple", function(self)
self:runAI("target_simple")
self:runAI("move_simple")
end)
......@@ -11,6 +11,13 @@ require "engine.Savefile"
require "engine.Tiles"
engine.Tiles.prefix = "/data/gfx/"
-- Setup the user directory
engine.homepath = fs.getUserPath()..fs.getPathSeparator()..fs.getHomePath()..fs.getPathSeparator().."4.0"
fs.setWritePath(fs.getUserPath())
fs.mkdir(fs.getHomePath())
fs.mkdir(fs.getHomePath().."/4.0/")
fs.setWritePath(fs.getHomePath())
-- Setup a default key handler
local key = engine.KeyCommand.new()
key:setCurrent()
......
......@@ -3,6 +3,29 @@ require "engine.class"
--- Handles actors artificial intelligence (or dumbness ... ;)
module(..., package.seeall, class.make)
_M.ai_def = {}
--- Deinfe AI
function _M:newAI(name, fct)
_M.ai_def[name] = fct
end
--- Defines AIs
-- Static!
function _M:loadDefinition(dir)
for i, file in ipairs(fs.list(dir)) do
if file:find("%.lua$") then
local f, err = loadfile(dir.."/"..file)
if not f and err then error(err) end
setfenv(f, setmetatable({
Map = require("engine.Map"),
newAI = function(name, fct) self:newAI(name, fct) end,
}, {__index=_G}))
f()
end
end
end
function _M:init(t)
self.ai_state = {}
self.ai_target = {}
......@@ -11,7 +34,7 @@ function _M:init(t)
end
function _M:aiFindTarget()
self.target = game.player
self.ai_target.actor = game.player
end
function _M:onTakeHit(value, src)
......@@ -19,6 +42,15 @@ end
--- Main entry point for AIs
function _M:doAI()
local l = line.new(self.x, self.y, self.target.x, self.target.y)
self:move()
if not self.ai then return end
-- If we have a target but it is dead (it was not yet garbage collected but it'll come)
-- we forget it
if self.ai_target.actor and self.ai_target.actor.dead then self.ai_target.actor = nil end
self:runAI(self.ai)
end
function _M:runAI(ai)
return _M.ai_def[ai](self)
end
......@@ -13,6 +13,7 @@ local Actor = require "mod.class.Actor"
local ActorStats = require "engine.interface.ActorStats"
local ActorResource = require "engine.interface.ActorResource"
local ActorTalents = require "engine.interface.ActorTalents"
local ActorAI = require "engine.interface.ActorAI"
local Player = require "mod.class.Player"
local NPC = require "mod.class.NPC"
......@@ -54,6 +55,8 @@ function _M:run()
ActorStats:defineStat("Constitution", "con", 10, 1, 100, "Constitution defines your character's ability to withstand and resist damage. It increases your maximun life and physical resistance.")
-- Actor autolevel schemes
dofile("/data/autolevel_schemes.lua")
-- Actor AIs
ActorAI:loadDefinition("/engine/ai/")
self.log = LogDisplay.new(0, self.h * 0.80, self.w * 0.5, self.h * 0.20, nil, nil, nil, {255,255,255}, {30,30,30})
self.player_display = PlayerDisplay.new(0, 0, self.w * 0.2, self.h * 0.8, {30,30,0})
......
......@@ -6,16 +6,19 @@ module(..., package.seeall, class.inherit(mod.class.Actor, engine.interface.Acto
function _M:init(t)
mod.class.Actor.init(self, t)
ActorAI(self, t)
ActorAI.init(self, t)
end
function _M:act()
-- Do basic actor stuff
mod.class.Actor.act(self)
ActorAI:doAI()
-- Let the AI think .... beware of Shub !
self:doAI()
end
--- Called by ActorLife interface
-- We use it to pass aggression values to the AIs
function _M:onTakeHit(value, src)
self:aiAddThreat(value, src)
-- self:aiAddThreat(value, src)
end
......@@ -5,6 +5,7 @@ return {
display = "D", color_r=255,
level_range = {1, 10}, exp_worth = 1,
autolevel = "warrior",
ai = "simple",
max_life = 20,
max_mana = 1000,
max_stamina = 1000,
......@@ -19,6 +20,7 @@ return {
faction = "poorsods",
level_range = {1, 4}, exp_worth = 1,
autolevel = "caster",
ai = "simple",
max_life = 30,
max_mana = 1000,
max_stamina = 1000,
......
......@@ -7,7 +7,7 @@ return {
-- persistant = true,
generator = {
map = {
class= "engine.generator.map.Empty",
class= "engine.generator.map.Rooms",
floor = "FLOOR",
wall = "WALL",
up = "UP",
......@@ -16,7 +16,7 @@ return {
},
actor = {
class = "engine.generator.actor.Random",
nb_npc = {400, 400},
nb_npc = {40, 40},
level_range = {5, 10},
adjust_level_to_player = {-2, 2},
},
......
......@@ -34,6 +34,7 @@ project "TEngine"
files { "src/*.c", }
links { "physfs", "lua", "fov", "luasocket" }
defines { "_DEFAULT_VIDEOMODE_FLAGS_='SDL_HWSURFACE|SDL_DOUBLEBUF'" }
defines { [[TENGINE_HOME_PATH='".t-engine"']] }
configuration "macosx"
linkoptions { "mac/SDLmain.m", "-framework SDL", "-framework SDL_gfx", "-framework SDL_image", "-framework SDL_ttf", "-framework SDL_mixer", "-framework Cocoa" }
......@@ -43,7 +44,15 @@ configuration "macosx"
configuration "not macosx"
links { "SDL", "SDL_ttf", "SDL_image", "SDL_gfx", "SDL_mixer" }
configuration "windows"
defines { [[TENGINE_HOME_PATH='"T-Engine"']] }
----------------------------------------------------------------
----------------------------------------------------------------
-- Librairies used by T-Engine
----------------------------------------------------------------
----------------------------------------------------------------
project "physfs"
kind "StaticLib"
language "C"
......
......@@ -965,7 +965,13 @@ static int lua_fs_set_write_dir(lua_State *L)
static int lua_fs_get_home_path(lua_State *L)
{
lua_pushfstring(L, "%s%s.tengine%s4.0", PHYSFS_getUserDir(), PHYSFS_getDirSeparator(), PHYSFS_getDirSeparator());
lua_pushstring(L, TENGINE_HOME_PATH);
return 1;
}
static int lua_fs_get_user_path(lua_State *L)
{
lua_pushstring(L, PHYSFS_getUserDir());
return 1;
}
......@@ -986,6 +992,7 @@ static const struct luaL_reg fslib[] =
{"setWritePath", lua_fs_set_write_dir},
{"getPathSeparator", lua_fs_get_path_separator},
{"getRealPath", lua_fs_get_real_path},
{"getUserPath", lua_fs_get_user_path},
{"getHomePath", lua_fs_get_home_path},
{"mount", lua_fs_mount},
{"umount", lua_fs_umount},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment