File size: 6,135 Bytes
b6a38d7 |
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 |
if FirstLoad then
Game = false
end
PersistableGlobals.Game = true
if not Platform.ged then
DefineClass.GameClass = {
__parents = { "CooldownObj", "GameSettings", "LabelContainer" },
}
end
function NewGame(game)
DoneGame()
if not IsKindOf(game, "GameClass") then
game = GameClass:new(game)
end
game.save_id = nil
Game = game
InitGameVars()
Msg("NewGame", game)
NetGossip("NewGame", game.id, GetGameSettingsTable(game))
return game
end
function DoneGame()
local game = Game
if not game then
return
end
NetGossip("DoneGame", GameTime(), game.id)
DoneGameVars()
Game = false
Msg("DoneGame", game)
game:delete()
end
function DevReloadMap()
ReloadMap(true)
end
function RestartGame()
CreateRealTimeThread(function()
LoadingScreenOpen("idLoadingScreen", "RestartGame")
local map = GetOrigMapName()
local game2 = CloneObject(Game)
ChangeMap("")
NewGame(game2)
ChangeMap(map)
LoadingScreenClose("idLoadingScreen", "RestartGame")
end)
end
function RestartGameFromMenu(host, parent)
CreateRealTimeThread(function(host, parent)
if WaitQuestion(parent or host, T(354536203098, "<RestartMapText()>"), T(1000852, "Are you sure you want to restart the map? Any unsaved progress will be lost."), T(147627288183, "Yes"), T(1139, "No")) == "ok" then
LoadingScreenOpen("idLoadingScreen", "RestartMap")
if host.window_state ~= "destroying" then
host:Close()
end
RestartGame()
LoadingScreenClose("idLoadingScreen", "RestartMap")
end
end, host, parent)
end
function OnMsg.ChangeMap(map, mapdata)
ChangeGameState("gameplay", false)
end
function GetDefaultGameParams()
end
function OnMsg.PreNewMap(map, mapdata)
if map ~= "" and not Game and mapdata.GameLogic and mapdata.MapType ~= "system" then
NewGame(GetDefaultGameParams())
end
end
function OnMsg.ChangeMapDone(map)
if map ~= "" and mapdata.GameLogic then
ChangeGameState("gameplay", true)
end
end
function OnMsg.LoadGame()
assert( GetMap() ~= "" )
ChangeGameState("gameplay", true)
if not Game then return end
Game.loaded_from_id = Game.save_id
NetGossip("LoadGame", Game.id, Game.loaded_from_id, GetGameSettingsTable(Game))
end
function OnMsg.SaveGameStart()
if not Game then return end
Game.save_id = random_encode64(48)
NetGossip("SaveGame", GameTime(), Game.id, Game.save_id)
end
function GetGameSettingsTable(game)
local settings = {}
assert(IsKindOf(game, "GameSettings"))
for _, prop_meta in ipairs(GameSettings:GetProperties()) do
settings[prop_meta.id] = game:GetProperty(prop_meta.id)
end
return settings
end
function OnMsg.NewMap()
NetGossip("map", GetMapName(), MapLoadRandom)
end
function OnMsg.ChangeMap(map)
if map == "" then
NetGossip("map", "")
end
end
function OnMsg.NetConnect()
if Game then
NetGossip("GameInProgress", GameTime(), Game.id, Game.loaded_from_id, GetMapName(), MapLoadRandom, GetGameSettingsTable(Game))
end
end
function OnMsg.BugReportStart(print_func)
if Game then
print_func("\nGameSettings:", TableToLuaCode(GetGameSettingsTable(Game), " "), "\n")
end
end
-- GameVars (persistable, reset on new game)
GameVars = {}
GameVarValues = {}
function GameVar(name, value, meta)
if type(value) == "table" then
local org_value = value
value = function()
local v = table.copy(org_value, false)
setmetatable(v, getmetatable(org_value) or meta)
return v
end
end
if FirstLoad or rawget(_G, name) == nil then
rawset(_G, name, false)
end
GameVars[#GameVars + 1] = name
GameVarValues[name] = value or false
PersistableGlobals[name] = true
end
function InitGameVars()
for _, name in ipairs(GameVars) do
local value = GameVarValues[name]
if type(value) == "function" then
value = value()
end
_G[name] = value or false
end
end
function DoneGameVars()
for _, name in ipairs(GameVars) do
_G[name] = false
end
end
function OnMsg.PersistPostLoad(data)
-- create missing game vars (unexisting at the time of the save)
for _, name in ipairs(GameVars) do
if data[name] == nil then
local value = GameVarValues[name]
if type(value) == "function" then
value = value()
end
_G[name] = value or false
end
end
end
function GetCurrentGameVarValues()
local gvars = {}
for _, name in ipairs(GameVars) do
gvars[name] = _G[name]
end
return gvars
end
function GetPersistableGameVarValues()
local gvars = {}
for _, name in ipairs(GameVars) do
if PersistableGlobals[name] then
gvars[name] = _G[name]
end
end
return gvars
end
----
GameVar("LastPlaytime", 0)
if FirstLoad then
PlaytimeCheckpoint = false
end
function OnMsg.SaveGameStart()
LastPlaytime = GetCurrentPlaytime()
PlaytimeCheckpoint = GetPreciseTicks()
end
function OnMsg.LoadGame()
PlaytimeCheckpoint = GetPreciseTicks()
end
function OnMsg.NewGame()
PlaytimeCheckpoint = GetPreciseTicks() -- also called on LoadGame
end
function OnMsg.DoneGame()
PlaytimeCheckpoint = false
end
function GetCurrentPlaytime()
return PlaytimeCheckpoint and (LastPlaytime + (GetPreciseTicks() - PlaytimeCheckpoint)) or 0
end
function FormatElapsedTime(time, format)
format = format or "dhms"
local sec = 1000
local min = 60 * sec
local hour = 60 * min
local day = 24 * hour
local res = {}
if format:find_lower("d") then
res[#res + 1] = time / day
time = time % day
end
if format:find_lower("h") then
res[#res + 1] = time / hour
time = time % hour
end
if format:find_lower("m") then
res[#res + 1] = time / min
time = time % min
end
if format:find_lower("s") then
res[#res + 1] = time / sec
time = time % sec
end
res[#res + 1] = time
return table.unpack(res)
end
if Platform.asserts then
function OnMsg.NewMapLoaded()
if not Game then return end
local last_game = LocalStorage.last_game
local count = 0
for _, prop_meta in ipairs(GameSettings:GetProperties()) do
if prop_meta.remember_as_last then
local value = Game[prop_meta.id]
last_game = last_game or {}
if value ~= last_game[prop_meta.id] then
last_game[prop_meta.id] = value
count = count + 1
end
end
end
if count == 0 then return end
LocalStorage.last_game = last_game
SaveLocalStorageDelayed()
end
end -- Platform.asserts
|