author | alfadur |
Thu, 09 Jul 2020 00:45:27 +0300 | |
changeset 15734 | b496233ac26d |
parent 14832 | d65e25e211d4 |
child 16000 | a803428704fd |
permissions | -rw-r--r-- |
13665
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
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 |
|
14496
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
3 |
HedgewarsScriptLoad("/Scripts/Locale.lua") |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
4 |
|
13665
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
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) |
14252
74bf2d906097
Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents:
13665
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) |
14252
74bf2d906097
Turn accidental globals to locals in Lua libraries
Wuzzy <Wuzzy2@mail.ru>
parents:
13665
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 |
13215 | 26 |
|
27 |
local function drawFullMap(erase, flush) |
|
28 |
for x = 200,4000,600 do |
|
29 |
for y = 100,2000,150 do |
|
30 |
AddPoint(x, y, 63, erase) |
|
31 |
end |
|
32 |
end |
|
33 |
if flush ~= false then |
|
34 |
FlushPoints() |
|
35 |
end |
|
36 |
end |
|
37 |
||
14496
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
38 |
local function challengeRecordToString(recordType, value) |
14546
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
39 |
if recordType == "TimeRecord" then |
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
40 |
return string.format(loc("Team's best time: %.3fs"), value/1000) |
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
41 |
elseif recordType == "TimeRecordHigh" then |
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
42 |
return string.format(loc("Team's longest time: %.3fs"), value/1000) |
14496
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
43 |
elseif recordType == "Highscore" then |
14546
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
44 |
return string.format(loc("Team highscore: %d"), value) |
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
45 |
elseif recordType == "Lowscore" then |
029f40c609b4
Display team records in training menu
Wuzzy <Wuzzy2@mail.ru>
parents:
14496
diff
changeset
|
46 |
return string.format(loc("Team lowscore: %d"), value) |
14612
b4089fa16b34
Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14546
diff
changeset
|
47 |
elseif recordType == "AccuracyRecord" then |
b4089fa16b34
Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14546
diff
changeset
|
48 |
return string.format(loc("Team's top accuracy: %d%"), value) |
14496
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
49 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
50 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
51 |
|
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
52 |
function getReadableChallengeRecord(recordType) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
53 |
local record = tonumber(GetMissionVar(recordType)) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
54 |
if type(record) ~= "number" then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
55 |
return "" |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
56 |
else |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
57 |
return challengeRecordToString(recordType, record) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
58 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
59 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
60 |
|
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
61 |
function updateChallengeRecord(recordType, value, stat) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
62 |
local oldRecord = tonumber(GetMissionVar(recordType)) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
63 |
local newRecord = false |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
64 |
if stat == nil then |
14612
b4089fa16b34
Keep track of accuracy record in target practice challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14546
diff
changeset
|
65 |
stat = recordType ~= "AccuracyRecord" |
14496
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
66 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
67 |
if type(oldRecord) ~= "number" then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
68 |
newRecord = true |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
69 |
else |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
70 |
local recordBeaten = false |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
71 |
if recordType == "Lowscore" or recordType == "TimeRecord" then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
72 |
if value < oldRecord then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
73 |
recordBeaten = true |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
74 |
newRecord = true |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
75 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
76 |
else |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
77 |
if value > oldRecord then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
78 |
recordBeaten = true |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
79 |
newRecord = true |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
80 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
81 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
82 |
if stat then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
83 |
if recordBeaten then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
84 |
SendStat(siCustomAchievement, loc("You have beaten the team record, congratulations!")) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
85 |
else |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
86 |
SendStat(siCustomAchievement, challengeRecordToString(recordType, oldRecord)) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
87 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
88 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
89 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
90 |
if newRecord then |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
91 |
SaveMissionVar(recordType, value) |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
92 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
93 |
end |
2113296b7a29
Keep track of singleplayer high scores in challenges
Wuzzy <Wuzzy2@mail.ru>
parents:
14252
diff
changeset
|
94 |
|
13215 | 95 |
-- Completely fill the map with land. Requires MapGen=mgDrawn. |
96 |
-- If flush is false, FlushPoints() is not called. |
|
97 |
function fillMap(flush) |
|
98 |
drawFullMap(false, flush) |
|
99 |
end |
|
100 |
||
101 |
-- Completely erase all land from drawn maps. Requires MapGen=mgDrawn. |
|
102 |
-- If flush is false, FlushPoints() is not called. |
|
103 |
function eraseMap(flush) |
|
104 |
drawFullMap(true, flush) |
|
105 |
end |
|
106 |
||
14832
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
107 |
-- Approximative but desync-safe version of square root. This function follows the Babylonian method. |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
108 |
function integerSqrt(num) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
109 |
local temp = num |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
110 |
while (temp*temp - div(temp, 2) > num) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
111 |
do |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
112 |
temp = div((temp + div(num, temp)), 2) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
113 |
end |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
114 |
return math.abs(temp) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
115 |
end |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
116 |
|
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
117 |
-- Integer square root of (x^2, y^2), works without desyncs. Is approximative. |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
118 |
-- This is the same as the length of the hypotenuse of a triangle with legs x, y. |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
119 |
function integerHypotenuse(x, y) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
120 |
-- To fix overflows |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
121 |
if(((math.abs(x)^2) + (math.abs(y)^2)) > 2^26) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
122 |
then |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
123 |
local bitr = 2^13 |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
124 |
return integerSqrt((div(math.abs(x), bitr)^2) + (div(math.abs(y), bitr)^2)) * bitr |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
125 |
else |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
126 |
return integerSqrt((math.abs(x)^2) + (math.abs(y) ^ 2)) |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
127 |
end |
d65e25e211d4
Add integerSqrt and integerHypotenuse to Utils lib
Wuzzy <Wuzzy2@mail.ru>
parents:
14612
diff
changeset
|
128 |
end |
13665
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
129 |
|
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
130 |
--[[ GLOBAL VARIABLES ]] |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
131 |
|
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
132 |
-- Shared common land color values for land sprites. |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
133 |
-- 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:
13215
diff
changeset
|
134 |
-- To be used as tint argument of PlaceSprite. |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
135 |
U_LAND_TINT_NORMAL = 0xFFFFFFFF -- tint for normal land |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
136 |
U_LAND_TINT_INDESTRUCTIBLE = 0x960000FF -- tint for indestructible land |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
137 |
U_LAND_TINT_ICE = 0x00FAFAFA -- tint for icy land |
5664650befcd
Move common sprite tint values into Utils Lua library
Wuzzy <Wuzzy2@mail.ru>
parents:
13215
diff
changeset
|
138 |
U_LAND_TINT_BOUNCY = 0x00FA00FF -- tint for bouncy land |