src/ai/ai_threads.cpp
author truebrain
Mon, 16 Jun 2008 14:43:19 +0000
branchnoai
changeset 10978 13fd0364b2c6
parent 10875 f730e5098471
child 11094 72be0534cd0f
permissions -rw-r--r--
(svn r13532) [NoAI] -Fix: in MultiPlayer SignID wasn't set correctly, causing weird effects. Code is now more unified, which should avoid simular effects in the future (tnx to glx for initial patch)
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"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    11
#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
    12
#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
    13
#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
    14
#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
    15
#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
    16
#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
    17
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
    18
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
    19
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
    20
	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
    21
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
	/** 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
    23
	enum FiberState {
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    24
		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
    25
		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
    26
		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
    27
		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
    28
		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
    29
		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
    30
	};
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    31
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
    32
public:
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    33
	/**
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
    34
	 * 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
    35
	 * @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
    36
	 * @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
    37
	 */
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
    38
	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
    39
		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
    40
		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
    41
		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
    42
		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
    43
		fiber(NULL)
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    44
	{
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
    45
		/* 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
    46
		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
    47
		/* 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
    48
		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
    49
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
    50
		/* 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
    51
		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
    52
			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
    53
		} 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
    54
			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
    55
			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
    56
		}
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
    57
	}
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
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
    59
	~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
    60
	{
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
    61
		/* 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
    62
		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
    63
		/* 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
    64
		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
    65
	}
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
	/**
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
    68
	 * 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
    69
	 *  suspending command.
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    70
	 */
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
    71
	bool Start()
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    72
	{
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
    73
		/* 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
    74
		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
    75
		/* 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
    76
		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
    77
		/* 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
    78
		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
    79
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
    80
		/* 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
    81
		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
    82
		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
    83
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
    84
		/* 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
    85
		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
    86
		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
    87
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
		return true;
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    89
	}
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
	/**
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
    92
	 * 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
    93
	 * @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
    94
	 * @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
    95
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
    96
	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
    97
	{
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
    98
		/* 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
    99
		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
   100
		/* 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
   101
		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
   102
		/* 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
   103
		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
   104
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   105
		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
   106
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
   107
		/* 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
   108
		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
   109
		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
   110
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
   111
		/* 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
   112
		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
   113
			/* 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
   114
			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
   115
			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
   116
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
			/* 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
   118
			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
   119
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
   120
			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
   121
		}
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
   122
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
   123
		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
   124
	}
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
	/**
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
   127
	 * 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
   128
	 * @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
   129
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   130
	void Resume()
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   131
	{
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
   132
		/* 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
   133
		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
   134
		/* 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
   135
		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
   136
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   137
		/* 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
   138
		 * 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
   139
		 * 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
   140
		 * 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
   141
		 * 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
   142
		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
   143
	}
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
	/**
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
   146
	 * 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
   147
	 *  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
   148
	 *  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
   149
	 * @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
   150
	 */
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   151
	void RunTick()
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   152
	{
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
   153
		/* 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
   154
		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
   155
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
   156
		/* 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
   157
		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
   158
		/* 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
   159
		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
   160
		/* 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
   161
		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
   162
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   163
		this->controller->IncreaseTick();
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   164
9450
d675836e865c (svn r9278) [NoAI] -Add: added AISettings which adds the function to control the Delay-value on DoCommands
truelight
parents: 9445
diff changeset
   165
		/* 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
   166
		 *  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
   167
		 *  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
   168
		 *  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
   169
		 *  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
   170
		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
   171
		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
   172
		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
   173
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
   174
		/* 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
   175
		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
   176
	}
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
	/**
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
   179
	 * 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
   180
	 *  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
   181
	 * @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
   182
	 *  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
   183
	 * @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
   184
	 */
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
   185
	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
   186
	{
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
   187
		/* 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
   188
		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
   189
		/* 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
   190
		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
   191
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
   192
		/* 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
   193
		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
   194
		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
   195
		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
   196
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
   197
		/* 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
   198
		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
   199
	}
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
	/**
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
   202
	 * 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
   203
	 *  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
   204
	 *  (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
   205
	 *  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
   206
	 */
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
   207
	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
   208
	{
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
   209
		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
   210
		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
   211
		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
   212
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
   213
		/* 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
   214
		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
   215
	}
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
	/**
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
   218
	 * 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
   219
	 */
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
   220
	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
   221
	{
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
   222
		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
   223
		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
   224
	}
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
	/**
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
   227
	 * 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
   228
	 */
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
   229
	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
   230
	{
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
   231
		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
   232
		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
   233
		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
   234
	}
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
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
   236
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
   237
	/**
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
   238
	 * 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
   239
	 *  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
   240
	 * @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
   241
	 */
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
   242
	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
   243
	{
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
   244
		/* 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
   245
		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
   246
		/* 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
   247
		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
   248
		/* 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
   249
		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
   250
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
		/* 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
   252
		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
   253
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
		/* 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
   255
		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
   256
		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
   257
		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
   258
	}
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
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
   260
	/**
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
	 * 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
   262
	 */
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
	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
   264
	{
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
		/* 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
   266
		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
   267
		/* 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
   268
		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
   269
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
		/* 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
   271
		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
   272
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
   273
		/* 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
   274
		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
   275
		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
   276
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
   277
		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
   278
			/* 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
   279
			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
   280
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
   281
			/* 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
   282
			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
   283
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
   284
			/* 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
   285
			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
   286
				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
   287
			}
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
   288
		}
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
		/* 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
   291
		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
   292
		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
   293
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
		/* 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
   295
		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
   296
	}
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
   297
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
	/**
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
   299
	 * 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
   300
	 */
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
   301
	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
   302
	{
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
   303
		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
   304
		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
   305
	}
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
	/**
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
   308
	 * 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
   309
	 * @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
   310
	 */
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
   311
	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
   312
	{
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
   313
		/* 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
   314
		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
   315
		/* 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
   316
		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
   317
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
   318
		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
   319
	}
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
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
   321
	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
   322
	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
   323
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
   324
	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
   325
	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
   326
	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
   327
	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
   328
	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
   329
};
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
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
   331
/* 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
   332
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
   333
/**
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
 * 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
   335
 * @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
   336
 * @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
   337
 * @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
   338
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   339
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
   340
{
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
   341
	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
   342
	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
   343
	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
   344
	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
   345
}
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
/**
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
   348
 * 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
   349
 * @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
   350
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   351
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
   352
{
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
   353
	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
   354
	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
   355
	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
   356
		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
   357
		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
   358
	}
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
	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
   360
}
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
 * 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
   364
 * @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
   365
 * @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
   366
 */
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
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
   368
{
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
   369
	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
   370
	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
   371
	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
   372
	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
   373
	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
   374
}
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
/**
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   377
 * 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
   378
 * @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
   379
 */
9444
fd27df7ca2a0 (svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents: 9441
diff changeset
   380
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
   381
{
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
   382
	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
   383
	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
   384
	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
   385
	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
   386
	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
   387
}
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
 * 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
   391
 *  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
   392
 */
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
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
   394
{
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
   395
	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
   396
	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
   397
	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
   398
	/* 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
   399
	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
   400
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
   401
	/* 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
   402
	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
   403
}
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
   404