From 3803b5ffae9c12717e8afd644679904899fd4015 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 20 Apr 2011 20:08:34 +0000
Subject: [PATCH] plop

git-svn-id: http://svn.net-core.org/repos/t-engine4@3247 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Entity.lua        |  3 +-
 game/modules/tome/class/GameState.lua         | 35 +++++++++++++++++--
 game/modules/tome/data/general/npcs/all.lua   | 26 ++++++++++++--
 .../tome/data/general/npcs/random_zone.lua    | 20 +++++++++++
 .../modules/tome/data/talents/gifts/gifts.lua |  2 +-
 .../tome/data/talents/spells/phantasm.lua     |  1 +
 6 files changed, 79 insertions(+), 8 deletions(-)
 create mode 100644 game/modules/tome/data/general/npcs/random_zone.lua

diff --git a/game/engines/default/engine/Entity.lua b/game/engines/default/engine/Entity.lua
index 1baa4d94a3..1451eb72ae 100644
--- a/game/engines/default/engine/Entity.lua
+++ b/game/engines/default/engine/Entity.lua
@@ -684,8 +684,7 @@ function _M:loadList(file, no_default, res, mod, loaded)
 			end
 
 			local e = newenv.class.new(t, no_default)
-
-			if mod then mod(e) end
+			if type(mod) == "function" then mod(e) end
 
 			res[#res+1] = e
 			if t.define_as then res[t.define_as] = e end
diff --git a/game/modules/tome/class/GameState.lua b/game/modules/tome/class/GameState.lua
index bc9ef5082e..94b819edfe 100644
--- a/game/modules/tome/class/GameState.lua
+++ b/game/modules/tome/class/GameState.lua
@@ -1101,6 +1101,8 @@ function _M:createRandomZone()
 	data.depth = rng.range(2, 4)
 	data.min_lev, data.max_lev = game.player.level, game.player.level + 15
 	data.w, data.h = rng.range(40, 60), rng.range(40, 60)
+	data.max_material_level = util.bound(math.ceil(data.min_lev / 10), 1, 5)
+	data.min_material_level = data.max_material_level - 1
 
 	data.less_dir = rng.table{2, 4, 6, 8}
 	data.more_dir = ({[2]=8, [8]=2, [4]=6, [6]=4})[data.less_dir]
@@ -1138,7 +1140,7 @@ function _M:createRandomZone()
 	------------------------------------------------------------
 	-- Create a boss
 	------------------------------------------------------------
-	local npcs = mod.class.NPC:loadList("/data/general/npcs/all.lua")
+	local npcs = mod.class.NPC:loadList("/data/general/npcs/random_zone.lua")
 	local list = {}
 	for _, e in ipairs(npcs) do
 		if e.rarity and e.level_range and e.level_range[1] <= data.min_lev and (not e.level_range[2] or e.level_range[2] >= data.min_lev) and e.rank > 1 and not e.unique then
@@ -1149,9 +1151,26 @@ function _M:createRandomZone()
 	local boss, boss_id = self:createRandomBoss(base, data.min_lev + data.depth + rng.range(2, 4))
 	npcs[boss_id] = boss
 
+	------------------------------------------------------------
+	-- Entities
+	------------------------------------------------------------
+	local base_nb = math.sqrt(data.w * data.h)
+	local nb_npc = { math.ceil(base_nb * 0.4), math.ceil(base_nb * 0.6) }
+	local nb_trap = { math.ceil(base_nb * 0.1), math.ceil(base_nb * 0.2) }
+	local nb_object = { math.ceil(base_nb * 0.06), math.ceil(base_nb * 0.12) }
+	if rng.percent(20) then nb_trap = {0,0} end
+	if rng.percent(10) then nb_object = {0,0} end
+
+	------------------------------------------------------------
+	-- Name
+	------------------------------------------------------------
 	local ngd = NameGenerator.new(randart_name_rules.default2)
 	local name = ngd:generate()
 	local short_name = name:lower():gsub("[^a-z]", "_")
+
+	------------------------------------------------------------
+	-- Final glue
+	------------------------------------------------------------
 	local zone = engine.Zone.new(short_name, {
 		name = name,
 		level_range = {data.min_lev, data.max_lev},
@@ -1162,10 +1181,13 @@ function _M:createRandomZone()
 		color_shown = data.tint_s,
 		color_obscure = data.tint_o,
 		ambient_music = rng.table(musics),
+		min_material_level = data.min_material_level,
+		max_material_level = data.max_material_level,
 		generator =  {
 			map = layout.gen(data),
-			actor = { class = "engine.generator.actor.Random", nb_npc = {5, 7}, guardian = boss_id },
-			trap = { class = "engine.generator.trap.Random", nb_trap = {3, 3}, },
+			actor = { class = "engine.generator.actor.Random", nb_npc = nb_npc, guardian = boss_id },
+			trap = { class = "engine.generator.trap.Random", nb_trap = nb_trap, },
+			object = { class = "engine.generator.object.Random", nb_object = nb_object, },
 		},
 		npc_list = npcs,
 		grid_list = grids,
@@ -1179,6 +1201,9 @@ function _M:createRandomBoss(base, level)
 	local boss_id = "RND_ZONE_BOSS"
 	local b = base:clone()
 
+	------------------------------------------------------------
+	-- Basic stuff, name, rank, ...
+	------------------------------------------------------------
 	local ngd = NameGenerator.new(randart_name_rules.default)
 	local name = ngd:generate()
 	b.name = name.." the "..b.name
@@ -1218,6 +1243,10 @@ function _M:createRandomBoss(base, level)
 	b[#b+1] = resolvers.drops{chance=100, nb=3, {tome_drops="boss"} }
 	b[#b+1] = resolvers.drop_randart{}
 
+	------------------------------------------------------------
+	-- Apply talents from classes
+	------------------------------------------------------------
+
 	-- Apply a class
 	b.learn_tids = {}
 	local function apply_class(class)
diff --git a/game/modules/tome/data/general/npcs/all.lua b/game/modules/tome/data/general/npcs/all.lua
index 7f6e724ef3..a7fe14e3ad 100644
--- a/game/modules/tome/data/general/npcs/all.lua
+++ b/game/modules/tome/data/general/npcs/all.lua
@@ -17,9 +17,16 @@
 -- Nicolas Casalini "DarkGod"
 -- darkgod@te4.org
 
-local loadIfNot = function(f)
+local random_zone_mode = false
+if type(entity_mod) == "table" then
+	random_zone_mode = entity_mod.random_zone_mode
+	entity_mod = false
+end
+local todo = {}
+
+local loadIfNot = loadIfNot or function(f)
 	if loaded[f] then return end
-	load(f, entity_mod)
+	todo[#todo+1] = {f=f, mod=entity_mod}
 end
 
 -- Load all NPCs anyway but with higher rarity
@@ -73,3 +80,18 @@ loadIfNot("/data/general/npcs/vermin.lua")
 loadIfNot("/data/general/npcs/wight.lua")
 loadIfNot("/data/general/npcs/xorn.lua")
 
+-- Select some random dominant ones for random zones
+if random_zone_mode then
+	local nt = {}
+	local nb = #todo
+	for i = 1, nb do nt[#nt+1] = rng.tableRemove(todo) end
+	todo = nt
+	-- The first 4 are much more likely
+	for i = 4, #todo do
+		todo[i].mod = rarity(4, 35)
+	end
+end
+
+for i = 1, #todo do
+	load(todo[i].f, todo[i].mod)
+end
diff --git a/game/modules/tome/data/general/npcs/random_zone.lua b/game/modules/tome/data/general/npcs/random_zone.lua
new file mode 100644
index 0000000000..29c6c9aa45
--- /dev/null
+++ b/game/modules/tome/data/general/npcs/random_zone.lua
@@ -0,0 +1,20 @@
+-- ToME - Tales of Maj'Eyal
+-- Copyright (C) 2009, 2010, 2011 Nicolas Casalini
+--
+-- This program is free software: you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation, either version 3 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
+--
+-- Nicolas Casalini "DarkGod"
+-- darkgod@te4.org
+
+load("/data/general/npcs/all.lua", {random_zone_mode = true})
diff --git a/game/modules/tome/data/talents/gifts/gifts.lua b/game/modules/tome/data/talents/gifts/gifts.lua
index 4fbc2f9fa0..e836daf5ce 100644
--- a/game/modules/tome/data/talents/gifts/gifts.lua
+++ b/game/modules/tome/data/talents/gifts/gifts.lua
@@ -91,7 +91,7 @@ function setupSummon(self, m, x, y, no_control)
 	m.ai_state.tactic_leash = 100
 	-- Try to use stored AI talents to preserve tweaking over multiple summons
 	m.ai_talents = self.stored_ai_talents and self.stored_ai_talents[m.name] or {}
-	local main_weapon = self:getInven("MAINHAND")[1]
+	local main_weapon = self:getInven("MAINHAND") and self:getInven("MAINHAND")[1]
 	m:attr("combat_apr", self:combatAPR(main_weapon))
 	m.inc_damage = table.clone(self.inc_damage, true)
 	m:attr("stun_immune", self:attr("stun_immune"))
diff --git a/game/modules/tome/data/talents/spells/phantasm.lua b/game/modules/tome/data/talents/spells/phantasm.lua
index 4d5e35d6dc..86ee724335 100644
--- a/game/modules/tome/data/talents/spells/phantasm.lua
+++ b/game/modules/tome/data/talents/spells/phantasm.lua
@@ -31,6 +31,7 @@ newTalent{
 		if self:getTalentLevel(t) >= 3 then
 			return 2
 		end
+		return 0
 	end},
 	getBlindPower = function(self, t) return 3 + self:getTalentLevel(t) end,
 	action = function(self, t)
-- 
GitLab