Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CrewContracts - onUpdateBB #5596

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 58 additions & 17 deletions data/modules/CrewContracts/CrewContracts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,41 @@ local isEnabled = function (ref)
return numCrewmenAvailable > 0
end

local function N_equilibrium(station)
local pop = station.path:GetSystemBody().parent.population -- E.g. Earth=7, Mars=0.3
local pop_bonus = 9 * math.log(pop * 0.45 + 1) -- something that gives resonable result
if station.type == "STARPORT_SURFACE" then
pop_bonus = pop_bonus * 1.5
end

return 2 + pop_bonus
end

local newCrew = function()
local hopefulCrew = Character.New()
-- Roll new stats, with a 1/3 chance that they're utterly inexperienced
hopefulCrew:RollNew(Engine.rand:Integer(0, 2) > 0)
-- Make them a title if they're good at anything
local maxScore = math.max(hopefulCrew.engineering,
hopefulCrew.piloting,
hopefulCrew.navigation,
hopefulCrew.sensors)
if maxScore > 45 then
if hopefulCrew.engineering == maxScore then hopefulCrew.title = lui.SHIPS_ENGINEER end
if hopefulCrew.piloting == maxScore then hopefulCrew.title = lui.PILOT end
if hopefulCrew.navigation == maxScore then hopefulCrew.title = lui.NAVIGATOR end
if hopefulCrew.sensors == maxScore then hopefulCrew.title = lui.SENSORS_AND_DEFENCE end
end
return hopefulCrew
end

local function removeAd (station, num)
if not nonPersistentCharactersForCrew[station] then
nonPersistentCharactersForCrew[station] = {}
end
table.remove(nonPersistentCharactersForCrew[station], num)
end

local onCreateBB = function (station)
-- Create non-persistent Characters as available crew
nonPersistentCharactersForCrew[station] = {}
Expand All @@ -392,27 +427,33 @@ local onCreateBB = function (station)
isEnabled = isEnabled})] = station

-- Number is based on population, nicked from Assassinations.lua and tweaked
for i = 1, Engine.rand:Integer(0, math.ceil(Game.system.population) * 2 + 1) do
local hopefulCrew = Character.New()
-- Roll new stats, with a 1/3 chance that they're utterly inexperienced
hopefulCrew:RollNew(Engine.rand:Integer(0, 2) > 0)
-- Make them a title if they're good at anything
local maxScore = math.max(hopefulCrew.engineering,
hopefulCrew.piloting,
hopefulCrew.navigation,
hopefulCrew.sensors)
if maxScore > 45 then
if hopefulCrew.engineering == maxScore then hopefulCrew.title = lui.SHIPS_ENGINEER end
if hopefulCrew.piloting == maxScore then hopefulCrew.title = lui.PILOT end
if hopefulCrew.navigation == maxScore then hopefulCrew.title = lui.NAVIGATOR end
if hopefulCrew.sensors == maxScore then hopefulCrew.title = lui.SENSORS_AND_DEFENCE end
end
table.insert(nonPersistentCharactersForCrew[station],hopefulCrew)
for i = 1, Engine.rand:Poisson(N_equilibrium(station)) do
table.insert(nonPersistentCharactersForCrew[station],newCrew())
end
end

Event.Register("onCreateBB", onCreateBB)

local onUpdateBB = function (station)
local tau = 7*24 -- half life of a ship advert in hours
local lambda = 0.693147 / tau -- removal probability= ln(2) / tau
local prod = N_equilibrium(station) * lambda -- creation probability

-- remove with decay rate lambda. Call ONCE/hour for each ship advert in station
for k,v in pairs(nonPersistentCharactersForCrew[station]) do
if Engine.rand:Number(0,1) < lambda then -- remove one random ship (sold)
table.remove(nonPersistentCharactersForCrew[station],k)
end
end

-- spawn a new ship adverts, call for each station
if Engine.rand:Number(0,1) <= prod then
table.insert(nonPersistentCharactersForCrew[station], 1, newCrew())
end
print("#nonPersistentCharactersForCrew: " .. #nonPersistentCharactersForCrew[station])
if prod > 1 then print("Warning: crew market not in equilibrium") end
end
Event.Register("onUpdateBB", onUpdateBB)

-- Wipe temporary crew out when hyperspacing
Event.Register("onEnterSystem", function(ship)
if ship:IsPlayer() then
Expand Down