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 | |
3 | CombineAreaEvent = {}; |
4 | CombineAreaEvent_mt = Class(CombineAreaEvent, Event); |
5 | |
6 | InitStaticEventClass(CombineAreaEvent, "CombineAreaEvent", EventIds.EVENT_COMBINE_AREA); |
7 | |
8 | function CombineAreaEvent:emptyNew() |
9 | local self = Event:new(CombineAreaEvent_mt); |
10 | return self; |
11 | end |
12 | |
13 | function CombineAreaEvent:new(workAreas, bitType, fillType) |
14 | local self = CombineAreaEvent:emptyNew() |
15 | self.workAreas = workAreas; |
16 | self.bitType = bitType; |
17 | assert(self.bitType >= 0 and self.bitType <= 3); |
18 | self.fillType = fillType; |
19 | return self; |
20 | end |
21 | |
22 | function CombineAreaEvent:readStream(streamId, connection) |
23 | local fillType = streamReadUIntN(streamId, Fillable.sendNumBits); |
24 | local numAreas = streamReadUIntN(streamId, 4); |
25 | |
26 | local v = {}; |
27 | for i=1, numAreas do |
28 | v[i] = streamReadUIntN(streamId, g_currentMission.numWindrowChannels); |
29 | end |
30 | |
31 | local refX = streamReadFloat32(streamId); |
32 | local refY = streamReadFloat32(streamId); |
33 | local values = Utils.readCompressed2DVectors(streamId, refX, refY, numAreas*3-1, 0.01, true); |
34 | local fruitType = FruitUtil.fillTypeToFruitType[fillType]; |
35 | for i=1, numAreas do |
36 | local vi = i-1; |
37 | local x = values[vi*3+1].x; |
38 | local z = values[vi*3+1].y; |
39 | local x1 = values[vi*3+2].x; |
40 | local z1 = values[vi*3+2].y; |
41 | local x2 = values[vi*3+3].x; |
42 | local z2 = values[vi*3+3].y; |
43 | |
44 | Utils.updateFruitWindrowArea(fruitType, x, z, x1, z1, x2, z2, v[i], true); |
45 | end |
46 | end |
47 | |
48 | function CombineAreaEvent:writeStream(streamId, connection) |
49 | streamWriteUIntN(streamId, self.fillType, Fillable.sendNumBits); |
50 | local numAreas = table.getn(self.workAreas); |
51 | streamWriteUIntN(streamId, numAreas, 4); |
52 | |
53 | for i=1, numAreas do |
54 | streamWriteUIntN(streamId, self.workAreas[i][7], g_currentMission.numWindrowChannels); |
55 | end |
56 | |
57 | local refX, refY; |
58 | local values = {}; |
59 | for i=1, numAreas do |
60 | local d = self.workAreas[i]; |
61 | if i==1 then |
62 | refX = d[1]; |
63 | refY = d[2]; |
64 | streamWriteFloat32(streamId, d[1]); |
65 | streamWriteFloat32(streamId, d[2]); |
66 | else |
67 | table.insert(values, {x=d[1], y=d[2]}); |
68 | end |
69 | table.insert(values, {x=d[3], y=d[4]}); |
70 | table.insert(values, {x=d[5], y=d[6]}); |
71 | end |
72 | assert(table.getn(values) == numAreas*3 - 1); |
73 | Utils.writeCompressed2DVectors(streamId, refX, refY, values, 0.01, self.bitType); |
74 | end |
75 | |
76 | function CombineAreaEvent:run(connection) |
77 | print("Error: Do not run CombineAreaEvent locally"); |
78 | end |
79 | |
80 | function CombineAreaEvent.runLocally(workAreas, fillType, accumulatedStrawAreaValues) |
81 | |
82 | local numAreas = table.getn(workAreas); |
83 | |
84 | local refX, refY; |
85 | local values = {}; |
86 | for i=1, numAreas do |
87 | local d = workAreas[i]; |
88 | if i==1 then |
89 | refX = d[1]; |
90 | refY = d[2]; |
91 | else |
92 | table.insert(values, {x=d[1], y=d[2]}); |
93 | end |
94 | table.insert(values, {x=d[3], y=d[4]}); |
95 | table.insert(values, {x=d[5], y=d[6]}); |
96 | end |
97 | assert(table.getn(values) == numAreas*3 - 1); |
98 | |
99 | local values, bitType = Utils.simWriteCompressed2DVectors(refX, refY, values, 0.01, true); |
100 | |
101 | local retWorkAreas = {}; |
102 | for i=1, numAreas do |
103 | local vi = i-1; |
104 | local x = values[vi*3+1].x; |
105 | local z = values[vi*3+1].y; |
106 | local x1 = values[vi*3+2].x; |
107 | local z1 = values[vi*3+2].y; |
108 | local x2 = values[vi*3+3].x; |
109 | local z2 = values[vi*3+3].y; |
110 | |
111 | local area = workAreas[i][7]; |
112 | |
113 | area = area + accumulatedStrawAreaValues[i]; |
114 | accumulatedStrawAreaValues[i] = 0; |
115 | |
116 | if area > 0 then |
117 | local fruitType = FruitUtil.fillTypeToFruitType[fillType]; |
118 | local old, total = Utils.getFruitWindrowArea(fruitType, x, z, x1, z1, x2, z2); |
119 | local value = (area+old) / total; |
120 | value = math.floor(value); |
121 | |
122 | if value >= 1 then |
123 | -- calculate the error we make by the rounding and clamping, so we can add it the next time |
124 | value = math.min(value, g_currentMission.maxWindrowValue); |
125 | accumulatedStrawAreaValues[i] = math.min(math.max(area+old - value*total, 0), g_currentMission.maxWindrowValue); |
126 | |
127 | Utils.updateFruitWindrowArea(fruitType, x, z, x1, z1, x2, z2, value, true); |
128 | |
129 | workAreas[i][7] = value; |
130 | table.insert(retWorkAreas, workAreas[i]); |
131 | else |
132 | -- calculate the error we make by not changing the drop area |
133 | accumulatedStrawAreaValues[i] = area; |
134 | end |
135 | end |
136 | end |
137 | return retWorkAreas, bitType; |
138 | end
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de