src/ai/api/ai_transactionmode.hpp
author truebrain
Thu, 24 Apr 2008 23:39:18 +0000
branchnoai
changeset 10339 ce6cd68d9eb8
parent 9843 29f34a179daf
permissions -rw-r--r--
(svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
-Note: this is perfectly safe; when a type changes, any sane compiler starts complaining about redefining the typedef to an other type
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     1
/* $Id$ */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     2
9829
80fbe02a4184 (svn r12491) [NoAI] -Documentation: made parts of the comments more uniform (@file header and class header)
truebrain
parents: 9629
diff changeset
     3
/** @file ai_transactionmode.hpp Switch the AI to Transaction Mode. */
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     4
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     5
#ifndef AI_TRANSACTIONMODE_HPP
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     6
#define AI_TRANSACTIONMODE_HPP
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     7
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     8
#include "ai_object.hpp"
10339
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 9843
diff changeset
     9
#include "../../command_type.h"
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    10
#include <queue>
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    11
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    12
/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    13
 * Class to switch current mode to Transaction mode.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    14
 * If you create an instance of this class, the mode will be switched to
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    15
 *   Transaction. The original mode is stored and recovered from when ever the
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    16
 *   instance is destroyed.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    17
 * In Transaction mode all commands will be tested, and if not fail, queued.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    18
 *   Later on you can execute a queue for real. The Transaction keeps on
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    19
 *   recording all commands till it is destructed or execute or stopped.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    20
 * On execute the transaction can return false, because maps change over time.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    21
 *   If this happens you can use the rollback feature to remove all already
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    22
 *   built things.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    23
 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    24
class AITransactionMode : public AIObject {
9829
80fbe02a4184 (svn r12491) [NoAI] -Documentation: made parts of the comments more uniform (@file header and class header)
truebrain
parents: 9629
diff changeset
    25
public:
80fbe02a4184 (svn r12491) [NoAI] -Documentation: made parts of the comments more uniform (@file header and class header)
truebrain
parents: 9629
diff changeset
    26
	static const char *GetClassName() { return "AITransactionMode"; }
80fbe02a4184 (svn r12491) [NoAI] -Documentation: made parts of the comments more uniform (@file header and class header)
truebrain
parents: 9629
diff changeset
    27
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    28
private:
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    29
	struct AITransactionModeCommand {
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    30
		TileIndex tile;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    31
		uint32 p1;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    32
		uint32 p2;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    33
		uint procc;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    34
		char *text;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    35
	};
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    36
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    37
	AIModeProc *last_mode;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    38
	AIObject *last_instance;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    39
	std::queue<AITransactionModeCommand> command_stack;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    40
	std::queue<AITransactionModeCommand> reverse_stack;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    41
	bool stopped;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9596
diff changeset
    42
	CommandCost costs;
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    43
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    44
protected:
9475
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    45
	/**
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    46
	 * The callback proc for Transaction mode.
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    47
	 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9596
diff changeset
    48
	static bool ModeProc(TileIndex tile, uint32 p1, uint32 p2, uint procc, CommandCost costs);
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    49
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    50
public:
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    51
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    52
	 * Creating instance of this class switches the build mode to Transaction.
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
    53
	 * @note When the instance is destroyed, he restores the mode that was
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    54
	 *   current when the instance was created!
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    55
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    56
	AITransactionMode();
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    57
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    58
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    59
	 * Destroying this instance reset the building mode to the mode it was
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    60
	 *   in when the instance was created.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    61
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    62
	~AITransactionMode();
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    63
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    64
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    65
	 * Execute all recorded commands.
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
    66
	 * @return False if any command recorded failed to execute. All other
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    67
	 *   commands that follow won't be executed either.
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
    68
	 * @note When Execute() is called, the transaction is stopped (like calling
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    69
	 *   Stop() yourself).
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    70
	 * @note Execute() is always executed, no matter what mode you gave it in
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    71
	 *   the outside world. It forces itself into executing it for real. To add
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    72
	 *   it to an other list, use Append().
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    73
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    74
	bool Execute();
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    75
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    76
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    77
	 * Stop recording the commands and switch to the last mode, like the
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    78
	 *   instance was destroyed.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    79
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    80
	void Stop();
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    81
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    82
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    83
	 * Rollbacks rolls the whole transaction back in case Execute() returned
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    84
	 *   false. In case Execute() returned true, there won't be anything to roll
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    85
	 *   back, so this command will do nothing.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    86
	 * It rolls back all commands by looking up the reverse of every command
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    87
	 *   issued and executes that. Of course it can happen that even that fails.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    88
	 *   In that case the problem will be silently ignored.
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
    89
	 * @note As you might want to get a costs estimate about the rollback first
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    90
	 *   you need to make sure you set the right mode yourself!
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
    91
	 * @note This command isn't finished yet!!
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    92
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    93
	void Rollback();
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    94
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    95
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    96
	 * Get the costs it takes to execute this transaction (on average, real
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    97
	 *   numbers can always differ).
9843
29f34a179daf (svn r12509) [NoAI] -Fix: add some missing return/parameter documentation; probably not the last, but Doxygen couldn't find these.
rubidium
parents: 9839
diff changeset
    98
	 * @return The costs of this transaction.
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    99
	 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9596
diff changeset
   100
	Money GetCosts();
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   101
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   102
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   103
	 * Append one transaction list to an other.
9839
ea94d60d13e7 (svn r12505) [NoAI] -Documentation: still forgot some files ;) (minor stuff, capitals mostly)
truebrain
parents: 9829
diff changeset
   104
	 * @param transaction The list that will be appended after the instance you call append on.
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   105
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   106
	void Append(AITransactionMode *transaction);
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   107
};
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   108
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   109
#endif /* AI_TRANSACTIONMODE_HPP */