Decaying levels and Zone:getGenerator

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Decaying levels and Zone:getGenerator

#1 Post by yufra »

The current ToME decay level code is found here:

Code: Select all

	-- Decay level ?
	if self.level.last_turn and self.level.data.decay and self.level.last_turn + self.level.data.decay[1] * 10 < game.turn then
		local nb_actor, remain_actor = self.level:decay(Map.ACTOR, function(e) return not e.unique and self.level.last_turn + rng.range(self.level.data.decay[1], self.level.data.decay[2]) < game.turn * 10 end)
		local nb_object, remain_object = self.level:decay(Map.OBJECT, function(e) return not e.unique and self.level.last_turn + rng.range(self.level.data.decay[1], self.level.data.decay[2]) < game.turn * 10 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
A bug in shoob's module showed that if the zone's zone.lua does not have an object generator defined the lua code crashes on the zone:getGenerator("object", self.level) call from above. Looking at Zone:getGenerator it is obvious why. There is no check that level.data.generator[what] exists before trying to access it. Maybe the zone:getGenerator should return nil if level.data.generator[what] does not exist?

Code: Select all

function _M:getGenerator(what, level, spots)
	print("[GENERATOR] requiring", what, level.data.generator and level.data.generator[what] and level.data.generator[what].class)
	return require(level.data.generator[what].class).new(
			self,
			level.map,
			level,
			spots
		)
end
EDIT: As a side note, this is how the Zone:newLevel code works. Maybe the checks should be internal to getGenerator, or at least the decay level should have similar checks. Thoughts?

Code: Select all

	if level_data.generator.actor and level_data.generator.actor.class then
		local generator = self:getGenerator("actor", level, spots)
		generator:generate()
	end
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: Decaying levels and Zone:getGenerator

#2 Post by darkgod »

I changed it with asserts to give info about why it fails :)
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: Decaying levels and Zone:getGenerator

#3 Post by yufra »

Great!
<DarkGod> lets say it's intended

Post Reply