(svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries noai
authortruebrain
Wed, 26 Mar 2008 15:17:40 +0000
branchnoai
changeset 9823 0b7f816cf46f
parent 9822 e2f83940bc52
child 9824 2c2a5a27c4eb
(svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
bin/ai/regression/run.sh
src/ai/ai_squirrel.cpp
src/ai/api/ai_event.hpp
src/ai/api/ai_event.hpp.sq
src/ai/api/ai_event_types.hpp
src/ai/api/ai_event_types.hpp.sq
src/economy.cpp
--- a/bin/ai/regression/regression.nut	Wed Mar 26 15:16:40 2008 +0000
+++ b/bin/ai/regression/regression.nut	Wed Mar 26 15:17:40 2008 +0000
@@ -337,6 +337,8 @@
 	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 	e = AIEventController.GetNextEvent();
 	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
+
+	AIEventController.EnableAllEvents();
 }
 
 function Regression::Industry()
@@ -1150,6 +1152,31 @@
 	this.Vehicle();
 	/* Order has to be after Vehicle */
 	this.Order();
+
+	/* Sleep now, to give time for events to happen */
+	Sleep(4000);
+
+	while (AIEventController.IsEventWaiting()) {
+		local e = AIEventController.GetNextEvent();
+		print("  GetNextEvent:          " + (e == null ? "null" : "instance"));
+		print("    GetEventType:        " + e.GetEventType());
+		switch (e.GetEventType()) {
+			case AIEvent.AI_ET_SUBSIDIARY_OFFER:
+				local c = AIEventSubsidiaryOffer.Convert(e);
+				print("      EventName:         SubsidiaryOffer");
+				print("      CargoID:           " + c.GetCargoID());
+				print("      GetFromTownID:     " + c.GetFromTownID());
+				print("      GetToTownID:       " + c.GetToTownID());
+				print("      GetFromIndustryID: " + c.GetFromIndustryID());
+				print("      GetToIndustryID:   " + c.GetToIndustryID());
+				break;
+
+			default:
+				print("      Unknown Event");
+				break;
+		}
+	}
+	print("  IsEventWaiting:        false");
 }
 
 function Regression::Stop()
--- a/bin/ai/regression/regression.txt	Wed Mar 26 15:16:40 2008 +0000
+++ b/bin/ai/regression/regression.txt	Wed Mar 26 15:17:40 2008 +0000
@@ -6303,3 +6303,12 @@
   Count():             1
   Location ListDump:
     11 => 33417
+  GetNextEvent:          instance
+    GetEventType:        3
+      EventName:         SubsidiaryOffer
+      CargoID:           0
+      GetFromTownID:     16
+      GetToTownID:       65535
+      GetFromIndustryID: 65535
+      GetToIndustryID:   0
+  IsEventWaiting:        false
--- a/bin/ai/regression/run.sh	Wed Mar 26 15:16:40 2008 +0000
+++ b/bin/ai/regression/run.sh	Wed Mar 26 15:17:40 2008 +0000
@@ -10,7 +10,7 @@
 params=""
 gdb=""
 if [ "$1" != "-r" ]; then
-	params="-snull -mnull -vnull:ticks=10000"
+	params="-snull -mnull -vnull:ticks=20000"
 fi
 if [ "$1" = "-g" ]; then
 	gdb="gdb --ex run --args "
--- a/src/ai/ai_squirrel.cpp	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/ai/ai_squirrel.cpp	Wed Mar 26 15:17:40 2008 +0000
@@ -226,6 +226,7 @@
 	SQAIEngineList_Register(this->engine);
 	SQAIEvent_Register(this->engine);
 	SQAIEventController_Register(this->engine);
+	SQAIEventSubsidiaryOffer_Register(this->engine);
 	SQAIEventTest_Register(this->engine);
 	SQAIEventVehicleCrash_Register(this->engine);
 	SQAIExecMode_Register(this->engine);
--- a/src/ai/api/ai_event.hpp	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/ai/api/ai_event.hpp	Wed Mar 26 15:17:40 2008 +0000
@@ -21,6 +21,7 @@
 		AI_ET_INVALID = 0,
 		AI_ET_TEST,
 		AI_ET_CRASHED_VEHICLE,
+		AI_ET_SUBSIDIARY_OFFER,
 	};
 
 	/**
--- a/src/ai/api/ai_event.hpp.sq	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/ai/api/ai_event.hpp.sq	Wed Mar 26 15:17:40 2008 +0000
@@ -18,9 +18,10 @@
 	SQAIEvent.PreRegister(engine);
 	SQAIEvent.AddConstructor<void (AIEvent::*)(AIEvent::AIEventType type), 2>(engine, "xi");
 
-	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_INVALID,         "AI_ET_INVALID");
-	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_TEST,            "AI_ET_TEST");
-	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_CRASHED_VEHICLE, "AI_ET_CRASHED_VEHICLE");
+	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_INVALID,          "AI_ET_INVALID");
+	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_TEST,             "AI_ET_TEST");
+	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_CRASHED_VEHICLE,  "AI_ET_CRASHED_VEHICLE");
+	SQAIEvent.DefSQConst(engine, AIEvent::AI_ET_SUBSIDIARY_OFFER, "AI_ET_SUBSIDIARY_OFFER");
 
 	SQAIEvent.DefSQStaticMethod(engine, &AIEvent::GetClassName, "GetClassName", 1, "x");
 
--- a/src/ai/api/ai_event_types.hpp	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/ai/api/ai_event_types.hpp	Wed Mar 26 15:17:40 2008 +0000
@@ -7,6 +7,8 @@
 
 #include "ai_object.hpp"
 #include "ai_event.hpp"
+#include "ai_town.hpp"
+#include "ai_industry.hpp"
 
 /**
  * A simple test event, to see if the event system is working. Triggered via
@@ -14,13 +16,10 @@
  */
 class AIEventTest : public AIEvent {
 public:
-	/**
-	 * The name of the class, needed by several sub-processes.
-	 */
 	static const char *GetClassName() { return "AIEventTest"; }
 
 	/**
-	 * Constructor for this event class.
+	 * @param test A test value.
 	 */
 	AIEventTest(uint test) :
 		AIEvent(AI_ET_TEST),
@@ -50,13 +49,11 @@
  */
 class AIEventVehicleCrash : public AIEvent {
 public:
-	/**
-	 * The name of the class, needed by several sub-processes.
-	 */
 	static const char *GetClassName() { return "AIEventVehicleCrash"; }
 
 	/**
-	 * Constructor for this event class.
+	 * @param vehicle The vehicle that crashed.
+	 * @param crash_site Where the vehicle crashed.
 	 */
 	AIEventVehicleCrash(VehicleID vehicle, TileIndex crash_site) :
 		AIEvent(AI_ET_CRASHED_VEHICLE),
@@ -70,7 +67,7 @@
 	static AIEventVehicleCrash *Convert(AIEvent *instance) { return (AIEventVehicleCrash *)instance; }
 
 	/**
-	 * Get the vehicleID of the crashed vehicle.
+	 * Get the VehicleID of the crashed vehicle.
 	 */
 	VehicleID GetVehicleID() { return vehicle; }
 
@@ -91,4 +88,71 @@
 	VehicleID vehicle;
 };
 
+/**
+ * A subsidiary is offered. You can get the type of cargo the subsidiary is
+ *  for, if it is between towns or industry, and from where to where (which
+ *  is either in TownID or IndustryID).
+ */
+class AIEventSubsidiaryOffer : public AIEvent {
+public:
+	static const char *GetClassName() { return "AIEventSubsidiaryOffer"; }
+
+	/**
+	 * @param cargo The cargo for the subsidiary.
+	 * @param from_town True if 'from' is a town, else it is an industry.
+	 * @param from Either TownID or IndustryID to move the cargo from.
+	 * @param to_town True if 'to' is a town, else it is an industry.
+	 * @param to Either TownID or IndustryID to move the cargo to.
+	 */
+	AIEventSubsidiaryOffer(CargoID cargo, bool from_town, uint32 from, bool to_town, uint32 to) :
+		AIEvent(AI_ET_SUBSIDIARY_OFFER),
+		cargo(cargo),
+		from_town(from_town),
+		from(from),
+		to_town(to_town),
+		to(to)
+	{}
+
+	/**
+	 * Convert an AIEvent to the real instance.
+	 */
+	static AIEventSubsidiaryOffer *Convert(AIEvent *instance) { return (AIEventSubsidiaryOffer *)instance; }
+
+	/**
+	 * Get the CargoID of the subsidiary.
+	 */
+	CargoID GetCargoID() { return cargo; }
+
+	/**
+	 * Get the TownID from where you need to move the cargo.
+	 * @return Either the TownID, or AITown::IsValidTown() returns false.
+	 */
+	TownID GetFromTownID() { return from_town ? from : -1; }
+
+	/**
+	 * Get the TownID to where you need to move the cargo.
+	 * @return Either the TownID, or AITown::IsValidTown() returns false.
+	 */
+	TownID GetToTownID() { return to_town ? to : -1; }
+
+	/**
+	 * Get the IndustryID from where you need to move the cargo.
+	 * @return Either the IndustryID, or AIIndustry::IsValidIndustry() returns false.
+	 */
+	IndustryID GetFromIndustryID() { return from_town ? -1 : from; }
+
+	/**
+	 * Get the IndustryID to where you need to move the cargo.
+	 * @return Either the IndustryID, or AIIndustry::IsValidIndustry() returns false.
+	 */
+	IndustryID GetToIndustryID() { return to_town ? -1 : to; }
+
+private:
+	CargoID cargo;
+	bool from_town;
+	uint32 from;
+	bool to_town;
+	uint32 to;
+};
+
 #endif /* AI_EVENT_TYPES_HPP */
--- a/src/ai/api/ai_event_types.hpp.sq	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/ai/api/ai_event_types.hpp.sq	Wed Mar 26 15:17:40 2008 +0000
@@ -45,3 +45,29 @@
 
 	SQAIEventVehicleCrash.PostRegister(engine);
 }
+
+namespace SQConvert {
+	/* Allow AIEventSubsidiaryOffer to be used as Squirrel parameter */
+	template <> AIEventSubsidiaryOffer *GetParam(ForceType<AIEventSubsidiaryOffer *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIEventSubsidiaryOffer *)instance; }
+	template <> AIEventSubsidiaryOffer &GetParam(ForceType<AIEventSubsidiaryOffer &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIEventSubsidiaryOffer *)instance; }
+	template <> const AIEventSubsidiaryOffer *GetParam(ForceType<const AIEventSubsidiaryOffer *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIEventSubsidiaryOffer *)instance; }
+	template <> const AIEventSubsidiaryOffer &GetParam(ForceType<const AIEventSubsidiaryOffer &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIEventSubsidiaryOffer *)instance; }
+	template <> int Return<AIEventSubsidiaryOffer *>(HSQUIRRELVM vm, AIEventSubsidiaryOffer *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIEventSubsidiaryOffer", res, NULL, DefSQDestructorCallback<AIEventSubsidiaryOffer>); return 1; }
+}; // namespace SQConvert
+
+void SQAIEventSubsidiaryOffer_Register(Squirrel *engine) {
+	DefSQClass <AIEventSubsidiaryOffer> SQAIEventSubsidiaryOffer("AIEventSubsidiaryOffer");
+	SQAIEventSubsidiaryOffer.PreRegister(engine, "AIEvent");
+	SQAIEventSubsidiaryOffer.AddConstructor<void (AIEventSubsidiaryOffer::*)(CargoID cargo, bool from_town, uint32 from, bool to_town, uint32 to), 6>(engine, "xibibi");
+
+	SQAIEventSubsidiaryOffer.DefSQStaticMethod(engine, &AIEventSubsidiaryOffer::GetClassName, "GetClassName", 1, "x");
+	SQAIEventSubsidiaryOffer.DefSQStaticMethod(engine, &AIEventSubsidiaryOffer::Convert,      "Convert",      2, "xx");
+
+	SQAIEventSubsidiaryOffer.DefSQMethod(engine, &AIEventSubsidiaryOffer::GetCargoID,        "GetCargoID",        1, "x");
+	SQAIEventSubsidiaryOffer.DefSQMethod(engine, &AIEventSubsidiaryOffer::GetFromTownID,     "GetFromTownID",     1, "x");
+	SQAIEventSubsidiaryOffer.DefSQMethod(engine, &AIEventSubsidiaryOffer::GetToTownID,       "GetToTownID",       1, "x");
+	SQAIEventSubsidiaryOffer.DefSQMethod(engine, &AIEventSubsidiaryOffer::GetFromIndustryID, "GetFromIndustryID", 1, "x");
+	SQAIEventSubsidiaryOffer.DefSQMethod(engine, &AIEventSubsidiaryOffer::GetToIndustryID,   "GetToIndustryID",   1, "x");
+
+	SQAIEventSubsidiaryOffer.PostRegister(engine);
+}
--- a/src/economy.cpp	Wed Mar 26 15:16:40 2008 +0000
+++ b/src/economy.cpp	Wed Mar 26 15:17:40 2008 +0000
@@ -1176,6 +1176,9 @@
 					s->age = 0;
 					pair = SetupSubsidyDecodeParam(s, 0);
 					AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
+					for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
+						AI_Event(i, new AIEventSubsidiaryOffer(s->cargo_type, s->cargo_type == CT_PASSENGERS, s->from, (GetCargo(s->cargo_type)->town_effect == TE_GOODS || GetCargo(s->cargo_type)->town_effect == TE_FOOD), s->to));
+					}
 					modified = true;
 					break;
 				}