src/ai/api/ai_transactionmode.hpp
author truebrain
Wed, 26 Mar 2008 15:17:40 +0000
branchnoai
changeset 9823 0b7f816cf46f
parent 9629 66dde6412125
child 9829 80fbe02a4184
permissions -rw-r--r--
(svn r12431) [NoAI] -Add: added AIEventSubsidiaryOffer, which keeps you informed about new Subsidiaries
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
9595
91423dbb6f5f (svn r9628) [NoAI] -Fix: missing svn:eol-style and svn:keywords
rubidium
parents: 9594
diff changeset
     3
/** @file ai_transactionmode.hpp class to 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"
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
     9
#include <queue>
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    10
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
 * 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
    13
 * 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
    14
 *   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
    15
 *   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
    16
 * 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
    17
 *   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
    18
 *   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
    19
 * 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
    20
 *   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
    21
 *   built things.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    22
 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    23
class AITransactionMode : public AIObject {
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    24
private:
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    25
	struct AITransactionModeCommand {
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    26
		TileIndex tile;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    27
		uint32 p1;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    28
		uint32 p2;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    29
		uint procc;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    30
		char *text;
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    31
	};
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    32
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    33
	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
    34
	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
    35
	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
    36
	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
    37
	bool stopped;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9596
diff changeset
    38
	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
    39
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    40
protected:
9475
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    41
	/**
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    42
	 * The callback proc for Transaction mode.
58c20c0e394f (svn r9320) [NoAI] -Fix: added some doxygen comments to make doxygen happy
truelight
parents: 9474
diff changeset
    43
	 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9596
diff changeset
    44
	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
    45
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    46
public:
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    47
	/**
9529
5f26f4bc574b (svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
truelight
parents: 9527
diff changeset
    48
	 * The name of the class, needed by several sub-processes.
5f26f4bc574b (svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
truelight
parents: 9527
diff changeset
    49
	 */
5f26f4bc574b (svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
truelight
parents: 9527
diff changeset
    50
	static const char *GetClassName() { return "AITransactionMode"; }
5f26f4bc574b (svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
truelight
parents: 9527
diff changeset
    51
5f26f4bc574b (svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
truelight
parents: 9527
diff changeset
    52
	/**
9474
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    53
	 * Creating instance of this class switches the build mode to Transaction.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    54
	 * @note when the instance is destroyed, he restores the mode that was
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    55
	 *   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
    56
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    57
	AITransactionMode();
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
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    60
	 * 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
    61
	 *   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
    62
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    63
	~AITransactionMode();
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
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    66
	 * Execute all recorded commands.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    67
	 * @return false if any command recorded failed to execute. All other
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    68
	 *   commands that follow won't be executed either.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    69
	 * @note when Execute() is called, the transaction is stopped (like calling
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    70
	 *   Stop() yourself).
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    71
	 * @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
    72
	 *   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
    73
	 *   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
    74
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    75
	bool Execute();
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
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    78
	 * 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
    79
	 *   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
    80
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    81
	void Stop();
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
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    84
	 * 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
    85
	 *   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
    86
	 *   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
    87
	 * 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
    88
	 *   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
    89
	 *   In that case the problem will be silently ignored.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    90
	 * @note as you might want to get a costs estimate about the rollback first
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    91
	 *   you need to make sure you set the right mode yourself!
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    92
	 * @note this command isn't finished yet!!
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    93
	 */
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    94
	void Rollback();
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
	/**
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
    97
	 * 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
    98
	 *   numbers can always differ).
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.
2b53e154a8d5 (svn r9319) [NoAI] -Add: added AITransactionMode, which allows you to record commands and execute it later on.
truelight
parents:
diff changeset
   104
	 * @param transaction the list that will be appended after the instance you call append on.
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 */