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

modules can return true from their tick() to pause the ticking until the next...

modules can return true from their tick() to pause the ticking until the next SDL event, reducing CPU usage draastically


git-svn-id: http://svn.net-core.org/repos/t-engine4@591 51575b47-30f0-44d4-a5cc-537603b46e54
parent a338e9a5
No related branches found
No related tags found
No related merge requests found
......@@ -18,13 +18,16 @@
-- darkgod@te4.org
require "engine.class"
local Map = require "engine.Map"
--- Pathfinding using A*
module(..., package.seeall, class.make)
--- Initializes Astar for a map and an actor
function _M:init(map, actor)
self.map = map
self.actor = actor
self.move_cache = {}
end
--- The default heuristic for A*, simple distance
......@@ -58,6 +61,8 @@ end
-- @param ty the end coord
-- @return either nil if no path or a list of nodes in the form { {x=...,y=...}, {x=...,y=...}, ..., {x=tx,y=ty}}
function _M:calc(sx, sy, tx, ty)
if self.actor.changed then
local w, h = self.map.w, self.map.h
local start = self:toSingle(sx, sy)
local stop = self:toSingle(tx, ty)
......@@ -70,7 +75,7 @@ function _M:calc(sx, sy, tx, ty)
local checkPos = function(node, nx, ny)
local nnode = self:toSingle(nx, ny)
if not closed[nnode] and self.map:isBound(nx, ny) and not self.map:checkAllEntities(nx, ny, "block_move", self.actor) then
if not closed[nnode] and self.map:isBound(nx, ny) and not self.map:checkEntity(nx, ny, Map.TERRAIN, "block_move", self.actor) then
local tent_g_score = g_score[node] + 1 -- we can adjust hre for difficult passable terrain
local tent_is_better = false
if not open[nnode] then open[nnode] = true; tent_is_better = true
......@@ -99,10 +104,14 @@ function _M:calc(sx, sy, tx, ty)
closed[node] = true
local x, y = self:toDouble(node)
-- Check right
-- Check sides
checkPos(node, x + 1, y)
checkPos(node, x - 1, y)
checkPos(node, x, y + 1)
checkPos(node, x, y - 1)
checkPos(node, x + 1, y + 1)
checkPos(node, x + 1, y - 1)
checkPos(node, x - 1, y + 1)
checkPos(node, x - 1, y - 1)
end
end
......@@ -60,6 +60,9 @@ function _M:init()
engine.interface.GameMusic.init(self)
engine.interface.GameSound.init(self)
-- Pause at birth
self.paused = true
-- Same init as when loaded from a savefile
self:loaded()
end
......@@ -280,6 +283,7 @@ function _M:tick()
-- (since display is on a set FPS while tick() ticks as much as possible
-- engine.GameEnergyBased.tick(self)
end
if game.paused then return true end
end
--- Called every game turns
......@@ -694,7 +698,7 @@ function _M:setupMouse()
end
game.level.map._map:setScroll(game.level.map.mx, game.level.map.my)
elseif button == "none" then
if self.key.status[self.key._LSHIFT] and self.test_x ~= tmx or self.test_y ~= tmy then
if self.key.status[self.key._LSHIFT] and (self.test_x ~= tmx or self.test_y ~= tmy) then
local Astar = require"engine.Astar"
local a = Astar.new(self.level.map, self.player)
local path = a:calc(self.player.x, self.player.y, tmx, tmy)
......
......@@ -74,6 +74,7 @@ ActorResource:defineResource("Stamina", "stamina", ActorTalents.T_STAMINA_POOL,
ActorResource:defineResource("Mana", "mana", ActorTalents.T_MANA_POOL, "mana_regen", "Mana represents your reserve of magical energies. Each spell cast consumes mana and each sustained spell reduces your maximun mana.")
ActorResource:defineResource("Equilibrium", "equilibrium", ActorTalents.T_EQUILIBRIUM_POOL, "equilibrium_regen", "Equilibrium represents your stand in the grand balance of nature. The closer it is to 0 the more inbalance you are. Higher equilibrium states will impact negatively your ability to use Wild Gifts.")
ActorResource:defineResource("Vim", "vim", ActorTalents.T_VIM_POOL, "vim_regen", "Vim represents the amount of life energies/souls you have stolen. Each corruption talents requires some.")
--ActorResource:defineResource("Destiny", "destiny", nil, "destiny_regen", "")
-- Actor stats
ActorStats:defineStat("Strength", "str", 10, 1, 100, "Strength defines your character's ability to apply physical force. It increases your melee damage, damage with heavy weapons, your chance to resist physical effects, and carrying capacity.")
......
......@@ -47,6 +47,7 @@ int current_game = LUA_NOREF;
bool exit_engine = FALSE;
bool no_sound = FALSE;
bool isActive = TRUE;
bool tickPaused = FALSE;
static int traceback (lua_State *L) {
lua_Debug ar;
......@@ -222,7 +223,8 @@ void on_tick()
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
docall(L, 1, 0);
docall(L, 1, 1);
tickPaused = lua_toboolean(L, -1);
}
/* Gather our frames per second */
......@@ -525,7 +527,8 @@ int main(int argc, char *argv[])
SDL_Event event;
while (!exit_engine)
{
if (!isActive) SDL_WaitEvent(NULL);
if (!isActive || tickPaused) SDL_WaitEvent(NULL);
// else if (tickPaused) SDL_Delay(1000 / 30);
/* handle the events in the queue */
while (SDL_PollEvent(&event))
......@@ -566,6 +569,7 @@ int main(int argc, char *argv[])
case SDL_KEYUP:
/* handle key presses */
on_event(&event);
tickPaused = FALSE;
break;
case SDL_QUIT:
/* handle quit requests */
......@@ -583,7 +587,7 @@ int main(int argc, char *argv[])
}
/* draw the scene */
if (isActive) on_tick();
if (isActive && !tickPaused) on_tick();
}
SDL_Quit();
......
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