-
Notifications
You must be signed in to change notification settings - Fork 4
/
control.lua
167 lines (145 loc) · 5.09 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-- Copyright (c) 2021 StefanT <stt1@gmx.at>
-- See LICENSE.md in the project directory for license information.
require "script.constants"
require "script.output"
require "script.settings"
require "script.utils"
require "script.find"
require "script.filter"
require "script.train"
require "script.dialog"
require "script.events"
--
-- Initialize the global variables.
--
function initGlobalVariables()
log("init storage variables")
-- The shuttle trains that are currently active.
-- Key is the LuaTrain::id, value is a structure with:
-- train The LuaTrain
-- player The controlling player
-- status The control status, see the STATUS_xy constants in constants.lua
-- destinationName The LuaEntity::backer_name of the destination station
-- timeout The game.tick time when the train's current action times out
storage.trackedTrains = storage.trackedTrains or {}
-- The selected station category
-- Key is the LuaPlayer::id, value is the name of the category.
storage.selectedCategory = storage.selectedCategory or {}
-- The history of the selected stations per player.
-- Key is the LuaPlayer::id, value is a list of the selected stations.
storage.history = storage.history or {}
-- The schedule of the train the player is currently configuring.
-- Key is the LuaPlayer::id, value is a structure with:
-- id The ID of the LuaTrain
-- schedule The train's schedule records
storage.playerTrain = storage.playerTrain or {}
end
--
-- Add the given station to the player's station history, remove the oldest if the
-- list is too long.
--
-- @param player The LuaPlayer
-- @param stationName The name of the station to add
--
local function updateHistory(player, stationName)
local history = { stationName }
local maxEntries = settings.get_player_settings(player)["shuttle-train-gui-height"].value
for _,name in pairs(storage.history[player.index] or {}) do
if name ~= stationName and #history < maxEntries then
table.insert(history, name)
end
end
storage.history[player.index] = history
end
--
-- Call a shuttle train for the player
--
-- @param player The LuaPlayer that called for a shuttle train
-- @param station The LuaEntity of the station where the shuttle train shall be sent (optional)
--
function callShuttleTrain(player, station)
local train = findShuttleTrainFor(player)
if not train then
player.print{"error.noTrainFound"}
return
end
if distance(player, train.front_stock) <= SEARCH_RANGE then
player.print{"info.useTrainNearby"}
return
end
if not station then
station = findPickupStationFor(player)
end
if station then
updateHistory(player, station.backer_name)
if train.station == station then
player.print{"info.pickupTrainAtStation", stationRef(station)}
else
player.print{"info.sendPickupTrain", trainRef(train), stationRef(station)}
sendPickupTrain(train, player, station)
end
elseif findNearbyEntity(player, "train-stop") then
player.print{"error.unsuitableStationFound"}
else
local rail = findNearbyEntity(player, "straight-rail")
if rail then
player.print{"info.sendPickupTrainToRail", trainRef(train)}
sendPickupTrain(train, player, rail)
else
player.print{"error.noPickupFound"}
end
end
end
--
-- Transport the player to the given station using the train the player is in.
--
-- @param player The LuaPlayer that controls the train
-- @param stationName The name of the destination station
--
function playerClickedStation(player, stationName)
local station = findStationByName(stationName, player)
if not station then
log("player selected unknown station "..stationName)
player.print{"error.unknownStation"}
return
end
if controlsShuttleTrain(player) then
storage.playerTrain[player.index] = nil
local train = player.vehicle.train
if train.station and train.station.backer_name == stationName then
player.print{"info.alreadyThere", stationRef(station)}
return
end
updateHistory(player, stationName)
transportTo(train, player, station)
closeDialog(player)
elseif not player.vehicle or not player.vehicle.train then
callShuttleTrain(player, station)
closeDialog(player)
end
end
--
-- Called when a player manually changed the schedule of a shuttle train.
--
-- @param player The LuaPlayer who did the change
-- @param train The LuaTrain that was changed
--
function playerChangedTrainSchedule(player, train)
if not settings.get_player_settings(player)["shuttle-train-smart-manual-destinations"].value then
return
end
local playerTrainInfo = storage.playerTrain[player.index] or {}
log("player "..player.name.." manually changed schedule of shuttle train #"..tostring(playerTrainInfo.id))
local oldRecs = playerTrainInfo.schedule or {}
local dest = findChangedScheduleRecord(oldRecs, (train.schedule or {}).records or {})
if not dest then
log("no new train schedule record was added")
return
elseif dest.station then
dest = findStationByName(dest.station, player)
else
dest = dest.rail
end
log("destination "..tostring(dest))
transportTo(train, player, dest)
end