-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGC_AddOnTreeMarker.lua
424 lines (352 loc) · 12.6 KB
/
GC_AddOnTreeMarker.lua
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
--
-- GlobalCompany - AddOn - Icons
--
-- @Interface: --
-- @Author: LS-Modcompany / kevink98
-- @Date: 15.07.2019
-- @Version: 1.0.0.0
--
-- @Support: LS-Modcompany
--
-- Changelog:
--
--
-- v1.0.0.0 (15.07.2019):
-- - convert to fs19 (kevink98)
--
-- Notes:
--
--
-- ToDo:
--
--
--
GC_AddOnTreeMarker = {}
GC_AddOnTreeMarker.MAX_DISTANCE = 5;
GC_AddOnTreeMarker.MARKER_X = 1;
GC_AddOnTreeMarker.MARKER_PL = 2;
GC_AddOnTreeMarker.MARKER_ST = 3;
function GC_AddOnTreeMarker:initGlobalCompany(customEnvironment, baseDirectory, xmlFile)
if (g_company == nil) or (GC_AddOnTreeMarker.isInitiated ~= nil) then
return;
end
GC_AddOnTreeMarker.debugIndex = g_company.debug:registerScriptName("GC_AddOnTreeMarker");
GC_AddOnTreeMarker.modName = customEnvironment;
GC_AddOnTreeMarker.isInitiated = true;
g_company.addOnTreeMarker = GC_AddOnTreeMarker;
g_company.addInit(GC_AddOnTreeMarker, GC_AddOnTreeMarker.init);
end
function GC_AddOnTreeMarker:init()
self.isServer = g_server ~= nil;
self.isClient = g_dedicatedServerInfo == nil;
self.isMultiplayer = g_currentMission.missionDynamicInfo.isMultiplayer;
self.supportetTrees = {};
self.supportetTrees["pine_stage1"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["pine_stage2"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["pine_stage3"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["pine_stage4"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["pine_stage5"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["pine_stage6"] = {"treeFir2", "treeFir3", "treeFir4"};
self.supportetTrees["spruce_stage1"] = {"treeFir5", "treeFir6", "treeFir7"};
self.supportetTrees["spruce_stage2"] = {"treeFir5", "treeFir6", "treeFir7"};
self.supportetTrees["spruce_stage3"] = {"treeFir5", "treeFir6", "treeFir7"};
self.supportetTrees["spruce_stage4"] = {"treeFir5", "treeFir6", "treeFir7"};
self.supportetTrees["spruce_stage5"] = {"treeFir5", "treeFir6", "treeFir7"};
end
function GC_AddOnTreeMarker:findNextTree(p_x, p_y, p_z)
local tree = nil;
local distance = 1000000000;
if self.trees == nil then
self.trees = {};
self:search(getRootNode());
end;
for _, t in pairs(self.trees) do
if entityExists(t) then
local x,y,z = getWorldTranslation(t);
local d = MathUtil.vector3LengthSq(x - p_x, 0, z - p_z);
if d < distance then
distance = d;
tree = t;
end;
end;
end
--[[
for _, t in pairs(g_currentMission.plantedTrees.growingTrees) do
local d = Utils.vector3LengthSq(t.x - p_x, 0, t.z - p_z);
if d < distance and not TreePlantUtil[t.treeType] == createdTreeTypes[t.treeType] then
distance = d;
tree = t.node;
elseif string.format("%.2f",d) == string.format("%.2f",distance) then
self.printE = true;
return nil;
end;
end
]]--
if distance > GC_AddOnTreeMarker.MAX_DISTANCE then
return nil;
end;
return tree;
end;
function GC_AddOnTreeMarker:search(id)
for i=1, getNumOfChildren(id) do
local c = getChildAt(id, i-1);
if self.supportetTrees[getName(c)] ~= nil then
table.insert(self.trees, c);
else
self:search(c);
end;
end;
end;
function GC_AddOnTreeMarker:markTree(tree, markerType)
if not self.isServer then
return;
end;
local supTrees = self.supportetTrees[getName(tree)];
local treeType = supTrees[markerType]
if tree ~= 0 and type(tree) == "number" and supTrees ~= nil and treeType ~= nil then
local x,y,z = getWorldTranslation(tree);
local rx,ry,rz = getRotation(tree);
local growthStateI = tonumber(string.sub(getName(tree), string.len(getName(tree))))
local growthState = growthStateI / ((table.getn(g_treePlantManager.nameToTreeType[string.upper(treeType)].treeFilenames)-1));
g_treePlantManager:plantTree(g_treePlantManager.nameToTreeType[treeType:upper()].index, x, y, z, rx, ry, rz, growthState, growthStateI + 1, true, nil)
delete(tree);
g_treePlantManager:cleanupDeletedTrees()
end;
end
GC_AddOnTreeMarkerHandTool = {};
local GC_AddOnTreeMarkerHandTool_mt = Class(GC_AddOnTreeMarkerHandTool, HandTool);
InitObjectClass(GC_AddOnTreeMarkerHandTool, "GC_AddOnTreeMarkerHandTool");
function GC_AddOnTreeMarkerHandTool:new(isServer, isClient, customMt)
local mt = customMt;
if mt == nil then
mt = GC_AddOnTreeMarkerHandTool_mt;
end
local self = HandTool:new(isServer, isClient, mt);
return self;
end;
function GC_AddOnTreeMarkerHandTool:load(xmlFilename, player)
if not GC_AddOnTreeMarkerHandTool:superClass().load(self, xmlFilename, player) then
return false;
end
local xmlFile = loadXMLFile("TempXML", xmlFilename)
if xmlFile == 0 then
return false
end
local typ = getXMLString(xmlFile, "handTool.handToolType#type");
if typ ~= nil then
if typ == "x" then
self.type = GC_AddOnTreeMarker.MARKER_X;
elseif typ == "pl" then
self.type = GC_AddOnTreeMarker.MARKER_PL;
elseif typ == "st" then
self.type = GC_AddOnTreeMarker.MARKER_ST;
end;
else
self.type = GC_AddOnTreeMarker.MARKER_X;
end;
self.canMark = false;
self.setCanMarkIfCan = false;
self.soundRunStart = 0;
self.soundRunMark = 0;
--self:register(false);
if g_company.addOnTreeMarker.isClient then
self.sounds = {};
self.sounds.onActivateSound = g_soundManager:loadSampleFromXML(xmlFile, "handTool.sounds", "onActivateSound", self.baseDirectory, self.rootNode, 0, AudioGroup.VEHICLE, nil, nil);
self.sounds.onMark = g_soundManager:loadSampleFromXML(xmlFile, "handTool.sounds", "onMark", self.baseDirectory, self.rootNode, 0, AudioGroup.VEHICLE, nil, nil);
local animationNodes = g_company.animationNodes:new(self.isServer, self.isClient)
if animationNodes:load(self.rootNode, self, xmlFile, "handTool") then
self.animationNodes = animationNodes;
end
end;
delete(xmlFile);
return true;
end;
function GC_AddOnTreeMarkerHandTool:delete()
if self.animationNodes ~= nil then
self.animationNodes:delete();
end;
if self.sounds ~= nil then
g_soundManager:deleteSamples(self.sounds)
end;
--self:unregister();
GC_AddOnTreeMarkerHandTool:superClass().delete(self);
end;
function GC_AddOnTreeMarkerHandTool:onActivate(allowInput)
GC_AddOnTreeMarkerHandTool:superClass().onActivate(self);
if self.sounds ~= nil then
g_soundManager:playSample(self.sounds.onActivateSound);
end;
self.soundRunStart = 3000;
self.soundRunMark = 0;
self.canMark = true;
self.activatePressed = false;
end;
function GC_AddOnTreeMarkerHandTool:onDeactivate(allowInput)
GC_AddOnTreeMarkerHandTool:superClass().onDeactivate(self);
--self.canMark = false;
if self.sounds ~= nil then
g_soundManager:stopSample(self.sounds.onActivateSound);
g_soundManager:stopSample(self.sounds.onMark);
end;
if self.animationNodes ~= nil then
self.animationNodes:setAnimationNodesState(false);
end;
end;
function GC_AddOnTreeMarkerHandTool:update(dt, allowInput)
GC_AddOnTreeMarkerHandTool:superClass().update(self, dt, allowInput);
if g_company.addOnTreeMarker.isClient then
if self.soundRunStart > 0 then
self.soundRunStart = math.max(self.soundRunStart - dt, 0);
if self.soundRunStart == 0 then
if self.sounds ~= nil then
g_soundManager:stopSample(self.sounds.onActivateSound);
end;
end;
end;
if not self.activatePressed and self.setCanMarkIfCan then
self.canMark = true;
end;
if self.soundRunMark > 0 then
self.soundRunMark = math.max(self.soundRunMark - dt, 0);
if self.soundRunMark == 0 then
if self.sounds ~= nil then
g_soundManager:stopSample(self.sounds.onMark);
end;
self:toggleEffect();
if self.animationNodes ~= nil then
self.animationNodes:setAnimationNodesState(false);
end;
self.canMark = true;
end;
elseif allowInput and self.activatePressed and self.canMark then
local x,y,z = getWorldTranslation(g_currentMission.player.rootNode);
self.canMark = false;
self:startSearchTree(x,y,z);
end;
self.activatePressed = false;
end;
end;
function GC_AddOnTreeMarkerHandTool:startSearchTree(x,y,z, noEventSend)
GC_AddOnTreeMarkerStartSearchTreeEvent.sendEvent(self.player, x,y,z, self.type, noEventSend);
if not g_company.addOnTreeMarker.isMultiplayer then
local nextTree = g_company.addOnTreeMarker:findNextTree(x,y,z);
if nextTree ~= nil then
g_company.addOnTreeMarker:markTree(nextTree, self.type);
self:startMarkTree();
else
self:findNoTree();
end;
end;
end;
function GC_AddOnTreeMarkerHandTool:startMarkTree()
if g_company.addOnTreeMarker.isClient then
if self.sounds ~= nil then
g_soundManager:playSample(self.sounds.onMark);
end;
if self.animationNodes ~= nil then
self.animationNodes:setAnimationNodesState(true);
end;
self:toggleEffect();
self.soundRunMark = 3000;
end;
end;
function GC_AddOnTreeMarkerHandTool:findNoTree()
self.setCanMarkIfCan = true;
end;
function GC_AddOnTreeMarkerHandTool:toggleEffect()
if self.animationNodes ~= nil then
for _,group in pairs(self.animationNodes.standardAnimations) do
for _,anim in pairs(group.animationNodes) do
setVisibility(anim.node, not getVisibility(anim.node));
end;
end;
end;
end
registerHandTool("GC_AddOnTreeMarkerHandTool", GC_AddOnTreeMarkerHandTool);
GC_AddOnTreeMarkerStartSearchTreeEvent = {};
GC_AddOnTreeMarkerStartSearchTreeEvent_mt = Class(GC_AddOnTreeMarkerStartSearchTreeEvent, Event);
InitEventClass(GC_AddOnTreeMarkerStartSearchTreeEvent, "GC_AddOnTreeMarkerStartSearchTreeEvent");
function GC_AddOnTreeMarkerStartSearchTreeEvent:emptyNew()
local self = Event:new(GC_AddOnTreeMarkerStartSearchTreeEvent_mt)
return self;
end;
function GC_AddOnTreeMarkerStartSearchTreeEvent:new(player, x,y,z, type)
local self = GC_AddOnTreeMarkerStartSearchTreeEvent:emptyNew()
self.player = player;
self.x = x;
self.y = y;
self.z = z;
self.type = type;
return self
end;
function GC_AddOnTreeMarkerStartSearchTreeEvent:readStream(streamId, connection)
self.player = NetworkUtil.readNodeObject(streamId);
self.x = streamReadFloat32(streamId);
self.y = streamReadFloat32(streamId);
self.z = streamReadFloat32(streamId);
self.type = streamReadInt8(streamId);
self:run(connection);
end;
function GC_AddOnTreeMarkerStartSearchTreeEvent:writeStream(streamId, connection)
NetworkUtil.writeNodeObject(streamId, self.player);
streamWriteFloat32(streamId, self.x);
streamWriteFloat32(streamId, self.y);
streamWriteFloat32(streamId, self.z);
streamWriteInt8(streamId, self.type);
end;
function GC_AddOnTreeMarkerStartSearchTreeEvent:run(connection)
if not connection:getIsServer() then
local nextTree = g_company.addOnTreeMarker:findNextTree(self.x, self.y, self.z);
if nextTree ~= nil then
g_company.addOnTreeMarker:markTree(nextTree, self.type);
g_server:broadcastEvent(GC_AddOnTreeMarkerClientEffectsEvent:new(self.player, true), nil, nil, self.player);
else
g_server:broadcastEvent(GC_AddOnTreeMarkerClientEffectsEvent:new(self.player, false), nil, nil, self.player);
end;
end;
end;
function GC_AddOnTreeMarkerStartSearchTreeEvent.sendEvent(player, x,y,z, type, noEventSend)
local currentTool = player.baseInformation.currentHandtool;
if currentTool ~= nil and currentTool.startMarkTree ~= nil then
if noEventSend == nil or noEventSend == false then
if g_server ~= nil then
g_server:broadcastEvent(GC_AddOnTreeMarkerStartSearchTreeEvent:new(player, x,y,z, type), nil, nil, player);
else
g_client:getServerConnection():sendEvent(GC_AddOnTreeMarkerStartSearchTreeEvent:new(player, x,y,z, type));
end;
end;
end;
end;
GC_AddOnTreeMarkerClientEffectsEvent = {};
GC_AddOnTreeMarkerClientEffectsEvent_mt = Class(GC_AddOnTreeMarkerClientEffectsEvent, Event);
InitEventClass(GC_AddOnTreeMarkerClientEffectsEvent, "GC_AddOnTreeMarkerClientEffectsEvent");
function GC_AddOnTreeMarkerClientEffectsEvent:emptyNew()
return Event:new(GC_AddOnTreeMarkerClientEffectsEvent_mt);
end;
function GC_AddOnTreeMarkerClientEffectsEvent:new(player, state)
local self = GC_AddOnTreeMarkerClientEffectsEvent:emptyNew()
self.player = player;
self.state = state;
return self;
end;
function GC_AddOnTreeMarkerClientEffectsEvent:readStream(streamId, connection)
self.player = NetworkUtil.readNodeObject(streamId);
self.state = streamReadBool(streamId);
self:run(connection);
end;
function GC_AddOnTreeMarkerClientEffectsEvent:writeStream(streamId, connection)
NetworkUtil.writeNodeObject(streamId, self.player);
streamWriteBool(streamId, self.state);
end;
function GC_AddOnTreeMarkerClientEffectsEvent:run(connection)
local currentTool = self.player.baseInformation.currentHandtool;
if self.state then
if currentTool ~= nil and currentTool.startMarkTree ~= nil then
currentTool:startMarkTree();
end;
else
if currentTool ~= nil and currentTool.findNoTree ~= nil then
currentTool:findNoTree();
end;
end;
end;