Skip to content

Commit

Permalink
Working Alliance
Browse files Browse the repository at this point in the history
  • Loading branch information
dirtyredz committed Sep 17, 2017
1 parent a673b8a commit 46cf1bb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
6 changes: 3 additions & 3 deletions mods/MoveUI/scripts/entity/MoveUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ function MoveUIOptions.onAllowUIMovement(checkbox, value)
return
end
if value then
Player():setValue('MoveUI',true)
Player(callingPlayer):setValue('MoveUI',true)
return
end
Player():setValue('MoveUI',nil)
Player(callingPlayer):setValue('MoveUI',nil)
end

function MoveUIOptions.onEnableUI(checkbox, value, hudIndex)
Expand All @@ -131,7 +131,7 @@ function MoveUIOptions.onEnableUI(checkbox, value, hudIndex)
invokeServerFunction('onEnableUI',nil,value,hudIndex)
return
end
local player = Player(callingplayer)
local player = Player(callingPlayer)
local hudOptions = MoveUIOptions.HudList[hudIndex]
if value then
if hudOptions.Restriction(player) then
Expand Down
15 changes: 8 additions & 7 deletions mods/MoveUI/scripts/entity/merchants/scrapyard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ function Scrapyard.updateServer(timeStep)

Data = Scrapyard.secure()
licenses = Data["licenses"]
--local licenses = {}

local x,y = Sector():getCoordinates()
for playerIndex,duration in pairs(licenses) do
local player = Player(playerIndex)
local faction = Faction(playerIndex)

if player.isPlayer then
if faction.isPlayer or faction.isAlliance then
-- read current or init new
local pLicenses = Scrapyard.GetPlayerLicense(playerIndex)
local time = round(duration - timeStep)
if time < 0 then
if time <= 0 then
time = nil
end
pLicenses[x][y] = time

player:setValue("MoveUI#Licenses", serialize(pLicenses))
faction:setValue("MoveUI#Licenses", serialize(pLicenses))
end
end

Expand All @@ -30,11 +31,11 @@ end
function Scrapyard.GetPlayerLicense(playerIndex)

local x,y = Sector():getCoordinates()
local player = Player(playerIndex)
local faction = Faction(playerIndex)

local licenses
local PlayerLicenses = player:getValue("MoveUI#Licenses") or false
if player and player.isPlayer and PlayerLicenses then
local PlayerLicenses = faction:getValue("MoveUI#Licenses") or false
if faction and (faction.isPlayer or faction.isAlliance) and PlayerLicenses then
licenses = loadstring(PlayerLicenses)()
else
licenses = {}
Expand Down
98 changes: 73 additions & 25 deletions mods/MoveUI/scripts/player/ScrapyardLicenses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require ("utility")
-- namespace ScrapyardLicenses
ScrapyardLicenses = {}

local FactionValues = {}
local Title = 'ScrapyardLicenses'

function ScrapyardLicenses.initialize()
Expand All @@ -22,6 +23,16 @@ function ScrapyardLicenses.initialize()
end
end

function ScrapyardLicenses.TableSize(tabl)
local i = 0
for x,cols in pairs(tabl) do
for y,data in pairs(cols) do
i = i + 1
end
end
return i
end

function ScrapyardLicenses.onPreRenderHud()
if onClient() then
local rect = Rect(vec2(), vec2(400, 100))
Expand All @@ -31,25 +42,25 @@ function ScrapyardLicenses.onPreRenderHud()
rect.position = MoveUI.CheckOverride(Player(), DefaulPosition, OverridePosition, Title)

--get the licenses
local licenses = Player():getValue("MoveUI#Licenses") or 'return {}'
local player = Player()
local playerShip = Entity(player.craftIndex)
local ShipFaction = playerShip.factionIndex

licenses = loadstring(licenses)()
local i = 0
if licenses then
for x,cols in pairs(licenses) do
for y,data in pairs(cols) do
i = i + 1
end
end
end
ScrapyardLicenses.GetFactionValues(ShipFaction)
ScrapyardLicenses.sync()

local FactionLicenses = FactionValues['MoveUI#Licenses'] or 'return { }'
FactionLicenses = loadstring(FactionLicenses)()

local HSplit = UIHorizontalMultiSplitter(rect, 10, 8, i - 1)
local FactionLicensesSize = ScrapyardLicenses.TableSize(FactionLicenses)

if i == 0 then licenses = nil end
local HSplit = UIHorizontalMultiSplitter(rect, 10, 8, FactionLicensesSize - 1)

if FactionLicensesSize == 0 then FactionLicenses = nil end
--Reset index
i = 0
if licenses then
for x,cols in pairs(licenses) do
local i = 0
if FactionLicenses then
for x,cols in pairs(FactionLicenses) do
for y,duration in pairs(cols) do
local color = MoveUI.TimeRemainingColor(duration)
drawTextRect(x..' : '..y, HSplit:partition(i), -1, 0, color, 15, 0, 0, 0)
Expand All @@ -61,7 +72,6 @@ function ScrapyardLicenses.onPreRenderHud()
end
end


--MoveUI stuff
OverridePosition, Moving = MoveUI.Enabled(Player(), rect, OverridePosition)
if OverridePosition and not Moving then
Expand All @@ -74,26 +84,64 @@ function ScrapyardLicenses.setNewPosition(Position)
MoveUI.AssignPlayerOverride(Player(), Title, Position)
end

function ScrapyardLicenses.GetFactionValues(factionIndex)
if onClient() then
invokeServerFunction('GetFactionValues',factionIndex)
return
end
local faction = Faction(factionIndex)
if not faction then return end
FactionValues = faction:getValues()
end

function ScrapyardLicenses.SetFactionValues(factionIndex,licenses)
if onClient() then
invokeServerFunction('GetFactionValues',factionIndex,licenses)
return
end
local faction = Faction(factionIndex)
if not faction then return end
faction:setValue("MoveUI#Licenses", MoveUI.Serialize(licenses))
end

function ScrapyardLicenses.sync(values)
if onClient() then
if values then
FactionValues = values.FactionValues
return
end
invokeServerFunction('sync')
return
end
invokeClientFunction(Player(callingPlayer),'sync',{FactionValues = FactionValues})
end

function ScrapyardLicenses.onSectorEntered(playerIndex,x,y)
local licenses = Player():getValue("MoveUI#Licenses") or 'return {}'
local player = Player()
local playerShip = Entity(player.craftIndex)
local ShipFaction = playerShip.factionIndex

ScrapyardLicenses.GetFactionValues(ShipFaction)
ScrapyardLicenses.sync()

local FactionLicenses = FactionValues['MoveUI#Licenses'] or 'return { }'
FactionLicenses = loadstring(FactionLicenses)()

licenses = loadstring(licenses)()
local x,y = Sector():getCoordinates()

if (type(licenses[x]) == "table") then
if (type(FactionLicenses[x]) == "table") then
local count = 0
for _ in pairs( licenses[x] ) do
for _ in pairs( FactionLicenses[x] ) do
count = count + 1
end
if count == 0 then
--Remove X table since its empty
licenses[x] = nil
elseif (type(licenses[x][y]) == "number") then
FactionLicenses[x] = nil
elseif (type(FactionLicenses[x][y]) == "number") then
--Delete this sectors licenses since any active scrapyards will update
licenses[x][y] = nil
FactionLicenses[x][y] = nil
print('Removing:',x,y,'from licenses')
end
end

Player():setValue("MoveUI#Licenses", MoveUI.Serialize(licenses))
ScrapyardLicenses.SetFactionValues(ShipFaction,FactionLicenses)
end

0 comments on commit 46cf1bb

Please sign in to comment.