From fc2cc52db8bfdae6b0473c7511c1781cadaa2240 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Sat, 9 Nov 2019 15:13:26 +0100
Subject: [PATCH] New Gameplay option to display a warning and stop receiving
 key/mouse input for 2 seconds after loosing X% of max life in a turn
 (disabled by default)

---
 game/engines/default/engine/Key.lua                | 13 +++++++++++++
 game/engines/default/engine/KeyBind.lua            |  1 +
 game/engines/default/engine/KeyCommand.lua         |  1 +
 game/engines/default/engine/Mouse.lua              | 13 +++++++++++++
 game/modules/tome/class/Player.lua                 |  9 +++++++++
 game/modules/tome/data/zones/gorbat-pride/zone.lua |  2 +-
 game/modules/tome/dialogs/GameOptions.lua          | 12 ++++++++++++
 7 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/game/engines/default/engine/Key.lua b/game/engines/default/engine/Key.lua
index 79c639db98..62cc552b1d 100644
--- a/game/engines/default/engine/Key.lua
+++ b/game/engines/default/engine/Key.lua
@@ -29,6 +29,18 @@ function _M:init()
 	self.status = {}
 end
 
+--- Check if we are disabled
+function _M:isEnabled()
+	if not self.disable_until then return true end
+	if core.game.getTime() < self.disable_until then 
+		if game.log then game.log("#LIGHT_RED#Keyboard input temporarily disabled.") end
+		return false
+	else
+		self.disable_until = nil
+		return true
+	end
+end
+
 --- Called when a key is pressed
 -- @number sym a number representing the key, see all the _FOO fields
 -- @param[type=boolean] ctrl is the control key pressed?
@@ -39,6 +51,7 @@ end
 -- @param[type=boolean] isup true if the key was released, false if pressed
 -- @string key the unicode representation of the key pressed (without accounting for modifiers)
 function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode, isup, key)
+	if not self:isEnabled() then return end
 	self:handleStatus(sym, ctrl, shift, alt, meta, unicode, isup)
 end
 
diff --git a/game/engines/default/engine/KeyBind.lua b/game/engines/default/engine/KeyBind.lua
index ed2bb3f22f..2d80d7fc8e 100644
--- a/game/engines/default/engine/KeyBind.lua
+++ b/game/engines/default/engine/KeyBind.lua
@@ -212,6 +212,7 @@ function _M:formatKeyString(ks)
 end
 
 function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode, isup, key, ismouse)
+	if not self:isEnabled() then return end
 	if unicode and not self.use_unicode then return end
 
 	self:handleStatus(sym, ctrl, shift, alt, meta, unicode, isup)
diff --git a/game/engines/default/engine/KeyCommand.lua b/game/engines/default/engine/KeyCommand.lua
index 31b31ec0a7..4fcfdcbdc3 100644
--- a/game/engines/default/engine/KeyCommand.lua
+++ b/game/engines/default/engine/KeyCommand.lua
@@ -61,6 +61,7 @@ function _M:setupRebootKeys()
 end
 
 function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode, isup, key)
+	if not self:isEnabled() then return end
 	self:handleStatus(sym, ctrl, shift, alt, meta, unicode, isup)
 
 	if self.ignore[sym] then return end
diff --git a/game/engines/default/engine/Mouse.lua b/game/engines/default/engine/Mouse.lua
index 92c67e324c..645a689d7a 100644
--- a/game/engines/default/engine/Mouse.lua
+++ b/game/engines/default/engine/Mouse.lua
@@ -35,6 +35,18 @@ function _M:allowDownEvent(v)
 	self.allow_down = v
 end
 
+--- Check if we are disabled
+function _M:isEnabled()
+	if not self.disable_until then return true end
+	if core.game.getTime() < self.disable_until then 
+		if game.log then game.log("#LIGHT_RED#Mouse input temporarily disabled.") end
+		return false
+	else
+		self.disable_until = nil
+		return true
+	end
+end
+
 --- Called when a mouse is pressed
 -- @param button
 -- @param x coordinate of the click
@@ -45,6 +57,7 @@ end
 function _M:receiveMouse(button, x, y, isup, force_name, extra)
 	self.last_pos = { x = x, y = y }
 	self.status[button] = not isup
+	if not self:isEnabled() then return end
 	if not self.allow_down and not isup then return end
 
 	if _M.drag then
diff --git a/game/modules/tome/class/Player.lua b/game/modules/tome/class/Player.lua
index da5c50c6f0..2a3f0af184 100644
--- a/game/modules/tome/class/Player.lua
+++ b/game/modules/tome/class/Player.lua
@@ -380,6 +380,15 @@ function _M:act()
 	-- Funky shader things !
 	self:updateMainShader()
 
+	if config.settings.tome.life_lost_warning then
+		local perc = (self.shader_old_life - self.life) / self.max_life
+		if perc > (config.settings.tome.life_lost_warning / 100) then
+			game.bignews:say(100, "#LIGHT_RED#LIFE LOST WARNING!")
+			game.key.disable_until = core.game.getTime() + 2000
+			game.mouse.disable_until = core.game.getTime() + 2000
+		end
+	end
+
 	self.shader_old_life = self.life
 	self.old_air = self.air
 	self.old_psi = self.psi
diff --git a/game/modules/tome/data/zones/gorbat-pride/zone.lua b/game/modules/tome/data/zones/gorbat-pride/zone.lua
index 0cdbce9e58..a8d154c199 100644
--- a/game/modules/tome/data/zones/gorbat-pride/zone.lua
+++ b/game/modules/tome/data/zones/gorbat-pride/zone.lua
@@ -70,7 +70,7 @@ return {
 	{
 		[1] = {
 			generator = { map = {
-				up = "UP_WILDERNESS",
+				['<'] = "UP_WILDERNESS",
 			}, },
 		},
 	},
diff --git a/game/modules/tome/dialogs/GameOptions.lua b/game/modules/tome/dialogs/GameOptions.lua
index b4e2683191..c955181c43 100644
--- a/game/modules/tome/dialogs/GameOptions.lua
+++ b/game/modules/tome/dialogs/GameOptions.lua
@@ -482,6 +482,18 @@ function _M:generateListGameplay()
 		end, 1))
 	end,}
 
+	local zone = Textzone.new{width=self.c_desc.w, height=self.c_desc.h, text=string.toTString"If you loose more than this percentage of life in a turn, a warning will display and all key/mouse input will be ignored for 2 seconds to prevent mistakes.#WHITE#"}
+	list[#list+1] = { zone=zone, name=string.toTString"#GOLD##{bold}#Life Lost Warning#WHITE##{normal}#", status=function(item)
+		return (not config.settings.tome.life_lost_warning or config.settings.tome.life_lost_warning == 100) and "disabled" or tostring(config.settings.tome.life_lost_warning).."%"
+	end, fct=function(item)
+		game:registerDialog(GetQuantity.new("Life lost percentage (out of max life)", "From 1 to 99 (100 to disable)", config.settings.tome.life_lost_warning or 100, 100, function(qty)
+			qty = util.bound(qty, 1, 100)
+			game:saveSettings("tome.life_lost_warning", ("tome.life_lost_warning = %d\n"):format(qty))
+			config.settings.tome.life_lost_warning = qty
+			self.c_list:drawItem(item)
+		end, 1))
+	end,}
+
 	local zone = Textzone.new{width=self.c_desc.w, height=self.c_desc.h, text=string.toTString"Enables or disables weather effects in some zones.\nDisabling it can gain some performance. It will not affect previously visited zones.#WHITE#"}
 	list[#list+1] = { zone=zone, name=string.toTString"#GOLD##{bold}#Weather effects#WHITE##{normal}#", status=function(item)
 		return tostring(config.settings.tome.weather_effects and "enabled" or "disabled")
-- 
GitLab