src/ai/ai_threads.cpp
author truebrain
Sun, 08 Jun 2008 22:38:29 +0000
branchnoai
changeset 10875 f730e5098471
parent 10871 326ee226e9d7
child 10978 13fd0364b2c6
permissions -rw-r--r--
(svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     1
/* $Id$ */
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     2
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     3
/** @file ai_threads.cpp handles the threading of AIs */
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     4
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     5
#include "../stdafx.h"
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     6
#include "../openttd.h"
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     7
#include "../variables.h"
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     8
#include "../debug.h"
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
     9
#include "../thread.h"
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    10
#include "../fiber.hpp"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9694
diff changeset
    11
#include "../vehicle_func.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    12
#include "../player_func.h"
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
    13
#include "ai.h"
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    14
#include "ai_threads.h"
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
    15
#include "api/ai_controller.hpp"
9496
05ebee9884b3 (svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
truelight
parents: 9450
diff changeset
    16
#include "api/ai_object.hpp"
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
    17
#include <map>
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
    18
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    19
class AIFiber : public SimpleCountedObject {
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    20
private:
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    21
	typedef std::map<int, CCountedPtr<AIFiber> > Fibers;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    22
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    23
	/** Different states of the AI fiber. */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    24
	enum FiberState {
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    25
		INITIALIZED, ///< The mutex and conditions are initialized
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    26
		STARTING,    ///< The fiber is just started
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    27
		RUNNING,     ///< We are inside the main AI
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    28
		SUSPENDED,   ///< We are suspended (either by sleep or block)
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    29
		STOPPING,    ///< We are being stopped
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    30
		STOPPED,     ///< The main AI has stopped
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    31
	};
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    32
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    33
public:
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    34
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    35
	 * Initialize this AIFiber for the given controller.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    36
	 * @param player The PlayerID of this AI.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    37
	 * @param controller The AI we have to run.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    38
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    39
	AIFiber(PlayerID player, AIController *controller) :
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    40
		fiber_id(player),
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    41
		state(INITIALIZED),
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    42
		ticks_to_sleep(0),
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    43
		controller(controller),
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    44
		fiber(NULL)
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    45
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    46
		/* The player shouldn't have a fiber yet */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    47
		assert(s_fibers[player] == NULL);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    48
		/* Register the new fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    49
		s_fibers[player] = this;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    50
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    51
		/* Ensure that main fiber has its wrapper too */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    52
		if (player != MAIN_FIBER) {
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    53
			stFind(MAIN_FIBER);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    54
		} else {
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    55
			this->fiber = Fiber::AttachCurrent(this);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    56
			assert(this->fiber->IsRunning());
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    57
		}
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
    58
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
    59
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    60
	~AIFiber()
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
    61
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    62
		/* Make sure we are not in the active list anymore */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    63
		assert(this->fiber_id == MAIN_FIBER || stFind(this->fiber_id) == NULL);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    64
		/* Clean up */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    65
		delete this->fiber;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    66
	}
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    67
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    68
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    69
	 * Start the AI fiber and return once the AI calls Sleep or any other
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    70
	 *  suspending command.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    71
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    72
	bool Start()
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    73
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    74
		/* Make sure we start fibers from the main fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    75
		assert(stCurrentFiber() == stFind(MAIN_FIBER));
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    76
		/* We shouldn't have a fiber yet */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    77
		assert(this->fiber == NULL);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    78
		/* And we shouldn't be starting the main fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    79
		assert(fiber_id != MAIN_FIBER);
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    80
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    81
		/* Create fiber for this AI */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    82
		this->fiber = Fiber::New(&stFiberProc, this);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    83
		if (!this->fiber->IsRunning()) return false;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    84
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    85
		/* Switch to STARTING mode */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    86
		this->state = SUSPENDED;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    87
		this->SwitchToState(STARTING);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    88
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    89
		return true;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    90
	}
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    91
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    92
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    93
	 * Suspend this AI for a 'timeout' of period.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    94
	 * @param timeout Time to suspend. < 0 means infinite (MultiPlayer only!)
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    95
	 * @note Only call this from within the fiber you want to execute the function on.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    96
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    97
	void Suspend(int timeout)
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    98
	{
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
    99
		/* Should not attempt to suspend main fiber */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   100
		assert(this->fiber_id != MAIN_FIBER);
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   101
		/* AI fiber should be running */
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   102
		assert(this->state == RUNNING);
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   103
		/* We can only suspend ourself */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   104
		assert(this->stCurrentFiber() == this);
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   105
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   106
		this->ticks_to_sleep = timeout;
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   107
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   108
		/* When AI fiber gets suspended, it always switches back to main fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   109
		AIFiber *main = stFind(MAIN_FIBER);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   110
		main->SwitchToState(RUNNING);
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   111
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   112
		/* The main fiber wants this AI to die */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   113
		if (this->state == STOPPING) {
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   114
			/* Suspend, and exit */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   115
			this->state = SUSPENDED;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   116
			this->Exit();
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   117
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   118
			/* Here we will never come as... the fiber is dead */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   119
			assert(false);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   120
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   121
			return;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   122
		}
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   123
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   124
		assert(this->state == RUNNING);
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   125
	}
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   126
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   127
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   128
	 * Set the AI fiber to resume at the next call of RunTick.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   129
	 * @note Only call this from within the main fiber.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   130
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   131
	void Resume()
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   132
	{
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   133
		/* Should be called from main fiber only */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   134
		assert(stCurrentFiber() == stFind(MAIN_FIBER));
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   135
		/* AI fiber should be suspended */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   136
		assert(this->state == SUSPENDED);
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   137
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   138
		/* Normally the ticks_to_sleep hangs at -1 for MP. Possible the MP is
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   139
		 * faster then the delay requested by the user. In this case the value
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   140
		 * is lower. To let the normal delay system kick in, we reverse the value
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   141
		 * of ticks_to_sleep. But now it doesn't directly continue when the value
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   142
		 * was 'hanging', so we subtract 1 and it all works fine. */
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   143
		this->ticks_to_sleep = -this->ticks_to_sleep - 1;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   144
	}
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   145
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   146
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   147
	 * Let the AI fiber run for a while and return when it is done.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   148
	 *  However, when the fiber is suspended and the suspend timeout has not yet
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   149
	 *  passed, nothing happens except decrementing the before mentioned timeout.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   150
	 * @note Only call this from within the main fiber.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   151
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   152
	void RunTick()
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   153
	{
9848
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   154
		/* If we already stopped, don't do anything */
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   155
		if (this->state == STOPPED) return;
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   156
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   157
		/* Should be called from main fiber only */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   158
		assert(stCurrentFiber() == stFind(MAIN_FIBER));
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   159
		/* Only AI fibers should be resumed this way */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   160
		assert(this != stFind(MAIN_FIBER));
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   161
		/* AI fiber should be still suspended */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   162
		assert(this->state == SUSPENDED);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   163
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   164
		this->controller->IncreaseTick();
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   165
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   166
		/* If the value is < -1, the user wants a delay which might exceed the delay
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   167
		 *  of the MP. Therefor we keep adding value to ticks_to_sleep till it
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   168
		 *  reaches -1, and we 'hang' it there infinitely, until the MP commands
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   169
		 *  comes in. In the case it happens sooner, the Resume() codes handles it
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   170
		 *  nicely and makes the value positive with the remaining ticks to wait. */
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   171
		if (this->ticks_to_sleep < -1)  this->ticks_to_sleep++;
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   172
		if (this->ticks_to_sleep < 0)   return; // We have to wait infinitely
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   173
		if (--this->ticks_to_sleep > 0) return; // We have to wait a little longer
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   174
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   175
		/* Make the fiber running again */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   176
		this->SwitchToState(RUNNING);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   177
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   178
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   179
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   180
	 * Try to stop the AI, and mark it as such. You can cleanup the AI after
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   181
	 *  this if you like; it won't be become active for sure.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   182
	 * @note There is no guarantee the fiber dies or anything, we just promise
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   183
	 *  it won't be called anymore.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   184
	 * @note Only call this from within the main fiber.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   185
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   186
	void Stop()
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   187
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   188
		/* Should be called from main fiber only */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   189
		assert(stCurrentFiber() == stFind(MAIN_FIBER));
9848
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   190
		/* If we already stopped, don't do anything */
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   191
		if (this->state == STOPPED) return;
b4c0116b6b0a (svn r12515) [NoAI] -Fix: when an AI dies on its own, don't kill the AI-script, just mark it as dead and don't do anything until the company dies with it
truebrain
parents: 9768
diff changeset
   192
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   193
		/* Trigger a STOPPING round */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   194
		assert(this->state == SUSPENDED);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   195
		this->SwitchToState(STOPPING);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   196
		assert(this->state == SUSPENDED);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   197
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   198
		/* Mark the fiber as STOPPED */
9761
6c3e0f29a232 (svn r12249) [NoAI] -Fix: finally found why closing the game gave an assert() on running AIs
truebrain
parents: 9760
diff changeset
   199
		this->state = STOPPED;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   200
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   201
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   202
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   203
	 * Find and return the fiber object by its 'id' (player_id or MAIN_FIBER).
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   204
	 *  Returns NULL if such fiber object doesn't exist. If
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   205
	 *  (fiber_id == MAIN_FIBER) and the fiber object with this id doesn't
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   206
	 *  exist, the new one is created and attached to the current (main) fiber.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   207
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   208
	static AIFiber *stFind(int fiber_id)
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   209
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   210
		Fibers::iterator it = s_fibers.find(fiber_id);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   211
		if (it != s_fibers.end()) return (*it).second;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   212
		if (fiber_id != MAIN_FIBER) return NULL;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   213
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   214
		/* Main fiber doesn't have its own fiber object, create it */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   215
		return new AIFiber((PlayerID)MAIN_FIBER, NULL);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   216
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   217
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   218
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   219
	 * Remove fiber object from the collection by its 'id' and destroy it.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   220
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   221
	static void stRelease(int fiber_id)
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   222
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   223
		Fibers::iterator it = s_fibers.find(fiber_id);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   224
		if (it != s_fibers.end()) s_fibers.erase(it);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   225
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   226
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   227
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   228
	 * Find the fiber object that belongs to the currently running fiber (caller).
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   229
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   230
	static AIFiber *stCurrentFiber()
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   231
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   232
		AIFiber *cur = (AIFiber *)Fiber::GetCurrentFiberData();
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   233
		assert(cur != NULL);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   234
		return cur;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   235
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   236
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   237
private:
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   238
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   239
	 * Switch the state of a fiber. It becomes active and executed until it
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   240
	 *  changes the state. The current active fiber becomes SUSPENDED.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   241
	 * @note Only call this from an other fiber.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   242
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   243
	void SwitchToState(FiberState new_state)
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   244
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   245
		/* You can't switch the state of an already active fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   246
		assert(stCurrentFiber() != this);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   247
		/* The fiber should be suspended */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   248
		assert(this->state == SUSPENDED);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   249
		/* And there should be an active fiber on it */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   250
		assert(this->fiber != NULL);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   251
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   252
		/* Suspend the fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   253
		stCurrentFiber()->state = SUSPENDED;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   254
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   255
		/* Switch to new state, activate fiber, and check if we are suspended again */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   256
		this->state = new_state;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   257
		this->fiber->SwitchToFiber();
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   258
		assert(this->state == SUSPENDED);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   259
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   260
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   261
	/**
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   262
	 * The method that runs in the new Fiber. Automaticly called when the Fiber becomes active.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   263
	 */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   264
	void FiberProc()
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   265
	{
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   266
		/* We should be starting */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   267
		assert(this->state == STARTING);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   268
		/* We should be the active thread */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   269
		assert(stCurrentFiber() == this);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   270
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   271
		/* Switch to running */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   272
		this->state = RUNNING;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   273
10870
20b6cff3d6b2 (svn r13421) [NoAI] -Fix: remove the need for a Sleep(1) before executing a command at the beginning of Start(), by skipping the first tick before calling Start() in the first place
truebrain
parents: 10412
diff changeset
   274
		/* We want to skip the first tick, so switch back to the main fiber */
20b6cff3d6b2 (svn r13421) [NoAI] -Fix: remove the need for a Sleep(1) before executing a command at the beginning of Start(), by skipping the first tick before calling Start() in the first place
truebrain
parents: 10412
diff changeset
   275
		AIFiber *main = stFind(MAIN_FIBER);
20b6cff3d6b2 (svn r13421) [NoAI] -Fix: remove the need for a Sleep(1) before executing a command at the beginning of Start(), by skipping the first tick before calling Start() in the first place
truebrain
parents: 10412
diff changeset
   276
		main->SwitchToState(RUNNING);
20b6cff3d6b2 (svn r13421) [NoAI] -Fix: remove the need for a Sleep(1) before executing a command at the beginning of Start(), by skipping the first tick before calling Start() in the first place
truebrain
parents: 10412
diff changeset
   277
10875
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   278
		if (this->state != STOPPING) {
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   279
			/* Start up the AI (this should be an infinite loop) */
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   280
			this->controller->Start();
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   281
10875
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   282
			/* If we come here, the AI exited because it wanted to */
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   283
			DEBUG(ai, 1, "We've got a suicidal AI for player %d", this->fiber_id);
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   284
10875
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   285
			/* Wait until we are killed nicely by the game */
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   286
			while (this->state != STOPPING) {
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   287
				main->SwitchToState(RUNNING);
f730e5098471 (svn r13426) [NoAI] -Fix: in the unlikely case an AI is killed before his ->Start() was started, an assert was triggered
truebrain
parents: 10871
diff changeset
   288
			}
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   289
		}
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   290
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   291
		/* Suspend, and exit */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   292
		this->state = SUSPENDED;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   293
		this->Exit();
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   294
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   295
		/* Here we will never come as... the fiber is dead */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   296
		assert(false);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   297
	}
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   298
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   299
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   300
	 * Function that is started as process of the new AI fiber.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   301
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   302
	static void CDECL stFiberProc(void *fiber)
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   303
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   304
		AIFiber *cur = (AIFiber *)fiber;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   305
		cur->FiberProc();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   306
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   307
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   308
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   309
	 * Exit a running fiber.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   310
	 * @note Only call this from within the fiber you want to execute the function on.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   311
	 */
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   312
	void Exit()
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   313
	{
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   314
		/* We can only exit if we are the thread */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   315
		assert(stCurrentFiber() == this);
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   316
		/* We can never exit the main fiber */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   317
		assert(fiber_id != MAIN_FIBER);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   318
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   319
		this->fiber->Exit();
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   320
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   321
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   322
	static Fibers s_fibers;          ///< Collection of active fibers.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   323
	static const int MAIN_FIBER = -1;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   324
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   325
	int            fiber_id;         ///< id of this fiber (or MAIN_FIBER for the main).
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   326
	FiberState     state;            ///< The current state of the AI.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   327
	int            ticks_to_sleep;   ///< Sleep this many runticks.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   328
	AIController   *controller;      ///< Controller of this AI.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   329
	Fiber          *fiber;           ///< Fiber we are connected to.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   330
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   331
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   332
/* static */ AIFiber::Fibers AIFiber::s_fibers;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   333
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   334
/**
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   335
 * Suspend the AI handler of a player.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   336
 * @param player the player to suspend for
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   337
 * @param timeout Time to suspend. < 0 means infinite (MultiPlayer only!)
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   338
 * @note This should be called from within the fiber running this player!
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   339
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   340
void AI_SuspendPlayer(PlayerID player, int timeout)
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   341
{
10412
ef44f62cb8b9 (svn r12954) [NoAI] -Fix: move the DEBUG level of non-essential debug statements up a few levels, so it doesn't show up at least before lvl 5
truebrain
parents: 10355
diff changeset
   342
	DEBUG(ai, 6, "AI_SuspendPlayer(%d, %d) from thr %d", player, timeout, ThreadObject::CurrentId());
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   343
	AIFiber *thr = AIFiber::stCurrentFiber();
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   344
	assert(thr != NULL);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   345
	thr->Suspend(timeout);
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   346
}
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   347
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   348
/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   349
 * Run the fiber of an AI player. Return when it is suspended again.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   350
 * @param player the player to run this tick for
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   351
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   352
void AI_RunTick(PlayerID player)
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   353
{
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   354
	DEBUG(ai, 6, "AI_RunTick(%d) from thr %d", player, ThreadObject::CurrentId());
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   355
	AIFiber *thr = AIFiber::stFind(player);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   356
	if (thr == NULL) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   357
		DEBUG(ai, 0, "AI_RunTick() called for dead AI player #%d", player);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   358
		return;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   359
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   360
	thr->RunTick();
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   361
}
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   362
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   363
/**
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   364
 * Run an AI player for the first time.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   365
 * @param player     the (AI) player to start
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   366
 * @param controller the actual AI
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   367
 */
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   368
void AI_StartPlayer(PlayerID player, AIController *controller)
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   369
{
10412
ef44f62cb8b9 (svn r12954) [NoAI] -Fix: move the DEBUG level of non-essential debug statements up a few levels, so it doesn't show up at least before lvl 5
truebrain
parents: 10355
diff changeset
   370
	DEBUG(ai, 6, "AI_StartPlayer(%d) from thr %d", player, ThreadObject::CurrentId());
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   371
	AIFiber *thr = AIFiber::stFind(player);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   372
	assert(thr == NULL);
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   373
	thr = new AIFiber(player, controller);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   374
	thr->Start();
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   375
}
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   376
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   377
/**
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   378
 * Stops the given player
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   379
 * @param player the (AI) player to stop
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   380
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   381
void AI_StopPlayer(PlayerID player)
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   382
{
10412
ef44f62cb8b9 (svn r12954) [NoAI] -Fix: move the DEBUG level of non-essential debug statements up a few levels, so it doesn't show up at least before lvl 5
truebrain
parents: 10355
diff changeset
   383
	DEBUG(ai, 6, "AI_StopPlayer(%d) from thr %d", player, ThreadObject::CurrentId());
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   384
	AIFiber *thr = AIFiber::stFind(player);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   385
	if (thr == NULL) return;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   386
	thr->Stop();
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   387
	AIFiber::stRelease(player);
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   388
}
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   389
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   390
/**
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   391
 * Callback for when a network command was executed.
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   392
 *  As commands are always ordered, we don't have to worry about right command.
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   393
 */
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   394
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   395
{
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   396
	DEBUG(ai, 6, "CcAI(%d) from thr %d", _current_player, ThreadObject::CurrentId());
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   397
	AIFiber *thr = AIFiber::stFind(_current_player);
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   398
	assert(thr != NULL);
9760
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   399
	/* Store if we were a success or not */
265fdd2130c3 (svn r12248) [NoAI] -Codechange: last_command_res was in AIThread, while it should be in AIObject, like all other variables like it
truebrain
parents: 9724
diff changeset
   400
	AIObject::SetLastCommandRes(success);
9496
05ebee9884b3 (svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
truelight
parents: 9450
diff changeset
   401
	/* Store some values inside the AIObject static memory */
05ebee9884b3 (svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
truelight
parents: 9450
diff changeset
   402
	AIObject::SetNewVehicleID(_new_vehicle_id);
05ebee9884b3 (svn r9368) [NoAI] -Fix: store _new_vehicle_id directly after successful handling the command in a per-AI-player-safe piece of memory, so we can restore the value when ever we want later in the process
truelight
parents: 9450
diff changeset
   403
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
   404
	/* Resume the fiber now */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 9496
diff changeset
   405
	thr->Resume();
9441
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   406
}
03da911c8d5f (svn r9255) [NoAI] -Add: each AI now runs in a seperate thread. The main thread is suspended if any AI thread is running, only one AI thread runs at the time.
truelight
parents:
diff changeset
   407