From e45d9382ea050cca3253f9a1a146eb1fa03f1db2 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Mon, 5 Apr 2010 22:57:53 +0000
Subject: [PATCH]                   fix dialog when runing

git-svn-id: http://svn.net-core.org/repos/t-engine4@512 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Dialog.lua                       | 17 +++++++----------
 game/engine/Game.lua                         |  8 ++++++++
 game/engine/Key.lua                          |  2 +-
 game/engine/Mouse.lua                        |  2 +-
 game/engine/interface/GameMusic.lua          |  4 ++--
 game/modules/tome/class/interface/Combat.lua |  5 +++--
 6 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/game/engine/Dialog.lua b/game/engine/Dialog.lua
index b2b0b9af49..882b861148 100644
--- a/game/engine/Dialog.lua
+++ b/game/engine/Dialog.lua
@@ -100,12 +100,9 @@ function _M:drawDialog(s)
 end
 
 function _M:keyCommands(t, b)
-	self.old_key = game.key
-	game.key = engine.KeyBind.new()
-	if t then game.key:addCommands(t) end
-	if b then game.key:addBinds(b) end
-	game.key:setCurrent()
-	self.key = game.key
+	self.key = engine.KeyBind.new()
+	if t then self.key:addCommands(t) end
+	if b then self.key:addBinds(b) end
 end
 
 function _M:mouseZones(t)
@@ -117,13 +114,12 @@ function _M:mouseZones(t)
 		end
 	end
 
-	self.old_mouse = game.mouse
-	game.mouse = engine.Mouse.new()
-	game.mouse:registerZones(t)
-	game.mouse:setCurrent()
+	self.mouse = engine.Mouse.new()
+	self.mouse:registerZones(t)
 end
 
 function _M:unload()
+--[[
 	if self.old_key then
 		game.key = self.old_key
 		game.key:setCurrent()
@@ -132,6 +128,7 @@ function _M:unload()
 		game.mouse = self.old_mouse
 		game.mouse:setCurrent()
 	end
+]]
 end
 
 function _M:drawWBorder(s, x, y, w)
diff --git a/game/engine/Game.lua b/game/engine/Game.lua
index 9d5437ddfc..50e2d016f8 100644
--- a/game/engine/Game.lua
+++ b/game/engine/Game.lua
@@ -129,6 +129,8 @@ end
 function _M:registerDialog(d)
 	table.insert(self.dialogs, d)
 	self.dialogs[d] = #self.dialogs
+	if d.key then d.key:setCurrent() end
+	if d.mouse then d.mouse:setCurrent() end
 end
 
 --- Undisplay a dialog, removing its own keyhandler if needed
@@ -137,6 +139,12 @@ function _M:unregisterDialog(d)
 	table.remove(self.dialogs, self.dialogs[d])
 	self.dialogs[d] = nil
 	d:unload()
+	-- Update positions
+	for i, id in ipairs(self.dialogs) do self.dialogs[id] = i end
+
+	local last = self.dialogs[#self.dialogs] or self
+	if last.key then last.key:setCurrent() end
+	if last.mouse then last.mouse:setCurrent() end
 end
 
 --- The C core gives us command line arguments
diff --git a/game/engine/Key.lua b/game/engine/Key.lua
index d49268f2e4..1d2e1c1937 100644
--- a/game/engine/Key.lua
+++ b/game/engine/Key.lua
@@ -40,7 +40,7 @@ end
 --- Setups as the current game keyhandler
 function _M:setCurrent()
 	core.key.set_current_handler(self)
-	if game then game.key = self end
+--	if game then game.key = self end
 	_M.current = self
 end
 
diff --git a/game/engine/Mouse.lua b/game/engine/Mouse.lua
index bd7e9aafdc..585d9f41d0 100644
--- a/game/engine/Mouse.lua
+++ b/game/engine/Mouse.lua
@@ -50,7 +50,7 @@ end
 --- Setups as the current game keyhandler
 function _M:setCurrent()
 	core.mouse.set_current_handler(self)
-	if game then game.mouse = self end
+--	if game then game.mouse = self end
 	_M.current = self
 end
 
diff --git a/game/engine/interface/GameMusic.lua b/game/engine/interface/GameMusic.lua
index 5477d3caea..cb80dd25f3 100644
--- a/game/engine/interface/GameMusic.lua
+++ b/game/engine/interface/GameMusic.lua
@@ -32,7 +32,7 @@ function _M:loaded()
 	self.loaded_musics = self.loaded_musics or {}
 end
 
-function _M:playMusic(name)
+function _M:playMusic(name, loop)
 	name = name or self.current_music
 	if not name then return end
 	local m = self.loaded_musics[name]
@@ -45,7 +45,7 @@ function _M:playMusic(name)
 	if self.current_music then
 		core.sound.musicStop()
 	end
-	m:play()
+	m:play(loop or -1)
 	self.current_music = name
 	self.playing_music = true
 end
diff --git a/game/modules/tome/class/interface/Combat.lua b/game/modules/tome/class/interface/Combat.lua
index 84275424f2..3e3cc5cdde 100644
--- a/game/modules/tome/class/interface/Combat.lua
+++ b/game/modules/tome/class/interface/Combat.lua
@@ -164,6 +164,7 @@ function _M:archeryShoot(damtype, mult, on_hit, tg, params)
 
 			local target = game.level.map(tx, ty, game.level.map.ACTOR)
 			if not target then return end
+			ret.firsttarget = ret.firsttarget or target
 			ammo = ammo.combat
 
 			damtype = damtype or ammo.damtype or DamageType.PHYSICAL
@@ -211,8 +212,8 @@ function _M:archeryShoot(damtype, mult, on_hit, tg, params)
 	-- If we used only one arrow, use it
 	if params.one_shot then self:removeObject(self:getInven("QUIVER"), 1) end
 
-	if sound then game:playSoundNear(self, sound)
-	elseif sound_miss then game:playSoundNear(self, sound_miss) end
+	if sound then game:playSoundNear(ret.firsttarget or self, sound)
+	elseif sound_miss then game:playSoundNear(ret.firsttarget or self, sound_miss) end
 
 	return ret.hitted
 end
-- 
GitLab