Untitled diff

Created Diff never expires
83 removals
514 lines
25 additions
461 lines
package.path = package.path .. ";data/scripts/lib/?.lua"
package.path = package.path .. ";data/scripts/lib/?.lua"
require ("galaxy")
require ("galaxy")
require ("utility")
require ("utility")
require ("faction")
require ("faction")
require ("randomext")
require ("randomext")
require("stringutility")
require("stringutility")
local Dialog = require("dialogutility")
local Dialog = require("dialogutility")


-- Don't remove or alter the following comment, it tells the game the namespace this script lives in. If you remove it, the script will break.
-- Don't remove or alter the following comment, it tells the game the namespace this script lives in. If you remove it, the script will break.
-- namespace Scrapyard
-- namespace Scrapyard
Scrapyard = {}
Scrapyard = {}


-- server
-- server
local licenses = {}
local licenses = {}
local illegalActions = {}
local illegalActions = {}
local newsBroadcastCounter = 0
local newsBroadcastCounter = 0




-- client
-- client
local tabbedWindow = 0
local tabbedWindow = 0
local planDisplayer = 0
local planDisplayer = 0
local sellButton = 0
local sellButton = 0
local sellWarningLabel = 0
local sellWarningLabel = 0
local priceLabel1 = 0
local priceLabel1 = 0
local priceLabel2 = 0
local priceLabel2 = 0
local priceLabel3 = 0
local priceLabel3 = 0
local priceLabel4 = 0
local priceLabel4 = 0
local licenseDuration = 0
local licenseDuration = 0
local uiMoneyValue = 0
local uiMoneyValue = 0
local visible = false
local visible = false


-- if this function returns false, the script will not be listed in the interaction window on the client,
-- if this function returns false, the script will not be listed in the interaction window on the client,
-- even though its UI may be registered
-- even though its UI may be registered
function Scrapyard.interactionPossible(playerIndex, option)
function Scrapyard.interactionPossible(playerIndex, option)
return CheckFactionInteraction(playerIndex, -10000)
return CheckFactionInteraction(playerIndex, -10000)
end
end


function Scrapyard.restore(data)
function Scrapyard.restore(data)
-- clear earlier data
-- clear earlier data
licenses = data.licenses
licenses = data.licenses
illegalActions = data.illegalActions
illegalActions = data.illegalActions
end
end


function Scrapyard.secure()
function Scrapyard.secure()
-- save licenses
-- save licenses
local data = {}
local data = {}
data.licenses = licenses
data.licenses = licenses
data.illegalActions = illegalActions
data.illegalActions = illegalActions


return data
return data
end
end


-- this function gets called on creation of the entity the script is attached to, on client and server
-- this function gets called on creation of the entity the script is attached to, on client and server
function Scrapyard.initialize()
function Scrapyard.initialize()


if onServer() then
if onServer() then
Sector():registerCallback("onHullHit", "onHullHit")
Sector():registerCallback("onHullHit", "onHullHit")


local station = Entity()
local station = Entity()
if station.title == "" then
if station.title == "" then
station.title = "Scrapyard"%_t
station.title = "Scrapyard"%_t
end
end


end
end


if onClient() and EntityIcon().icon == "" then
if onClient() and EntityIcon().icon == "" then
EntityIcon().icon = "data/textures/icons/pixel/scrapyard_fat.png"
EntityIcon().icon = "data/textures/icons/pixel/scrapyard_fat.png"
InteractionText().text = Dialog.generateStationInteractionText(Entity(), random())
InteractionText().text = Dialog.generateStationInteractionText(Entity(), random())
end
end


end
end


-- this function gets called on creation of the entity the script is attached to, on client only
-- this function gets called on creation of the entity the script is attached to, on client only
-- AFTER initialize above
-- AFTER initialize above
-- create all required UI elements for the client side
-- create all required UI elements for the client side
function Scrapyard.initUI()
function Scrapyard.initUI()


local res = getResolution()
local res = getResolution()
local size = vec2(700, 650)
local size = vec2(700, 650)


local menu = ScriptUI()
local menu = ScriptUI()
local mainWindow = menu:createWindow(Rect(res * 0.5 - size * 0.5, res * 0.5 + size * 0.5))
local mainWindow = menu:createWindow(Rect(res * 0.5 - size * 0.5, res * 0.5 + size * 0.5))
menu:registerWindow(mainWindow, "Scrapyard"%_t)
menu:registerWindow(mainWindow, "Scrapyard"%_t)
mainWindow.caption = "Scrapyard"%_t
mainWindow.caption = "Scrapyard"%_t
mainWindow.showCloseButton = 1
mainWindow.showCloseButton = 1
mainWindow.moveable = 1
mainWindow.moveable = 1


-- create a tabbed window inside the main window
-- create a tabbed window inside the main window
tabbedWindow = mainWindow:createTabbedWindow(Rect(vec2(10, 10), size - 10))
tabbedWindow = mainWindow:createTabbedWindow(Rect(vec2(10, 10), size - 10))


-- create a "Sell" tab inside the tabbed window
-- create a "Sell" tab inside the tabbed window
local sellTab = tabbedWindow:createTab("Sell Ship"%_t, "", "Sell your ship to the scrapyard"%_t)
local sellTab = tabbedWindow:createTab("Sell Ship"%_t, "", "Sell your ship to the scrapyard"%_t)
size = sellTab.size
size = sellTab.size


planDisplayer = sellTab:createPlanDisplayer(Rect(0, 0, size.x - 20, size.y - 60))
planDisplayer = sellTab:createPlanDisplayer(Rect(0, 0, size.x - 20, size.y - 60))
planDisplayer.showStats = 0
planDisplayer.showStats = 0


sellButton = sellTab:createButton(Rect(0, size.y - 40, 150, size.y), "Sell Ship"%_t, "onSellButtonPressed")
sellButton = sellTab:createButton(Rect(0, size.y - 40, 150, size.y), "Sell Ship"%_t, "onSellButtonPressed")
sellWarningLabel = sellTab:createLabel(vec2(200, size.y - 30), "Warning! You will not get refunds for crews or turrets!"%_t, 15)
sellWarningLabel = sellTab:createLabel(vec2(200, size.y - 30), "Warning! You will not get refunds for crews or turrets!"%_t, 15)
sellWarningLabel.color = ColorRGB(1, 1, 0)
sellWarningLabel.color = ColorRGB(1, 1, 0)


-- create a second tab
-- create a second tab
local salvageTab = tabbedWindow:createTab("Salvaging /*UI Tab title*/"%_t, "", "Buy a salvaging license"%_t)
local salvageTab = tabbedWindow:createTab("Salvaging /*UI Tab title*/"%_t, "", "Buy a salvaging license"%_t)
size = salvageTab.size -- not really required, all tabs have the same size
size = salvageTab.size -- not really required, all tabs have the same size


local textField = salvageTab:createTextField(Rect(0, 0, size.x, 50), "You can buy a temporary salvaging license here. This license makes it legal to damage or mine wreckages in this sector."%_t)
local textField = salvageTab:createTextField(Rect(0, 0, size.x, 50), "You can buy a temporary salvaging license here. This license makes it legal to damage or mine wreckages in this sector."%_t)
textField.padding = 7
textField.padding = 7


salvageTab:createButton(Rect(size.x - 210, 80, 200 + size.x - 210, 40 + 80), "Buy License"%_t, "onBuyLicenseButton1Pressed")
salvageTab:createButton(Rect(size.x - 210, 80, 200 + size.x - 210, 40 + 80), "Buy License"%_t, "onBuyLicenseButton1Pressed")
salvageTab:createButton(Rect(size.x - 210, 130, 200 + size.x - 210, 40 + 130), "Buy License"%_t, "onBuyLicenseButton2Pressed")
salvageTab:createButton(Rect(size.x - 210, 130, 200 + size.x - 210, 40 + 130), "Buy License"%_t, "onBuyLicenseButton2Pressed")
salvageTab:createButton(Rect(size.x - 210, 180, 200 + size.x - 210, 40 + 180), "Buy License"%_t, "onBuyLicenseButton3Pressed")
salvageTab:createButton(Rect(size.x - 210, 180, 200 + size.x - 210, 40 + 180), "Buy License"%_t, "onBuyLicenseButton3Pressed")
salvageTab:createButton(Rect(size.x - 210, 230, 200 + size.x - 210, 40 + 230), "Buy License"%_t, "onBuyLicenseButton4Pressed")
salvageTab:createButton(Rect(size.x - 210, 230, 200 + size.x - 210, 40 + 230), "Buy License"%_t, "onBuyLicenseButton4Pressed")


local fontSize = 18
local fontSize = 18
salvageTab:createLabel(vec2(15, 85), "5", fontSize)
salvageTab:createLabel(vec2(15, 85), "5", fontSize)
salvageTab:createLabel(vec2(15, 135), "15", fontSize)
salvageTab:createLabel(vec2(15, 135), "15", fontSize)
salvageTab:createLabel(vec2(15, 185), "30", fontSize)
salvageTab:createLabel(vec2(15, 185), "30", fontSize)
salvageTab:createLabel(vec2(15, 235), "60", fontSize)
salvageTab:createLabel(vec2(15, 235), "60", fontSize)


salvageTab:createLabel(vec2(60, 85), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 85), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 135), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 135), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 185), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 185), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 235), "Minutes"%_t, fontSize)
salvageTab:createLabel(vec2(60, 235), "Minutes"%_t, fontSize)


priceLabel1 = salvageTab:createLabel(vec2(200, 85), "", fontSize)
priceLabel1 = salvageTab:createLabel(vec2(200, 85), "", fontSize)
priceLabel2 = salvageTab:createLabel(vec2(200, 135), "", fontSize)
priceLabel2 = salvageTab:createLabel(vec2(200, 135), "", fontSize)
priceLabel3 = salvageTab:createLabel(vec2(200, 185), "", fontSize)
priceLabel3 = salvageTab:createLabel(vec2(200, 185), "", fontSize)
priceLabel4 = salvageTab:createLabel(vec2(200, 235), "", fontSize)
priceLabel4 = salvageTab:createLabel(vec2(200, 235), "", fontSize)


timeLabel = salvageTab:createLabel(vec2(10, 310), "", fontSize)
timeLabel = salvageTab:createLabel(vec2(10, 310), "", fontSize)


end
end


-- this function gets called whenever the ui window gets rendered, AFTER the window was rendered (client only)
-- this function gets called whenever the ui window gets rendered, AFTER the window was rendered (client only)
function Scrapyard.renderUI()
function Scrapyard.renderUI()


if tabbedWindow:getActiveTab().name == "Sell Ship"%_t then
if tabbedWindow:getActiveTab().name == "Sell Ship"%_t then
renderPrices(planDisplayer.lower + 20, "Ship Value:"%_t, uiMoneyValue, nil)
renderPrices(planDisplayer.lower + 20, "Ship Value:"%_t, uiMoneyValue, nil)
end
end
end
end


-- this function gets called every time the window is shown on the client, ie. when a player presses F and if interactionPossible() returned 1
-- this function gets called every time the window is shown on the client, ie. when a player presses F and if interactionPossible() returned 1
function Scrapyard.onShowWindow()
function Scrapyard.onShowWindow()
local ship = Player().craft
local ship = Player().craft


-- get the plan of the player's ship
-- get the plan of the player's ship
local plan = ship:getPlan()
local plan = ship:getPlan()
planDisplayer.plan = plan
planDisplayer.plan = plan


if ship.isDrone then
if ship.isDrone then
sellButton.active = false
sellButton.active = false
sellWarningLabel:hide()
sellWarningLabel:hide()
else
else
sellButton.active = true
sellButton.active = true
sellWarningLabel:show()
sellWarningLabel:show()
end
end


priceLabel1.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 5)}
priceLabel1.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 5)}
priceLabel2.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 15)}
priceLabel2.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 15)}
priceLabel3.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 30)}
priceLabel3.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 30)}
priceLabel4.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 60)}
priceLabel4.caption = "$${money}"%_t % {money = Scrapyard.getLicensePrice(Player(), 60)}


uiMoneyValue = Scrapyard.getShipValue(plan)
uiMoneyValue = Scrapyard.getShipValue(plan)


Scrapyard.getLicenseDuration()
Scrapyard.getLicenseDuration()


visible = true
visible = true
end
end


-- this function gets called every time the window is closed on the client
-- this function gets called every time the window is closed on the client
function Scrapyard.onCloseWindow()
function Scrapyard.onCloseWindow()
local station = Entity()
local station = Entity()
displayChatMessage("Please, do come again."%_t, station.title, 0)
displayChatMessage("Please, do come again."%_t, station.title, 0)


visible = false
visible = false
end
end


function Scrapyard.onSellButtonPressed()
function Scrapyard.onSellButtonPressed()
invokeServerFunction("sellCraft")
invokeServerFunction("sellCraft")
end
end


function Scrapyard.onBuyLicenseButton1Pressed()
function Scrapyard.onBuyLicenseButton1Pressed()
invokeServerFunction("buyLicense", 60 * 5)
invokeServerFunction("buyLicense", 60 * 5)
end
end


function Scrapyard.onBuyLicenseButton2Pressed()
function Scrapyard.onBuyLicenseButton2Pressed()
invokeServerFunction("buyLicense", 60 * 15)
invokeServerFunction("buyLicense", 60 * 15)
end
end


function Scrapyard.onBuyLicenseButton3Pressed()
function Scrapyard.onBuyLicenseButton3Pressed()
invokeServerFunction("buyLicense", 60 * 30)
invokeServerFunction("buyLicense", 60 * 30)
end
end


function Scrapyard.onBuyLicenseButton4Pressed()
function Scrapyard.onBuyLicenseButton4Pressed()
invokeServerFunction("buyLicense", 60 * 60)
invokeServerFunction("buyLicense", 60 * 60)
end
end




-- this function gets called once each frame, on client only
-- this function gets called once each frame, on client only
function Scrapyard.getUpdateInterval()
function Scrapyard.getUpdateInterval()
return 1
return 1
end
end


function Scrapyard.updateClient(timeStep)
function Scrapyard.updateClient(timeStep)
licenseDuration = licenseDuration - timeStep
licenseDuration = licenseDuration - timeStep


if visible then
if visible then
if licenseDuration > 0 then
if licenseDuration > 0 then
timeLabel.caption = "Your license will expire in ${time}."%_t % {time = createReadableTimeString(licenseDuration)}
timeLabel.caption = "Your license will expire in ${time}."%_t % {time = createReadableTimeString(licenseDuration)}
else
else
timeLabel.caption = "You don't have a valid license."%_t
timeLabel.caption = "You don't have a valid license."%_t
end
end
end
end
end
end


function Scrapyard.transactionComplete()
function Scrapyard.transactionComplete()
ScriptUI():stopInteraction()
ScriptUI():stopInteraction()
end
end


function Scrapyard.getLicenseDuration()
function Scrapyard.getLicenseDuration()
invokeServerFunction("sendLicenseDuration")
invokeServerFunction("sendLicenseDuration")
end
end


function Scrapyard.setLicenseDuration(duration)
function Scrapyard.setLicenseDuration(duration)
licenseDuration = duration
licenseDuration = duration
end
end


-- this function gets called once each frame, on client and server
-- this function gets called once each frame, on client and server
--function update(timeStep)
--function update(timeStep)
--
--
--end
--end


function Scrapyard.getLicensePrice(orderingFaction, minutes)
function Scrapyard.getLicensePrice(orderingFaction, minutes)


local price = minutes * 150 * (1.0 + GetFee(Faction(), orderingFaction)) * Balancing_GetSectorRichnessFactor(Sector():getCoordinates())
local price = minutes * 150 * (1.0 + GetFee(Faction(), orderingFaction)) * Balancing_GetSectorRichnessFactor(Sector():getCoordinates())


local discountFactor = 1.0
local discountFactor = 1.0
if minutes > 5 then discountFactor = 0.93 end
if minutes > 5 then discountFactor = 0.93 end
if minutes > 15 then discountFactor = 0.86 end
if minutes > 15 then discountFactor = 0.86 end
if minutes > 40 then discountFactor = 0.80 end
if minutes > 40 then discountFactor = 0.80 end


return round(price * discountFactor);
return round(price * discountFactor);


end
end


-- this function gets called once each frame, on server only
-- this function gets called once each frame, on server only
function Scrapyard.updateServer(timeStep)
function Scrapyard.updateServer(timeStep)


local station = Entity();
local station = Entity();


newsBroadcastCounter = newsBroadcastCounter + timeStep
newsBroadcastCounter = newsBroadcastCounter + timeStep
if newsBroadcastCounter > 60 then
if newsBroadcastCounter > 60 then
Sector():broadcastChatMessage(station.title, 0, "Get a salvaging license now and try your luck with the wreckages!"%_t)
Sector():broadcastChatMessage(station.title, 0, "Get a salvaging license now and try your luck with the wreckages!"%_t)
newsBroadcastCounter = 0
newsBroadcastCounter = 0
end
end


for factionIndex, actions in pairs(illegalActions) do
-- counter for update, this is only executed once per second to save performance.
for playerIndex, actions in pairs(illegalActions) do


actions = actions - 1
actions = actions - 1


if actions <= 0 then
if actions <= 0 then
illegalActions[factionIndex] = nil
illegalActions[playerIndex] = nil
else
else
illegalActions[factionIndex] = actions
illegalActions[playerIndex] = actions
end
end
end
end


for factionIndex, time in pairs(licenses) do
for playerIndex, time in pairs(licenses) do


time = time - timeStep
time = time - timeStep


local faction = Faction(factionIndex)
if not faction then goto continue end

local here = false
if faction.isAlliance then
faction = Alliance(factionIndex)
elseif faction.isPlayer then
faction = Player(factionIndex)

local px, py = faction:getSectorCoordinates()
local sx, sy = Sector():getCoordinates()

here = (px == sx and py == sy)
end

local doubleSend = false
local msg = nil

-- warn player if his time is running out
-- warn player if his time is running out
if time + 1 > 10 and time <= 10 then
if time + 1 > 10 and time <= 10 then
if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license will run out in 10 seconds."%_t);
msg = "Your salvaging license will run out in 10 seconds."%_t
Player(playerIndex):sendChatMessage(station.title, 2, "Your salvaging license will run out in 10 seconds."%_t);
else
msg = "Your salvaging license in %s will run out in 10 seconds."%_t
end

doubleSend = true
end
end


if time + 1 > 20 and time <= 20 then
if time + 1 > 20 and time <= 20 then
if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license will run out in 20 seconds."%_t);
msg = "Your salvaging license will run out in 20 seconds."%_t
Player(playerIndex):sendChatMessage(station.title, 2, "Your salvaging license will run out in 20 seconds."%_t);
else
msg = "Your salvaging license in %s will run out in 20 seconds."%_t
end

doubleSend = true
end
end


if time + 1 > 30 and time <= 30 then
if time + 1 > 30 and time <= 30 then
if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license will run out in 30 seconds. Renew it and save yourself some trouble!"%_t);
msg = "Your salvaging license will run out in 30 seconds. Renew it and save yourself some trouble!"%_t
else
msg = "Your salvaging license in %s will run out in 30 seconds. Renew it and save yourself some trouble!"%_t
end
end
end


if time + 1 > 60 and time <= 60 then
if time + 1 > 60 and time <= 60 then
if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license will run out in 60 seconds. Renew it NOW and save yourself some trouble!"%_t);
msg = "Your salvaging license will run out in 60 seconds. Renew it NOW and save yourself some trouble!"%_t
else
msg = "Your salvaging license in %s will run out in 60 seconds. Renew it NOW and save yourself some trouble!"%_t
end
end
end


if time + 1 > 120 and time <= 120 then
if time + 1 > 120 and time <= 120 then
if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license will run out in 2 minutes. Renew it immediately and save yourself some trouble!"%_t);
msg = "Your salvaging license will run out in 2 minutes. Renew it immediately and save yourself some trouble!"%_t
else
msg = "Your salvaging license in %s will run out in 2 minutes. Renew it immediately and save yourself some trouble!"%_t
end
end
end


if time < 0 then
if time < 0 then
licenses[factionIndex] = nil
licenses[playerIndex] = nil


if here then
Player(playerIndex):sendChatMessage(station.title, 0, "Your salvaging license expired. You may no longer salvage in this area."%_t);
msg = "Your salvaging license expired. You may no longer salvage in this area."%_t
else
msg = "Your salvaging license in %s expired. You may no longer salvage in this area."%_t
end
else
else
licenses[factionIndex] = time
licenses[playerIndex] = time
end
end

if msg then
local x, y = Sector():getCoordinates()
local coordinates = "${x}:${y}" % {x = x, y = y}

faction:sendChatMessage(station.title, 0, msg, coordinates)
if doubleSend then
faction:sendChatMessage(station.title, 2, msg, coordinates)
end
end

::continue::
end
end


end
end


function Scrapyard.sellCraft()
function Scrapyard.sellCraft()
local buyer, ship, player = getInteractingFaction(callingPlayer, AlliancePrivilege.ModifyCrafts, AlliancePrivilege.SpendResources)
local buyer, ship, player = getInteractingFaction(callingPlayer, AlliancePrivilege.ModifyCrafts, AlliancePrivilege.SpendResources)
if not buyer then return end
if not buyer then return end


-- don't allow selling drones, would be an infinite income source
-- don't allow selling drones, would be an infinite income source
if ship.isDrone then return end
if ship.isDrone then return end


-- Create Wreckage
-- Create Wreckage
local position = ship.position
local position = ship.position
local plan = ship:getPlan();
local plan = ship:getPlan();


-- remove the old craft
-- remove the old craft
Sector():deleteEntity(ship)
Sector():deleteEntity(ship)


-- create a wreckage in its place
-- create a wreckage in its place
local wreckageIndex = Sector():createWreckage(plan, position)
local wreckageIndex = Sector():createWreckage(plan, position)


local moneyValue = Scrapyard.getShipValue(plan)
local moneyValue = Scrapyard.getShipValue(plan)
buyer:receive(moneyValue)
buyer:receive(moneyValue)


invokeClientFunction(player, "transactionComplete")
invokeClientFunction(player, "transactionComplete")
end
end


function Scrapyard.getShipValue(plan)
function Scrapyard.getShipValue(plan)
local sum = plan:getMoneyValue()
local sum = plan:getMoneyValue()
local resourceValue = {plan:getResourceValue()}
local resourceValue = {plan:getResourceValue()}


for i, v in pairs (resourceValue) do
for i, v in pairs (resourceValue) do
sum = sum + Material(i - 1).costFactor * v * 10;
sum = sum + Material(i - 1).costFactor * v * 10;
end
end


-- players only get money, and not even the full value.
-- players only get money, and not even the full value.
-- This is to avoid exploiting the scrapyard functionality by buying and then selling ships
-- This is to avoid exploiting the scrapyard functionality by buying and then selling ships
return sum * 0.75
return sum * 0.75
end
end


function Scrapyard.buyLicense(duration)
function Scrapyard.buyLicense(duration)


local buyer, ship, player = getInteractingFaction(callingPlayer, AlliancePrivilege.SpendResources)
local buyer, ship, player = getInteractingFaction(callingPlayer, AlliancePrivilege.SpendResources)
if not buyer then return end
if not buyer then return end


local price = Scrapyard.getLicensePrice(buyer, duration / 60) -- minutes!
local price = Scrapyard.getLicensePrice(buyer, duration / 60) -- minutes!


local station = Entity()
local station = Entity()


local canPay, msg, args = buyer:canPay(price)
local canPay, msg, args = buyer:canPay(price)
if not canPay then
if not canPay then
player:sendChatMessage(station.title, 1, msg, unpack(args));
player:sendChatMessage(station.title, 1, msg, unpack(args));
return;
return;
end
end


buyer:pay(price)
buyer:pay(price)


-- register player's license
-- register player's license
licenses[buyer.index] = duration
licenses[buyer.index] = duration


-- send a message as response
-- send a message as response
local minutes = licenses[buyer.index] / 60
local minutes = licenses[buyer.index] / 60
player:sendChatMessage(station.title, 0, "You bought a %s minutes salvaging license."%_t, minutes);
player:sendChatMessage(station.title, 0, "You bought a %s minutes salvaging license."%_t, minutes);
player:sendChatMessage(station.title, 0, "%s cannot be held reliable for any damage to ships or deaths caused by salvaging."%_t, Faction().name);
player:sendChatMessage(station.title, 0, "%s cannot be held reliable for any damage to ships or deaths caused by salvaging."%_t, Faction().name);


Scrapyard.sendLicenseDuration()
Scrapyard.sendLicenseDuration()
end
end


function Scrapyard.sendLicenseDuration()
function Scrapyard.sendLicenseDuration()
local duration = licenses[callingPlayer]
local duration = licenses[callingPlayer]


if duration ~= nil then
if duration ~= nil then
invokeClientFunction(Player(callingPlayer), "setLicenseDuration", duration)
invokeClientFunction(Player(callingPlayer), "setLicenseDuration", duration)
end
end
end
end


function Scrapyard.onHullHit(objectIndex, block, shootingCraftIndex, damage, position)
function Scrapyard.onHullHit(objectIndex, block, shootingCraftIndex, damage, position)
local object = Entity(objectIndex)
local object = Entity(objectIndex)
if object and object.isWreckage then
if object and object.isWreckage then
local shooter = Entity(shootingCraftIndex)
local shooter = Entity(shootingCraftIndex)
if shooter then
if shooter then
local faction = Faction(shooter.factionIndex)
local faction = Faction(shooter.factionIndex)
if not faction.isAIFaction and licenses[faction.index] == nil then
if not faction.isAIFaction and licenses[faction.index] == nil then
Scrapyard.unallowedDamaging(shooter, faction, damage)
Scrapyard.unallowedDamaging(shooter, faction, damage)
end
end
end
end
end
end
end
end


function Scrapyard.unallowedDamaging(shooter, faction, damage)
function Scrapyard.unallowedDamaging(shooter, faction, damage)


local pilots = {}
local pilots = {}


if faction.isAlliance then
if faction.isAlliance then
for _, playerIndex in pairs({shooter:getPilotIndices()}) do
for _, playerIndex in pairs({shooter:getPilotIndices()}) do
local player = Player(playerIndex)
local player = Player(playerIndex)


if player then
if player then
table.insert(pilots, player)
table.insert(pilots, player)
end
end
end
end


elseif faction.isPlayer then
elseif faction.isPlayer then
table.insert(pilots, Player(faction.index))
table.insert(pilots, Player(faction.index))
end
end


local station = Entity()
local station = Entity()


local actions = illegalActions[faction.index]
local actions = illegalActions[faction.index]
if actions == nil then
if actions == nil then
actions = 0
actions = 0
end
end


newActions = actions + damage
newActions = actions + damage


for _, player in pairs(pilots) do
for _, player in pairs(pilots) do
if actions < 10 and newActions >= 10 then
if actions < 10 and newActions >= 10 then
player:sendChatMessage(station.title, 0, "Salvaging or damaging wreckages in this sector is illegal. Please buy a salvaging license."%_t);
player:sendChatMessage(station.title, 0, "Salvaging or damaging wreckages in this sector is illegal. Please buy a salvaging license."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end


if actions < 200 and newActions >= 200 then
if actions < 200 and newActions >= 200 then
player:sendChatMessage(station.title, 0, "Salvaging wreckages in this sector is forbidden. Please buy a salvaging license."%_t);
player:sendChatMessage(station.title, 0, "Salvaging wreckages in this sector is forbidden. Please buy a salvaging license."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end


if actions < 500 and newActions >= 500 then
if actions < 500 and newActions >= 500 then
player:sendChatMessage(station.title, 0, "Wreckages in this sector are the property of %s. Please buy a salvaging license."%_t, Faction().name);
player:sendChatMessage(station.title, 0, "Wreckages in this sector are the property of %s. Please buy a salvaging license."%_t, Faction().name);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end


if actions < 1000 and newActions >= 1000 then
if actions < 1000 and newActions >= 1000 then
player:sendChatMessage(station.title, 0, "Illegal salvaging will be punished by destruction. Buy a salvaging license or there will be consequences."%_t);
player:sendChatMessage(station.title, 0, "Illegal salvaging will be punished by destruction. Buy a salvaging license or there will be consequences."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end


if actions < 1500 and newActions >= 1500 then
if actions < 1500 and newActions >= 1500 then
player:sendChatMessage(station.title, 0, "This is your last warning. If you do not stop salvaging without a license, you will be destroyed."%_t);
player:sendChatMessage(station.title, 0, "This is your last warning. If you do not stop salvaging without a license, you will be destroyed."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end


if actions < 2000 and newActions >= 2000 then
if actions < 2000 and newActions >= 2000 then
player:sendChatMessage(station.title, 0, "You have been warned. You will be considered an enemy of %s if you do not stop your illegal activities."%_t, Faction().name);
player:sendChatMessage(station.title, 0, "You have been warned. You will be considered an enemy of %s if you do not stop your illegal activities."%_t, Faction().name);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
player:sendChatMessage(station.title, 2, "You need a salvaging license for this sector."%_t);
end
end
end
end


if newActions > 5 then
if newActions > 5 then
Galaxy():changeFactionRelations(Faction(), faction, -newActions / 100)
Galaxy():changeFactionRelations(Faction(), faction, -newActions / 100)
end
end


illegalActions[faction.index] = newActions
illegalActions[faction.index] = newActions


end
end


if not pcall(require, "mods/ScrapyardPlus/scripts/entity/merchants/scrapyard") then print("Failed to load ScrapyardPlus") end -- DNightmare/ScrapyardPlus END


if not pcall(require, 'mods.MoveUI.scripts.entity.merchants.scrapyard') then print('Mod: MoveUI, failed to extend scrapyard.lua!') end