Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ContractorMod.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- Worker playerBodyIndex can be set from 1 to 4
- for male character, use player01.xml file
- for female character, use player02.xml file
- Other attributes should better be retrieved from ContractorMod.xml file in savegame directory once configured in wardrobe screen.
- Other attributes should better be retrieved from ContractorMod.xml file in savegame directory once configured in wardrobe screen.
If you want to modify some element on a savegame, you can edit similar file in corresponding savegame directory
-->
<workers numWorkers="4">
Expand Down Expand Up @@ -38,7 +38,7 @@
</worker>
</workers>
<displaySettings>
<!--
<!--
You can change here the position and size of current character name displayed on screen
Default values used on FS17: x="0.9828" y="0.45" size="0.024"
-->
Expand All @@ -49,4 +49,8 @@
<!-- mods can be added here so they won't be overwritten if ContractorMod is updated -->
<Passenger vehiclesName="$moddir$FS22_JohnDeere8r2016/series8R.xml" seatIndex="1" x="0.5494" y="2.3036" z="-1.3561" rx="0" ry="0" rz="0"/>
</passengerSeats>
<wageSettings>
<monthlyWage default="0" />
<hourlyWage factor="1.0" />
</wageSettings>
</ContractorMod>
114 changes: 80 additions & 34 deletions scripts/ContractorMod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ function ContractorMod:init()
ContractorMod.numWorkers = 2
ContractorMod.workers = {}
ContractorMod.shouldStopWorker = true
ContractorMod.switching = false
ContractorMod.switching = false
ContractorMod.displayPlayerNames = true
ContractorMod.wageSettings = {}
ContractorMod.wageSettings.defaultMonthlyWage = 0
ContractorMod.wageSettings.hourlyWageFactor = 1.0
g_messageCenter:subscribe(MessageType.PERIOD_CHANGED, self.onDayChanged, self)

self:registerXmlSchema()

Expand Down Expand Up @@ -71,9 +75,31 @@ function ContractorMod:init()
}
}
end

self:extendWageFunctions()

g_currentMission.nickname = ContractorMod.workers[ContractorMod.currentID].name
end

function ContractorMod:extendWageFunctions()
for _, jt in g_currentMission.aiJobTypeManager.jobTypes do
jt_class = jt.classObject
jt_class.getPricePerMs = Utils.overwrittenFunction(jt_class.getPricePerMs, ContractorMod.getPricePerMs)
end
end

function ContractorMod.getPricePerMs(self, superFunc, ...)
return superFunc() * ContractorMod.wageSettings.hourlyWageFactor
end

function ContractorMod:onDayChanged()
local farm = g_farmManager:getFarmById(1)
for _, w in pairs(self.workers) do
farm:changeBalance(-w.wage, MoneyType.AI)
g_currentMission:addMoneyChange(-w.wage, 1, MoneyType.AI, true)
end
end

function ContractorMod:initFromSave()
if ContractorMod.debug then print("ContractorMod:initFromSave()") end
-- Copy ContractorMod.xml from zip to modSettings dir
Expand All @@ -90,44 +116,61 @@ function ContractorMod:initFromSave()
xml:delete()
return false
end

ContractorMod.wageSettings.defaultMonthlyWage = xml:getInt("ContractorMod.wageSettings.monthlyWage#default") or 0
ContractorMod.wageSettings.hourlyWageFactor = xml:getFloat("ContractorMod.wageSettings.hourlyWage#factor") or 1.0

print(ContractorMod.wageSettings.defaultMonthlyWage)
print(ContractorMod.wageSettings.hourlyWageFactor)

local num = xml:getInt("ContractorMod.workers#numWorkers") or 0
for i = 1, num do
local key = string.format("ContractorMod.workers.worker(%d)", i-1)
local name = xml:getString(key.."#name")
local pos = xml:getString(key.."#position")
local rot = xml:getString(key.."#rotation")
local style = PlayerStyle.new()
style:loadFromXMLFile(xml, key..".style")
local worker = ContractorModWorker:new(name, i, style)
if ContractorMod.debug then print(pos) end
local posVector = string.getVector(pos)
if ContractorMod.debug then print("posVector "..tostring(posVector)) end
local rotVector = string.getVector(rot)
worker.x = posVector[1]
worker.y = posVector[2]
worker.z = posVector[3]
worker.dx = rotVector[1]
worker.dy = rotVector[2]
worker.rotY = rotVector[2]
worker.dz = rotVector[3]
local vehicleID = xml:getString(key.."#vehicleID")
if vehicleID ~= "0" then
if ContractorMod.mapVehicleLoad ~= nil then
-- map savegame vehicle id and network id
local saveId = ContractorMod.mapVehicleLoad[vehicleID]
local vehicle = NetworkUtil.getObject(tonumber(saveId))
if vehicle ~= nil then
if ContractorMod.debug then print("ContractorMod: vehicle not nil") end
worker.currentVehicle = vehicle
local currentSeat = xml:getInt(key.."#currentSeat")
if currentSeat ~= nil then
worker.currentSeat = currentSeat
if xml:getString(key.."#name") ~= nil then
local name = xml:getString(key.."#name")
local wage = xml:getFloat(key.."#wage") or ContractorMod.wageSettings.defaultMonthlyWage
local pos = xml:getString(key.."#position")
local rot = xml:getString(key.."#rotation")
local style = PlayerStyle.new()
style:loadFromXMLFile(xml, key..".style")
local worker = ContractorModWorker:new(name, i, style)
if ContractorMod.debug then print(pos) end
local posVector = string.getVector(pos)
if ContractorMod.debug then print("posVector "..tostring(posVector)) end
local rotVector = string.getVector(rot)
worker.wage = wage
worker.x = posVector[1]
worker.y = posVector[2]
worker.z = posVector[3]
worker.dx = rotVector[1]
worker.dy = rotVector[2]
worker.rotY = rotVector[2]
worker.dz = rotVector[3]
local vehicleID = xml:getString(key.."#vehicleID")
if vehicleID ~= "0" then
if ContractorMod.mapVehicleLoad ~= nil then
-- map savegame vehicle id and network id
local saveId = ContractorMod.mapVehicleLoad[vehicleID]
local vehicle = NetworkUtil.getObject(tonumber(saveId))
if vehicle ~= nil then
if ContractorMod.debug then print("ContractorMod: vehicle not nil") end
worker.currentVehicle = vehicle
local currentSeat = xml:getInt(key.."#currentSeat")
if currentSeat ~= nil then
worker.currentSeat = currentSeat
end
end
end
end
table.insert(ContractorMod.workers, worker)
else
local workerStyle = g_helperManager:getRandomHelperStyle()
table.insert(ContractorMod.workers, ContractorModWorker:new("Worker" .. i, i, workerStyle))
ContractorMod.workers[i].wage = ContractorMod.wageSettings.defaultMonthlyWage
end
end
table.insert(ContractorMod.workers, worker)
end
end


xmlKey = "ContractorMod.displaySettings.characterName"
ContractorMod.displaySettings = {}
ContractorMod.displaySettings.characterName = {}
Expand Down Expand Up @@ -184,7 +227,10 @@ function ContractorMod:registerXmlSchema()
ContractorMod.xmlSchema = XMLSchema.new("ContractorMod")
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.workers#numWorkers", "Number of workers", nil, true)
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.workers.worker(?)#name", "Name of worker", nil, true)
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.workers.worker(?)#wage", "Monthly Wage of worker", nil, true)
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.workers.worker(?)#vehicleID", "ID of vehicle if any", nil, true)
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.wageSettings.monthlyWage#default", "Default Wage of new workers", nil, true)
ContractorMod.xmlSchema:register(XMLValueType.STRING, "ContractorMod.wageSettings.hourlyWage#factor", "Factor Applied to hourly cost of all workers", nil, true)
PlayerStyle.registerSavegameXMLPaths(ContractorMod.xmlSchema, "ContractorMod.workers.worker(?).style")
end

Expand All @@ -210,7 +256,7 @@ function ContractorMod:onSwitchWorker(action)
prevID = ContractorMod.currentID - 1
else
prevID = ContractorMod.numWorkers
end
end
self:setCurrentContractorModWorker(prevID)
end
end
Expand Down
17 changes: 10 additions & 7 deletions scripts/ContractorModWorker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function ContractorModWorker:new(name, index, workerStyle)
self.playerStyle = PlayerStyle.defaultStyle()
self.playerStyle:copyFrom(workerStyle)

self.wage = 0

self.color = Farm.COLORS[index]
if g_localPlayer ~= nil then
self.x, self.y, self.z, self.rotY = g_localPlayer:getPosition()
Expand All @@ -43,7 +45,7 @@ function ContractorModWorker:displayName(contractorMod)
if self.name == "PLAYER" then return end
setTextBold(true);
setTextAlignment(RenderText.ALIGN_RIGHT);

setTextColor(self.color[1], self.color[2], self.color[3], 1.0);
local x = 0.9828
local y = 0.45
Expand All @@ -54,7 +56,7 @@ function ContractorModWorker:displayName(contractorMod)
size = contractorMod.displaySettings.characterName.size
end
renderText(x, y, size, self.name);

if ContractorModWorker.debug then
if self.currentVehicle ~= nil then
local vehicleName = ""
Expand Down Expand Up @@ -106,7 +108,7 @@ function ContractorModWorker:beforeSwitch(noEventSend)

-- local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, self.x, 300, self.z)
-- self.y = math.max(terrainHeight + 0.1, self.y + 0.9)

-- self.player:moveRootNodeToAbsolute(self.x, self.y, self.z)
-- self.player:moveTo(self.x, self.y-0.8, self.z, true, true)
g_localPlayer:teleportTo(self.x, self.y, self.z)
Expand All @@ -121,10 +123,10 @@ function ContractorModWorker:beforeSwitch(noEventSend)
--[[
self.player.baseInformation.isOnGround = true
self.player:moveToAbsoluteInternal(self.x, self.y, self.z)

local dx, _, dz = localDirectionToWorld(g_currentMission.player.rootNode, 0, 0, -1) --]]
-- self.rotY = MathUtil.getYRotationFromDirection(dx, dz)

-- setRotation(self.player.graphicsRootNode, 0, self.rotY + math.rad(180.0), 0)
-- setRotation(self.player.cameraNode, self.rotX, self.rotY, 0)
end
Expand All @@ -149,12 +151,12 @@ function ContractorModWorker:afterSwitch(noEventSend)
g_localPlayer.playerHotspot:setColor(unpack(self.color))
if self.currentVehicle == nil then
-- target worker is not in a vehicle
if g_localPlayer ~= nil then --g_currentMission.controlPlayer and
if g_localPlayer ~= nil then --g_currentMission.controlPlayer and
-- if ContractorModWorker.debug then print("ContractorModWorker: moveTo "..tostring(g_localPlayer.model.style.playerName)); end
-- setTranslation(g_currentMission.player.rootNode, self.x, self.y, self.z);
-- g_currentMission.player:moveRootNodeToAbsolute(self.x, self.y-0.2, self.z);
g_localPlayer:teleportTo(self.x, self.y, self.z)

-- g_localPlayer:setRotation(self.rotX, self.rotY)
-- self.player.isEntered = true
-- self.player.isControlled = true
Expand All @@ -181,5 +183,6 @@ function ContractorModWorker:afterSwitch(noEventSend)
end
-- end
end

end

7 changes: 6 additions & 1 deletion scripts/cmSavegameController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ SavegameController.onSaveComplete = Utils.prependedFunction(SavegameController.o
if currentWorker ~= nil then
currentWorker:beforeSwitch(true)
end

local workerKey = rootXmlKey .. ".workers"
xmlFile:setInt(workerKey.."#numWorkers", ContractorMod.numWorkers)
for i = 1, ContractorMod.numWorkers do
local worker = ContractorMod.workers[i]
local key = string.format(rootXmlKey .. ".workers.worker(%d)", i - 1)
xmlFile:setString(key.."#name", worker.name)
xmlFile:setFloat(key .. "#wage", worker.wage)
worker.playerStyle:saveToXMLFile(xmlFile, key .. ".style")
local pos = Utils.getNoNil(worker.x, "0.")..' '..Utils.getNoNil(worker.y, "0.")..' '..Utils.getNoNil(worker.z, "0.")
xmlFile:setString(key.."#position", pos)
Expand All @@ -44,6 +45,10 @@ SavegameController.onSaveComplete = Utils.prependedFunction(SavegameController.o
xmlFile:setFloat(xmlKey .. "#size", ContractorMod.displaySettings.characterName.size)
xmlKey = rootXmlKey .. ".displaySettings.playerName"
xmlFile:setBool(xmlKey .. "#displayPlayerNames", ContractorMod.displayPlayerNames)
xmlKey = rootXmlKey .. ".wageSettings.monthlyWage"
xmlFile:setInt(xmlKey .. "#default", ContractorMod.wageSettings.defaultMonthlyWage)
xmlKey = rootXmlKey .. ".wageSettings.hourlyWage"
xmlFile:setFloat(xmlKey .. "#factor", ContractorMod.wageSettings.hourlyWageFactor)
xmlFile:save()
xmlFile:delete()
end
Expand Down