diff --git a/manual/luadoc/object/train.json b/manual/luadoc/object/train.json index 22e51b65..cd2777b9 100644 --- a/manual/luadoc/object/train.json +++ b/manual/luadoc/object/train.json @@ -5,5 +5,33 @@ "emergency_stop": {}, "powered": {}, "active": {}, - "mode": {} -} \ No newline at end of file + "mode": {}, + "on_block_entered": { + "parameters": [ + { + "name": "train" + }, + { + "name": "block" + }, + { + "name": "direction" + } + ], + "since": "0.3" + }, + "on_block_left": { + "parameters": [ + { + "name": "train" + }, + { + "name": "block" + }, + { + "name": "direction" + } + ], + "since": "0.3" + } +} diff --git a/server/src/board/tile/rail/blockrailtile.cpp b/server/src/board/tile/rail/blockrailtile.cpp index 4ba44b26..cbce043f 100644 --- a/server/src/board/tile/rail/blockrailtile.cpp +++ b/server/src/board/tile/rail/blockrailtile.cpp @@ -295,6 +295,8 @@ void BlockRailTile::inputItemValueChanged(BlockInputMapItem& item) trains.appendInternal(blockStatus); updateTrainMethodEnabled(); + train->blockEntered(*this, direction); + fireEvent( onTrainEntered, blockStatus->train.value(), @@ -366,11 +368,16 @@ void BlockRailTile::inputItemValueChanged(BlockInputMapItem& item) updateState(); + auto train = blockStatus->train.value(); + auto direction = blockStatus->direction.value(); + + train->blockLeft(*this, direction); + fireEvent( onTrainLeft, - blockStatus->train.value(), + train, shared_ptr(), - blockStatus->direction.value()); + direction); blockStatus->destroy(); #ifndef NDEBUG diff --git a/server/src/train/train.cpp b/server/src/train/train.cpp index b79a28c6..5cc6b8b3 100644 --- a/server/src/train/train.cpp +++ b/server/src/train/train.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2021,2023-2024 Reinder Feenstra + * Copyright (C) 2019-2024 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -146,6 +146,8 @@ Train::Train(World& world, std::string_view _id) : mode{this, "mode", TrainMode::ManualUnprotected, PropertyFlags::ReadWrite | PropertyFlags::StoreState | PropertyFlags::ScriptReadOnly}, blocks{*this, "blocks", {}, PropertyFlags::ReadOnly | PropertyFlags::StoreState}, notes{this, "notes", "", PropertyFlags::ReadWrite | PropertyFlags::Store} + , onBlockEntered{*this, "on_block_entered", EventFlags::Scriptable} + , onBlockLeft{*this, "on_block_left", EventFlags::Scriptable} { vehicles.setValueInternal(std::make_shared(*this, vehicles.name())); @@ -199,9 +201,30 @@ Train::Train(World& world, std::string_view _id) : Attributes::addDisplayName(notes, DisplayName::Object::notes); m_interfaceItems.add(notes); + m_interfaceItems.add(onBlockEntered); + m_interfaceItems.add(onBlockLeft); + updateEnabled(); } +void Train::blockEntered(BlockRailTile& block, BlockTrainDirection trainDirection) +{ + fireEvent( + onBlockEntered, + shared_ptr(), + block.shared_ptr(), + trainDirection); +} + +void Train::blockLeft(BlockRailTile& block, BlockTrainDirection trainDirection) +{ + fireEvent( + onBlockLeft, + shared_ptr(), + block.shared_ptr(), + trainDirection); +} + void Train::addToWorld() { IdObject::addToWorld(); diff --git a/server/src/train/train.hpp b/server/src/train/train.hpp index 188adf71..0f30835c 100644 --- a/server/src/train/train.hpp +++ b/server/src/train/train.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2021,2023 Reinder Feenstra + * Copyright (C) 2019-2024 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -25,7 +25,9 @@ #include "../core/idobject.hpp" #include +#include #include +#include "../core/event.hpp" #include "../core/method.hpp" #include "../core/objectproperty.hpp" #include "../core/objectvectorproperty.hpp" @@ -36,6 +38,7 @@ class TrainVehicleList; class TrainBlockStatus; +class BlockRailTile; class PoweredRailVehicle; class Train : public IdObject @@ -98,8 +101,13 @@ class Train : public IdObject //! If the train changes direction this list will be reversed. ObjectVectorProperty blocks; Property notes; + Event&, const std::shared_ptr&, BlockTrainDirection> onBlockEntered; + Event&, const std::shared_ptr&, BlockTrainDirection> onBlockLeft; Train(World& world, std::string_view _id); + + void blockEntered(BlockRailTile& block, BlockTrainDirection trainDirection); + void blockLeft(BlockRailTile& block, BlockTrainDirection trainDirection); }; #endif