From 45a76a4dd13536ec166969a017930a058f6ad8af Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Mon, 20 Sep 2010 19:05:20 +0000
Subject: [PATCH] Fixed the graphical bug that made textures go white when
 resizing/changing game mode Right click popup will not allow talents to be
 used on the world map Static map generator will not clone terrain that does
 not requests cloning

git-svn-id: http://svn.net-core.org/repos/t-engine4@1256 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Entity.lua        |  8 ++--
 game/engines/default/engine/Game.lua          |  8 +++-
 .../default/engine/generator/map/Static.lua   |  2 +-
 game/modules/tome/class/Game.lua              |  2 +-
 game/modules/tome/class/Grid.lua              |  2 +-
 game/modules/tome/data/zones/test/zone.lua    | 10 ++---
 game/modules/tome/dialogs/MapMenu.lua         | 44 ++++++++++---------
 src/core_lua.c                                |  1 +
 src/map.c                                     | 22 +++++++++-
 src/map.h                                     |  1 +
 10 files changed, 64 insertions(+), 36 deletions(-)

diff --git a/game/engines/default/engine/Entity.lua b/game/engines/default/engine/Entity.lua
index 5c2ed05d8a..61c7483bfd 100644
--- a/game/engines/default/engine/Entity.lua
+++ b/game/engines/default/engine/Entity.lua
@@ -35,16 +35,16 @@ _M._no_save_fields = {}
 
 -- Setup the uids & MO repository as a weak value table, when the entities are no more used anywhere else they disappear from there too
 setmetatable(__uids, {__mode="v"})
-setmetatable(_M.__mo_repo, {__mode="v"})
+setmetatable(_M.__mo_repo, {__mode="k"})
 setmetatable(_M.__mo_final_repo, {__mode="k"})
 
 --- Invalidates the whole MO repository
 function _M:invalidateAllMO()
-	for i, mo in ipairs(_M.__mo_repo) do
+	for mo, _ in pairs(_M.__mo_repo) do
 		mo:invalidate()
 	end
 	_M.__mo_repo = {}
-	setmetatable(_M.__mo_repo, {__mode="v"})
+	setmetatable(_M.__mo_repo, {__mode="k"})
 	setmetatable(_M.__mo_final_repo, {__mode="k"})
 end
 
@@ -178,7 +178,7 @@ function _M:makeMapObject(tiles, idx)
 		self:check("display_y") or 0,
 		self:check("display_scale") or 1
 	)
-	_M.__mo_repo[#_M.__mo_repo+1] = self._mo
+	_M.__mo_repo[self._mo] = true
 
 	-- Setup tint
 	self._mo:tint(self.tint_r, self.tint_g, self.tint_b)
diff --git a/game/engines/default/engine/Game.lua b/game/engines/default/engine/Game.lua
index 00e0492d68..f329445dc1 100644
--- a/game/engines/default/engine/Game.lua
+++ b/game/engines/default/engine/Game.lua
@@ -207,8 +207,12 @@ function _M:saveSettings(file, data)
 	local restore = fs.getWritePath()
 	fs.setWritePath(engine.homepath)
 	local f = fs.open("/settings/"..file..".cfg", "w")
-	f:write(data)
-	f:close()
+	if f then
+		f:write(data)
+		f:close()
+	else
+		print("WARNING: could not save settings in ", file, "::", data)
+	end
 	if restore then fs.setWritePath(restore) end
 end
 
diff --git a/game/engines/default/engine/generator/map/Static.lua b/game/engines/default/engine/generator/map/Static.lua
index 0d2499e7ea..9fd9c87bfc 100644
--- a/game/engines/default/engine/generator/map/Static.lua
+++ b/game/engines/default/engine/generator/map/Static.lua
@@ -158,7 +158,7 @@ function _M:generate(lev, old_lev)
 		local c = self.gen_map[i][j]
 		local g = self:resolve("grid", c)
 		if g then
-			g = g:clone()
+			if g.force_clone then g = g:clone() end
 			g:resolve()
 			g:resolve(nil, true)
 			self.map(i-1, j-1, Map.TERRAIN, g)
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index 06f62939fb..ecf7c5d0b4 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -550,7 +550,7 @@ function _M:setupCommands()
 		end,
 		[{"_g","ctrl"}] = function()
 			if config.settings.tome.cheat then
-				self:changeLevel(1, "slime-tunnels")
+				self:changeLevel(1, "test")
 --				self.player:grantQuest("master-jeweler")
 			end
 		end
diff --git a/game/modules/tome/class/Grid.lua b/game/modules/tome/class/Grid.lua
index b7753586be..eb08bc5460 100644
--- a/game/modules/tome/class/Grid.lua
+++ b/game/modules/tome/class/Grid.lua
@@ -72,7 +72,7 @@ function _M:on_move(x, y, who, forced)
 	end
 end
 
-function _M:tooltip()
+function _M:tooltip(x, y)
 	if self.show_tooltip then
 		local name = ((self.show_tooltip == true) and self.name or self.show_tooltip)
 		if self.desc then
diff --git a/game/modules/tome/data/zones/test/zone.lua b/game/modules/tome/data/zones/test/zone.lua
index de3152c8f6..610a87c2ad 100644
--- a/game/modules/tome/data/zones/test/zone.lua
+++ b/game/modules/tome/data/zones/test/zone.lua
@@ -31,14 +31,14 @@ return {
 	generator =  {
 		map = {
 			class = "engine.generator.map.Forest",
-			floor = "VOID",
-			wall = "SPACE_TURBULENCE",
-			up = "UP",
-			down = "DOWN",
+			floor = "FLOOR",
+			wall = "FLOOR",
+			up = "FLOOR",
+			down = "FLOOR",
 		},
 		actor = {
 			class = "engine.generator.actor.Random",
-			nb_npc = {40, 50},
+			nb_npc = {0, 0},
 		},
 --[[
 		object = {
diff --git a/game/modules/tome/dialogs/MapMenu.lua b/game/modules/tome/dialogs/MapMenu.lua
index 88f932960f..c7e04a7545 100644
--- a/game/modules/tome/dialogs/MapMenu.lua
+++ b/game/modules/tome/dialogs/MapMenu.lua
@@ -104,30 +104,32 @@ function _M:generateList()
 	if self.on_player and (player.unused_stats > 0 or player.unused_talents > 0 or player.unused_generics > 0 or player.unused_talents_types > 0) then list[#list+1] = {name="Levelup!", action="levelup", color=colors.simple(colors.YELLOW)} end
 
 	-- Talents
-	local tals = {}
-	for tid, _ in pairs(player.talents) do
-		local t = player:getTalentFromId(tid)
-		if t.mode ~= "passive" and player:preUseTalent(t, true, true) and not player:isTalentCoolingDown(t) then
-			local rt = util.getval(t.requires_target, player, t)
-			if self.on_player and not rt then
-				tals[#tals+1] = {name=t.name, talent=t, action="talent", color=colors.simple(colors.GOLD)}
-			elseif not self.on_player and rt then
-				tals[#tals+1] = {name=t.name, talent=t, action="talent", set_target=true, color=colors.simple(colors.GOLD)}
+	if self.zone and not self.zone.wilderness then
+	       local tals = {}
+		for tid, _ in pairs(player.talents) do
+			local t = player:getTalentFromId(tid)
+			if t.mode ~= "passive" and player:preUseTalent(t, true, true) and not player:isTalentCoolingDown(t) then
+				local rt = util.getval(t.requires_target, player, t)
+				if self.on_player and not rt then
+					tals[#tals+1] = {name=t.name, talent=t, action="talent", color=colors.simple(colors.GOLD)}
+				elseif not self.on_player and rt then
+					tals[#tals+1] = {name=t.name, talent=t, action="talent", set_target=true, color=colors.simple(colors.GOLD)}
+				end
 			end
 		end
+		table.sort(tals, function(a, b)
+			local ha, hb
+			for i = 1, 36 do if player.hotkey[i] and player.hotkey[i][1] == "talent" and player.hotkey[i][2] == a.talent.id then ha = i end end
+			for i = 1, 36 do if player.hotkey[i] and player.hotkey[i][1] == "talent" and player.hotkey[i][2] == b.talent.id then hb = i end end
+
+			if ha and hb then return ha < hb
+			elseif ha and not hb then return ha < 999999
+			elseif hb and not ha then return hb > 999999
+			else return a.talent.name < b.talent.name
+			end
+		end)
+		for i = 1, #tals do list[#list+1] = tals[i] end
 	end
-	table.sort(tals, function(a, b)
-		local ha, hb
-		for i = 1, 36 do if player.hotkey[i] and player.hotkey[i][1] == "talent" and player.hotkey[i][2] == a.talent.id then ha = i end end
-		for i = 1, 36 do if player.hotkey[i] and player.hotkey[i][1] == "talent" and player.hotkey[i][2] == b.talent.id then hb = i end end
-
-		if ha and hb then return ha < hb
-		elseif ha and not hb then return ha < 999999
-		elseif hb and not ha then return hb > 999999
-		else return a.talent.name < b.talent.name
-		end
-	end)
-	for i = 1, #tals do list[#list+1] = tals[i] end
 
 	self.max = 0
 	self.maxh = 0
diff --git a/src/core_lua.c b/src/core_lua.c
index e4c2410858..b8da63ada6 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -1034,6 +1034,7 @@ static int sdl_free_texture(lua_State *L)
 	GLuint *t = (GLuint*)auxiliar_checkclass(L, "gl{texture}", 1);
 	glDeleteTextures(1, t);
 	lua_pushnumber(L, 1);
+//	printf("freeing texture %d\n", *t);
 	return 1;
 }
 
diff --git a/src/map.c b/src/map.c
index 448b524bda..6ef462519f 100644
--- a/src/map.c
+++ b/src/map.c
@@ -48,6 +48,7 @@ static int map_object_new(lua_State *L)
 	map_object *obj = (map_object*)lua_newuserdata(L, sizeof(map_object));
 	auxiliar_setclass(L, "core{mapobj}", -1);
 	obj->textures = calloc(nb_textures, sizeof(GLuint));
+	obj->textures_ref = calloc(nb_textures, sizeof(int));
 	obj->textures_is3d = calloc(nb_textures, sizeof(bool));
 	obj->nb_textures = nb_textures;
 	obj->uid = uid;
@@ -66,6 +67,7 @@ static int map_object_new(lua_State *L)
 	{
 		obj->textures[i] = 0;
 		obj->textures_is3d[i] = FALSE;
+		obj->textures_ref[i] = LUA_NOREF;
 	}
 
 	return 1;
@@ -74,8 +76,14 @@ static int map_object_new(lua_State *L)
 static int map_object_free(lua_State *L)
 {
 	map_object *obj = (map_object*)auxiliar_checkclass(L, "core{mapobj}", 1);
+	int i;
+
+	for (i = 0; i < obj->nb_textures; i++)
+		if (obj->textures_ref[i] != LUA_NOREF)
+			luaL_unref(L, LUA_REGISTRYINDEX, obj->textures_ref[i]);
 
 	free(obj->textures);
+	free(obj->textures_ref);
 	free(obj->textures_is3d);
 
 	lua_pushnumber(L, 1);
@@ -101,7 +109,11 @@ static int map_object_texture(lua_State *L)
 	bool is3d = lua_toboolean(L, 4);
 	if (i < 0 || i >= obj->nb_textures) return 0;
 
-//	printf("C Map Object setting texture %d = %d\n", i, *t);
+	if (obj->textures_ref[i] != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, obj->textures_ref[i]);
+
+	lua_pushvalue(L, 3); // Get the texture
+	obj->textures_ref[i] = luaL_ref(L, LUA_REGISTRYINDEX); // Ref the texture
+//	printf("C Map Object setting texture %d = %d (ref %x)\n", i, *t, obj->textures_ref[i]);
 	obj->textures[i] = *t;
 	obj->textures_is3d[i] = is3d;
 	return 0;
@@ -127,6 +139,13 @@ static int map_object_tint(lua_State *L)
 	return 0;
 }
 
+static int map_object_print(lua_State *L)
+{
+	map_object *obj = (map_object*)auxiliar_checkclass(L, "core{mapobj}", 1);
+	printf("Map object texture 0: %d\n", obj->textures[0]);
+	return 0;
+}
+
 static int map_object_invalid(lua_State *L)
 {
 	map_object *obj = (map_object*)auxiliar_checkclass(L, "core{mapobj}", 1);
@@ -808,6 +827,7 @@ static const struct luaL_reg map_object_reg[] =
 	{"texture", map_object_texture},
 	{"tint", map_object_tint},
 	{"shader", map_object_shader},
+	{"print", map_object_print},
 	{"invalidate", map_object_invalid},
 	{"isValid", map_object_is_valid},
 	{"onSeen", map_object_on_seen},
diff --git a/src/map.h b/src/map.h
index 3ee372020d..cf8ce45074 100644
--- a/src/map.h
+++ b/src/map.h
@@ -25,6 +25,7 @@
 
 typedef struct {
 	int nb_textures;
+	int *textures_ref;
 	GLuint *textures;
 	bool *textures_is3d;
 	GLuint shader;
-- 
GitLab