author | rubidium |
Sat, 14 Apr 2007 20:38:10 +0000 | |
branch | noai |
changeset 9594 | 5009a30f320a |
parent 9530 | 5b93bc87cc5e |
child 9596 | 8af5a1399842 |
permissions | -rw-r--r-- |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
1 |
/* $Id$ */ |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
2 |
|
9388
032008c3f6e3
(svn r9180) [NoAI] -Fix: copy/pasting can be nasty: wrong @file headers
truelight
parents:
9372
diff
changeset
|
3 |
/** @file ai_controler.hpp declaration of class for AIController class */ |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
4 |
|
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
5 |
#ifndef AI_CONTROLLER_HPP |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
6 |
#define AI_CONTROLLER_HPP |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
7 |
|
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
8 |
#include "../../stdafx.h" |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
9 |
|
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
10 |
/** |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
11 |
* Class that handles the core of every AI. Each AI needs to extend this class |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
12 |
* in order to work. The OpenTTD core makes sure GetTick gets increased when |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
13 |
* needed. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
14 |
*/ |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
15 |
class AIController { |
9372
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
16 |
private: |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
17 |
uint tick; |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
18 |
|
9372
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
19 |
public: |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
20 |
/** |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
21 |
* Initializer of the AIController. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
22 |
*/ |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
23 |
AIController() : |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
24 |
tick(0) |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
25 |
{} |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
26 |
|
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
27 |
/** |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
28 |
* Destructor of the AIController. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
29 |
*/ |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
30 |
virtual ~AIController() { } |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
31 |
|
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
32 |
/** |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
33 |
* This function is called to start your AI. Your AI starts here. If you |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
34 |
* return from this function, your AI dies, so make sure that doesn't |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
35 |
* happen. It is okay to use while() {} loops, as long as you put a |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
36 |
* Sleep() in it to give the rest of the game time to do things. Also |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
37 |
* don't keep the system busy for too long, as people will consider |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
38 |
* that annoying. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
39 |
* @note Cannot be called from within your AI. |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
40 |
*/ |
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:
9439
diff
changeset
|
41 |
virtual void Start() = 0; |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
42 |
|
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
43 |
/** |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
44 |
* When this function is called, the AI must stop as soon as possible. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
45 |
* It may not call any function that might suspend, like DoCommand() |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
46 |
* and Sleep(). |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
47 |
* @note Cannot be called from within your AI. |
9444
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
48 |
*/ |
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
49 |
virtual void Stop() = 0; |
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
50 |
|
fd27df7ca2a0
(svn r9260) [NoAI] -Codechange: do the AI threading properly using a mutex and condition signalling.
rubidium
parents:
9441
diff
changeset
|
51 |
/** |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
52 |
* Increase the internal ticker. You should never call this yourself, |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
53 |
* as it is called by the OpenTTD system itself. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
54 |
* @note Cannot be called from within your AI. |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
55 |
*/ |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
56 |
void IncreaseTick() { this->tick++; } |
9372
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
57 |
|
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
58 |
/** |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
59 |
* Find at which tick your AI currently is. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
60 |
* @return returns the current tick. |
9372
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
61 |
*/ |
bd60b398f92b
(svn r9159) [NoAI] -Change: moved 'tick' from public to private. Use GetTick() to get its value.
truelight
parents:
9364
diff
changeset
|
62 |
uint GetTick() { return this->tick; } |
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:
9439
diff
changeset
|
63 |
|
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:
9439
diff
changeset
|
64 |
/** |
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:
9439
diff
changeset
|
65 |
* Sleep for X ticks. The code continues after this line when the X AI ticks |
9448
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
66 |
* are passed. Mind that an AI tick is different from in-game ticks and |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
67 |
* differ per AI speed. |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
68 |
* @param ticks the ticks to wait |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
69 |
* @post the value of GetTick() will be changed exactly 'ticks' in value after |
2a4c4340233d
(svn r9273) [NoAI] -Documentation: finished documenting the last few files; they should now be readable for any non-programmer.
truelight
parents:
9444
diff
changeset
|
70 |
* calling this. |
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:
9439
diff
changeset
|
71 |
*/ |
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:
9439
diff
changeset
|
72 |
void Sleep(uint ticks); |
9361
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
73 |
}; |
7bb2bd22b16e
(svn r9144) [NoAI] -Change: moved command functions to AIObject, and made AIBase depend on AIObject
truelight
parents:
diff
changeset
|
74 |
|
9439
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
75 |
#endif /* AI_CONTROLLER_HPP */ |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
76 |
|
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
77 |
/* |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
78 |
* The next part of the code is outside of the multiple inclusions |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
79 |
* protection. This is done because the Squirrel code needs the |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
80 |
* AIController class declaration to make the Squirrel registration |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
81 |
* functions. So in the first include DEFINE_SQUIRREL_CLASS is not |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
82 |
* defined, but in the second one it will when included from the |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
83 |
* Squirrel AI implementation. |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
84 |
* All other instances that include this header twice must not have |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
85 |
* set DEFINE_SQUIRREL_CLASS, so this part will not be included |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
86 |
* twice under those circumstances. |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
87 |
*/ |
b0482f81af3c
(svn r9250) [NoAI] -Fix (r9247): some parts of the ai_controller weren't header protected on purpose, however it wasn't made evident.
rubidium
parents:
9436
diff
changeset
|
88 |
|
9425
8eec6d10844a
(svn r9227) [NoAI] -Change: renamed SQUIRREL_CLASS to DEFINE_SQUIRREL_CLASS
truelight
parents:
9422
diff
changeset
|
89 |
#ifdef DEFINE_SQUIRREL_CLASS |
9422
33efcc5f1b09
(svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents:
9404
diff
changeset
|
90 |
void SQAIControllerRegister(Squirrel *engine) { |
9404
ef9e171617a3
(svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents:
9388
diff
changeset
|
91 |
DefSQClass <AIControllerSquirrel> SQAIController("AIController"); |
ef9e171617a3
(svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents:
9388
diff
changeset
|
92 |
SQAIController.PreRegister(engine); |
9530
5b93bc87cc5e
(svn r9451) [NoAI] -Add: allow static and non-static members for SQ
truelight
parents:
9520
diff
changeset
|
93 |
SQAIController.DefSQMethod(engine, &AIControllerSquirrel::GetTick, "GetTick"); |
5b93bc87cc5e
(svn r9451) [NoAI] -Add: allow static and non-static members for SQ
truelight
parents:
9520
diff
changeset
|
94 |
SQAIController.DefSQMethod(engine, &AIControllerSquirrel::Sleep, "Sleep"); |
9404
ef9e171617a3
(svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents:
9388
diff
changeset
|
95 |
SQAIController.PostRegister(engine); |
ef9e171617a3
(svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents:
9388
diff
changeset
|
96 |
} |
9520
f7cf8bea10db
(svn r9440) [NoAI] -Codechange: use the identifier used for the #ifdef in the #endif not something that looks like the identifier.
rubidium
parents:
9448
diff
changeset
|
97 |
#endif /* DEFINE_SQUIRREL_CLASS */ |