aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/include/g_bot_cl.h
blob: 66e3871f6f96efd13268256cb9417d86f1f059a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifndef G_BOT_CL_H_
#define G_BOT_CL_H_

// simple timeout handling ----------------------------------------------------
// 
class UTL_RefFrameTimeout
{
protected:
	refframe_t m_RefFrames;
	refframe_t m_Base;
	refframe_t m_Timeout;

public:
	// ctors
	UTL_RefFrameTimeout()
	{
		m_Timeout   = 0;
		Reset();
	}
	UTL_RefFrameTimeout( refframe_t _Timeout )
	{
		Set( _Timeout );
	}

	// set the timeout value
	void Set( refframe_t _Timeout )
	{
		m_Timeout   = _Timeout;
		Reset();
	}

	// reset timeout
	void Reset()
	{
		m_RefFrames = m_Timeout;
		m_Base      = SYSs_GetRefFrameCount();
	}

	// check for timeout
	bool_t IsTimeout()
	{
		refframe_t diff = SYSs_GetRefFrameCount() - m_Base;
		m_RefFrames -= diff;
		if ( m_RefFrames < 0 ) {
			m_RefFrames += m_Timeout;
			return true;
		} else {
			return false;
		}
	}
};


// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
//FIXME:
// this belongs into seperate OCT module
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************

// object control orientations ------------------------------------------------
//
#define OCT_PITCH_UP	-1
#define OCT_PITCH_DOWN  +1
#define OCT_YAW_LEFT	+1
#define OCT_YAW_RIGHT   -1
#define OCT_ROLL_LEFT   +1
#define OCT_ROLL_RIGHT  -1
#define OCT_ACCELERATE  +1
#define OCT_DECELERATE  -1

// object control structure ---------------------------------------------------
//
struct object_control_s
{
	ShipObject* pShip;

	float		rot_x;		// do a rotation around the x axis - PITCH - ( -1 = divedown, +1 = pullup )
	float		rot_y;		// do a rotation around the y axis - YAW   - ( -1 = left,     +1 = right )
	float		rot_z;		// do a rotation around the z axis - ROLL  - ( -1 = right,    +1 = left )
	float		accel;		// accel control ( +1 accelerate, -1 decelerate )

	//FIXME: slide horiz/vert

	//FIXME: numerically sane versions
	bool_t IsPitch() const	{ return rot_x != 0.0f; }
	bool_t IsYaw() const	{ return rot_y != 0.0f; }
	bool_t IsRoll() const	{ return rot_z != 0.0f; }
	bool_t IsMove() const	{ return accel != 0.0f; }
};

// do the desired object control
int	OCT_DoControl( object_control_s* objctl );

// dump an object control to the console
void OCT_Dump(object_control_s* objctl );

// ****************************************************************************
// ****************************************************************************
// ****************************************************************************




// ----------------------------------------------------------------------------
//
class UTL_LocomotionController
{
protected:
	
	int			m_nRelaxedHeadingAngle;
	int			m_nFullSpeedHeading;
	float		m_fMinSpeedTurn;			// min. speed to keep when doing a sharp turn

public:
	// standard ctor
	UTL_LocomotionController()
	{
		m_nRelaxedHeadingAngle	= 5;
		m_nFullSpeedHeading		= 30;
		m_fMinSpeedTurn			= FIXED_TO_FLOAT( 500 );
	}

	// set the max. degrees the heading and the desired heading can differ
	void SetRelaxedHeadingAngle( int nRelaxedHeadingAngle )
	{
		m_nRelaxedHeadingAngle = nRelaxedHeadingAngle;
	}

	// set the heading at which we should go full speed again after a turn
	void SetFullSpeedHeading( int nFullSpeedHeading )
	{
		m_nFullSpeedHeading = nFullSpeedHeading;
	}

	// set the min. speed to keep during a turn
	void SetMinSpeedTurn( fixed_t _MinSpeedTurn )
	{
		m_fMinSpeedTurn = FIXED_TO_FLOAT( _MinSpeedTurn );
	}

	void ControlOjbect( object_control_s* pObjctl, Vector3* pDesiredVelocity, fixed_t _DesiredSpeed );
};


// bot character properties ---------------------------------------------------
//
class BOT_Character
{
protected:
	float		m_fPlanInterval;			// planning interval in secs 
	float		m_fGoalCheckInterval;		// goal checking interval in secs
	float		m_fInputChangeInterval;		// input change interval in secs

public:

	//ctor
	BOT_Character();

	// reset to defaults
	void Reset();

	// select the attack target
	ShipObject* SelectAttackTarget( ShipObject* pAttacker );
	ExtraObject* SelectEnergyObject();
	ExtraObject* SelectRepairObject();

	// getters
	float GetPlanInterval_sec() const			{ return m_fPlanInterval; }
	float GetGoalCheckInterval_sec() const		{ return m_fGoalCheckInterval; }
	float GetInputChangeInterval_sec() const	{ return m_fInputChangeInterval; }

	// friend functions
	friend void RealizeBotChar();
};

// bot goal -------------------------------------------------------------------
//
class BOT_Goal
{
protected:
	Vector3		m_Pos;
	GenObject*  m_pObject;

public:
	BOT_Goal()
	{
		Reset();
	}

	void Reset()
	{
		memset( &m_Pos, 0, sizeof( Vector3 ) );
		m_pObject = NULL;
	}

	// get the position the goal is at
	Vector3* GetGoalPosition()
	{ 
		return &m_Pos; 
	}

	// set/get target object
	void		SetTargetObject( GenObject* pObject ) { m_pObject = pObject; }
	GenObject*	GetTargetObject() const { return m_pObject; }
};


// current state of the bot ---------------------------------------------------
//
class BOT_State
{
protected:
	BOT_Goal			m_CurGoal;
	object_control_s	m_oc;
public:

	BOT_State()
	{
	}

	// reset the dynamic state of the bot
	void Reset( ShipObject* pShip )
	{
		m_CurGoal.Reset();
		memset( &m_oc, 0, sizeof( object_control_s ) );
		m_oc.pShip = pShip;
	}

	BOT_Goal*			GetCurGoal()		{ return &m_CurGoal; }
	object_control_s*	GetObjectControl()	{ return &m_oc; }
};


// type specifying the agent modes --------------------------------------------
//
enum agentmode_t {
	AGENTMODE_IDLE			= 1,
	AGENTMODE_POWERUP		= 2,
	AGENTMODE_ATTACK		= 3,
	AGENTMODE_RETREAT		= 4,

	AGENTMODE_MAX			= AGENTMODE_RETREAT
};

// main class for handling the bot AI -----------------------------------------
//
class BOT_AI
{
protected:
	agentmode_t			m_nAgentMode;
	BOT_State			m_State;
	BOT_Character		m_Character;
	ShipObject*			m_pShip;

	UTL_RefFrameTimeout	m_PlanTimeout;
	UTL_RefFrameTimeout	m_GoalCheckTimeout;
	UTL_RefFrameTimeout	m_InputTimeout;

	Vector3				m_AgentPos;

public:

	BOT_AI()
	{
		Reset();
	}

	// reset the bot AI
	void Reset()
	{
		//m_nAgentMode	= AGENTMODE_IDLE;
		m_nAgentMode	= AGENTMODE_ATTACK;
		m_pShip         = NULL;
		m_State.Reset( m_pShip );

		m_PlanTimeout.Set	( (refframe_t)m_Character.GetPlanInterval_sec()        * FRAME_MEASURE_TIMEBASE );
		m_GoalCheckTimeout.Set	( (refframe_t)m_Character.GetGoalCheckInterval_sec()   * FRAME_MEASURE_TIMEBASE );
		m_InputTimeout.Set	( (refframe_t)m_Character.GetInputChangeInterval_sec() * FRAME_MEASURE_TIMEBASE );
	}

	// set the ship object we want to control
	void SetShipObject( ShipObject* pShip ) 
	{
		m_pShip = pShip;
		m_State.Reset( pShip );
	}



	// main think function, called every render or sim frame ( client or server )
	void DoThink();

	// get the character of the bot
	BOT_Character* GetCharacter() { return &m_Character; }

protected:

	void _DoPlan();

	void _GoalCheck_AgentMode_Idle();
	void _GoalCheck_AgentMode_Powerup();
	void _GoalCheck_AgentMode_Attack();
	void _GoalCheck_AgentMode_Retreat();

	void _SteerToPosition( Vector3* targetPos, object_control_s* pObjctl );
};



// easy access to the singleton -----------------------------------------------
//
#define TheBot		( BOT_ClientSide::GetClientSideBot() )


// forward decls --------------------------------------------------------------
//
PRIVATE int BOT_CallThink( void* param );

// the timeout in refframes to wait between status checks ---------------------
//
#define BOT_STATUS_CHECK_TIMEOUT		DEFAULT_REFFRAME_FREQUENCY

// class for the client-side bot ----------------------------------------------
//
class BOT_ClientSide : public BOT_AI
{
protected:

	// standard ctor
	BOT_ClientSide() :
		 m_Started( FALSE ),
		 m_bWantToBeConnected( TRUE ),
		 m_bWantToBeJoined( TRUE ),
		 m_NextStatusCheckRefFrame( 0 )
	{
		memset( m_szServer, 0, MAX_SERVER_NAME * sizeof( char ) );
	}

	// standard dtor
	~BOT_ClientSide()
	{
	}

	// check whether current status (connected/joined etc. ) matches the desired one
	void _CheckStatus();

protected:
	int			m_Started;
	char		m_szServer[ MAX_SERVER_NAME ];

	int			m_bWantToBeConnected;
	int			m_bWantToBeJoined;

	// next refframe, we check for connect/disconnect/joined/unjoined
	refframe_t	m_NextStatusCheckRefFrame;			

public:
	// SINGLETON pattern
	static BOT_ClientSide* GetClientSideBot()
	{
		static BOT_ClientSide _TheClientSideBot;
		return &_TheClientSideBot;
	}

	// set/get of status flags
	int		GetDesiredConnStatus() const { return m_bWantToBeConnected; }
	int		GetDesiredJoinStatus() const { return m_bWantToBeJoined; }
	void	SetDesiredConnStatus( int bWantToBeConnected )	{ m_bWantToBeConnected = bWantToBeConnected; }
	void	SetDesiredJoinStatus( int bWantToBeJoined )		{ m_bWantToBeJoined = bWantToBeJoined; }

	// overwritten think method to allow special client-side things
	void DoThink();

	// start the bot
	void Start();

	// stop the bot
	void Stop();

	// get the servername the client bot should connect to
	char* GetConnectServer()
	{
		return m_szServer;
	}

	// set the server we want to connect to
	void SetConnectServer( char* pszServer )
	{
		// trim left
		char *p = NULL;
		for( p = pszServer; *p == ' '; p++ );

		if ( strncmp( p, "\"\"", 2 ) == 0 ) {
			m_szServer[ 0 ] = '\0';
		} else {
			strncpy( m_szServer, p, MAX_SERVER_NAME );
		}
	}
};

#endif // G_BOT_CL_H_