-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSS
198 lines (174 loc) · 7.32 KB
/
SSS
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
--SIMPLE STATICS SAVING by Pikey, May 2019
--updated March 2020 to v2 (different methods)
--Usage of this script should credit the following contributors:
--Pikey
--Speed & Grimes for their work on Serialising tables, included below,
--FlightControl for MOOSE (Required)
--INTENDED USAGE
--DCS Server Admins looking to do long term multi session play that will need a server reboot in between and they wish to keep
--Statics surviving from one mission reload to the next.
--USAGE
--Ensure LFS and IO are not santitised in missionScripting.lua. This enables writing of files. If you don't know what this does, don't attempt to use this script.
--Requires versions of MOOSE.lua and DCS 2.5.6 due to the static handling and current bug set
--MIST not required, but should work OK with it regardless.
--Edit 'SaveScheduleUnits' below, (line 34) to the number of seconds between saves. Low impact. 10 seconds is a fast schedule.
--Place statics wherever you want on the map as normal. Avoid ships. Smoke and farps, oilrigs are ignored.
--Run this script at Mission start
--The script will create a small file in the root of your DCS game folder with the list of Statics called SaveStatics.lua.
--At Mission Start it will check for a save file, if not there, create it fresh
--If the table is there, it loads it destroys everything that was killed last mission.
--The table is updated throughout mission play
--The next time the mission is loaded it goes through all the Statics again and loads them from the save file.
--You may delete the marked lines 138-156 if you do NOT want dead replacements spawned. (also puts an error per static spawn if not deleted)
--LIMITATIONS
--Ships always report alive, even when dead. Avoid static ships. DCS bug.
--Smoke effects are ignored, because you cannot get descriptions, types, heading etc and they wont destroy() or spawn. DCS bug (WIP)
--Farps and Oilrigs are ignored because they cannot be accessed or destroyed in the same way as smoke. DCS bug or working as designed.
--Moose currently errors when a Static is spawned. I can't fix this. It might be because I spawn statics dead.
---------------------------------
--Configurable for user:
SaveScheduleStatics=30 --how many seconds between each check and save.
-----------------------------------
--Do not edit below here
-----------------------------------
local version = "2.0 - March 2020"
function IntegratedbasicSerialize(s)
if s == nil then
return "\"\""
else
if ((type(s) == 'number') or (type(s) == 'boolean') or (type(s) == 'function') or (type(s) == 'table') or (type(s) == 'userdata') ) then
return tostring(s)
elseif type(s) == 'string' then
return string.format('%q', s)
end
end
end
-- imported slmod.serializeWithCycles (Speed)
function IntegratedserializeWithCycles(name, value, saved)
local basicSerialize = function (o)
if type(o) == "number" then
return tostring(o)
elseif type(o) == "boolean" then
return tostring(o)
else -- assume it is a string
return IntegratedbasicSerialize(o)
end
end
local t_str = {}
saved = saved or {} -- initial value
if ((type(value) == 'string') or (type(value) == 'number') or (type(value) == 'table') or (type(value) == 'boolean')) then
table.insert(t_str, name .. " = ")
if type(value) == "number" or type(value) == "string" or type(value) == "boolean" then
table.insert(t_str, basicSerialize(value) .. "\n")
else
if saved[value] then -- value already saved?
table.insert(t_str, saved[value] .. "\n")
else
saved[value] = name -- save name for next time
table.insert(t_str, "{}\n")
for k,v in pairs(value) do -- save its fields
local fieldname = string.format("%s[%s]", name, basicSerialize(k))
table.insert(t_str, IntegratedserializeWithCycles(fieldname, v, saved))
end
end
end
return table.concat(t_str)
else
return ""
end
end
function file_exists(name) --check if the file already exists for writing
if lfs.attributes(name) then
return true
else
return false end
end
function writemission(data, file)--Function for saving to file (commonly found)
File = io.open(file, "w")
File:write(data)
File:close()
end
function Alive(static)
if static:IsAlive() then return false else return true end
end
function rng()
local roll = math.random(1,100)
return roll
end
function rngsmokes(coordinate)
smokes={coordinate:BigSmokeMedium(0.1),coordinate:BigSmokeMedium(0.2),coordinate:BigSmokeMedium(0.3),coordinate:BigSmokeMedium(0.2), coordinate:BigSmokeMedium(0.5), coordinate:BigSmokeMedium(0.8),coordinate:BigSmokeLarge(0.1),coordinate:BigSmokeHuge(0.1),}
if rng() > 95 then
smoke = smokes[math.random(1,#smokes)]
else
end
end
--SCRIPT START
env.info("Loaded Simple Statics Saving, by Pikey, 2018 version " .. version)
mismatch=0 --counter for objects in file and in table differences
if file_exists("SaveStatics.lua") then
env.info("Script loading existing database")
dofile("SaveStatics.lua")
AllStatics = SET_STATIC:New():FilterStart()
AllStatics:ForEach(function (stat)
local _name = stat:GetName()
if AIRBASE:FindByName(_name) ~= nil or stat:GetDesc() == nil then
-- env.info(_name.." is a type of airbase, ignoring")
elseif SaveStatics[_name] == nil then
--this object was added to the mission since save
mismatch=mismatch+1
elseif SaveStatics[_name]["dead"] == true then
stat:Destroy()
-- to not replace models with dead models, delete from here
local tempTable =
{
["heading"] = SaveStatics[_name]["heading"],
["heading"] = SaveStatics[_name]["heading"],
["shape_name"] = SaveStatics[_name]["shape_name"],
["type"] = SaveStatics[_name]["type"],
["unitId"] = SaveStatics[_name]["unitId"],
["rate"] = 20,
["name"] = _name,
["category"] = SaveStatics[_name]["category"],
["y"] = SaveStatics[_name]["y"],
["x"] = SaveStatics[_name]["x"],
["dead"] = true,
["Country"] = SaveStatics[_name]["Country"],
}
coalition.addStaticObject(SaveStatics[_name]["Country"], tempTable) --SaveStatics[k]["Country"]
--to here. Note this will produce an error in moose because it doesn't like adding a dead static to the database.
end
end)
else --Save File does not exist we start a fresh table
SaveStatics={}
AllStatics = SET_STATIC:New():FilterStart()
end
if mismatch > 0 then
env.error("IMPORTANT - you have ".. mismatch .." new items in your save file since the last time. Suggest you recreate it")
end
--THE SAVING SCHEDULE
SCHEDULER:New( nil, function()
AllStatics:ForEach(function (grp)
local _name = grp:GetName()
if AIRBASE:FindByName(_name) ~= nil or grp:GetDesc() == nil then --excludes all static types that will error it
else
SaveStatics[grp:GetName()] =
{
["heading"] = grp:GetHeading(),
["groupId"] =grp:GetID(),
["shape_name"] = grp:GetTypeName(),
["type"] = grp:GetTypeName(),
["unitId"] = grp:GetID(),
["rate"] = 20,
["name"] = grp:GetName(),
["category"] = grp:GetCategoryName(),
["y"] = grp:GetVec2().y,
["x"] = grp:GetVec2().x,
["dead"] = Alive(grp),
["Country"] = grp:GetCountry()
}
end
end)
local newMissionStr = IntegratedserializeWithCycles("SaveStatics",SaveStatics)
writemission(newMissionStr, "SaveStatics.lua")
SaveStatics={}
end, {}, 5, SaveScheduleStatics)