1-- Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4-- This module is a substitute for not having a propper "bar" /
5-- "bartender" where a player could go and get latest rumours or
6-- advice from more experienced pilots. Instead, we just put them on
7-- the BBS for now.
8
9local Lang = require 'Lang'
10local Event = require 'Event'
11local Rand = require 'Rand'
12local Serializer = require 'Serializer'
13
14local l = Lang.GetResource("module-advice")
15
16-- probability to have one advice/rumour (per BBS):
17local advice_probability = .2
18
19
20-- There are three different versions of flavours "Rumours",
21-- "Traveller's tale", and "Traveller's advice", which have fake /
22-- arbitrary indices, for inflated feeling of rich universe and lore.
23local rumours_num = 1
24local travellers_tale_num = 1
25local travellers_advice_indices = {481, -- tame black market
26								   16,  -- road faster taken
27								   173, -- don't get stranded
28								   13,  -- it's all relative
29								   5,   -- never loose money
30								   121, -- read the news
31								   81,  -- donate
32								   23,  -- double trouble
33								   52,  -- service ship
34								   248, -- change faction
35}
36
37-- Hold all different types of advice/rumours available:
38local flavours = {}
39
40-- Hold the ones published on the BBS:
41local ads = {}
42
43-- add Traveller strings to flavours-table:
44for i = 1,#travellers_advice_indices do
45	local num = travellers_advice_indices[i]
46	table.insert(flavours, {
47		ID = l['TRAVELLERS_ADVICE'] .. " #" .. num,
48		headline = l["ADVICE_" .. i .. "_HEADLINE"],
49		bodytext = l["ADVICE_" .. i .. "_BODYTEXT"],
50	})
51end
52
53-- add Traveller tale strings to flavours-table:
54for i = 1,travellers_tale_num do
55	table.insert(flavours, {
56		ID = l['TRAVELLERS_TALE'],
57		headline = l["TALE_" .. i .. "_HEADLINE"],
58		bodytext = l["TALE_" .. i .. "_BODYTEXT"],
59	})
60end
61
62-- add rumours to flavours-table:
63for i = 1,rumours_num do
64	table.insert(flavours, {
65		ID = l['RUMOUR'],
66		headline = l["RUMOUR_" .. i .. "_HEADLINE"],
67		bodytext = l["RUMOUR_" .. i .. "_BODYTEXT"],
68	})
69end
70
71-- print ad to BBS
72local onChat = function (form, ref, option)
73	local ad = ads[ref]
74	form:Clear()
75	form:SetTitle(ad.id)
76	form:SetMessage(ad.bodytext)
77end
78
79local onDelete = function (ref)
80	ads[ref] = nil
81end
82
83-- when we enter a system the BBS is created and this function is called
84local onCreateBB = function (station)
85	local rand = Rand.New(station.seed)
86	local n = rand:Integer(1, #flavours)
87
88	local ad = {
89		id = flavours[n].ID,
90		headline = flavours[n].ID .. ": " .. flavours[n].headline,
91		bodytext = flavours[n].bodytext,
92		station  = station
93	}
94
95	-- only create one per BBS, with advice_probability
96	local ref
97	if rand:Number() < advice_probability then
98		ref = station:AddAdvert({
99			description = ad.headline,
100			icon        = "advice",
101			onChat      = onChat,
102			onDelete    = onDelete})
103		ads[ref] = ad
104	end
105end
106
107
108local loaded_data
109
110local onGameStart = function ()
111	ads = {}
112
113	if not loaded_data or not loaded_data.ads then return end
114
115	for k,ad in pairs(loaded_data.ads or {}) do
116		local ref = ad.station:AddAdvert({
117			description = ad.headline,
118			icon        = "advice",
119			onChat      = onChat,
120			onDelete    = onDelete})
121		ads[ref] = ad
122	end
123
124	loaded_data = nil
125end
126
127local serialize = function ()
128	return { ads = ads }
129end
130
131local unserialize = function (data)
132	loaded_data = data
133end
134
135Event.Register("onCreateBB", onCreateBB)
136Event.Register("onGameStart", onGameStart)
137
138Serializer:Register("Advice", serialize, unserialize)
139