forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.lua
255 lines (212 loc) · 8.31 KB
/
register.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
--COURSEPLAY
SpecializationUtil.registerSpecialization('courseplay', 'courseplay', g_currentModDirectory .. 'courseplay.lua');
if courseplay.houstonWeGotAProblem then
return;
end;
local drivableSpec = SpecializationUtil.getSpecialization('drivable');
local courseplaySpec = SpecializationUtil.getSpecialization('courseplay');
local numInstallationsVehicles = 0;
function courseplay:register(secondTime)
if secondTime then
print('## Courseplay: register later loaded mods');
end
for typeName,vehicleType in pairs(VehicleTypeUtil.vehicleTypes) do
if vehicleType and not SpecializationUtil.hasSpecialization(courseplay, vehicleType.specializations) then
for i,spec in pairs(vehicleType.specializations) do
if spec and spec == drivableSpec then
if courseplay.isDevVersion then
print((' adding Courseplay to %q'):format(tostring(vehicleType.name)));
end
table.insert(vehicleType.specializations, courseplaySpec);
vehicleType.hasCourseplaySpec = true;
vehicleType.hasDrivableSpec = true;
numInstallationsVehicles = numInstallationsVehicles + 1;
break;
end;
end;
end;
end;
end;
AIVehicle.startAIVehicle = Utils.overwrittenFunction(AIVehicle.startAIVehicle,courseplay.startAIVehicle)
function courseplay:attachablePostLoad(xmlFile)
if self.cp == nil then self.cp = {}; end;
if self.cp.xmlFileName == nil then
self.cp.xmlFileName = courseplay.utils:getFileNameFromPath(self.configFileName);
end;
--SET SPECIALIZATION VARIABLE
courseplay:setNameVariable(self);
courseplay:setCustomSpecVariables(self);
if courseplay.liquidManureOverloaders == nil then
courseplay.liquidManureOverloaders ={}
end
if self.cp.isLiquidManureOverloader then
courseplay.liquidManureOverloaders[self.rootNode] = self
end
--SEARCH AND SET OBJECT'S self.name IF NOT EXISTING
if self.name == nil then
self.name = courseplay:getObjectName(self, xmlFile);
end;
end;
Attachable.postLoad = Utils.appendedFunction(Attachable.postLoad, courseplay.attachablePostLoad);
function courseplay:attachableDelete()
if self.cp ~= nil then
if self.cp.isLiquidManureOverloader then
courseplay.liquidManureOverloaders[self.rootNode] = nil
end
end;
end;
Attachable.delete = Utils.prependedFunction(Attachable.delete, courseplay.attachableDelete);
function courseplay.vehiclePostLoadFinished(self)
if self.cp == nil then self.cp = {}; end;
-- XML FILE NAME VARIABLE
if self.cp.xmlFileName == nil then
self.cp.xmlFileName = courseplay.utils:getFileNameFromPath(self.configFileName);
end;
-- make sure every vehicle has the CP API functions
self.getIsCourseplayDriving = courseplay.getIsCourseplayDriving;
self.setIsCourseplayDriving = courseplay.setIsCourseplayDriving;
self.setCpVar = courseplay.setCpVar;
courseplay:setNameVariable(self);
-- combines table
if courseplay.combines == nil then
courseplay.combines = {};
end;
if self.cp.isCombine or self.cp.isChopper or self.cp.isHarvesterSteerable or self.cp.isSugarBeetLoader or courseplay:isAttachedCombine(self) then
courseplay.combines[self.rootNode] = self;
end;
end;
Vehicle.loadFinished = Utils.appendedFunction(Vehicle.loadFinished, courseplay.vehiclePostLoadFinished);
-- NOTE: using loadFinished() instead of load() so any other mod that overwrites Vehicle.load() doesn't interfere
function courseplay:prePreDelete(self)
if self.cp ~= nil then
courseplay:deleteMapHotspot(self);
end
end;
FSBaseMission.removeVehicle = Utils.prependedFunction(FSBaseMission.removeVehicle, courseplay.prePreDelete);
function courseplay:vehicleDelete()
if self.cp ~= nil then
--if vehicle is a courseplayer, delete the vehicle from activeCourseplayers
if CpManager.activeCoursePlayers[self.rootNode] then
CpManager:removeFromActiveCoursePlayers(self);
end
-- Remove created nodes
if self.cp.notesToDelete and #self.cp.notesToDelete > 0 then
for _, nodeId in ipairs(self.cp.notesToDelete) do
if nodeId and nodeId ~= 0 then
delete(nodeId);
end;
end;
self.cp.notesToDelete = nil;
end;
if courseplay.combines[self.rootNode] then
for _, courseplayer in pairs(g_currentMission.steerables) do
if courseplayer.cp then
if courseplayer.cp.activeCombine and courseplayer.cp.activeCombine == self then
courseplay:unregisterFromCombine(courseplayer, self)
end
if courseplayer.cp.lastActiveCombine and courseplayer.cp.lastActiveCombine == self then
courseplayer.cp.lastActiveCombine = nil
end
end
end
courseplay.combines[self.rootNode] = nil;
end;
end;
end;
Vehicle.delete = Utils.prependedFunction(Vehicle.delete, courseplay.vehicleDelete);
function courseplay:foldableLoad(savegame)
if self.cp == nil then self.cp = {}; end;
--FOLDING PARTS STARTMOVEDIRECTION
local startMoveDir = getXMLInt(self.xmlFile, 'vehicle.foldingParts#startMoveDirection');
if startMoveDir == nil then
local singleDir;
local i = 0;
while true do -- go through single foldingPart entries
local key = string.format('vehicle.foldingParts.foldingPart(%d)', i);
if not hasXMLProperty(self.xmlFile, key) then break; end;
local dir = getXMLInt(self.xmlFile, key .. '#startMoveDirection');
if dir then
if singleDir == nil then --first foldingPart -> set singleDir
singleDir = dir;
elseif dir ~= singleDir then -- two or more foldingParts have non-matching startMoveDirections --> not valid
singleDir = nil;
break;
elseif dir == singleDir then -- --> valid
end;
end;
i = i + 1;
end;
if singleDir then -- startMoveDirection found in single foldingPart
startMoveDir = singleDir;
end;
end;
self.cp.foldingPartsStartMoveDirection = Utils.getNoNil(startMoveDir, 0);
end;
Foldable.load = Utils.appendedFunction(Foldable.load, courseplay.foldableLoad);
courseplay.locales = courseplay.utils.table.copy(g_i18n.texts, true);
-- make l10n global so they can be used in GUI XML files directly (Thanks Mogli!)
for n,t in pairs( g_i18n.texts ) do
if string.sub( n, 1, 10 ) == "COURSEPLAY" then
g_i18n.globalI18N.texts[n] = t
end
end
courseplay:register();
print(string.format('### Courseplay: installed into %d vehicles', numInstallationsVehicles));
-- TODO: Remove the AIVehicleUtil.driveToPoint overwrite when the new patch goes out to fix it. (Temp fix from Giants: Emil)
local originalDriveToPoint = AIVehicleUtil.driveToPoint;
AIVehicleUtil.driveToPoint = function(self, dt, acceleration, allowedToDrive, moveForwards, tX, tZ, maxSpeed, doNotSteer)
if self.firstTimeRun then
if allowedToDrive then
local tX_2 = tX * 0.5;
local tZ_2 = tZ * 0.5;
local d1X, d1Z = tZ_2, -tX_2;
if tX > 0 then
d1X, d1Z = -tZ_2, tX_2;
end
local hit,_,f2 = Utils.getLineLineIntersection2D(tX_2,tZ_2, d1X,d1Z, 0,0, tX, 0);
if doNotSteer == nil or not doNotSteer then
local rotTime = 0;
local radius = 0;
if hit and math.abs(f2) < 100000 then
radius = tX * f2;
rotTime = self.wheelSteeringDuration * ( math.atan(1/radius) / math.atan(1/self.maxTurningRadius) );
end
local targetRotTime = 0;
if rotTime >= 0 then
targetRotTime = math.min(rotTime, self.maxRotTime)
else
targetRotTime = math.max(rotTime, self.minRotTime)
end
if targetRotTime > self.rotatedTime then
self.rotatedTime = math.min(self.rotatedTime + dt*self.aiSteeringSpeed, targetRotTime);
else
self.rotatedTime = math.max(self.rotatedTime - dt*self.aiSteeringSpeed, targetRotTime);
end
-- adjust maxSpeed
local steerDiff = targetRotTime - self.rotatedTime;
local fac = math.abs(steerDiff) / math.max(self.maxRotTime, -self.minRotTime);
maxSpeed = maxSpeed * math.max( 0.01, 1.0 - fac);
end;
end
self.motor:setSpeedLimit(maxSpeed);
if self.cruiseControl.state ~= Drivable.CRUISECONTROL_STATE_ACTIVE then
self:setCruiseControlState(Drivable.CRUISECONTROL_STATE_ACTIVE);
end
if not allowedToDrive then
acceleration = 0;
end
if not moveForwards then
acceleration = -acceleration;
end
if not g_currentMission.missionInfo.stopAndGoBraking then
if acceleration ~= self.nextMovingDirection then
if not self.hasStopped then
if math.abs(self.lastSpeedAcceleration) < 0.0001 and math.abs(self.lastSpeedReal) < 0.0001 and math.abs(self.lastMovedDistance) < 0.001 then
acceleration = 0;
end
end
end
end
WheelsUtil.updateWheelsPhysics(self, dt, self.lastSpeedReal, acceleration, not allowedToDrive, self.requiredDriveMode);
end
end