(svn r12242) [NoAI] -Add: added AITileList_Industry(Accepting|Producing), giving tiles on which you want to build a station for an industry noai
authortruebrain
Sun, 24 Feb 2008 23:34:42 +0000
branchnoai
changeset 9757 5cdc14959fb6
parent 9756 7e637829cbd3
child 9758 f01f3df7cfbe
(svn r12242) [NoAI] -Add: added AITileList_Industry(Accepting|Producing), giving tiles on which you want to build a station for an industry
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/ai_squirrel.cpp
src/ai/api/ai_tilelist.cpp
src/ai/api/ai_tilelist.hpp
src/ai/api/ai_tilelist.hpp.sq
--- a/bin/ai/regression/regression.nut	Sun Feb 24 23:00:00 2008 +0000
+++ b/bin/ai/regression/regression.nut	Sun Feb 24 23:34:42 2008 +0000
@@ -255,6 +255,24 @@
 	}
 	print("  Valid Industries:    " + j);
 	print("  GetIndustryCount():  " + AIIndustry.GetIndustryCount());
+
+	local list = AITileList_IndustryAccepting(0, 3);
+	print("");
+	print("--TileList_IndustryAccepting--");
+	print("  Count():             " + list.Count());
+	print("  Location ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
+
+	local list = AITileList_IndustryProducing(1, 3);
+	print("");
+	print("--TileList_IndustryProducing--");
+	print("  Count():             " + list.Count());
+	print("  Location ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
 }
 
 function Regression::IndustryList()
--- a/bin/ai/regression/regression.txt	Sun Feb 24 23:00:00 2008 +0000
+++ b/bin/ai/regression/regression.txt	Sun Feb 24 23:34:42 2008 +0000
@@ -4502,6 +4502,153 @@
   Valid Industries:    69
   GetIndustryCount():  69
 
+--TileList_IndustryAccepting--
+  Count():             47
+  Location ListDump:
+    21490 => 0
+    21489 => 0
+    21488 => 0
+    21487 => 0
+    21486 => 0
+    21485 => 0
+    21484 => 0
+    21235 => 0
+    21234 => 0
+    21233 => 0
+    21232 => 0
+    21231 => 0
+    21230 => 0
+    21229 => 0
+    21228 => 0
+    20979 => 0
+    20978 => 0
+    20977 => 0
+    20976 => 0
+    20975 => 0
+    20974 => 0
+    20973 => 0
+    20972 => 0
+    20723 => 0
+    20722 => 0
+    20718 => 0
+    20717 => 0
+    20716 => 0
+    20467 => 0
+    20466 => 0
+    20462 => 0
+    20461 => 0
+    20460 => 0
+    20211 => 0
+    20210 => 0
+    20206 => 0
+    20205 => 0
+    20204 => 0
+    19955 => 0
+    19954 => 0
+    19950 => 0
+    19949 => 0
+    19948 => 0
+    19699 => 0
+    19698 => 0
+    19694 => 0
+    19693 => 0
+
+--TileList_IndustryProducing--
+  Count():             92
+  Location ListDump:
+    46920 => 0
+    46919 => 0
+    46918 => 0
+    46917 => 0
+    46916 => 0
+    46915 => 0
+    46914 => 0
+    46913 => 0
+    46912 => 0
+    46911 => 0
+    46664 => 0
+    46663 => 0
+    46662 => 0
+    46661 => 0
+    46660 => 0
+    46659 => 0
+    46658 => 0
+    46657 => 0
+    46656 => 0
+    46655 => 0
+    46408 => 0
+    46407 => 0
+    46406 => 0
+    46405 => 0
+    46404 => 0
+    46403 => 0
+    46402 => 0
+    46401 => 0
+    46400 => 0
+    46399 => 0
+    46152 => 0
+    46151 => 0
+    46150 => 0
+    46149 => 0
+    46146 => 0
+    46145 => 0
+    46144 => 0
+    46143 => 0
+    45896 => 0
+    45895 => 0
+    45894 => 0
+    45889 => 0
+    45888 => 0
+    45887 => 0
+    45640 => 0
+    45639 => 0
+    45638 => 0
+    45633 => 0
+    45632 => 0
+    45631 => 0
+    45384 => 0
+    45383 => 0
+    45382 => 0
+    45377 => 0
+    45376 => 0
+    45375 => 0
+    45128 => 0
+    45127 => 0
+    45126 => 0
+    45121 => 0
+    45120 => 0
+    45119 => 0
+    44872 => 0
+    44871 => 0
+    44870 => 0
+    44869 => 0
+    44868 => 0
+    44867 => 0
+    44866 => 0
+    44865 => 0
+    44864 => 0
+    44863 => 0
+    44616 => 0
+    44615 => 0
+    44614 => 0
+    44613 => 0
+    44612 => 0
+    44611 => 0
+    44610 => 0
+    44609 => 0
+    44608 => 0
+    44607 => 0
+    44360 => 0
+    44359 => 0
+    44358 => 0
+    44357 => 0
+    44356 => 0
+    44355 => 0
+    44354 => 0
+    44353 => 0
+    44352 => 0
+    44351 => 0
+
 --IndustryList--
   Count():             69
   Location ListDump:
--- a/src/ai/ai_squirrel.cpp	Sun Feb 24 23:00:00 2008 +0000
+++ b/src/ai/ai_squirrel.cpp	Sun Feb 24 23:34:42 2008 +0000
@@ -261,6 +261,8 @@
 	SQAITestMode_Register(this->engine);
 	SQAITile_Register(this->engine);
 	SQAITileList_Register(this->engine);
+	SQAITileList_IndustryAccepting_Register(this->engine);
+	SQAITileList_IndustryProducing_Register(this->engine);
 	SQAITileList_vBuildable_Register(this->engine);
 	SQAITileList_vBuildableRectangle_Register(this->engine);
 	SQAITileList_vCargoAcceptance_Register(this->engine);
--- a/src/ai/api/ai_tilelist.cpp	Sun Feb 24 23:00:00 2008 +0000
+++ b/src/ai/api/ai_tilelist.cpp	Sun Feb 24 23:34:42 2008 +0000
@@ -3,9 +3,14 @@
 /** @file ai_tilelist.cpp handles all functions of the AITileList class */
 
 #include "ai_tilelist.hpp"
+#include "ai_industry.hpp"
 
 #include "../../landscape.h"
+#include "../../settings_type.h"
+#include "../../station.h"
 #include "../../map_func.h"
+#include "../../tile_map.h"
+#include "../../industry_map.h"
 
 void AITileList::FixRectangleSpan(TileIndex &t1, TileIndex &t2)
 {
@@ -63,3 +68,48 @@
 
 	this->RemoveItem(tile);
 }
+
+AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_id, uint radius)
+{
+	if (!AIIndustry::IsValidIndustry(industry_id)) return;
+
+	const Industry *i = ::GetIndustry(industry_id);
+	const IndustrySpec *indsp = ::GetIndustrySpec(i->type);
+
+	/* Check if this industry accepts anything */
+	if (indsp->accepts_cargo[0] == CT_INVALID) return;
+
+	if (!_patches.modified_catchment) radius = CA_UNMODIFIED;
+
+	BEGIN_TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius)) {
+		/* Exclude all tiles that belong to this industry */
+		if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
+
+		/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
+		 *  industry triggers the acceptance). */
+		AcceptedCargo accepts;
+		::GetAcceptanceAroundTiles(accepts, cur_tile, 1, 1, radius);
+		if (accepts[indsp->accepts_cargo[0]] == 0) continue;
+
+		this->AddTile(cur_tile);
+	} END_TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius))
+}
+
+AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_id, uint radius)
+{
+	if (!AIIndustry::IsValidIndustry(industry_id)) return;
+
+	const Industry *i = ::GetIndustry(industry_id);
+	const IndustrySpec *indsp = ::GetIndustrySpec(i->type);
+
+	/* Check if this industry produces anything */
+	if (indsp->produced_cargo[0] == CT_INVALID) return;
+
+	if (!_patches.modified_catchment) radius = CA_UNMODIFIED;
+
+	BEGIN_TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius)) {
+		/* Exclude all tiles that belong to this industry */
+		if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
+		this->AddTile(cur_tile);
+	} END_TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius))
+}
--- a/src/ai/api/ai_tilelist.hpp	Sun Feb 24 23:00:00 2008 +0000
+++ b/src/ai/api/ai_tilelist.hpp	Sun Feb 24 23:34:42 2008 +0000
@@ -6,6 +6,7 @@
 #define AI_TILELIST_HPP
 
 #include "ai_abstractlist.hpp"
+#include "../../industry.h"
 
 /**
  * Class that creates a simple empty list of tiles which you can manipulate.
@@ -59,4 +60,42 @@
 	void RemoveTile(TileIndex tile);
 };
 
+/**
+ * Class that creates a list of tiles that will accept cargo for the given
+ *  industry.
+ * @note If a simular industry is close, it might happen that this industry
+ *  receives the cargo.
+ */
+class AITileList_IndustryAccepting : public AITileList {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITileList_IndustryAccepting"; }
+
+	/**
+	 * The constructor to make a list of tiles that will accept cargo for the
+	 *  given industry.
+	 */
+	AITileList_IndustryAccepting(IndustryID industry_id, uint radius);
+};
+
+/**
+ * Class that creates a list of tiles which the industry checks to see if a
+ *  station is there to receive cargo produced by this industry.
+ */
+class AITileList_IndustryProducing : public AITileList {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITileList_IndustryProducing"; }
+
+	/**
+	 * The constructor to make a list of tiles which the industry checks to see
+	 * if a station is there to receive cargo produced by this industry.
+	 */
+	AITileList_IndustryProducing(IndustryID industry_id, uint radius);
+};
+
 #endif /* AI_TILELIST_HPP */
--- a/src/ai/api/ai_tilelist.hpp.sq	Sun Feb 24 23:00:00 2008 +0000
+++ b/src/ai/api/ai_tilelist.hpp.sq	Sun Feb 24 23:34:42 2008 +0000
@@ -23,3 +23,41 @@
 
 	SQAITileList.PostRegister(engine);
 }
+
+namespace SQConvert {
+	/* Allow AITileList_IndustryAccepting to be used as Squirrel parameter */
+	template <> AITileList_IndustryAccepting *GetParam(ForceType<AITileList_IndustryAccepting *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_IndustryAccepting *)instance; }
+	template <> AITileList_IndustryAccepting &GetParam(ForceType<AITileList_IndustryAccepting &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_IndustryAccepting *)instance; }
+	template <> const AITileList_IndustryAccepting *GetParam(ForceType<const AITileList_IndustryAccepting *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_IndustryAccepting *)instance; }
+	template <> const AITileList_IndustryAccepting &GetParam(ForceType<const AITileList_IndustryAccepting &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_IndustryAccepting *)instance; }
+	template <> int Return<AITileList_IndustryAccepting *>(HSQUIRRELVM vm, AITileList_IndustryAccepting *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AITileList_IndustryAccepting", res, NULL, DefSQDestructorCallback<AITileList_IndustryAccepting>); return 1; }
+}; // namespace SQConvert
+
+void SQAITileList_IndustryAccepting_Register(Squirrel *engine) {
+	DefSQClass <AITileList_IndustryAccepting> SQAITileList_IndustryAccepting("AITileList_IndustryAccepting");
+	SQAITileList_IndustryAccepting.PreRegister(engine, "AITileList");
+	SQAITileList_IndustryAccepting.AddConstructor<void (AITileList_IndustryAccepting::*)(IndustryID industry_id, uint radius), 3>(engine, "xii");
+
+	SQAITileList_IndustryAccepting.DefSQStaticMethod(engine, &AITileList_IndustryAccepting::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileList_IndustryAccepting.PostRegister(engine);
+}
+
+namespace SQConvert {
+	/* Allow AITileList_IndustryProducing to be used as Squirrel parameter */
+	template <> AITileList_IndustryProducing *GetParam(ForceType<AITileList_IndustryProducing *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_IndustryProducing *)instance; }
+	template <> AITileList_IndustryProducing &GetParam(ForceType<AITileList_IndustryProducing &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_IndustryProducing *)instance; }
+	template <> const AITileList_IndustryProducing *GetParam(ForceType<const AITileList_IndustryProducing *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_IndustryProducing *)instance; }
+	template <> const AITileList_IndustryProducing &GetParam(ForceType<const AITileList_IndustryProducing &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_IndustryProducing *)instance; }
+	template <> int Return<AITileList_IndustryProducing *>(HSQUIRRELVM vm, AITileList_IndustryProducing *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AITileList_IndustryProducing", res, NULL, DefSQDestructorCallback<AITileList_IndustryProducing>); return 1; }
+}; // namespace SQConvert
+
+void SQAITileList_IndustryProducing_Register(Squirrel *engine) {
+	DefSQClass <AITileList_IndustryProducing> SQAITileList_IndustryProducing("AITileList_IndustryProducing");
+	SQAITileList_IndustryProducing.PreRegister(engine, "AITileList");
+	SQAITileList_IndustryProducing.AddConstructor<void (AITileList_IndustryProducing::*)(IndustryID industry_id, uint radius), 3>(engine, "xii");
+
+	SQAITileList_IndustryProducing.DefSQStaticMethod(engine, &AITileList_IndustryProducing::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileList_IndustryProducing.PostRegister(engine);
+}