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

actor/object decaying

most levels will lose their object/actors after a while of not
visiting them and be replaced by new ones


git-svn-id: http://svn.net-core.org/repos/t-engine4@489 51575b47-30f0-44d4-a5cc-537603b46e54
parent 6e28a109
No related branches found
No related tags found
No related merge requests found
Showing
with 128 additions and 68 deletions
......@@ -98,3 +98,27 @@ function _M:removed()
end
end end
end
--- Decay the level
-- Decaying means we look on the map for the given type of entities and if we are allowed to we delete them
-- @param what what Map feature to decay (ACTOR, OBJECT, ...)
-- @param check either a boolean or a function, if true the given entity will be decayed
-- @return the number of decayed entities and the total number of such entities remaining
function _M:decay(what, check)
local total, nb = 0, 0
for i = 0, self.map.w - 1 do for j = 0, self.map.h - 1 do
local e = self.map(i, j, what)
if e and not e.no_decay and util.getval(check, e, i, j) then
print("[DECAY] decaying", e.uid, e.name)
if self:hasEntity(e) then
self:removeEntity(e)
else
self.map:remove(i, j, what)
end
nb = nb + 1
elseif e then
total = total + 1
end
end end
return nb, total
end
......@@ -388,6 +388,15 @@ function _M:getLevel(game, lev, old_lev, no_close)
return level
end
function _M:getGenerator(what, level, spots)
return require(level.data.generator[what].class).new(
self,
level.map,
level,
spots
)
end
function _M:newLevel(level_data, lev, old_lev, game)
local map = self.map_class.new(level_data.width, level_data.height)
if level_data.all_lited then map:liteAll(0, 0, map.w, map.h) end
......@@ -422,34 +431,19 @@ function _M:newLevel(level_data, lev, old_lev, game)
-- Generate objects
if level_data.generator.object then
local generator = require(level_data.generator.object.class).new(
self,
map,
level,
spots
)
local generator = self:getGenerator("object", level, spots)
generator:generate()
end
-- Generate traps
if level_data.generator.trap then
local generator = require(level_data.generator.trap.class).new(
self,
map,
level,
spots
)
local generator = self:getGenerator("trap", level, spots)
generator:generate()
end
-- Generate actors
if level_data.generator.actor then
local generator = require(level_data.generator.actor.class).new(
self,
map,
level,
spots
)
local generator = self:getGenerator("actor", level, spots)
generator:generate()
end
......
......@@ -23,8 +23,7 @@ require "engine.Generator"
module(..., package.seeall, class.inherit(engine.Generator))
function _M:init(zone, map, level)
engine.Generator.init(self, zone, map)
self.level = level
engine.Generator.init(self, zone, map, level)
local data = level.data.generator.actor
if data.adjust_level then
......@@ -38,22 +37,7 @@ function _M:init(zone, map, level)
end
function _M:generate()
for i = 1, rng.range(self.nb_npc[1], self.nb_npc[2]) do
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local m = self.zone:makeEntity(self.level, "actor", f)
if m then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while not m:canMove(x, y) and tries < 100 do
x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, m, "actor", x, y)
end
end
end
self:regenFrom(1)
if self.level.level < self.zone.max_level then return end
......@@ -75,3 +59,26 @@ function _M:generate()
end
end
end
function _M:generateOne()
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local m = self.zone:makeEntity(self.level, "actor", f)
if m then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while not m:canMove(x, y) and tries < 100 do
x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, m, "actor", x, y)
end
end
end
function _M:regenFrom(current)
for i = current, rng.range(self.nb_npc[1], self.nb_npc[2]) do
self:generateOne()
end
end
......@@ -23,8 +23,7 @@ require "engine.Generator"
module(..., package.seeall, class.inherit(engine.Generator))
function _M:init(zone, map, level)
engine.Generator.init(self, zone, map)
self.level = level
engine.Generator.init(self, zone, map, level)
local data = level.data.generator.object
if data.adjust_level_to_player and game:getPlayer() then
......@@ -36,20 +35,28 @@ function _M:init(zone, map, level)
end
function _M:generate()
for i = 1, rng.range(self.nb_object[1], self.nb_object[2]) do
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local o = self.zone:makeEntity(self.level, "object", f)
if o then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while (self.map:checkEntity(x, y, Map.TERRAIN, "block_move") or self.map(x, y, Map.OBJECT)) and tries < 100 do
x, y = rng.range(0, self.map.w-1), rng.range(0, self.map.h-1)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, o, "object", x, y)
end
self:regenFrom(1)
end
function _M:generateOne()
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local o = self.zone:makeEntity(self.level, "object", f)
if o then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while (self.map:checkEntity(x, y, Map.TERRAIN, "block_move") or self.map(x, y, Map.OBJECT)) and tries < 100 do
x, y = rng.range(0, self.map.w-1), rng.range(0, self.map.h-1)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, o, "object", x, y)
end
end
end
function _M:regenFrom(current)
for i = current, rng.range(self.nb_object[1], self.nb_object[2]) do
self:generateOne()
end
end
......@@ -23,8 +23,7 @@ require "engine.Generator"
module(..., package.seeall, class.inherit(engine.Generator))
function _M:init(zone, map, level)
engine.Generator.init(self, zone, map)
self.level = level
engine.Generator.init(self, zone, map, level)
local data = level.data.generator.trap
self.filters = data.filters
......@@ -33,20 +32,28 @@ function _M:init(zone, map, level)
end
function _M:generate()
for i = 1, rng.range(self.nb_trap[1], self.nb_trap[2]) do
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local o = self.zone:makeEntity(self.level, "trap", f)
if o then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while (self.map:checkEntity(x, y, Map.TERRAIN, "block_move") or self.map(x, y, Map.TRAP)) and tries < 100 do
x, y = rng.range(0, self.map.w-1), rng.range(0, self.map.h-1)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, o, "trap", x, y)
end
self:regenFrom(1)
end
function _M:generateOne()
local f = nil
if self.filters then f = self.filters[rng.range(1, #self.filters)] end
local o = self.zone:makeEntity(self.level, "trap", f)
if o then
local x, y = rng.range(0, self.map.w), rng.range(0, self.map.h)
local tries = 0
while (self.map:checkEntity(x, y, Map.TERRAIN, "block_move") or self.map(x, y, Map.TRAP)) and tries < 100 do
x, y = rng.range(0, self.map.w-1), rng.range(0, self.map.h-1)
tries = tries + 1
end
if tries < 100 then
self.zone:addEntity(self.level, o, "trap", x, y)
end
end
end
function _M:generate()
for i = 1, rng.range(self.nb_trap[1], self.nb_trap[2]) do
self:generateOne()
end
end
......@@ -194,6 +194,7 @@ function _M:leaveLevel(level, lev, old_lev)
else
level.exited.up = {x=self.player.x, y=self.player.y}
end
level.last_turn = game.turn
level:removeEntity(self.player)
end
end
......@@ -220,6 +221,18 @@ function _M:changeLevel(lev, zone)
end
self.zone:getLevel(self, lev, old_lev)
-- Decay level ?
if self.level.last_turn and self.level.data.decay and self.level.last_turn + self.level.data.decay[1] < game.turn then
local nb_actor, remain_actor = self.level:decay(Map.ACTOR, function(e) return self.level.last_turn + rng.range(self.level.data.decay[1], self.level.data.decay[2]) < game.turn end)
local nb_object, remain_object = self.level:decay(Map.OBJECT, function(e) return self.level.last_turn + rng.range(self.level.data.decay[1], self.level.data.decay[2]) < game.turn end)
local gen = self.zone:getGenerator("actor", self.level)
if gen.regenFrom then gen:regenFrom(remain_actor) end
local gen = self.zone:getGenerator("object", self.level)
if gen.regenFrom then gen:regenFrom(remain_object) end
end
-- Move back to old wilderness position
if self.zone.short_name == "wilderness" then
self.player:move(self.player.wild_x, self.player.wild_y, true)
......
......@@ -59,6 +59,7 @@ function _M:placeWorm(s)
if m then
local x, y = util.findFreeGrid(s.x, s.y, 5, true, {[Map.ACTOR]=true})
if x and y then
m.no_decay = true
self.zone:addEntity(self.level, m, "actor", x, y)
end
end
......
......@@ -22,6 +22,7 @@ return {
level_range = {1, 5},
level_scheme = "player",
max_level = 5,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {7, 18},
level_scheme = "player",
max_level = 7,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 40, height = 40,
-- all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {7, 18},
level_scheme = "player",
max_level = 7,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
-- all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {7, 18},
level_scheme = "player",
max_level = 7,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
-- all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {14, 25},
level_scheme = "player",
max_level = 9,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
-- all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {1, 5},
level_scheme = "player",
max_level = 5,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
-- all_remembered = true,
......
......@@ -22,6 +22,7 @@ return {
level_range = {1, 5},
level_scheme = "player",
max_level = 5,
decay = {300, 800},
actor_adjust_level = function(zone, level, e) return zone.base_level + level.level-1 + rng.range(-1,2) end,
width = 50, height = 50,
-- all_remembered = true,
......
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