Sprache Deutsch Language English

Script Dokumentation LS 2015 - TedderAreaEvent (Patch 1.3)

Script Dokumentation Übersicht

scripts/vehicles/specializations/TedderAreaEvent.lua

Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1-- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
2
3TedderAreaEvent = {};
4TedderAreaEvent_mt = Class(TedderAreaEvent, Event);
5
6InitStaticEventClass(TedderAreaEvent, "TedderAreaEvent", EventIds.EVENT_TEDDER_AREA);
7
8function TedderAreaEvent:emptyNew()
9 local self = Event:new(TedderAreaEvent_mt);
10 return self;
11end;
12
13function TedderAreaEvent:new(workAreas, bitType)
14 local self = TedderAreaEvent:emptyNew()
15 self.workAreas = workAreas;
16 self.bitType = bitType;
17 assert(self.bitType >= 0 and self.bitType <= 3);
18 assert(table.getn(self.workAreas) > 0 and table.getn(self.workAreas) < 16);
19 return self;
20end;
21
22function TedderAreaEvent:readStream(streamId, connection)
23 local numAreas = streamReadUIntN(streamId, 4);
24 local v = {};
25 for i=1,numAreas do
26 v[i] = streamReadUIntN(streamId, g_currentMission.numWindrowChannels);
27 end;
28
29 local refX = streamReadFloat32(streamId);
30 local refY = streamReadFloat32(streamId);
31 local values = Utils.readCompressed2DVectors(streamId, refX, refY, numAreas*6-1, 0.01, true);
32 for i=1,numAreas do
33 local vi = i-1;
34 local x = values[vi*6+1].x;
35 local z = values[vi*6+1].y;
36 local x1 = values[vi*6+2].x;
37 local z1 = values[vi*6+2].y;
38 local x2 = values[vi*6+3].x;
39 local z2 = values[vi*6+3].y;
40 local dx = values[vi*6+4].x;
41 local dz = values[vi*6+4].y;
42 local dx1 = values[vi*6+5].x;
43 local dz1 = values[vi*6+5].y;
44 local dx2 = values[vi*6+6].x;
45 local dz2 = values[vi*6+6].y;
46
47 Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_GRASS, x, z, x1, z1, x2, z2, 0);
48 Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_DRYGRASS, x, z, x1, z1, x2, z2, 0);
49
50 -- now that we removed the cut long and windrow, maybe there is some hidden drygrass to grow (set it to growth state 1 if there is some)
51 Utils.switchFruitTypeArea(FruitUtil.FRUITTYPE_GRASS, FruitUtil.FRUITTYPE_DRYGRASS, x, z, x1, z1, x2, z2, 2);
52
53 if v[i] >= 1 then
54 -- switch all dry grass to grass, and then destroy everything that is not grass
55 Utils.switchFruitTypeArea(FruitUtil.FRUITTYPE_GRASS, FruitUtil.FRUITTYPE_DRYGRASS, dx, dz, dx1, dz1, dx2, dz2);
56 Utils.destroyOtherFruit(FruitUtil.FRUITTYPE_GRASS, dx, dz, dx1, dz1, dx2, dz2);
57
58 -- place the windrow
59 Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_DRYGRASS, dx, dz, dx1, dz1, dx2, dz2, v[i], true, false);
60 end;
61 end;
62end;
63
64function TedderAreaEvent:writeStream(streamId, connection)
65 local numAreas = table.getn(self.workAreas);
66 streamWriteUIntN(streamId, numAreas, 4);
67
68 for i=1, numAreas do
69 streamWriteUIntN(streamId, self.workAreas[i][13], g_currentMission.numWindrowChannels);
70 end;
71
72 local refX, refY;
73 local values = {};
74 for i=1, numAreas do
75 local d = self.workAreas[i];
76 if i==1 then
77 refX = d[1];
78 refY = d[2];
79 streamWriteFloat32(streamId, d[1]);
80 streamWriteFloat32(streamId, d[2]);
81 else
82 table.insert(values, {x=d[1], y=d[2]});
83 end;
84 table.insert(values, {x=d[3], y=d[4]});
85 table.insert(values, {x=d[5], y=d[6]});
86 table.insert(values, {x=d[7], y=d[8]});
87 table.insert(values, {x=d[9], y=d[10]});
88 table.insert(values, {x=d[11], y=d[12]});
89 end;
90 assert(table.getn(values) == numAreas*6 - 1);
91 Utils.writeCompressed2DVectors(streamId, refX, refY, values, 0.01, self.bitType);
92end;
93
94function TedderAreaEvent:run(connection)
95 print("Error: Do not run TedderAreaEvent locally");
96end;
97
98function TedderAreaEvent.runLocally(workAreas, accumulatedWorkAreaValues)
99
100 local numAreas = table.getn(workAreas);
101
102 local refX, refY;
103 local values = {};
104 for i=1, numAreas do
105 local d = workAreas[i];
106 if i==1 then
107 refX = d[1];
108 refY = d[2];
109 else
110 table.insert(values, {x=d[1], y=d[2]});
111 end;
112 table.insert(values, {x=d[3], y=d[4]});
113 table.insert(values, {x=d[5], y=d[6]});
114 table.insert(values, {x=d[7], y=d[8]});
115 table.insert(values, {x=d[9], y=d[10]});
116 table.insert(values, {x=d[11], y=d[12]});
117 end;
118 assert(table.getn(values) == numAreas*6 - 1);
119
120 local values, bitType = Utils.simWriteCompressed2DVectors(refX, refY, values, 0.01, true);
121
122 local retWorkAreas = {};
123 for i=1, numAreas do
124 local vi = i-1;
125 local x = values[vi*6+1].x;
126 local z = values[vi*6+1].y;
127 local x1 = values[vi*6+2].x;
128 local z1 = values[vi*6+2].y;
129 local x2 = values[vi*6+3].x;
130 local z2 = values[vi*6+3].y;
131 local dx = values[vi*6+4].x;
132 local dz = values[vi*6+4].y;
133 local dx1 = values[vi*6+5].x;
134 local dz1 = values[vi*6+5].y;
135 local dx2 = values[vi*6+6].x;
136 local dz2 = values[vi*6+6].y;
137
138 local area = Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_GRASS, x, z, x1, z1, x2, z2, 0);
139 area = area + Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_DRYGRASS, x, z, x1, z1, x2, z2, 0);
140
141 -- add the accumulated value
142 area = area + accumulatedWorkAreaValues[i];
143 accumulatedWorkAreaValues[i] = 0;
144
145
146 if area > 0 then
147 -- now that we removed the cut long and windrow, maybe there is some hidden drygrass to grow (set it to growth state 1 if there is some)
148 Utils.switchFruitTypeArea(FruitUtil.FRUITTYPE_GRASS, FruitUtil.FRUITTYPE_DRYGRASS, x, z, x1, z1, x2, z2, 2);
149
150 local old, total = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_DRYGRASS, dx, dz, dx1, dz1, dx2, dz2);
151 local value = (area+old) / total;
152 value = math.floor(value);
153 --[[if value < 1 and value > 0.1 then
154 value = 1;
155 else
156 value = math.floor(value + 0.6); -- round, biased to the bigger value
157 end;]]
158 if value >= 1 then
159 -- calculate the error we make by the rounding and clamping, so we can add it the next time
160 value = math.min(value, g_currentMission.maxWindrowValue);
161 accumulatedWorkAreaValues[i] = math.min(math.max(area+old - value*total, 0), g_currentMission.maxWindrowValue);
162
163 -- switch all dry grass to grass, and then destroy everything that is not grass
164 Utils.switchFruitTypeArea(FruitUtil.FRUITTYPE_GRASS, FruitUtil.FRUITTYPE_DRYGRASS, dx, dz, dx1, dz1, dx2, dz2);
165 Utils.destroyOtherFruit(FruitUtil.FRUITTYPE_GRASS, dx, dz, dx1, dz1, dx2, dz2);
166
167 -- now place the windrow, this will switch back the grass to dry grass
168 Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_DRYGRASS, dx, dz, dx1, dz1, dx2, dz2, value, true, false);
169 else
170 -- calculate the error we make by not changing the drop area
171 accumulatedWorkAreaValues[i] = area;
172 end;
173 workAreas[i][13] = value;
174 if value > 0 then
175 table.insert(retWorkAreas, workAreas[i]);
176 end;
177 end;
178 end;
179 return retWorkAreas, bitType;
180end;
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
Script Dokumentation Übersicht