Skip to content

Instantly share code, notes, and snippets.

@Timmiej93
Last active September 29, 2017 18:43
Show Gist options
  • Save Timmiej93/4250eebcb295c5a37940e7f18d94ebe8 to your computer and use it in GitHub Desktop.
Save Timmiej93/4250eebcb295c5a37940e7f18d94ebe8 to your computer and use it in GitHub Desktop.
ShowMeTheMoney mod for Farming Simulator 17
-- ShowMeTheMoney for Farming Simulator 17
-- @description: This displays the total amount of money available on the server to all players when playing a multiplayer game
-- @author: Slivicon
-- History at end of file.
--
ShowMeTheMoney = {};
ShowMeTheMoney.enabled = true;
ShowMeTheMoney.fontSize = 0.014;
ShowMeTheMoney.money = 0;
ShowMeTheMoney.moneyDisplay = "";
ShowMeTheMoney.playerCount = 1;
ShowMeTheMoney.posX = 0;
ShowMeTheMoney.posY = 0;
local modItem = ModsUtil.findModItemByModName(g_currentModName);
ShowMeTheMoney.version = (modItem and modItem.version) and modItem.version or "?.?.?";
function ShowMeTheMoney:loadMap(name)
self.enabled = true
self.secondTimer = 0
self.previousServerMoney = 0;
self.serverMoney = 0;
end;
function ShowMeTheMoney:deleteMap()end;
function ShowMeTheMoney:mouseEvent(posX, posY, isDown, isUp, button)end;
function ShowMeTheMoney:keyEvent(unicode, sym, modifier, isDown)end;
function ShowMeTheMoney:update(dt)
if not self.firstRun then
self.firstRun = true
ShowMeTheMoney:sendEvent()
end
if g_currentMission:getIsServer() then
ShowMeTheMoney:secondTimerFun(dt)
end
if not g_currentMission.isMasterUser and g_dedicatedServerInfo == nil then -- todo: find out value of isMasterUser on the dedi
self.fontSize = g_currentMission.inGameMessage.textSize;
self.posX = g_currentMission.infoBarBgOverlay.x + g_currentMission.infoBarBgOverlay.width - g_currentMission.moneyTextOffsetX;
self.posY = g_currentMission.infoBarBgOverlay.y + g_currentMission.moneyTextOffsetY - self.fontSize;
self.moneyDisplay = tostring(g_i18n:formatNumber(self.serverMoney, 0));
end;
end;
function ShowMeTheMoney:secondTimerFun(dt)
self.secondTimer = self.secondTimer + dt
if self.secondTimer >= 1000 then
self.secondTimer = self.secondTimer - 1000
ShowMeTheMoney:sendEvent()
end
end
function ShowMeTheMoney:sendEvent()
local serverMoney = g_currentMission:getTotalMoney()
if serverMoney ~= self.previousServerMoney then
self.previousServerMoney = serverMoney
ShowMeTheMoneyEvent.sendEvent(serverMoney)
end
end
function ShowMeTheMoney:draw()
-- only run on multiplayer players who are not logged in as admin
if self.enabled and not g_currentMission.isMasterUser and g_dedicatedServerInfo == nil then
setTextAlignment(RenderText.ALIGN_RIGHT);
setTextColor(0.9961, 0.7490, 0.0039, 1); -- money icon colour [R, G, B, Alpha (0-1)]
renderText(self.posX, self.posY, self.fontSize, self.moneyDisplay);
setTextColor(1, 1, 1, 1); -- in case another text rendering function does not set color before rendering text, this resets it to white
setTextAlignment(RenderText.ALIGN_LEFT); -- in case another text rendering function does not set alignment, this resets it to left
end;
end;
function ShowMeTheMoney:setServerMoney(serverMoney)
self.serverMoney = serverMoney;
end;
addModEventListener(ShowMeTheMoney);
ShowMeTheMoneyEvent = {};
ShowMeTheMoneyEvent_mt = Class(ShowMeTheMoneyEvent, Event);
InitEventClass(ShowMeTheMoneyEvent, "ShowMeTheMoneyEvent");
function ShowMeTheMoneyEvent:emptyNew()
local self = Event:new(ShowMeTheMoneyEvent_mt);
return self;
end;
function ShowMeTheMoneyEvent:new(serverMoney)
local self = ShowMeTheMoneyEvent:emptyNew();
self.serverMoney = serverMoney;
return self;
end;
function ShowMeTheMoneyEvent:readStream(streamId, connection)
self.serverMoney = streamReadInt32(streamId)
self:run(connection);
end;
function ShowMeTheMoneyEvent:writeStream(streamId, connection)
streamWriteInt32(streamId, self.serverMoney)
end;
function ShowMeTheMoneyEvent:run(connection)
ShowMeTheMoney:setServerMoney(self.serverMoney)
end;
function ShowMeTheMoneyEvent.sendEvent(serverMoney)
if g_server ~= nil then
g_server:broadcastEvent(ShowMeTheMoneyEvent:new(serverMoney));
end
end;
print(string.format("Script loaded: ShowMeTheMoney.lua (v%s)", ShowMeTheMoney.version));
--
-- @history 1.0 2017-09-25 Initial release for Farming Simulator 17
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment