aboutsummaryrefslogtreecommitdiff
path: root/src/network/serveropcodes.cpp
blob: 44b65e8daa2821933cbcefb28ce172dd152d10f0 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "serveropcodes.h"

const static ToServerCommandHandler null_command_handler = { "TOSERVER_NULL", TOSERVER_STATE_ALL, &Server::handleCommand_Null };

const ToServerCommandHandler toServerCommandTable[TOSERVER_NUM_MSG_TYPES] =
{
	null_command_handler, // 0x00 (never use this)
	null_command_handler, // 0x01
	{ "TOSERVER_INIT",                     TOSERVER_STATE_NOT_CONNECTED, &Server::handleCommand_Init }, // 0x02
	null_command_handler, // 0x03
	null_command_handler, // 0x04
	null_command_handler, // 0x05
	null_command_handler, // 0x06
	null_command_handler, // 0x07
	null_command_handler, // 0x08
	null_command_handler, // 0x09
	null_command_handler, // 0x0a
	null_command_handler, // 0x0b
	null_command_handler, // 0x0c
	null_command_handler, // 0x0d
	null_command_handler, // 0x0e
	null_command_handler, // 0x0f
	null_command_handler, // 0x10
	{ "TOSERVER_INIT2",                    TOSERVER_STATE_NOT_CONNECTED, &Server::handleCommand_Init2 }, // 0x11
	null_command_handler, // 0x12
	null_command_handler, // 0x13
	null_command_handler, // 0x14
	null_command_handler, // 0x15
	null_command_handler, // 0x16
	{ "TOSERVER_MODCHANNEL_JOIN",          TOSERVER_STATE_INGAME, &Server::handleCommand_ModChannelJoin }, // 0x17
	{ "TOSERVER_MODCHANNEL_LEAVE",         TOSERVER_STATE_INGAME, &Server::handleCommand_ModChannelLeave }, // 0x18
	{ "TOSERVER_MODCHANNEL_MSG",           TOSERVER_STATE_INGAME, &Server::handleCommand_ModChannelMsg }, // 0x19
	null_command_handler, // 0x1a
	null_command_handler, // 0x1b
	null_command_handler, // 0x1c
	null_command_handler, // 0x1d
	null_command_handler, // 0x1e
	null_command_handler, // 0x1f
	null_command_handler, // 0x20
	null_command_handler, // 0x21
	null_command_handler, // 0x22
	{ "TOSERVER_PLAYERPOS",                TOSERVER_STATE_INGAME, &Server::handleCommand_PlayerPos }, // 0x23
	{ "TOSERVER_GOTBLOCKS",                TOSERVER_STATE_STARTUP, &Server::handleCommand_GotBlocks }, // 0x24
	{ "TOSERVER_DELETEDBLOCKS",            TOSERVER_STATE_INGAME, &Server::handleCommand_DeletedBlocks }, // 0x25
	null_command_handler, // 0x26
	null_command_handler, // 0x27
	null_command_handler, // 0x28
	null_command_handler, // 0x29
	null_command_handler, // 0x2a
	null_command_handler, // 0x2b
	null_command_handler, // 0x2c
	null_command_handler, // 0x2d
	null_command_handler, // 0x2e
	null_command_handler, // 0x2f
	null_command_handler, // 0x30
	{ "TOSERVER_INVENTORY_ACTION",         TOSERVER_STATE_INGAME, &Server::handleCommand_InventoryAction }, // 0x31
	{ "TOSERVER_CHAT_MESSAGE",             TOSERVER_STATE_INGAME, &Server::handleCommand_ChatMessage }, // 0x32
	null_command_handler, // 0x33
	null_command_handler, // 0x34
	{ "TOSERVER_DAMAGE",                   TOSERVER_STATE_INGAME, &Server::handleCommand_Damage }, // 0x35
	null_command_handler, // 0x36
	{ "TOSERVER_PLAYERITEM",               TOSERVER_STATE_INGAME, &Server::handleCommand_PlayerItem }, // 0x37
	{ "TOSERVER_RESPAWN",                  TOSERVER_STATE_INGAME, &Server::handleCommand_Respawn }, // 0x38
	{ "TOSERVER_INTERACT",                 TOSERVER_STATE_INGAME, &Server::handleCommand_Interact }, // 0x39
	{ "TOSERVER_REMOVED_SOUNDS",           TOSERVER_STATE_INGAME, &Server::handleCommand_RemovedSounds }, // 0x3a
	{ "TOSERVER_NODEMETA_FIELDS",          TOSERVER_STATE_INGAME, &Server::handleCommand_NodeMetaFields }, // 0x3b
	{ "TOSERVER_INVENTORY_FIELDS",         TOSERVER_STATE_INGAME, &Server::handleCommand_InventoryFields }, // 0x3c
	null_command_handler, // 0x3d
	null_command_handler, // 0x3e
	null_command_handler, // 0x3f
	{ "TOSERVER_REQUEST_MEDIA",            TOSERVER_STATE_STARTUP, &Server::handleCommand_RequestMedia }, // 0x40
	{ "TOSERVER_HAVE_MEDIA",               TOSERVER_STATE_INGAME, &Server::handleCommand_HaveMedia }, // 0x41
	null_command_handler, // 0x42
	{ "TOSERVER_CLIENT_READY",             TOSERVER_STATE_STARTUP, &Server::handleCommand_ClientReady }, // 0x43
	null_command_handler, // 0x44
	null_command_handler, // 0x45
	null_command_handler, // 0x46
	null_command_handler, // 0x47
	null_command_handler, // 0x48
	null_command_handler, // 0x49
	null_command_handler, // 0x4a
	null_command_handler, // 0x4b
	null_command_handler, // 0x4c
	null_command_handler, // 0x4d
	null_command_handler, // 0x4e
	null_command_handler, // 0x4f
	{ "TOSERVER_FIRST_SRP",                TOSERVER_STATE_NOT_CONNECTED, &Server::handleCommand_FirstSrp }, // 0x50
	{ "TOSERVER_SRP_BYTES_A",              TOSERVER_STATE_NOT_CONNECTED, &Server::handleCommand_SrpBytesA }, // 0x51
	{ "TOSERVER_SRP_BYTES_M",              TOSERVER_STATE_NOT_CONNECTED, &Server::handleCommand_SrpBytesM }, // 0x52
};

const static ClientCommandFactory null_command_factory = { "TOCLIENT_NULL", 0, false };

/*
	Channels used for Server -> Client communication
	2: Bulk data (mapblocks, media, ...)
	1: HUD packets
	0: everything else

	Packet order is only guaranteed inside a channel, so packets that operate on
	the same objects are *required* to be in the same channel.
*/

const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{
	null_command_factory, // 0x00
	null_command_factory, // 0x01
	{ "TOCLIENT_HELLO",                    0, true }, // 0x02
	{ "TOCLIENT_AUTH_ACCEPT",              0, true }, // 0x03
	{ "TOCLIENT_ACCEPT_SUDO_MODE",         0, true }, // 0x04
	{ "TOCLIENT_DENY_SUDO_MODE",           0, true }, // 0x05
	null_command_factory, // 0x06
	null_command_factory, // 0x07
	null_command_factory, // 0x08
	null_command_factory, // 0x09
	{ "TOCLIENT_ACCESS_DENIED",            0, true }, // 0x0A
	null_command_factory, // 0x0B
	null_command_factory, // 0x0C
	null_command_factory, // 0x0D
	null_command_factory, // 0x0E
	null_command_factory, // 0x0F
	{ "TOCLIENT_INIT",                     0, true }, // 0x10
	null_command_factory, // 0x11
	null_command_factory, // 0x12
	null_command_factory, // 0x13
	null_command_factory, // 0x14
	null_command_factory, // 0x15
	null_command_factory, // 0x16
	null_command_factory, // 0x17
	null_command_factory, // 0x18
	null_command_factory, // 0x19
	null_command_factory, // 0x1A
	null_command_factory, // 0x1B
	null_command_factory, // 0x1C
	null_command_factory, // 0x1D
	null_command_factory, // 0x1E
	null_command_factory, // 0x1F
	{ "TOCLIENT_BLOCKDATA",                2, true }, // 0x20
	{ "TOCLIENT_ADDNODE",                  0, true }, // 0x21
	{ "TOCLIENT_REMOVENODE",               0, true }, // 0x22
	null_command_factory, // 0x23
	null_command_factory, // 0x24
	null_command_factory, // 0x25
	null_command_factory, // 0x26
	{ "TOCLIENT_INVENTORY",                0, true }, // 0x27
	null_command_factory, // 0x28
	{ "TOCLIENT_TIME_OF_DAY",              0, true }, // 0x29
	{ "TOCLIENT_CSM_RESTRICTION_FLAGS",    0, true }, // 0x2A
	{ "TOCLIENT_PLAYER_SPEED",             0, true }, // 0x2B
	{ "TOCLIENT_MEDIA_PUSH",               0, true }, // 0x2C (sent over channel 1 too if legacy)
	null_command_factory, // 0x2D
	null_command_factory, // 0x2E
	{ "TOCLIENT_CHAT_MESSAGE",             0, true }, // 0x2F
	null_command_factory, // 0x30
	{ "TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD", 0, true }, // 0x31
	{ "TOCLIENT_ACTIVE_OBJECT_MESSAGES",   0, true }, // 0x32 (may be sent as unrel over channel 1 too)
	{ "TOCLIENT_HP",                       0, true }, // 0x33
	{ "TOCLIENT_MOVE_PLAYER",              0, true }, // 0x34
	{ "TOCLIENT_ACCESS_DENIED_LEGACY",     0, true }, // 0x35
	{ "TOCLIENT_FOV",                      0, true }, // 0x36
	{ "TOCLIENT_DEATHSCREEN",              0, true }, // 0x37
	{ "TOCLIENT_MEDIA",                    2, true }, // 0x38
	null_command_factory, // 0x39
	{ "TOCLIENT_NODEDEF",                  0, true }, // 0x3A
	null_command_factory, // 0x3B
	{ "TOCLIENT_ANNOUNCE_MEDIA",           0, true }, // 0x3C
	{ "TOCLIENT_ITEMDEF",                  0, true }, // 0x3D
	null_command_factory, // 0x3E
	{ "TOCLIENT_PLAY_SOUND",               0, true }, // 0x3f (may be sent as unrel too)
	{ "TOCLIENT_STOP_SOUND",               0, true }, // 0x40
	{ "TOCLIENT_PRIVILEGES",               0, true }, // 0x41
	{ "TOCLIENT_INVENTORY_FORMSPEC",       0, true }, // 0x42
	{ "TOCLIENT_DETACHED_INVENTORY",       0, true }, // 0x43
	{ "TOCLIENT_SHOW_FORMSPEC",            0, true }, // 0x44
	{ "TOCLIENT_MOVEMENT",                 0, true }, // 0x45
	{ "TOCLIENT_SPAWN_PARTICLE",           0, true }, // 0x46
	{ "TOCLIENT_ADD_PARTICLESPAWNER",      0, true }, // 0x47
	null_command_factory, // 0x48
	{ "TOCLIENT_HUDADD",                   1, true }, // 0x49
	{ "TOCLIENT_HUDRM",                    1, true }, // 0x4a
	{ "TOCLIENT_HUDCHANGE",                1, true }, // 0x4b
	{ "TOCLIENT_HUD_SET_FLAGS",            1, true }, // 0x4c
	{ "TOCLIENT_HUD_SET_PARAM",            1, true }, // 0x4d
	{ "TOCLIENT_BREATH",                   0, true }, // 0x4e
	{ "TOCLIENT_SET_SKY",                  0, true }, // 0x4f
	{ "TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO", 0, true }, // 0x50
	{ "TOCLIENT_LOCAL_PLAYER_ANIMATIONS",  0, true }, // 0x51
	{ "TOCLIENT_EYE_OFFSET",               0, true }, // 0x52
	{ "TOCLIENT_DELETE_PARTICLESPAWNER",   0, true }, // 0x53
	{ "TOCLIENT_CLOUD_PARAMS",             0, true }, // 0x54
	{ "TOCLIENT_FADE_SOUND",               0, true }, // 0x55
	{ "TOCLIENT_UPDATE_PLAYER_LIST",       0, true }, // 0x56
	{ "TOCLIENT_MODCHANNEL_MSG",           0, true }, // 0x57
	{ "TOCLIENT_MODCHANNEL_SIGNAL",        0, true }, // 0x58
	{ "TOCLIENT_NODEMETA_CHANGED",         0, true }, // 0x59
	{ "TOCLIENT_SET_SUN",                  0, true }, // 0x5a
	{ "TOCLIENT_SET_MOON",                 0, true }, // 0x5b
	{ "TOCLIENT_SET_STARS",                0, true }, // 0x5c
	null_command_factory, // 0x5d
	null_command_factory, // 0x5e
	null_command_factory, // 0x5f
	{ "TOSERVER_SRP_BYTES_S_B",            0, true }, // 0x60
	{ "TOCLIENT_FORMSPEC_PREPEND",         0, true }, // 0x61
	{ "TOCLIENT_MINIMAP_MODES",            0, true }, // 0x62
};
,wagon in pairs(minetest.luaentities) do if wagon.is_wagon and wagon.initialized and wagon.train_id==id then wagon:reattach_all() end end end end) end) minetest.register_on_dieplayer(function(player) return advtrains.pcall(function() local pname=player:get_player_name() local id=advtrains.player_to_train_mapping[pname] if id then local train=advtrains.trains[id] if not train then advtrains.player_to_train_mapping[pname]=nil return end for _,wagon in pairs(minetest.luaentities) do if wagon.is_wagon and wagon.initialized and wagon.train_id==id then --when player dies, detach him from the train --call get_off_plr on every wagon since we don't know which one he's on. wagon:get_off_plr(pname) end end end end) end) --[[ train step structure: - legacy stuff - preparing the initial path and creating index - setting node coverage old indices - handle velocity influences: - off-track - atc - player controls - environment collision - update index = move - create path - update node coverage - do less important stuff such as checking trainpartload or removing -- break -- - handle train collisions ]] function advtrains.train_step_a(id, train, dtime) --atprint("--- runcnt ",advtrains.mainloop_runcnt,": index",train.index,"end_index", train.end_index,"| max_iot", train.max_index_on_track, "min_iot", train.min_index_on_track, "<> pe_min", train.path_extent_min,"pe_max", train.path_extent_max) if train.min_index_on_track then assert(math.floor(train.min_index_on_track)==train.min_index_on_track) end --- 1. LEGACY STUFF --- if not train.drives_on or not train.max_speed then advtrains.update_trainpart_properties(id) end --TODO check for all vars to be present if not train.velocity then train.velocity=0 end if not train.movedir or (train.movedir~=1 and train.movedir~=-1) then train.movedir=1 end --- 2. prepare initial path and index if needed --- if not train.index then train.index=0 end if not train.path then if not train.last_pos then --no chance to recover atprint("train hasn't saved last-pos, removing train.") advtrains.trains[id]=nil return false end local node_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos), train.drives_on) if node_ok==nil then --block not loaded, do nothing atprint("last_pos", advtrains.round_vector_floor_y(train.last_pos), "not loaded and not in ndb, waiting") return nil elseif node_ok==false then atprint("no track here, (fail) removing train.") advtrains.trains[id]=nil return false end if not train.last_pos_prev then --no chance to recover atprint("train hasn't saved last-pos_prev, removing train.") advtrains.trains[id]=nil return false end local prevnode_ok=advtrains.get_rail_info_at(advtrains.round_vector_floor_y(train.last_pos_prev), train.drives_on) if prevnode_ok==nil then --block not loaded, do nothing atprint("last_pos_prev", advtrains.round_vector_floor_y(train.last_pos_prev), "not loaded and not in ndb, waiting") return nil elseif prevnode_ok==false then atprint("no track at prev, (fail) removing train.") advtrains.trains[id]=nil return false end train.index=(train.restore_add_index or 0)+(train.savedpos_off_track_index_offset or 0) --restore_add_index is set by save() to prevent trains hopping to next round index. should be between -0.5 and 0.5 --savedpos_off_track_index_offset is set if train went off track. see below. train.path={} train.path_dist={} train.path[0]=train.last_pos train.path[-1]=train.last_pos_prev train.path_dist[-1]=vector.distance(train.last_pos, train.last_pos_prev) train.path_extent_min=-1 train.path_extent_max=0 train.min_index_on_track=-1 train.max_index_on_track=0 --[[ Bugfix for trains randomly ignoring ATC rails: - Paths have been invalidated. 1 gets executed and ensures an initial path - 2a sets train end index. The problem is that path_dist is not known for the whole path, so train end index will be nearly trainlen - Since the detector indices are also unknown, they get set to the new (wrong) train_end_index. Enter_node calls are not executed for the nodes that lie in between real end_index and trainlen. - The next step, mistake is recognized, train leaves some positions. From there, everything works again. To overcome this, we will generate the full required path here so that path_dist is available for get_train_end_index(). ]] advtrains.pathpredict(id, train) end --- 2a. set train.end_index which is required in different places, IF IT IS NOT SET YET by STMT afterwards. --- --- table entry to avoid triple recalculation --- if not train.end_index then train.end_index=advtrains.get_train_end_index(train) end --- 2b. set node coverage old indices --- train.detector_old_index = math.floor(train.index) train.detector_old_end_index = math.floor(train.end_index) --- 3. handle velocity influences --- local train_moves=(train.velocity~=0) if train.recently_collided_with_env then train.tarvelocity=0 if not train_moves then train.recently_collided_with_env=false--reset status when stopped end end if train.locomotives_in_train==0 then train.tarvelocity=0 end --- 3a. this can be useful for debugs/warnings and is used for check_trainpartload --- local t_info, train_pos=sid(id), train.path[math.floor(train.index)] if train_pos then t_info=t_info.." @"..minetest.pos_to_string(train_pos) --atprint("train_pos:",train_pos) end --apply off-track handling: local front_off_track=train.max_index_on_track and train.index>train.max_index_on_track local back_off_track=train.min_index_on_track and train.end_index<train.min_index_on_track local pprint if front_off_track and back_off_track then--allow movement in both directions if train.tarvelocity>1 then train.tarvelocity=1 atwarn("Train",t_info,"is off track at both ends. Clipping velocity to 1") pprint=true end elseif front_off_track then--allow movement only backward if train.movedir==1 and train.tarvelocity>0 then train.tarvelocity=0 atwarn("Train",t_info,"is off track. Trying to drive further out. Velocity clipped to 0") pprint=true end if train.movedir==-1 and train.tarvelocity>1 then train.tarvelocity=1 atwarn("Train",t_info,"is off track. Velocity clipped to 1") pprint=true end elseif back_off_track then--allow movement only forward if train.movedir==-1 and train.tarvelocity>0 then train.tarvelocity=0 atwarn("Train",t_info,"is off track. Trying to drive further out. Velocity clipped to 0") pprint=true end if train.movedir==1 and train.tarvelocity>1 then train.tarvelocity=1 atwarn("Train",t_info,"is off track. Velocity clipped to 1") pprint=true end end if pprint then atprint("max_iot", train.max_index_on_track, "min_iot", train.min_index_on_track, "<> index", train.index, "end_index", train.end_index) end --interpret ATC command if train.atc_brake_target and train.atc_brake_target>=train.velocity then train.atc_brake_target=nil end if train.atc_wait_finish then if not train.atc_brake_target and train.velocity==train.tarvelocity then train.atc_wait_finish=nil end end if train.atc_command then if train.atc_delay<=0 and not train.atc_wait_finish then advtrains.atc.execute_atc_command(id, train) else train.atc_delay=train.atc_delay-dtime end end --make brake adjust the tarvelocity if necessary if train.brake and (math.ceil(train.velocity)-1)<train.tarvelocity then train.tarvelocity=math.max((math.ceil(train.velocity)-1), 0) end --- 3a. actually calculate new velocity --- if train.velocity~=train.tarvelocity then local applydiff=0 local mass=#train.trainparts local diff=train.tarvelocity-train.velocity if diff>0 then--accelerating, force will be brought on only by locomotives. --atprint("accelerating with default force") applydiff=(math.min((advtrains.train_accel_force*train.locomotives_in_train*dtime)/mass, math.abs(diff))) else--decelerating if front_off_track or back_off_track or train.recently_collided_with_env then --every wagon has a brake, so not divided by mass. --atprint("braking with emergency force") applydiff= -(math.min((advtrains.train_emerg_force*dtime), math.abs(diff))) elseif train.brake or (train.atc_brake_target and train.atc_brake_target<train.velocity) then --atprint("braking with default force") --no math.min, because it can grow beyond tarvelocity, see up there --dont worry, it will never fall below zero. applydiff= -((advtrains.train_brake_force*dtime)) else --atprint("roll") applydiff= -(math.min((advtrains.train_roll_force*dtime), math.abs(diff))) end end train.last_accel=(applydiff*train.movedir) train.velocity=math.min(math.max( train.velocity+applydiff , 0), train.max_speed or 10) else train.last_accel=0 end --- 4. move train --- train.index=train.index and train.index+(((train.velocity*train.movedir)/(train.path_dist[math.floor(train.index)] or 1))*dtime) or 0 --- 4a. update train.end_index to the new position --- train.end_index=advtrains.get_train_end_index(train) --- 5. extend path as necessary --- --why this is an extra function, see under 3. advtrains.pathpredict(id, train, true) --- 5a. make pos/yaw available for possible recover calls --- if train.max_index_on_track<train.index then --whoops, train went too far. the saved position will be the last one that lies on a track, and savedpos_off_track_index_offset will hold how far to go from here train.savedpos_off_track_index_offset=train.index-train.max_index_on_track train.last_pos=train.path[train.max_index_on_track] train.last_pos_prev=train.path[train.max_index_on_track-1] atprint("train is off-track (front), last positions kept at", train.last_pos, "/", train.last_pos_prev) elseif train.min_index_on_track+1>train.index then --whoops, train went even more far. same behavior train.savedpos_off_track_index_offset=train.index-train.min_index_on_track train.last_pos=train.path[train.min_index_on_track+1] train.last_pos_prev=train.path[train.min_index_on_track] atprint("train is off-track (back), last positions kept at", train.last_pos, "/", train.last_pos_prev) else --regular case train.savedpos_off_track_index_offset=nil train.last_pos=train.path[math.floor(train.index+0.5)] train.last_pos_prev=train.path[math.floor(train.index-0.5)] end --- 5b. Remove path items that are no longer used --- -- Necessary since path items are no longer invalidated in save steps local path_pregen_keep=20 local offtrack_keep=4 local gen_front_keep= path_pregen_keep local gen_back_keep= math.floor(- train.trainlen - path_pregen_keep) local delete_min=math.min(train.max_index_on_track - offtrack_keep, math.floor(train.index)+gen_back_keep) local delete_max=math.max(train.min_index_on_track + offtrack_keep, math.floor(train.index)+gen_front_keep) if train.path_extent_min<delete_min then atprint(sid(id),"clearing path min ",train.path_extent_min," to ",delete_min) for i=train.path_extent_min,delete_min-1 do train.path[i]=nil train.path_dist[i]=nil end train.path_extent_min=delete_min train.min_index_on_track=math.max(train.min_index_on_track, delete_min) end if train.path_extent_max>delete_max then atprint(sid(id),"clearing path max ",train.path_extent_max," to ",delete_max) train.path_dist[delete_max]=nil for i=delete_max+1,train.path_extent_max do train.path[i]=nil train.path_dist[i]=nil end train.path_extent_max=delete_max train.max_index_on_track=math.min(train.max_index_on_track, delete_max) end --- 6. update node coverage --- -- when paths get cleared, the old indices set above will be up-to-date and represent the state in which the last run of this code was made local ifo, ifn, ibo, ibn = train.detector_old_index, math.floor(train.index), train.detector_old_end_index, math.floor(train.end_index) local path=train.path if train.enter_node_all then --field set by create_new_train_at. --ensures that new train calls enter_node on all nodes for i=ibn, ifn do if path[i] then advtrains.detector.enter_node(path[i], id) end end train.enter_node_all=nil else for i=ibn, ifn do if path[i] then local pts=minetest.pos_to_string(path[i]) if not (advtrains.detector.on_node[pts] and advtrains.detector.on_node[pts]~=id) then advtrains.detector.stay_node(path[i], id) end end end if ifn>ifo then for i=ifo+1, ifn do if path[i] then local pts=minetest.pos_to_string(path[i]) if advtrains.detector.on_node[pts] and advtrains.detector.on_node[pts]~=id then --if another train has signed up for this position first, it won't be recognized in train_step_b. So do collision here. atprint("Collision detected in enter_node callbacks (front) @",pts,"with",sid(advtrains.detector.on_node[pts])) advtrains.collide_and_spawn_couple(id, path[i], advtrains.detector.on_node[pts], false) end atprint("enter_node (front) @index",i,"@",pts,"on_node",sid(advtrains.detector.on_node[pts])) advtrains.detector.enter_node(path[i], id) end end elseif ifn<ifo then for i=ifn+1, ifo do if path[i] then advtrains.detector.leave_node(path[i], id) end end end if ibn<ibo then for i=ibn, ibo-1 do if path[i] then local pts=minetest.pos_to_string(path[i]) if advtrains.detector.on_node[pts] and advtrains.detector.on_node[pts]~=id then --if another train has signed up for this position first, it won't be recognized in train_step_b. So do collision here. atprint("Collision detected in enter_node callbacks (back) @",pts,"on_node",sid(advtrains.detector.on_node[pts])) advtrains.collide_and_spawn_couple(id, path[i], advtrains.detector.on_node[pts], true) end atprint("enter_node (back) @index",i,"@",pts,"with",sid(advtrains.detector.on_node[pts])) advtrains.detector.enter_node(path[i], id) end end elseif ibn>ibo then for i=ibo, ibn-1 do if path[i] then advtrains.detector.leave_node(path[i], id) end end end end --- 7. do less important stuff --- --check for any trainpart entities if they have been unloaded. do this only if train is near a player, to not spawn entities into unloaded areas train.check_trainpartload=(train.check_trainpartload or 0)-dtime local node_range=(math.max((minetest.setting_get("active_block_range") or 0),1)*16) if train.check_trainpartload<=0 then local ori_pos=train_pos --see 3a. --atprint("[train "..id.."] at "..minetest.pos_to_string(vector.round(ori_pos))) local should_check=false for _,p in ipairs(minetest.get_connected_players()) do should_check=should_check or ((vector.distance(ori_pos, p:getpos())<node_range)) end if should_check then advtrains.update_trainpart_properties(id) end train.check_trainpartload=2 end --remove? if #train.trainparts==0 then atprint("[train "..sid(id).."] has empty trainparts, removing.")