share/hedgewars/Data/Scripts/Utils.lua
author Wuzzy <Wuzzy2@mail.ru>
Wed, 19 Dec 2018 20:34:07 +0100
changeset 14484 4e5280a9e782
parent 14480 2113296b7a29
child 14530 029f40c609b4
permissions -rw-r--r--
Lua: AddTeam / AddMissionTeam now return team index
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13670
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
     1
-- Library for miscellaneous utilitiy functions and global helper variables
4873
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
     2
14480
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
     3
HedgewarsScriptLoad("/Scripts/Locale.lua")
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
     4
13670
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
     5
--[[ FUNCTIONS ]]
4873
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
     6
-- Check if a gear is inside a box
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
     7
function gearIsInBox(gear, x, y, w, h)
14236
74bf2d906097 Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents: 13670
diff changeset
     8
    local gx, gy = GetGearPosition(gear)
4873
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
     9
    if gx >= x and gy >= y and gx <= x + w and gy <= y + h then
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    10
        return true
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    11
    end
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    12
    return false
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    13
end
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    14
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    15
-- Check if a gear is inside a circle
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    16
function gearIsInCircle(gear, x, y, r, useRadius)
14236
74bf2d906097 Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents: 13670
diff changeset
    17
    local gx, gy = GetGearPosition(gear)
4873
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    18
    if useRadius then
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    19
        r = r + GetGearRadius(gear)
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    20
    end
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    21
    if r ^ 2 >= (x - gx) ^ 2 + (y - gy) ^ 2 then
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    22
        return true
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    23
    end
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    24
    return false
98dbb9b985e5 add a new lua library with misc tools and removed redundant tracker functions
Henek
parents:
diff changeset
    25
end
13220
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    26
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    27
local function drawFullMap(erase, flush)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    28
	for x = 200,4000,600 do
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    29
		for y = 100,2000,150 do
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    30
			AddPoint(x, y, 63, erase)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    31
		end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    32
	end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    33
	if flush ~= false then
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    34
		FlushPoints()
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    35
	end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    36
end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    37
14480
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    38
local function challengeRecordToString(recordType, value)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    39
	if recordType == "TimeRecord" or recordType == "TimeRecordHigh" then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    40
		return string.format(loc("Team record: %.3fs"), value/1000)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    41
	elseif recordType == "Highscore" then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    42
		return string.format(loc("Team high score: %d"), value)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    43
	else
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    44
		return string.format(loc("Team record: %d"), value)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    45
	end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    46
end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    47
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    48
function getReadableChallengeRecord(recordType)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    49
	local record = tonumber(GetMissionVar(recordType))
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    50
	if type(record) ~= "number" then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    51
		return ""
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    52
	else
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    53
		return challengeRecordToString(recordType, record)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    54
	end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    55
end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    56
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    57
function updateChallengeRecord(recordType, value, stat)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    58
	local oldRecord = tonumber(GetMissionVar(recordType))
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    59
	local newRecord = false
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    60
	if stat == nil then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    61
		stat = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    62
	end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    63
	if type(oldRecord) ~= "number" then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    64
		newRecord = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    65
	else
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    66
		local recordBeaten = false
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    67
		if recordType == "Lowscore" or recordType == "TimeRecord" then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    68
			if value < oldRecord then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    69
				recordBeaten = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    70
				newRecord = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    71
			end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    72
		else
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    73
			if value > oldRecord then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    74
				recordBeaten = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    75
				newRecord = true
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    76
			end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    77
		end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    78
		if stat then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    79
			if recordBeaten then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    80
				SendStat(siCustomAchievement, loc("You have beaten the team record, congratulations!"))
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    81
			else
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    82
				SendStat(siCustomAchievement, challengeRecordToString(recordType, oldRecord))
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    83
			end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    84
		end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    85
	end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    86
	if newRecord then
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    87
		SaveMissionVar(recordType, value)
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    88
	end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    89
end
2113296b7a29 Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents: 14236
diff changeset
    90
13220
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    91
-- Completely fill the map with land. Requires MapGen=mgDrawn.
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    92
-- If flush is false, FlushPoints() is not called.
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    93
function fillMap(flush)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    94
	drawFullMap(false, flush)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    95
end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    96
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    97
-- Completely erase all land from drawn maps. Requires MapGen=mgDrawn.
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    98
-- If flush is false, FlushPoints() is not called.
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
    99
function eraseMap(flush)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
   100
	drawFullMap(true, flush)
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
   101
end
bc95df84395f Clear drawn maps in TechRacer
Wuzzy <Wuzzy2@mail.ru>
parents: 4873
diff changeset
   102
13670
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   103
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   104
--[[ GLOBAL VARIABLES ]]
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   105
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   106
-- Shared common land color values for land sprites.
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   107
-- These are useful if you want to make the land type visible.
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   108
-- To be used as tint argument of PlaceSprite.
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   109
U_LAND_TINT_NORMAL = 0xFFFFFFFF			-- tint for normal land
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   110
U_LAND_TINT_INDESTRUCTIBLE = 0x960000FF		-- tint for indestructible land
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   111
U_LAND_TINT_ICE = 0x00FAFAFA			-- tint for icy land
5664650befcd Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents: 13220
diff changeset
   112
U_LAND_TINT_BOUNCY = 0x00FA00FF			-- tint for bouncy land