Skip to content

Commit

Permalink
New BBS advert, offering double price on sold out commodity
Browse files Browse the repository at this point in the history
Is one of the system's major imports
  • Loading branch information
impaktor committed Dec 13, 2020
1 parent a8220d8 commit 50f09a9
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
26 changes: 26 additions & 0 deletions data/lang/module-soldout/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"MESSAGE": {
"description": "",
"message": "I'm {name}. I need {commodity} urgently, and I'll pay twice the market price ({price})"
},
"NOT_IN_STOCK": {
"description": "Regarding selling more than the player has",
"message": "Not enough in stock"
},
"SELL_1": {
"description": "Sell one single unit (probably tonne)",
"message": "Sell 1"
},
"SELL_N": {
"description": "Sell several units (probably in tonne), i.e. N > 1",
"message": "Sell {N}"
},
"SELL_ALL": {
"description": "Sell everything player has of the good/commodity",
"message": "Sell all"
},
"TITLE": {
"description": "",
"message": "WANTED: {commodity}. Will pay {price} per tonne"
}
}
142 changes: 142 additions & 0 deletions data/modules/SoldOut/SoldOut.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
-- Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local Engine = require 'Engine'
local Lang = require 'Lang'
local Game = require 'Game'
local Event = require 'Event'
local Serializer = require 'Serializer'
local Character = require 'Character'
local Format = require 'Format'
local Equipment = require 'Equipment'

local l = Lang.GetResource("module-soldout")

local ads = {}

local onChat = function (form, ref, option)
local ad = ads[ref]

local max = Game.player:CountEquip(ad.commodity, "cargo")

form:Clear()
form:SetTitle(ad.title)
form:SetFace(ad.client)

if option == 0 then
form:SetMessage(ad.message)
end

if option == -1 then
form:Close()
return
end

if option == "max" then
if max < 1 then
form:SetMessage(l.NOT_IN_STOCK)
else
Game.player:RemoveEquip(ad.commodity, max)
Game.player:AddMoney(ad.price * max)
form:SetMessage(ad.message)
end
elseif option > 0 then
if max < option then
form:SetMessage(l.NOT_IN_STOCK)
else
Game.player:RemoveEquip(ad.commodity, option)
Game.player:AddMoney(ad.price * option)
form:SetMessage(ad.message)
end
end

form:AddOption(l.SELL_1, 1)
for i, val in pairs{10, 100, 1000} do
form:AddOption(string.interp(l.SELL_N, {N=val}), val)
end
form:AddOption(l.SELL_ALL, "max")

return
end

local makeAdvert = function(station, commodity)
-- Select commodity to run low on
print("----Making sold out advert for:", commodity:GetName(), station.label)

local ad = {
station = station,
client = Character.New(),
price = 2 * station:GetEquipmentPrice(commodity),
commodity = commodity,
}

ad.title = string.interp(l.TITLE,
{commodity = ad.commodity:GetName(), price = Format.Money(ad.price)})
ad.message = string.interp(l.MESSAGE,
{name = ad.client.name, commodity = commodity:GetName(), price = Format.Money(ad.price)})

local ref = station:AddAdvert({
description = ad.title,
icon = "donate_to_cranks",
onChat = onChat,
onDelete = onDelete})
ads[ref] = ad
end

local onDelete = function (ref)
ads[ref] = nil
end

local onCreateBB = function (station)
-- A commodity must have zero stock to be sold out (probability set in SpaceStation.lua)
local sold_out = {}
for i, c in pairs(Equipment.cargo) do
local stock = station:GetEquipmentStock(c)
if stock == 0 then
print("--stock", stock, c:GetName(), station.label)
table.insert(sold_out, c)
end
end
print("-- number of sold out commodities found at station: ", #sold_out, station.label)

-- low random chance of spawning
for i, c in pairs(sold_out) do
print("--Test to make an advert:", c:GetName(), station.label)
if Engine.rand:Number(0, 1) > 0.5 then
makeAdvert(station, c)
end
end
print("\n\n")
end

local loaded_data

local onGameStart = function ()
ads = {}

if not loaded_data or not loaded_data.ads then return end

for k,ad in pairs(loaded_data.ads) do
local ref = ad.station:AddAdvert({
description = l.TITLE,
icon = "donate_to_cranks",
onChat = onChat,
onDelete = onDelete})
ads[ref] = ad
end

loaded_data = nil
end

local serialize = function ()
return { ads = ads }
end

local unserialize = function (data)
loaded_data = data
end

Event.Register("onCreateBB", onCreateBB)
Event.Register("onGameStart", onGameStart)

Serializer:Register("SoldOut", serialize, unserialize)

0 comments on commit 50f09a9

Please sign in to comment.