(svn r9282) [NoAI] -Add: added AIAccounting, which tracks the cost of your buying/selling.
DoCommand no longer returns the cost, it returns a bool.
Costs are available via this Accounting class.
--- a/projects/openttd.vcproj Sun Mar 18 16:02:41 2007 +0000
+++ b/projects/openttd.vcproj Sun Mar 18 18:02:27 2007 +0000
@@ -1009,6 +1009,9 @@
Name="AI API"
Filter="">
<File
+ RelativePath=".\..\src\ai\api\ai_accounting.hpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_base.hpp">
</File>
<File
@@ -1046,6 +1049,9 @@
Name="AI API Implementation"
Filter="">
<File
+ RelativePath=".\..\src\ai\api\ai_accounting.cpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_base.cpp">
</File>
<File
--- a/projects/openttd_vs80.vcproj Sun Mar 18 16:02:41 2007 +0000
+++ b/projects/openttd_vs80.vcproj Sun Mar 18 18:02:27 2007 +0000
@@ -1564,6 +1564,10 @@
Name="AI API"
>
<File
+ RelativePath=".\..\src\ai\api\ai_accounting.hpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_base.hpp"
>
</File>
@@ -1612,6 +1616,10 @@
Name="AI API Implementation"
>
<File
+ RelativePath=".\..\src\ai\api\ai_accounting.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_base.cpp"
>
</File>
--- a/source.list Sun Mar 18 16:02:41 2007 +0000
+++ b/source.list Sun Mar 18 18:02:27 2007 +0000
@@ -308,6 +308,7 @@
ai/NoAI/NoAI.hpp
# AI API
+ai/api/ai_accounting.hpp
ai/api/ai_base.hpp
ai/api/ai_cargo.hpp
ai/api/ai_company.hpp
@@ -321,6 +322,7 @@
ai/api/ai_testmode.hpp
# AI API Implementation
+ai/api/ai_accounting.cpp
ai/api/ai_base.cpp
ai/api/ai_cargo.cpp
ai/api/ai_company.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_accounting.cpp Sun Mar 18 18:02:27 2007 +0000
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+/** @file ai_accounting.cpp everything to handle AI accounting things */
+
+#include "ai_accounting.hpp"
+
+int32 AIAccounting::GetCosts()
+{
+ return this->GetDoCommandCosts();
+}
+
+void AIAccounting::ResetCosts()
+{
+ this->SetDoCommandCosts(0);
+}
+
+AIAccounting::AIAccounting()
+{
+ this->last_costs = this->GetDoCommandCosts();
+ this->SetDoCommandCosts(0);
+}
+
+AIAccounting::~AIAccounting()
+{
+ this->SetDoCommandCosts(this->last_costs);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_accounting.hpp Sun Mar 18 18:02:27 2007 +0000
@@ -0,0 +1,60 @@
+/* $Id$ */
+
+/** @file ai_accounting.hpp everything to handle AI accounting things */
+
+#ifndef AI_ACCOUNTING_HPP
+#define AI_ACCOUNTING_HPP
+
+#include "ai_object.hpp"
+
+/**
+ * Class that handles all AI accounting related functions.
+ * Example:
+ * {
+ * local costs = AIAccounting();
+ * BuildRoad(from_here, to_here);
+ * BuildRoad(from_there, to_there);
+ * print("Costs for route is: " + costs.GetCosts());
+ * }
+ */
+class AIAccounting : public AIObject {
+private:
+ int32 last_costs;
+public:
+ /**
+ * Creating instance of this class starts counting the costs of commands
+ * from zero.
+ * @note when the instance is destroyed, he restores the costs that was
+ * current when the instance was created!
+ */
+ AIAccounting();
+
+ /**
+ * Destroying this instance reset the costs to the value it was
+ * in when the instance was created.
+ */
+ ~AIAccounting();
+
+ /**
+ * Get the current value of the costs.
+ */
+ int32 GetCosts();
+
+ /**
+ * Reset the costs to zero.
+ */
+ void ResetCosts();
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+void SQAIAccountingRegister(Squirrel *engine) {
+ DefSQClass <AIAccounting> SQAIAccounting("AIAccounting");
+ SQAIAccounting.PreRegister(engine);
+ SQAIAccounting.AddConstructor(engine);
+ SQAIAccounting.DefSQFunction(engine, &AIAccounting::GetCosts, "GetCosts");
+ SQAIAccounting.DefSQFunction(engine, &AIAccounting::ResetCosts, "ResetCosts");
+ SQAIAccounting.PostRegister(engine);
+}
+#endif /* SQUIRREL_CLASS */
+
+#endif /* AI_ACCOUNTING_HPP */
--- a/src/ai/api/ai_company.cpp Sun Mar 18 16:02:41 2007 +0000
+++ b/src/ai/api/ai_company.cpp Sun Mar 18 18:02:27 2007 +0000
@@ -62,15 +62,15 @@
/* When we get/repay everything at once (or the maximum we can repay),
* use the shortcut for that. Otherwise send several commands at once */
if (loan == 0) {
- return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN));
+ return this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN);
}
if (loan == this->GetMaxLoanAmount()) {
- return this->CmdSucceeded(this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN));
+ return this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN);
}
bool increase = loan > this->GetLoanAmount();
for (uint diff_loan = abs(loan - this->GetLoanAmount()) / this->GetLoanInterval(); diff_loan > 0; diff_loan--) {
- if (this->CmdFailed(this->DoCommand(0, 0, false, DC_EXEC, increase ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN))) return false;
+ if (!this->DoCommand(0, 0, false, DC_EXEC, increase ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN)) return false;
}
return true;
--- a/src/ai/api/ai_object.cpp Sun Mar 18 16:02:41 2007 +0000
+++ b/src/ai/api/ai_object.cpp Sun Mar 18 18:02:27 2007 +0000
@@ -29,14 +29,19 @@
return AIObject::GetDoCommandStruct(_current_player)->mode;
}
-bool AIObject::CmdFailed(int32 res)
+void AIObject::SetDoCommandCosts(int32 value)
{
- return ::CmdFailed(res);
+ AIObject::GetDoCommandStruct(_current_player)->costs = value;
}
-bool AIObject::CmdSucceeded(int32 res)
+void AIObject::IncreaseDoCommandCosts(int32 value)
{
- return !::CmdFailed(res);
+ AIObject::GetDoCommandStruct(_current_player)->costs += value;
+}
+
+int32 AIObject::GetDoCommandCosts()
+{
+ return AIObject::GetDoCommandStruct(_current_player)->costs;
}
AIObject::AIDoCommandStruct *AIObject::GetDoCommandStruct(PlayerID player)
@@ -51,13 +56,14 @@
for (int i = 0; i < MAX_PLAYERS; i++) {
command_struct[i].mode = NULL;
command_struct[i].delay = 1;
+ command_struct[i].costs = 0;
}
}
return &command_struct[player];
}
-int32 AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
+bool AIObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc)
{
PlayerID old_lp;
int32 res = 0;
@@ -69,10 +75,13 @@
/* First, do a test-run to see if we can do this */
res = ::DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc);
/* The command failed, so return */
- if (this->CmdFailed(res)) return res;
+ if (::CmdFailed(res)) return false;
/* Check what the callback wants us to do */
- if (this->GetDoCommandMode() != NULL && !this->GetDoCommandMode()(tile, p1, p2, flags, procc)) return res;
+ if (this->GetDoCommandMode() != NULL && !this->GetDoCommandMode()(tile, p1, p2, flags, procc)) {
+ this->IncreaseDoCommandCosts(res);
+ return true;
+ }
/* Restore _cmd_text */
_cmd_text = tmp_cmdtext;
@@ -106,5 +115,8 @@
AI_SuspendPlayer(_current_player, this->GetDoCommandDelay());
}
- return res;
+ if (::CmdFailed(res)) return false;
+
+ this->IncreaseDoCommandCosts(res);
+ return true;
}
--- a/src/ai/api/ai_object.hpp Sun Mar 18 16:02:41 2007 +0000
+++ b/src/ai/api/ai_object.hpp Sun Mar 18 18:02:27 2007 +0000
@@ -21,6 +21,7 @@
struct AIDoCommandStruct {
AIModeProc *mode;
uint delay;
+ int32 costs;
};
/**
@@ -32,17 +33,22 @@
/**
* Executes a raw DoCommand for the AI.
*/
- int32 DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
+ bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
/**
- * Checks if the result of a DoCommand went wrong.
+ * Sets the DoCommand costs counter to a value.
*/
- bool CmdFailed(int32 res);
+ void SetDoCommandCosts(int32 value);
/**
- * Checks if the result of a DoCommand went okay.
+ * Increase the current value of the DoCommand costs counter.
*/
- bool CmdSucceeded(int32 res);
+ void IncreaseDoCommandCosts(int32 value);
+
+ /**
+ * Get the current DoCommand costs counter.
+ */
+ int32 GetDoCommandCosts();
/**
* Set the current mode of your AI to this proc.
--- a/src/ai/api/ai_settings.hpp Sun Mar 18 16:02:41 2007 +0000
+++ b/src/ai/api/ai_settings.hpp Sun Mar 18 18:02:27 2007 +0000
@@ -9,7 +9,6 @@
/**
* Class that handles all AI settings related functions.
- * @note This isn't available from your AI.
*/
class AISettings : public AIObject {
public: