-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathstartbox_utilities.lua
More file actions
175 lines (162 loc) · 4.31 KB
/
Copy pathstartbox_utilities.lua
File metadata and controls
175 lines (162 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local function WrappedInclude(x)
local env = getfenv()
local prevGTC = env.GetTeamCount -- typically nil but also works otherwise
env.GetTeamCount = Spring.Utilities.GetTeamCount -- for legacy mapside boxes
local ret = VFS.Include(x, env)
env.GetTeamCount = prevGTC
return ret
end
local function GetStartboxName(midX, midZ)
if (midX < 0.33) then
if (midZ < 0.33) then
return "North-West", "NW"
elseif (midZ > 0.66) then
return "South-West", "SW"
else
return "West", "W"
end
elseif (midX > 0.66) then
if (midZ < 0.33) then
return "North-East", "NE"
elseif (midZ > 0.66) then
return "South-East", "SE"
else
return "East", "E"
end
else
if (midZ < 0.33) then
return "North", "N"
elseif (midZ > 0.66) then
return "South", "S"
else
return "Center", "Center"
end
end
end
local function ParseBoxes ()
local mapsideBoxes = "mapconfig/map_startboxes.lua"
local modsideBoxes = "LuaRules/Configs/StartBoxes/" .. (Game.mapName or "") .. ".lua"
local startBoxConfig
if VFS.FileExists (modsideBoxes) then
startBoxConfig = WrappedInclude (modsideBoxes)
elseif VFS.FileExists (mapsideBoxes) then
startBoxConfig = WrappedInclude (mapsideBoxes)
else
startBoxConfig = { }
local startboxString = Spring.GetModOptions().startboxes
local startboxStringLoadedBoxes = false
if startboxString then
local springieBoxes = loadstring(startboxString)()
for id, box in pairs(springieBoxes) do
startboxStringLoadedBoxes = true -- Autohost always sends a table. Often it is empty.
local midX = (box[1]+box[3]) / 2
local midZ = (box[2]+box[4]) / 2
box[1] = box[1]*Game.mapSizeX
box[2] = box[2]*Game.mapSizeZ
box[3] = box[3]*Game.mapSizeX
box[4] = box[4]*Game.mapSizeZ
local longName, shortName = GetStartboxName(midX, midZ)
startBoxConfig[id] = {
boxes = {{
{box[1], box[2]},
{box[1], box[4]},
{box[3], box[4]},
{box[3], box[2]},
}},
startpoints = {
{(box[1]+box[3]) / 2, (box[2]+box[4]) / 2}
},
nameLong = longName,
nameShort = shortName
}
end
end
if not startboxStringLoadedBoxes then
if Game.mapSizeZ > Game.mapSizeX then
startBoxConfig[1] = {
boxes = {{
{0, 0},
{0, Game.mapSizeZ * 0.2},
{Game.mapSizeX, Game.mapSizeZ * 0.2},
{Game.mapSizeX, 0}
}},
startpoints = {
{Game.mapSizeX * 0.5, Game.mapSizeZ * 0.1}
},
nameLong = "North",
nameShort = "N"
}
startBoxConfig[2] = {
boxes = {{
{0, Game.mapSizeZ * 0.8},
{0, Game.mapSizeZ},
{Game.mapSizeX, Game.mapSizeZ},
{Game.mapSizeX, Game.mapSizeZ * 0.8}
}},
startpoints = {
{Game.mapSizeX * 0.5, Game.mapSizeZ * 0.9}
},
nameLong = "South",
nameShort = "S"
}
else
startBoxConfig[1] = {
boxes = {{
{0, 0},
{0, Game.mapSizeZ},
{Game.mapSizeX * 0.2, Game.mapSizeZ},
{Game.mapSizeX * 0.2, 0},
}},
startpoints = {
{Game.mapSizeX * 0.1, Game.mapSizeZ * 0.5}
},
nameLong = "West",
nameShort = "W"
}
startBoxConfig[2] = {
boxes = {{
{Game.mapSizeX * 0.8, 0},
{Game.mapSizeX * 0.8, Game.mapSizeZ},
{Game.mapSizeX, Game.mapSizeZ},
{Game.mapSizeX, 0},
}},
startpoints = {
{Game.mapSizeX * 0.9, Game.mapSizeZ * 0.5}
},
nameLong = "East",
nameShort = "E"
}
end
end
end
-- Fix rendering z-fighting with the `/mapborder` option,
-- also hides that the box is only skin-deep
-- Value tested to be the minimum for CCR Remake, max zoom,
-- scrolling in/out, 1920x1080 resolution.
local MAX_EDGE = Game.mapSizeZ - 0.5
for boxid, box in pairs(startBoxConfig) do
for i = 1, #box.boxes do
local polygon = box.boxes[i]
for j = 1, #polygon do
local vertex = polygon[j]
if vertex[2] > MAX_EDGE then
vertex[2] = MAX_EDGE
end
end
end
end
-- If the boxes start from 0, shift up (Allyteams start from 1)
if startBoxConfig[0] and not startBoxConfig[1] then
Spring.Echo("0-indexed startbox detected, shifting")
local ret = {}
for boxID, box in pairs(startBoxConfig) do
ret[boxID + 1] = box
end
startBoxConfig = ret
end
return startBoxConfig
end
return ParseBoxes, { -- I hate this but some mod gadget might depend on it
ParseBoxes = ParseBoxes,
GetStartboxName = GetStartboxName,
}