Add: [Script] ScriptEventVehicleAutoReplaced.

This commit is contained in:
glx 2019-09-01 18:27:18 +02:00 committed by Niels Martin Hansen
parent cbefc1d994
commit 12e43c697d
9 changed files with 79 additions and 1 deletions

View File

@ -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);

View File

@ -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(Vehicle **single_unit, DoCommandFlag flags, b
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 **chain, DoCommandFlag flags, bool wagon
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 **chain, DoCommandFlag flags, bool wagon
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 */

View File

@ -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");

View File

@ -405,3 +405,19 @@ void SQAIEventRoadReconstruction_Register(Squirrel *engine)
SQAIEventRoadReconstruction.PostRegister(engine);
}
template <> const char *GetClassName<ScriptEventVehicleAutoReplaced, ST_AI>() { return "AIEventVehicleAutoReplaced"; }
void SQAIEventVehicleAutoReplaced_Register(Squirrel *engine)
{
DefSQClass<ScriptEventVehicleAutoReplaced, ST_AI> 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);
}

View File

@ -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

View File

@ -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");

View File

@ -55,6 +55,7 @@ public:
ET_GOAL_QUESTION_ANSWER,
ET_EXCLUSIVE_TRANSPORT_RIGHTS,
ET_ROAD_RECONSTRUCTION,
ET_VEHICLE_AUTOREPLACED,
};
/**

View File

@ -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 */

View File

@ -266,3 +266,12 @@ namespace SQConvert {
template <> inline const ScriptEventRoadReconstruction &GetParam(ForceType<const ScriptEventRoadReconstruction &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventRoadReconstruction *)instance; }
template <> inline int Return<ScriptEventRoadReconstruction *>(HSQUIRRELVM vm, ScriptEventRoadReconstruction *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "EventRoadReconstruction", res, nullptr, DefSQDestructorCallback<ScriptEventRoadReconstruction>, true); return 1; }
} // namespace SQConvert
namespace SQConvert {
/* Allow ScriptEventVehicleAutoReplaced to be used as Squirrel parameter */
template <> inline ScriptEventVehicleAutoReplaced *GetParam(ForceType<ScriptEventVehicleAutoReplaced *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptEventVehicleAutoReplaced *)instance; }
template <> inline ScriptEventVehicleAutoReplaced &GetParam(ForceType<ScriptEventVehicleAutoReplaced &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventVehicleAutoReplaced *)instance; }
template <> inline const ScriptEventVehicleAutoReplaced *GetParam(ForceType<const ScriptEventVehicleAutoReplaced *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptEventVehicleAutoReplaced *)instance; }
template <> inline const ScriptEventVehicleAutoReplaced &GetParam(ForceType<const ScriptEventVehicleAutoReplaced &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptEventVehicleAutoReplaced *)instance; }
template <> inline int Return<ScriptEventVehicleAutoReplaced *>(HSQUIRRELVM vm, ScriptEventVehicleAutoReplaced *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "EventVehicleAutoReplaced", res, nullptr, DefSQDestructorCallback<ScriptEventVehicleAutoReplaced>, true); return 1; }
} // namespace SQConvert