src/ai/api/ai_transactionmode.hpp
branchnoai
changeset 9474 2b53e154a8d5
child 9475 58c20c0e394f
equal deleted inserted replaced
9473:dcbcd1c4496d 9474:2b53e154a8d5
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file ai_testmode.hpp class to switch the AI to Transaction mode */
       
     4 
       
     5 #ifndef AI_TRANSACTIONMODE_HPP
       
     6 #define AI_TRANSACTIONMODE_HPP
       
     7 
       
     8 #include "ai_object.hpp"
       
     9 #include <queue>
       
    10 
       
    11 /**
       
    12  * Class to switch current mode to Transaction mode.
       
    13  * If you create an instance of this class, the mode will be switched to
       
    14  *   Transaction. The original mode is stored and recovered from when ever the
       
    15  *   instance is destroyed.
       
    16  * In Transaction mode all commands will be tested, and if not fail, queued.
       
    17  *   Later on you can execute a queue for real. The Transaction keeps on
       
    18  *   recording all commands till it is destructed or execute or stopped.
       
    19  * On execute the transaction can return false, because maps change over time.
       
    20  *   If this happens you can use the rollback feature to remove all already
       
    21  *   built things.
       
    22  */
       
    23 class AITransactionMode : public AIObject {
       
    24 private:
       
    25 	struct AITransactionModeCommand {
       
    26 		TileIndex tile;
       
    27 		uint32 p1;
       
    28 		uint32 p2;
       
    29 		uint32 flags;
       
    30 		uint procc;
       
    31 		char *text;
       
    32 	};
       
    33 
       
    34 	AIModeProc *last_mode;
       
    35 	AIObject *last_instance;
       
    36 	std::queue<AITransactionModeCommand> command_stack;
       
    37 	std::queue<AITransactionModeCommand> reverse_stack;
       
    38 	bool stopped;
       
    39 	int32 costs;
       
    40 
       
    41 protected:
       
    42 	static bool ModeProc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, int32 costs);
       
    43 
       
    44 public:
       
    45 	/**
       
    46 	 * Creating instance of this class switches the build mode to Transaction.
       
    47 	 * @note when the instance is destroyed, he restores the mode that was
       
    48 	 *   current when the instance was created!
       
    49 	 */
       
    50 	AITransactionMode();
       
    51 
       
    52 	/**
       
    53 	 * Destroying this instance reset the building mode to the mode it was
       
    54 	 *   in when the instance was created.
       
    55 	 */
       
    56 	~AITransactionMode();
       
    57 
       
    58 	/**
       
    59 	 * Execute all recorded commands.
       
    60 	 * @return false if any command recorded failed to execute. All other
       
    61 	 *   commands that follow won't be executed either.
       
    62 	 * @note when Execute() is called, the transaction is stopped (like calling
       
    63 	 *   Stop() yourself).
       
    64 	 * @note Execute() is always executed, no matter what mode you gave it in
       
    65 	 *   the outside world. It forces itself into executing it for real. To add
       
    66 	 *   it to an other list, use Append().
       
    67 	 */
       
    68 	bool Execute();
       
    69 
       
    70 	/**
       
    71 	 * Stop recording the commands and switch to the last mode, like the
       
    72 	 *   instance was destroyed.
       
    73 	 */
       
    74 	void Stop();
       
    75 
       
    76 	/**
       
    77 	 * Rollbacks rolls the whole transaction back in case Execute() returned
       
    78 	 *   false. In case Execute() returned true, there won't be anything to roll
       
    79 	 *   back, so this command will do nothing.
       
    80 	 * It rolls back all commands by looking up the reverse of every command
       
    81 	 *   issued and executes that. Of course it can happen that even that fails.
       
    82 	 *   In that case the problem will be silently ignored.
       
    83 	 * @note as you might want to get a costs estimate about the rollback first
       
    84 	 *   you need to make sure you set the right mode yourself!
       
    85 	 * @note this command isn't finished yet!!
       
    86 	 */
       
    87 	void Rollback();
       
    88 
       
    89 	/**
       
    90 	 * Get the costs it takes to execute this transaction (on average, real
       
    91 	 *   numbers can always differ).
       
    92 	 */
       
    93 	int32 GetCosts();
       
    94 
       
    95 	/**
       
    96 	 * Append one transaction list to an other.
       
    97 	 * @param transaction the list that will be appended after the instance you call append on.
       
    98 	 */
       
    99 	void Append(AITransactionMode *transaction);
       
   100 };
       
   101 
       
   102 #ifdef DEFINE_SQUIRREL_CLASS
       
   103 void SQAITransactionModeRegister(Squirrel *engine) {
       
   104 	DefSQClass <AITransactionMode> SQAITransactionMode("AITransactionMode");
       
   105 	SQAITransactionMode.PreRegister(engine);
       
   106 	SQAITransactionMode.AddConstructor(engine);
       
   107 	SQAITransactionMode.DefSQFunction(engine, &AITransactionMode::Execute,  "Execute");
       
   108 	SQAITransactionMode.DefSQFunction(engine, &AITransactionMode::Stop,     "Stop");
       
   109 	SQAITransactionMode.DefSQFunction(engine, &AITransactionMode::Rollback, "Rollback");
       
   110 //	SQAITransactionMode.DefSQFunction(engine, &AITransactionMode::Append,   "Append");
       
   111 	SQAITransactionMode.PostRegister(engine);
       
   112 }
       
   113 #endif /* SQUIRREL_CLASS */
       
   114 
       
   115 #endif /* AI_TRANSACTIONMODE_HPP */