aboutsummaryrefslogtreecommitdiff
path: root/advtrains/models/advtrains_dtrack_cr_30.b3d
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/models/advtrains_dtrack_cr_30.b3d')
0 files changed, 0 insertions, 0 deletions
2'>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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
--atc.lua
--registers and controls the ATC system

local atc={}
-- ATC persistence table
atc.controllers = {}
--contents: {command="...", arrowconn=0-15 where arrow points}

advtrains.fpath_atc=minetest.get_worldpath().."/advtrains_atc"
local file, err = io.open(advtrains.fpath_atc, "r")
if not file then
	local er=err or "Unknown Error"
	atprint("[advtrains]Failed loading advtrains atc save file "..er)
else
	local tbl = minetest.deserialize(file:read("*a"))
	if type(tbl) == "table" then
		atc.controllers=tbl.controllers
	end
	file:close()
end
function atc.save()
	--leave space for more save data.
	local datastr = minetest.serialize({controllers = atc.controllers})
	if not datastr then
		minetest.log("error", "[advtrains] Failed to serialize trackdb data!")
		return
	end
	local file, err = io.open(advtrains.fpath_atc, "w")
	if err then
		return err
	end
	file:write(datastr)
	file:close()
end

--call from advtrains.detector subprogram

function atc.trigger_controller_train_enter(pos, train_id)
	atc.send_command(pos)
end

--general

function atc.send_command(pos)
	local pts=minetest.pos_to_string(pos)
	if atc.controllers[pts] then
		atprint("Called send_command at "..pts)
		local train_id = advtrains.detector.on_node[pts]
		if train_id then
			if advtrains.trains[train_id] then
				atprint("send_command inside if: "..sid(train_id))
				atc.train_reset_command(train_id)
				local arrowconn=atc.controllers[pts].arrowconn
				local train=advtrains.trains[train_id]
				for index, ppos in pairs(train.path) do
					if vector.equals(advtrains.round_vector_floor_y(ppos), pos) then
						advtrains.trains[train_id].atc_arrow =
								vector.equals(
										advtrains.dirCoordSet(pos, arrowconn),
										advtrains.round_vector_floor_y(train.path[index+train.movedir])
								)
						advtrains.trains[train_id].atc_command=atc.controllers[pts].command
						atprint("Sending ATC Command: "..atc.controllers[pts].command)
					end
				end
			end
		end
	end
	return false
end

function atc.train_reset_command(train_id)
	advtrains.trains[train_id].atc_command=nil
	advtrains.trains[train_id].atc_delay=0
	advtrains.trains[train_id].atc_brake_target=nil
	advtrains.trains[train_id].atc_wait_finish=nil
	advtrains.trains[train_id].atc_arrow=nil
end

--nodes
local idxtrans={static=1, mesecon=2, digiline=3}
local apn_func=function(pos)
	advtrains.reset_trackdb_position(pos)
	local meta=minetest.get_meta(pos)
	if meta then
		meta:set_string("infotext", "ATC controller, unconfigured.")
		meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
	end
end

advtrains.register_tracks("default", {
	nodename_prefix="advtrains:dtrack_atc",
	texture_prefix="advtrains_dtrack_atc",
	models_prefix="advtrains_dtrack_detector",
	models_suffix=".b3d",
	shared_texture="advtrains_dtrack_rail_atc.png",
	description="ATC controller",
	formats={},
	get_additional_definiton = function(def, preset, suffix, rotation)
		return {
			after_place_node=apn_func,
			on_place_rail=apn_func,
			after_dig_node=function(pos)
				advtrains.invalidate_all_paths()
				advtrains.reset_trackdb_position(pos)
				local pts=minetest.pos_to_string(pos)
				atc.controllers[pts]=nil
			end,
			on_receive_fields = function(pos, formname, fields, player)
				if minetest.is_protected(pos, player:get_player_name()) then
					minetest.chat_send_player(player:get_player_name(), "This position is protected!")
					return
				end
				local meta=minetest.get_meta(pos)
				if meta then
					if not fields.save then 
						--maybe only the dropdown changed
						if fields.mode then
							meta:set_string("mode", idxtrans[fields.mode])
							meta:set_string("infotext", "ATC controller, mode "..fields.mode.."\n"..( fields.mode=="digiline" and "Channel: "..meta:get_string("channel") or "Command: "..meta:get_string("command") ) )
							meta:set_string("formspec", atc.get_atc_controller_formspec(pos, meta))
						end
						return
					end
					meta:set_string("mode", idxtrans[fields.mode])
					meta:set_string("command", fields.command)
					meta:set_string("command_on", fields.command_on)
					meta:set_string("channel", fields.channel)