forked from ZeroK-RTS/Chobby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_skirmish.lua
More file actions
489 lines (421 loc) · 12.9 KB
/
Copy pathinterface_skirmish.lua
File metadata and controls
489 lines (421 loc) · 12.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
InterfaceSkirmish = Lobby:extends()
function InterfaceSkirmish:init()
self:super("init")
self.name = "singleplayer"
self.myUserName = "Player"
self.useTeamColor = true
end
function InterfaceSkirmish:WriteTable(key, value, tabs)
local str = tabs..'['..key..']\n'..tabs..'{\n'
-- First write Tables
for k, v in pairs(value) do
if type(v) == 'table' then
str = str .. self:WriteTable(k, v, tabs .. '\t')
end
end
-- Then the rest (purely for aesthetics)
for k, v in pairs(value) do
if type(v) ~= 'table' then
str = str..tabs..'\t'..k..' = '..v..';\n'
end
end
return str .. tabs .. '}\n'
end
function InterfaceSkirmish:MakeScriptTXT(script)
local str = '[Game]\n{\n'
-- First write Tables
for key, value in pairs(script) do
if type(value) == 'table' then
str = str .. self:WriteTable(key, value, '\t') .. '\n'
end
end
-- Then the rest (purely for aesthetics)
for key, value in pairs(script) do
if type(value) ~= 'table' then
str = str..'\t'..key..' = '..value..';\n'
end
end
str = str..'}'
return str
end
function InterfaceSkirmish:_StartScript(gameName, mapName, playerName, friendList, friendsReplaceAI, hostPort)
local allyTeams = {}
local allyTeamCount = 0
local teams = {}
local teamCount = 0
local players = {}
local playerCount = 0
local maxAllyTeamID = -1
local ais = {}
local aiCount = 0
friendList = friendList or {}
local friendAllyTeam
local aiReplaceCount = 0
local defaultColor = '0.99609375 0.546875 0'
local function getTeamColor(userName)
if not self.useTeamColor then
return defaultColor
end
local teamColor = self.userBattleStatus[userName].teamColor
if teamColor == nil then
return defaultColor
end
return tostring(teamColor[1]) .. " " .. tostring(teamColor[2]) .. " " .. tostring(teamColor[3])
end
-- Add the player, this is to make the player team 0.
for userName, data in pairs(self.userBattleStatus) do
if data.allyNumber and not data.aiLib then
players[playerCount] = {
Name = userName,
Team = teamCount,
IsFromDemo = 0,
Spectator = (data.isSpectator and 1) or nil,
rank = 0,
}
if not data.isSpectator then
teams[teamCount] = {
TeamLeader = 0,
AllyTeam = data.allyNumber,
RgbColor = getTeamColor(userName),
Side = WG.Chobby.Configuration:GetSideById(data.side).name,
}
maxAllyTeamID = math.max(maxAllyTeamID, data.allyNumber)
teamCount = teamCount + 1
end
playerCount = playerCount + 1
for i = 1, #friendList do
local friendName = friendList[i]
players[playerCount] = {
Name = friendName,
Team = teamCount,
IsFromDemo = 0,
Spectator = (data.isSpectator and 1) or nil,
Password = "12345",
rank = 0,
}
if not data.isSpectator then
teams[teamCount] = {
TeamLeader = playerCount,
AllyTeam = data.allyNumber,
RgbColor = getTeamColor(userName),
Side = WG.Chobby.Configuration:GetSideById(data.side).name,
}
teamCount = teamCount + 1
if friendsReplaceAI then
friendAllyTeam = data.allyNumber
aiReplaceCount = aiReplaceCount + 1
end
end
playerCount = playerCount + 1
end
end
end
-- Check for chicken difficutly modoption. Possibly add an AI due to it.
local chickenName
if self.modoptions and self.modoptions.chickenailevel and self.modoptions.chickenailevel ~= "none" then
chickenName = self.modoptions.chickenailevel
end
-- Add the AIs
local chickenAdded = false
for userName, data in pairs(self.userBattleStatus) do
if data.allyNumber and data.aiLib then
if friendAllyTeam == data.allyNumber and aiReplaceCount > 0 and not string.find(data.aiLib, "Chicken") then
aiReplaceCount = aiReplaceCount - 1
else
if chickenName and string.find(data.aiLib, "Chicken") then
-- Override chicken AI if difficulty modoption is present
ais[aiCount] = {
Name = chickenName,
Team = teamCount,
IsFromDemo = 0,
ShortName = chickenName,
Host = 0,
}
chickenAdded = true
else
ais[aiCount] = {
Name = userName,
Team = teamCount,
IsFromDemo = 0,
ShortName = data.aiLib,
Version = data.aiVersion,
options = data.aiOptions,
Host = 0,
}
end
teams[teamCount] = {
TeamLeader = 0,
AllyTeam = data.allyNumber,
RgbColor = getTeamColor(userName),
Side = WG.Chobby.Configuration:GetSideById(data.side).name,
}
maxAllyTeamID = math.max(maxAllyTeamID, data.allyNumber)
teamCount = teamCount + 1
aiCount = aiCount + 1
end
end
end
-- Add chicken from the modoption if no chicken is present
if chickenName and not chickenAdded then
ais[aiCount] = {
Name = chickenName,
Team = teamCount,
IsFromDemo = 0,
ShortName = chickenName,
Host = 0,
}
aiCount = aiCount + 1
teams[teamCount] = {
TeamLeader = 0,
AllyTeam = maxAllyTeamID + 1,
RgbColor = defaultColor,
}
maxAllyTeamID = maxAllyTeamID + 1
teamCount = teamCount + 1
end
-- Add allyTeams
for i, teamData in pairs(teams) do
if not allyTeams[teamData.AllyTeam] then
allyTeams[teamData.AllyTeam] = {
numallies = 0,
}
end
end
-- This kind of thing would prevent holes in allyTeams
--local allyTeamMap = {}
--for i, teamData in pairs(teams) do
-- if not allyTeamMap[teamData.AllyTeam] then
-- allyTeamMap[teamData.AllyTeam] = allyTeamCount
-- allyTeams[allyTeamCount] = {
-- numallies = 0,
-- }
-- allyTeamCount = allyTeamCount + 1
-- end
-- teamData.AllyTeam = allyTeamMap[teamData.AllyTeam]
--end
-- FIXME: I dislike treating rapid tags like this.
-- We shouldn't give special treatment for rapid tags, and just use them interchangabily with normal archives.
-- So we could just pass "rapid://tag:version" as gameName, while "tag:version" should be invalid.
-- The engine treats rapid dependencies just like normal archives and I see no reason we do otherwise.
if string.find(gameName, ":") and not string.find(gameName, "rapid://") then
gameName = "rapid://" .. gameName
end
local script = {
gametype = gameName,
hostip = "127.0.0.1",
hostport = hostPort or 0,
ishost = 1,
mapname = mapName,
myplayername = playerName,
nohelperais = 0,
numplayers = playerCount,
numusers = playerCount + aiCount,
startpostype = 2,
modoptions = self.modoptions,
GameStartDelay = 0,
}
for i, ai in pairs(ais) do
script["ai" .. i] = ai
end
for i, player in pairs(players) do
script["player" .. i] = player
end
for i, team in pairs(teams) do
script["team" .. i] = team
end
for i, allyTeam in pairs(allyTeams) do
script["allyTeam" .. i] = allyTeam
end
local scriptTxt = self:MakeScriptTXT(script)
Spring.Echo(scriptTxt)
--local scriptFileName = "scriptFile.txt"
--local scriptFile = io.open(scriptFileName, "w")
--scriptFile:write(scriptTxt)
--scriptFile:close()
local Config = WG.Chobby.Configuration
if Config.multiplayerLaunchNewSpring then
if WG.WrapperLoopback and WG.WrapperLoopback.StartNewSpring and WG.SettingsWindow and WG.SettingsWindow.GetSettingsString then
local params = {
StartScriptContent = scriptTxt,
Engine = Config:GetTruncatedEngineVersion(),
SpringSettings = WG.SettingsWindow.GetSettingsString(),
}
WG.WrapperLoopback.StartNewSpring(params)
return
end
end
Spring.Reload(scriptTxt)
end
function InterfaceSkirmish:StartReplay(replayFilename, myName, hostPort)
local scriptTxt =
[[
[GAME]
{
DemoFile=__FILE__;
HostIP=__IP__;
HostPort=__PORT__;
MyPlayerName=__MY_PLAYER_NAME__;
IsHost=1;
}
]]
scriptTxt = scriptTxt:gsub("__FILE__", replayFilename)
:gsub("__IP__", "127.0.0.1")
:gsub("__MY_PLAYER_NAME__", myName or "(spec)")
:gsub("__PORT__", hostPort or 0)
self:_CallListeners("OnBattleAboutToStart", "replay")
Spring.Echo(scriptTxt)
--local scriptFileName = "scriptFile.txt"
--local scriptFile = io.open(scriptFileName, "w")
--scriptFile:write(scriptTxt)
--scriptFile:close()
Spring.Reload(scriptTxt)
return false
end
function InterfaceSkirmish:StartGameFromLuaScript(gameType, scriptTable, friendList, hostPort)
self:_CallListeners("OnBattleAboutToStart", gameType)
friendList = friendList or {}
playerCount = 1 -- Local player is already present
for i = 1, #friendList do
local friendName = friendList[i]
scriptTable["player" .. i] = {
Name = friendName,
Team = 0, -- Player is always team 0 (I hope)
IsFromDemo = 0,
Password = "12345",
rank = 0,
}
playerCount = playerCount + 1
end
scriptTable.numplayers = playerCount
scriptTable.numusers = (playerCount - 2) + scriptTable.numusers
scriptTable.hostip = "127.0.0.1"
scriptTable.hostport = hostPort or 0
scriptTable.ishost = 1
local scriptTxt = self:MakeScriptTXT(scriptTable)
Spring.Echo(scriptTxt)
--local scriptFileName = "scriptFile.txt"
--local scriptFile = io.open(scriptFileName, "w")
--scriptFile:write(scriptTxt)
--scriptFile:close()
Spring.Reload(scriptTxt)
end
function InterfaceSkirmish:StartGameFromString(scriptString, gameType)
self:_CallListeners("OnBattleAboutToStart", gameType)
Spring.Reload(scriptString)
return false
end
function InterfaceSkirmish:StartGameFromFile(scriptFileName, gameType)
self:_CallListeners("OnBattleAboutToStart", gameType)
if self.useSpringRestart then
Spring.Restart(scriptFileName, "")
else
Spring.Start(scriptFileName, "")
end
return false
end
-- TODO: Needs clean implementation in lobby.lua
function InterfaceSkirmish:StartBattle(gameType, myName, friendList, friendsReplaceAI, hostPort)
local battle = self:GetBattle(self:GetMyBattleID())
if not battle.gameName then
Spring.Log(LOG_SECTION, LOG.ERROR, "Missing battle.gameName. Game cannot start")
return self
end
if not battle.mapName then
Spring.Log(LOG_SECTION, LOG.ERROR, "Missing battle.mapName. Game cannot start")
return self
end
self:_CallListeners("OnBattleAboutToStart", gameType, battle.gameName, battle.mapName)
self:_OnSaidBattleEx("Battle", "about to start", battle.gameName, battle.mapName, myName)
self:_StartScript(battle.gameName, battle.mapName, myName, friendList, friendsReplaceAI, hostPort)
return self
end
function InterfaceSkirmish:SelectMap(mapName)
self:_OnUpdateBattleInfo(self:GetMyBattleID(), {
mapName = mapName,
})
end
-- Skirmish only
function InterfaceSkirmish:SetBattleState(myUserName, gameName, mapName, title)
local myBattleID = 1
-- Clear all data when a new battle is created
self:_Clean()
self.battleAis = {}
self.userBattleStatus = {}
self:_OnAddUser(myUserName)
self.myUserName = myUserName
--(battleID, type, natType, founder, ip, port, maxPlayers, passworded, rank, mapHash, other, engineVersion, mapName, title, gameName, spectatorCount)
self:_OnBattleOpened(myBattleID, {
founder = myUserName,
users = {},
gameName = gameName,
mapName = mapName,
title = title,
})
self:_OnJoinBattle(myBattleID, myUserName)
self:_OnJoinedBattle(myBattleID, myUserName)
local modoptions = {}
if VFS.FileExists(LUA_DIRNAME .. "configs/testingModoptions.lua") then
modoptions = VFS.Include(LUA_DIRNAME .. "configs/testingModoptions.lua")
end
self:_OnSetModOptions(modoptions)
return self
end
-------------------------------------------------
-- BEGIN Client commands
-------------------------------------------------
function InterfaceSkirmish:AddAi(aiName, aiLib, allyNumber, version, options)
self:super("AddAi", aiName, aiLib, allyNumber, version, options)
self:_OnAddAi(self:GetMyBattleID(), aiName, {
aiLib = aiLib,
allyNumber = allyNumber,
owner = self:GetMyUserName(),
aiVersion = version,
aiOptions = options,
})
end
function InterfaceSkirmish:SayBattle(message)
self:super("SayBattle", message)
self:_OnSaidBattle(self:GetMyUserName(), message)
return self
end
function InterfaceSkirmish:SayBattleEx(message)
self:super("SayBattleEx", message)
self:_OnSaidBattleEx(self:GetMyUserName(), message)
return self
end
function InterfaceSkirmish:SetBattleStatus(status)
self:super("SetBattleStatus", status)
self:_OnUpdateUserBattleStatus(self:GetMyUserName(), status)
return self
end
function InterfaceSkirmish:LeaveBattle()
self:super("LeaveBattle")
local myBattleID = self:GetMyBattleID()
if myBattleID then
self:_OnLeftBattle(myBattleID, self:GetMyUserName())
self:_OnBattleClosed(myBattleID)
end
return self
end
function InterfaceSkirmish:RemoveAi(aiName)
self:_OnRemoveAi(self:GetMyBattleID(), aiName)
return self
end
function InterfaceSkirmish:UpdateAi(aiName, status)
self:super("SetBattleStatus", status)
self:_OnUpdateUserBattleStatus(aiName, status)
end
function InterfaceSkirmish:SetModOptions(data)
self:_OnSetModOptions(data)
return self
end
-------------------------------------------------
-- END Client commands
-------------------------------------------------
-------------------------------------------------
-- BEGIN Server commands
-------------------------------------------------
-------------------------------------------------
-- END Server commands
-------------------------------------------------
return InterfaceSkirmish