# HG changeset patch # User glx # Date 2019-09-01 16:27:18 # Node ID 800799329701c7a474c084eac24d84ac2698ff5a # Parent 2fb81d4368be5e24333a51c4343c6ecbaedea983 Add: [Script] ScriptEventVehicleAutoReplaced. diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -146,6 +146,7 @@ void AIInstance::RegisterAPI() SQAIEventSubsidyOffer_Register(this->engine); SQAIEventSubsidyOfferExpired_Register(this->engine); SQAIEventTownFounded_Register(this->engine); + SQAIEventVehicleAutoReplaced_Register(this->engine); SQAIEventVehicleCrashed_Register(this->engine); SQAIEventVehicleLost_Register(this->engine); SQAIEventVehicleUnprofitable_Register(this->engine); diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -21,6 +21,7 @@ #include "core/random_func.hpp" #include "vehiclelist.h" #include "road.h" +#include "ai/ai.hpp" #include "table/strings.h" @@ -419,6 +420,8 @@ static CommandCost ReplaceFreeUnit(Vehic TransferCargo(old_v, new_v, false); *single_unit = new_v; + + AI::NewEvent(old_v->owner, new ScriptEventVehicleAutoReplaced(old_v->index, new_v->index)); } /* Sell the old vehicle */ @@ -587,7 +590,10 @@ static CommandCost ReplaceChain(Vehicle cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w))); if ((flags & DC_EXEC) != 0) { old_vehs[i] = nullptr; - if (i == 0) old_head = nullptr; + if (i == 0) { + AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index)); + old_head = nullptr; + } } } @@ -641,6 +647,8 @@ static CommandCost ReplaceChain(Vehicle if ((flags & DC_EXEC) != 0) { TransferCargo(old_head, new_head, true); *chain = new_head; + + AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index)); } /* Sell the old vehicle */ diff --git a/src/script/api/ai/ai_event.hpp.sq b/src/script/api/ai/ai_event.hpp.sq --- a/src/script/api/ai/ai_event.hpp.sq +++ b/src/script/api/ai/ai_event.hpp.sq @@ -50,6 +50,7 @@ void SQAIEvent_Register(Squirrel *engine SQAIEvent.DefSQConst(engine, ScriptEvent::ET_GOAL_QUESTION_ANSWER, "ET_GOAL_QUESTION_ANSWER"); SQAIEvent.DefSQConst(engine, ScriptEvent::ET_EXCLUSIVE_TRANSPORT_RIGHTS, "ET_EXCLUSIVE_TRANSPORT_RIGHTS"); SQAIEvent.DefSQConst(engine, ScriptEvent::ET_ROAD_RECONSTRUCTION, "ET_ROAD_RECONSTRUCTION"); + SQAIEvent.DefSQConst(engine, ScriptEvent::ET_VEHICLE_AUTOREPLACED, "ET_VEHICLE_AUTOREPLACED"); SQAIEvent.DefSQMethod(engine, &ScriptEvent::GetEventType, "GetEventType", 1, "x"); diff --git a/src/script/api/ai/ai_event_types.hpp.sq b/src/script/api/ai/ai_event_types.hpp.sq --- a/src/script/api/ai/ai_event_types.hpp.sq +++ b/src/script/api/ai/ai_event_types.hpp.sq @@ -405,3 +405,19 @@ void SQAIEventRoadReconstruction_Registe SQAIEventRoadReconstruction.PostRegister(engine); } + + +template <> const char *GetClassName() { return "AIEventVehicleAutoReplaced"; } + +void SQAIEventVehicleAutoReplaced_Register(Squirrel *engine) +{ + DefSQClass SQAIEventVehicleAutoReplaced("AIEventVehicleAutoReplaced"); + SQAIEventVehicleAutoReplaced.PreRegister(engine, "AIEvent"); + + SQAIEventVehicleAutoReplaced.DefSQStaticMethod(engine, &ScriptEventVehicleAutoReplaced::Convert, "Convert", 2, ".x"); + + SQAIEventVehicleAutoReplaced.DefSQMethod(engine, &ScriptEventVehicleAutoReplaced::GetOldVehicleID, "GetOldVehicleID", 1, "x"); + SQAIEventVehicleAutoReplaced.DefSQMethod(engine, &ScriptEventVehicleAutoReplaced::GetNewVehicleID, "GetNewVehicleID", 1, "x"); + + SQAIEventVehicleAutoReplaced.PostRegister(engine); +} diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -34,6 +34,7 @@ * \li AIEngine::CanRunOnRoad * \li AIEngine::HasPowerOnRoad * \li AIRoadTypeList::RoadTypeList + * \li AIEventVehicleAutoReplaced * * Other changes: * \li AITile::DemolishTile works without a selected company diff --git a/src/script/api/game/game_event.hpp.sq b/src/script/api/game/game_event.hpp.sq --- a/src/script/api/game/game_event.hpp.sq +++ b/src/script/api/game/game_event.hpp.sq @@ -50,6 +50,7 @@ void SQGSEvent_Register(Squirrel *engine SQGSEvent.DefSQConst(engine, ScriptEvent::ET_GOAL_QUESTION_ANSWER, "ET_GOAL_QUESTION_ANSWER"); SQGSEvent.DefSQConst(engine, ScriptEvent::ET_EXCLUSIVE_TRANSPORT_RIGHTS, "ET_EXCLUSIVE_TRANSPORT_RIGHTS"); SQGSEvent.DefSQConst(engine, ScriptEvent::ET_ROAD_RECONSTRUCTION, "ET_ROAD_RECONSTRUCTION"); + SQGSEvent.DefSQConst(engine, ScriptEvent::ET_VEHICLE_AUTOREPLACED, "ET_VEHICLE_AUTOREPLACED"); SQGSEvent.DefSQMethod(engine, &ScriptEvent::GetEventType, "GetEventType", 1, "x"); diff --git a/src/script/api/script_event.hpp b/src/script/api/script_event.hpp --- a/src/script/api/script_event.hpp +++ b/src/script/api/script_event.hpp @@ -55,6 +55,7 @@ public: ET_GOAL_QUESTION_ANSWER, ET_EXCLUSIVE_TRANSPORT_RIGHTS, ET_ROAD_RECONSTRUCTION, + ET_VEHICLE_AUTOREPLACED, }; /** diff --git a/src/script/api/script_event_types.hpp b/src/script/api/script_event_types.hpp --- a/src/script/api/script_event_types.hpp +++ b/src/script/api/script_event_types.hpp @@ -1060,4 +1060,44 @@ public: static ScriptEventRoadReconstruction *Convert(ScriptEventCompanyTown *instance) { return (ScriptEventRoadReconstruction *)instance; } }; +/** + * Event VehicleAutoReplaced, indicating a vehicle has been auto replaced. + * @api ai + */ +class ScriptEventVehicleAutoReplaced : public ScriptEvent { +public: + /** + * @param old_id The vehicle that has been replaced. + * @param new_id The vehicle that has been created in replacement. + */ + ScriptEventVehicleAutoReplaced(VehicleID old_id, VehicleID new_id) : + ScriptEvent(ET_VEHICLE_AUTOREPLACED), + old_id(old_id), + new_id(new_id) + {} + + /** + * Convert an ScriptEvent to the real instance. + * @param instance The instance to convert. + * @return The converted instance. + */ + static ScriptEventVehicleAutoReplaced *Convert(ScriptEvent *instance) { return (ScriptEventVehicleAutoReplaced *)instance; } + + /** + * Get the VehicleID of the vehicle that has been replaced. + * @return The VehicleID of the vehicle that has been replaced. This ID is no longer valid for referencing the vehicle. + */ + VehicleID GetOldVehicleID() { return this->old_id; } + + /** + * Get the VehicleID of the vehicle that has been created in replacement. + * @return The VehicleID of the vehicle that has been created in replacement. + */ + VehicleID GetNewVehicleID() { return this->new_id; } + +private: + VehicleID old_id; ///< The vehicle that has been replaced. + VehicleID new_id; ///< The vehicle that has been created in replacement. +}; + #endif /* SCRIPT_EVENT_TYPES_HPP */ diff --git a/src/script/api/template/template_event_types.hpp.sq b/src/script/api/template/template_event_types.hpp.sq --- a/src/script/api/template/template_event_types.hpp.sq +++ b/src/script/api/template/template_event_types.hpp.sq @@ -266,3 +266,12 @@ namespace SQConvert { template <> inline const ScriptEventRoadReconstruction &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventRoadReconstruction *)instance; } template <> inline int Return(HSQUIRRELVM vm, ScriptEventRoadReconstruction *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "EventRoadReconstruction", res, nullptr, DefSQDestructorCallback, true); return 1; } } // namespace SQConvert + +namespace SQConvert { + /* Allow ScriptEventVehicleAutoReplaced to be used as Squirrel parameter */ + template <> inline ScriptEventVehicleAutoReplaced *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptEventVehicleAutoReplaced *)instance; } + template <> inline ScriptEventVehicleAutoReplaced &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventVehicleAutoReplaced *)instance; } + template <> inline const ScriptEventVehicleAutoReplaced *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptEventVehicleAutoReplaced *)instance; } + template <> inline const ScriptEventVehicleAutoReplaced &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventVehicleAutoReplaced *)instance; } + template <> inline int Return(HSQUIRRELVM vm, ScriptEventVehicleAutoReplaced *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "EventVehicleAutoReplaced", res, nullptr, DefSQDestructorCallback, true); return 1; } +} // namespace SQConvert