Sprache Deutsch Language English

Script Dokumentation LS 2015 - SolarCollectorPlaceable (Patch 1.3)

Script Dokumentation Übersicht

scripts/placeables/SolarCollectorPlaceable.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
3SolarCollectorPlaceable = {};
4SolarCollectorPlaceable_mt = Class(SolarCollectorPlaceable, Placeable);
5
6InitStaticObjectClass(SolarCollectorPlaceable, "SolarCollectorPlaceable", ObjectIds.OBJECT_SOLAR_COLLECTOR_PLACEABLE);
7
8function SolarCollectorPlaceable:new(isServer, isClient, customMt)
9 local mt = customMt;
10 if mt == nil then
11 mt = SolarCollectorPlaceable_mt;
12 end;
13
14 local self = Placeable:new(isServer, isClient, mt);
15
16 registerObjectClassName(self, "SolarCollectorPlaceable");
17
18 self.headNode = 0;
19 self.incomePerHour = 0;
20 self.headRotationRandom = 0;
21 return self;
22end;
23
24function SolarCollectorPlaceable:delete()
25 unregisterObjectClassName(self);
26 g_currentMission.environment:removeHourChangeListener(self);
27 SolarCollectorPlaceable:superClass().delete(self);
28end;
29
30function SolarCollectorPlaceable:deleteFinal()
31 SolarCollectorPlaceable:superClass().deleteFinal(self);
32end;
33
34function SolarCollectorPlaceable:readStream(streamId, connection)
35 if connection:getIsServer() then
36 self.headRotationRandom=Utils.readCompressedAngle(streamId);
37 end;
38 SolarCollectorPlaceable:superClass().readStream(self, streamId, connection);
39end;
40
41function SolarCollectorPlaceable:writeStream(streamId, connection)
42 if not connection:getIsServer() then
43 Utils.writeCompressedAngle(streamId, self.headRotationRandom);
44 end;
45 SolarCollectorPlaceable:superClass().writeStream(self, streamId, connection);
46end;
47
48--[[function SolarCollectorPlaceable:readUpdateStream(streamId, timestamp, connection)
49 SolarCollectorPlaceable:superClass().readStream(self, streamId, timestamp, connection);
50end;
51
52function SolarCollectorPlaceable:writeUpdateStream(streamId, connection, dirtyMask)
53 SolarCollectorPlaceable:superClass().writeUpdateStream(self, streamId, connection, dirtyMask);
54end;]]
55
56function SolarCollectorPlaceable:createNode(i3dFilename)
57 if not SolarCollectorPlaceable:superClass().createNode(self, i3dFilename) then
58 return false;
59 end;
60
61 if getNumOfChildren(self.nodeId) < 1 then
62 delete(self.nodeId);
63 self.nodeId = 0;
64 return false;
65 end;
66 self.headNode = getChildAt(self.nodeId, 0);
67
68 return true;
69end;
70
71function SolarCollectorPlaceable:load(xmlFilename, x,y,z, rx,ry,rz, initRandom)
72 if not SolarCollectorPlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, initRandom) then
73 return false;
74 end;
75
76 local xmlFile = loadXMLFile("TempXML", xmlFilename);
77
78 local difficultyMultiplier = 2 ^ (3 - g_currentMission.missionStats.difficulty); --4 2 1
79 self.incomePerHour = difficultyMultiplier * Utils.getNoNil(getXMLFloat(xmlFile, "placeable.incomePerHour"), 100);
80
81 delete(xmlFile);
82
83 return true;
84end;
85
86function SolarCollectorPlaceable:finalizePlacement()
87 SolarCollectorPlaceable:superClass().finalizePlacement(self);
88 g_currentMission.environment:addHourChangeListener(self);
89end
90
91function SolarCollectorPlaceable:initPose(x,y,z, rx,ry,rz, initRandom)
92 SolarCollectorPlaceable:superClass().initPose(self, x,y,z, rx,ry,rz, initRandom);
93
94 local rotVariation = 0.1;
95 self.headRotationRandom = math.rad(75-90) + math.random() * 2*rotVariation - rotVariation; -- 75-90 = rotate to light (75 y rot) x axis
96
97 self:updateHeadRotation();
98end;
99
100function SolarCollectorPlaceable:updateHeadRotation()
101
102 local headRotation = math.rad(75-90);
103 if g_currentMission.environment.sunLightId ~= nil then
104 local dx,_,dz = localDirectionToWorld(g_currentMission.environment.sunLightId, 0,0,1);
105 headRotation = math.atan2(dx, dz);
106 end
107 headRotation = headRotation + self.headRotationRandom;
108
109 local dx,_,dz = worldDirectionToLocal(self.nodeId, math.sin(headRotation),0,math.cos(headRotation));
110 setDirection(self.headNode, dx,0,dz, 0,1,0);
111end;
112
113function SolarCollectorPlaceable:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
114 local headRotationRandom = getXMLFloat(xmlFile, key.."#headRotationRandom");
115 if headRotationRandom == nil then
116 return false;
117 end;
118
119 self.headRotationRandom = headRotationRandom;
120
121 if not SolarCollectorPlaceable:superClass().loadFromAttributesAndNodes(self, xmlFile, key, resetVehicles) then
122 return false;
123 end;
124
125 return true;
126end;
127
128function SolarCollectorPlaceable:getSaveAttributesAndNodes(nodeIdent)
129 local attributes, nodes = SolarCollectorPlaceable:superClass().getSaveAttributesAndNodes(self, nodeIdent);
130 attributes = attributes .. ' headRotationRandom="'..self.headRotationRandom..'"';
131 return attributes, nodes;
132end;
133
134function SolarCollectorPlaceable:update(dt)
135end;
136
137--[[function SolarCollectorPlaceable:updateTick(dt)
138end;]]
139
140function SolarCollectorPlaceable:hourChanged()
141 if self.isServer then
142 g_currentMission:addSharedMoney(self.incomePerHour, "other");
143 g_currentMission:addMoneyChange(self.incomePerHour, FSBaseMission.MONEY_TYPE_HOUR_CHANGED);
144 end
145 self:updateHeadRotation();
146end
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