HedgeEditor: Show tags for gear timers and health
authorWuzzy <almikes@aol.com>
Wed, 20 Apr 2016 17:10:05 +0200
changeset 12023 ba71454538d0
parent 12022 34fdc3d70aca
child 12024 16dbc3a92d14
HedgeEditor: Show tags for gear timers and health
share/hedgewars/Data/Scripts/Multiplayer/HedgeEditor.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/HedgeEditor.lua	Mon Apr 18 22:44:26 2016 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/HedgeEditor.lua	Wed Apr 20 17:10:05 2016 +0200
@@ -318,7 +318,10 @@
 local shoppaPY = {}
 local shoppaPR = {}
 
+-- Misc. state variables
 local hedgeEditorMissionPanelShown = false
+local tagGears = {}
+local showGearTags = true
 
 ---------------------------------
 -- crates are made of this stuff
@@ -3014,6 +3017,116 @@
 
 	end
 
+	-- Barrel health tags, mine timer tags and health crate health tags
+	if showGearTags then
+		local actualValue	-- internal value
+		local printedValue	-- value exposed to HUD
+		for g, v in pairs(tagGears) do
+			local gt = GetGearType(g)
+			if v == -1 then
+				if gt == gtCase then
+					if(band(GetGearPos(g), 0x2) ~= 0) then
+						v = AddVisualGear(0, 0, vgtHealthTag, GetHealth(g), true)
+						SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0x808080FF)
+						tagGears[g] = v
+					else
+						tagGears[g] = nil
+					end
+				else
+					if gt == gtMine and GetHealth(g) == 0 then
+						local _, damage, health
+						_, _, _, _, _, _, _, _, _, _, _, damage = GetGearValues(g)
+						health = 36 - damage
+						v = AddVisualGear(0, 0, vgtHealthTag, health, true)
+						SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0x808080FF)
+						tagGears[g] = v
+					end
+				end
+			end
+			-- Delete tag for drowning gears
+			if(band(GetState(g), gstDrowning) ~= 0) then
+				DeleteVisualGear(v)
+				tagGears[g] = nil
+			elseif(tagGears[g] ~= nil and tagGears[g] ~= -1) then
+				local tag, actualvalue, offset_x, offset_y
+				tag = GetState(v)
+				if(gt == gtExplosives) then
+					actualValue = GetHealth(g)
+					printedValue = actualValue
+					offset_y = -20
+					offset_x = 0
+				elseif(gt == gtAirMine) then
+					if(band(GetState(g), gstAttacking) ~= 0) then
+						actualValue = GetTimer(g)
+						printedValue = actualValue
+					else
+						local _
+						_, _, actualValue = GetGearValues(g)
+						printedValue = actualValue
+					end
+					offset_y = 28
+					offset_x = 16
+				elseif(gt == gtCase) then
+					actualValue = GetHealth(g)
+					printedValue = actualValue
+					offset_y = -20
+					offset_x = 1
+				elseif(gt == gtMine) then
+					if(GetHealth(g) ~= 0) then
+						-- Non-dud: Show timer
+						actualValue = GetTimer(g)
+						printedValue = actualValue
+						offset_y = 28
+						offset_x = 16
+					else
+						-- Dud: Show health
+						local _
+						_, _, _, _, _, _, _, _, _, _, _, actualValue = GetGearValues(g)
+						printedValue = 36 - actualValue
+						offset_y = -12
+						offset_x = 0
+					end
+				elseif(gt == gtSMine) then
+					actualValue = GetTimer(g)
+					printedValue = actualValue
+					offset_y = 28
+					offset_x = 16
+				end
+				--[[ The timer tag normally disappears near the water line, this is a really
+				ugly hack to adjust the position of the tag so it is always displayed.
+				FIXME: Find a better solution to fix this. ]]
+				if (GetY(g) + offset_y) > WaterLine and (GetY(g) + offset_y) < WaterLine + 30 then
+					offset_y = (WaterLine - GetY(g))
+				end
+				local tint
+				if(gt == gtCase) then
+					tint = 0x80FF80FF
+				elseif(gt == gtExplosives or (gt == gtMine and GetHealth(g) == 0)) then
+					tint = 0x808080FF
+				elseif(band(GetState(g), gstAttacking) ~= 0) then
+					if(actualValue % 1000 > 500) then
+						tint = 0xFFA0A0FF
+					else
+						tint = 0xFF4040FF
+					end
+				else
+					tint = 0xFFFFFFFF
+				end
+				if(actualValue ~= tag) then
+					--[[ If timer/health changed, delete visual gear and add it again.
+					Changing the visual gear state does not have any effect, so we need this hack ]]
+					DeleteVisualGear(v)
+					v = AddVisualGear(GetX(g)+offset_x, GetY(g)+offset_y, vgtHealthTag, printedValue, true)
+					SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, tint)
+					tagGears[g] = v
+				else
+					-- Just update position if the health did not change
+					SetVisualGearValues(v, GetX(g)+offset_x, GetY(g)+offset_y, 0, 0, nil, nil, nil, nil, 240000, tint)
+				end
+			end
+		end
+	end
+
 end
 
 --------------------------------------------------
@@ -3596,6 +3709,32 @@
 		cGear = gear
 	end
 
+	if showGearTags then
+		if ((GetGearType(gear) == gtMine and GetHealth(gear) ~= 0) or GetGearType(gear) == gtSMine) then
+			local v = AddVisualGear(0, 0, vgtHealthTag, GetTimer(gear), true)
+			SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0xFFFFFFFF)
+			tagGears[gear] = v
+		elseif ((GetGearType(gear) == gtMine and GetHealth(gear) == 0)) then
+			local _, dmg
+			_, _, _, _, _, _, _, _, _, _, dmg = GetGearValues(gear)
+			local v = AddVisualGear(0, 0, vgtHealthTag, 36 - dmg, true)
+			SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0xFFFFFFFF)
+			tagGears[gear] = v
+		elseif (GetGearType(gear) == gtAirMine) then
+			local _, wdTimer
+			_, _, wdTimer = GetGearValues(gear)
+			local v = AddVisualGear(0, 0, vgtHealthTag, wdTimer, true)
+			SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0xFFFFFFFF)
+			tagGears[gear] = v
+		elseif (GetGearType(gear) == gtCase) then
+			tagGears[gear] = -1
+		elseif (GetGearType(gear) == gtExplosives) then 
+			local v = AddVisualGear(0, 0, vgtHealthTag, GetHealth(gear), true)
+			SetVisualGearValues(v, nil, nil, 0, 0, nil, nil, nil, nil, 240000, 0xFFFFFFFF)
+			tagGears[gear] = v
+		end
+	end
+
 	if isATrackedGear(gear) then
 		trackGear(gear)
 
@@ -3608,15 +3747,24 @@
 end
 
 function onGearDelete(gear)
-
-	if GetGearType(gear) == gtJetpack then
+	local gt = GetGearType(gear)
+
+	if gt == gtJetpack then
 		ufoGear = nil
 	end
 
-	if (GetGearType(gear) == gtAirAttack) or (GetGearType(gear) == gtGirder) then
+	if gt == gtAirAttack or gt == gtGirder then
 		cGear = nil
 	end
 
+	if (showGearTags and (gt == gtMine or gt == gtSMine or gt == gtAirMine or gt == gtExplosives or gt == gtCase)) then 
+		if(tagGears[gear] ~= -1) then
+			DeleteVisualGear(tagGears[gear])
+		end
+		tagGears[gear] = nil
+	end
+
+
 	if isATrackedGear(gear) then
 
 		if getGearValue(gear, "tCirc") ~= nil then