aboutsummaryrefslogtreecommitdiff
path: root/advtrains/trackplacer.lua
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/trackplacer.lua')
-rw-r--r--advtrains/trackplacer.lua25
1 files changed, 20 insertions, 5 deletions
diff --git a/advtrains/trackplacer.lua b/advtrains/trackplacer.lua
index eebf2ae..b24103a 100644
--- a/advtrains/trackplacer.lua
+++ b/advtrains/trackplacer.lua
@@ -129,7 +129,7 @@ function tp.bend_rail(originpos, conn, nnpref)
return false
end
end
-function tp.placetrack(pos, nnpref)
+function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing)
--1. find all rails that are likely to be connected
local tr=tp.tracks[nnpref]
local p_rails={}
@@ -140,9 +140,16 @@ function tp.placetrack(pos, nnpref)
end
if #p_rails==0 then
minetest.set_node(pos, {name=nnpref.."_"..tr.default})
+ if minetest.registered_nodes[nnpref.."_"..tr.default] and minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node then
+ minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node(pos, placer, itemstack, pointed_thing)
+ end
elseif #p_rails==1 then
tp.bend_rail(pos, p_rails[1], nnpref)
minetest.set_node(pos, tr.single_conn[p_rails[1]])
+ local nname=tr.single_conn[p_rails[1]].name
+ if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
+ minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
+ end
else
--iterate subsets
for k1, conn1 in ipairs(p_rails) do
@@ -152,6 +159,10 @@ function tp.placetrack(pos, nnpref)
tp.bend_rail(pos, conn1, nnpref)
tp.bend_rail(pos, conn2, nnpref)
minetest.set_node(pos, tr.double_conn[conn1.."_"..conn2])
+ local nname=tr.double_conn[conn1.."_"..conn2].name
+ if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
+ minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
+ end
return
end
end
@@ -160,6 +171,10 @@ function tp.placetrack(pos, nnpref)
--not found
tp.bend_rail(pos, p_rails[1], nnpref)
minetest.set_node(pos, tr.single_conn[p_rails[1]])
+ local nname=tr.single_conn[p_rails[1]].name
+ if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then
+ minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing)
+ end
end
end
@@ -183,7 +198,7 @@ function tp.register_track_placer(nnprefix, imgprefix, dispname)
end
if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to
and minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable then
- tp.placetrack(pos, nnprefix)
+ tp.placetrack(pos, nnprefix, placer, itemstack, pointed_thing)
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
@@ -230,7 +245,7 @@ minetest.register_craftitem("advtrains:trackworker",{
local modext=tp.tracks[nnprefix].twrotate[suffix]
if rotation==modext[#modext] then --increase param2
- minetest.set_node(pos, {name=nnprefix.."_"..suffix..modext[1], param2=(node.param2+1)%4})
+ minetest.swap_node(pos, {name=nnprefix.."_"..suffix..modext[1], param2=(node.param2+1)%4})
return
else
local modpos
@@ -239,7 +254,7 @@ minetest.register_craftitem("advtrains:trackworker",{
minetest.chat_send_player(placer:get_player_name(), "This node can't be rotated using the trackworker!")
return
end
- minetest.set_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2})
+ minetest.swap_node(pos, {name=nnprefix.."_"..suffix..modext[modpos+1], param2=node.param2})
end
advtrains.invalidate_all_paths()
end
@@ -269,7 +284,7 @@ minetest.register_craftitem("advtrains:trackworker",{
end
end
local nextsuffix=tp.tracks[nnprefix].twcycle[suffix]
- minetest.set_node(pos, {name=nnprefix.."_"..nextsuffix..rotation, param2=node.param2})
+ minetest.swap_node(pos, {name=nnprefix.."_"..nextsuffix..rotation, param2=node.param2})
--invalidate trains
advtrains.invalidate_all_paths()
else
id='n279' href='#n279'>279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
-- environment_ers.lua
if S.trains == nil then S.trains = {} end
if S.train_duration == nil then S.train_duration = {} end
if S.yards == nil then S.yards = {} end

F.known_rcs = {
  ["LILSHUNTER"] = "LILSHUNTER",
  ["CRYSTAL"] = "CRYSTAL",
  ["DEPOTUNLOADING"] = "DEPOTUNLOADING",
  ["ERSBALLASTLOAD"] = "ERSBALLASTLOAD",
  ["CORNUNLOAD"] = "CORNUNLOAD",
  ["MEGACORNLOAD"] = "MEGACORNLOAD",
  ["XERXES"] = "XERXES",
  ["BLOCK"] = "BLOCK",
  ["FERTILIZER"] = "FERTILIZER",
  ["FERTRUNNER"] = "FERTRUNNER",
  ["MULCH"] = "MULCH",
  ["ERSTAZIDEPOTUNLOAD"] = "ERSTAZIDEPOTUNLOAD",
  ["ERSTAZIDEPOT"] = "ERSTAZIDEPOT",
  ["ERSSINENSISDEPOT"] = "ERSSINENSISDEPOT",
  ["ERSTAZITST"] = "ERSTAZITST",
  ["FACTORY"] = "FACTORY",
  ["OILEXTRACT"] = "OILEXTRACT",
  ["SHUNT04A"] = "SHUNT04A",
  ["ERSTAZISHOP"] = "ERSTAZISHOP",
  ["TYARD"] = "TYARD",
  ["TY_COLLECT_SINESIS"] = "TY_COLLECT_SINESIS",
  ["TY_RTS"] = "TY_RTS",
  ["FAREAST"] = "FAREAST",
  ["KLYARD"] = "KLYARD",
}

F.print = function (str) if F.debug then print("".. (str or "nil") ) end end
F.isempty = function (s) return s == nil or s == "" end
F.get_rc_safe = function() return get_rc() or "" end
F.get_line_safe = function() return get_line() or "" end
F.get_train_length_safe = function() return train_length() or 0 end
F.avg = function(t)
  local sum = 0
  local count = 0
  for k,v in pairs(t) do
    if type(v) == "number" then
      sum = sum + v
      count = count + 1
    end
  end
  return (sum / count)
end

if event.init then
  F.debug = true
  F.max_displays = 52
  F.print("Initialized")
end

F.clear_main_depot_displays = function() for i = 1, F.max_displays, 1 do digiline_send("train_display" .. i, " ") end end

--[[
  EXAMPLE: F.has_rc("LILSHUNTER", F.get_rc_safe() )
  Merged F.has_rc and F.does_train_have_rc
  F.does_train_have_rc is deprecated
]]
F.has_rc = function(query,rc_list) -- query = string, single entry
  for word in rc_list:gmatch("[^%s]+") do
    if word == query then return true end
  end
  return false
end

F.send_route = function(passive_name, route, show_print)
  local message = ""
  local return_value = false
  if can_set_route(passive_name, route) then
    set_route(passive_name, route)
    message = passive_name .. " has been set to " .. route
    return_value = true
  else
    message = route .. " cannot be set for " .. passive_name .. ". Try another."
    return_value = false
  end
  if show_print == true then F.print(message) end
  return return_value
end

F.save_train = function()
  if not atc_id then return end
  if S.trains then
    S.trains[atc_id] = { ["id"] = atc_id, ["rc"] = F.get_rc_safe(), ["ln"] = F.get_line_safe(), ["cars_count"] = F.get_train_length_safe() }
  end
end

F.get_real_split_count = function(train_length_count, split_count)
  if split_count then
    if split_count == "all" then
      return 2
    else
      F.print("train_length_count (" .. train_length_count .. ") - split_count (" .. split_count .. ")")
      train_length_count = train_length_count + 1
      split_count = train_length_count - split_count
      return split_count
    end
  else
    return nil
  end
end

F.yard_siding_basic = function(yard, name, place)
  if event.train then
    if not atc_id then return end
    F.save_train()
    if place == "start" then
      if atc_arrow then
        atc_send("S5")
      else
        atc_send("B5S5")
      end
    elseif place == "end_alt" then
      do_something = false
    elseif place == "end" then
      if atc_arrow then
        atc_send("B0 W R")
      end
    else
      F.print("Place for " .. name .. " has not been defined")
    end
  end
end

F.yard_set_route = function(yard, train_id, atc_command)
  if atc_command == nil then atc_command = "A1 S5" end
  if S.yards[yard] and S.yards[yard]["state"] and S.yards[yard]["actions"] then
    status = S.yards[yard]["state"]["status"]
    current_sequence = S.yards[yard]["state"]["current"]
    current_operation = S.yards[yard]["actions"][current_sequence]["operation"]
    table_count = table.maxn(S.yards[yard]["actions"])
    next_sequence = current_sequence + 1
    -- F.print(next_sequence .. "/" .. table_count)
    if next_sequence <= table_count and S.yards[yard]["actions"][next_sequence] and  S.yards[yard]["actions"][next_sequence]["route"] then
      next_route = S.yards[yard]["actions"][next_sequence]["route"]
      next_operation = S.yards[yard]["actions"][next_sequence]["operation"]
      use_this_signal = S.yards[yard]["actions"][next_sequence]["signal"]
      atc_send_status = atc_send_to_train(train_id, atc_command)
      if atc_send_status then
        if use_this_signal == nil then
          F.print(train_id .. " is waiting at: nil")
          --[[
            Maybe change it so it shows which signal it is
            waiting at when completed with this message:
            F.print(train_id .. " has finished the sequences and is waiting.")
          ]]
        else
          can_set_route_response = can_set_route(use_this_signal, next_route)
          if can_set_route_response then
            -- set_train_length_count = S.yards[yard]["state"]["train_length_count"]
            -- S.yards[yard]["state"] = { status = "moving", current = next_sequence, train_length_count = set_train_length_count }
            set_route(use_this_signal, next_route)
            F.print("DIRECT COMMAND current_operation: " .. current_operation .. " next_operation: " .. next_operation)
            F.print(train_id .. " is heading to " .. next_route .. " through " .. use_this_signal)
          else
            F.print(train_id .. " cannot go to " .. next_route .. " at " .. use_this_signal)
          end
        end
      else
        F.print(atc_command .. " command failed")
      end
    else
      F.print("End of Operations for " .. train_id)
    end
  end
end

F.yard_siding_next_operation = function(yard, train_id, current_sequence)
  if S.yards[yard]["state"] then
    if current_sequence == nil then current_sequence = S.yards[yard]["state"]['current'] end
    local next_sequence = current_sequence + 1
    local set_train_length_count = S.yards[yard]["state"]["train_length_count"]
    local table_count = table.maxn(S.yards[yard]["actions"])
    F.print(next_sequence .. "/" .. table_count)
    S.yards[yard]["state"]["current"] = next_sequence
  end
end

F.yard_siding_operations_start = function(yard, train_id, place, current_operation, next_operation, current_split_count)
  -- F.print("THIS IS " .. place)
  if atc_arrow then
    if current_operation == "split_at_engine" then
      unset_autocouple()
      -- split_off_locomotive("A0B0")
      split_at_index(current_split_count, "S0")
      F.yard_set_route(yard, train_id, atc_command)
      F.yard_siding_next_operation(yard, train_id)
    -- elseif current_operation == "reverse" then
      -- unset_autocouple()
      -- atc_send("B0 W R S5")
    elseif next_operation == "split_at_engine" then
      F.print("MAYBE SPLIT HERE AT " .. place .. " current: " .. current_operation .. " next: " .. next_operation)
      F.yard_set_route(yard, train_id, atc_command)
      F.yard_siding_next_operation(yard, train_id)
    else