src/ai/api/ai_transactionmode.cpp
branchnoai
changeset 9486 a9b5f6b8667c
parent 9474 2b53e154a8d5
child 9560 ff2dde050a4c
equal deleted inserted replaced
9485:df09b849ca60 9486:a9b5f6b8667c
     6 #include "ai_execmode.hpp"
     6 #include "ai_execmode.hpp"
     7 #include "../../command.h"
     7 #include "../../command.h"
     8 #include "../../debug.h"
     8 #include "../../debug.h"
     9 #include <stack>
     9 #include <stack>
    10 
    10 
    11 bool AITransactionMode::ModeProc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, int32 costs)
    11 bool AITransactionMode::ModeProc(TileIndex tile, uint32 p1, uint32 p2, uint procc, int32 costs)
    12 {
    12 {
    13 	AITransactionMode *instance = (AITransactionMode *)AIObject::GetDoCommandModeInstance();
    13 	AITransactionMode *instance = (AITransactionMode *)AIObject::GetDoCommandModeInstance();
    14 	/* Don't record if we are stopped */
    14 	/* Don't record if we are stopped */
    15 	if (instance->stopped) return false;
    15 	if (instance->stopped) return false;
    16 	AITransactionModeCommand command;
    16 	AITransactionModeCommand command;
    17 
    17 
    18 	/* Store all params in a nice list, so we can execute them later */
    18 	/* Store all params in a nice list, so we can execute them later */
    19 	command.tile  = tile;
    19 	command.tile  = tile;
    20 	command.p1    = p1;
    20 	command.p1    = p1;
    21 	command.p2    = p2;
    21 	command.p2    = p2;
    22 	command.flags = flags;
       
    23 	command.procc = procc;
    22 	command.procc = procc;
    24 	command.text  = (_cmd_text == NULL) ? NULL : strdup(_cmd_text);
    23 	command.text  = (_cmd_text == NULL) ? NULL : strdup(_cmd_text);
    25 	instance->command_stack.push(command);
    24 	instance->command_stack.push(command);
    26 	instance->costs += costs;
    25 	instance->costs += costs;
    27 
    26 
    72 	AITransactionModeCommand command;
    71 	AITransactionModeCommand command;
    73 	while (!this->command_stack.empty()) {
    72 	while (!this->command_stack.empty()) {
    74 		command = this->command_stack.front();
    73 		command = this->command_stack.front();
    75 		_cmd_text = command.text;
    74 		_cmd_text = command.text;
    76 		/* Make sure we return 'false' if the command fails to execute. This allows rollback of failed builds. */
    75 		/* Make sure we return 'false' if the command fails to execute. This allows rollback of failed builds. */
    77 		if (!this->DoCommand(command.tile, command.p1, command.p2, command.flags, command.procc)) return false;
    76 		if (!this->DoCommand(command.tile, command.p1, command.p2, command.procc)) return false;
    78 		/* Add the command to the reverse stack */
    77 		/* Add the command to the reverse stack */
    79 		this->reverse_stack.push(command);
    78 		this->reverse_stack.push(command);
    80 		/* Remove the item from the list */
    79 		/* Remove the item from the list */
    81 		this->command_stack.pop();
    80 		this->command_stack.pop();
    82 	}
    81 	}
   110 		command = this->reverse_stack.front();
   109 		command = this->reverse_stack.front();
   111 		_cmd_text = command.text;
   110 		_cmd_text = command.text;
   112 
   111 
   113 		/* Find the reverse, and issue it */
   112 		/* Find the reverse, and issue it */
   114 		/* TODO -- Make a table which reverses every command.procc */
   113 		/* TODO -- Make a table which reverses every command.procc */
   115 		this->DoCommand(command.tile, command.p1, command.p2, command.flags, command.procc);
   114 		this->DoCommand(command.tile, command.p1, command.p2, command.procc);
   116 
   115 
   117 		/* Remove the item from the list */
   116 		/* Remove the item from the list */
   118 		this->reverse_stack.pop();
   117 		this->reverse_stack.pop();
   119 	}
   118 	}
   120 }
   119 }