aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/isdl_supp.cpp
blob: 647e7f5b24e61a9e3a4c0e6959d8f49b72eee518 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * PARSEC - Support Functions
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:39 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   2000
 *   Copyright (c) Clemens Beer        <cbx@parsec.org>   1999
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

// compilation flags/debug support
#include "config.h"


// C library
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"


// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "inp_defs.h"

// local module header
#include "isdl_supp.h"

// proprietary headers
#include "con_int.h"
#include "inp_user.h"


// flags
//#define NEW_JOYBUTTON_CODE // Wtf is this crap?



// joystick globals -----------------------------------------------------------
//
//int			isdl_bDisableJoystick = TRUE;	// joystick disabled
joystate_s	JoyState;						// generic joystick data
int			isdl_bHasThrottle = 0;			// indicates, the joystick has a throttle
int			isdl_bHasRudder   = 0;			// indicates, the joystick has a rudder


// shoot gun if activated -----------------------------------------------------
//
int ISDL_ActivateGun()
{
	// keyboard
	if ( DepressedKeys->key_ShootWeapon ) {
		return TRUE;
	}

	// joystick
	if ( QueryJoystick && Op_Joystick ) {

		if ( JoyState.Buttons[ 0 ] ) {
			return TRUE;
		}

	}
	// mouse
	if ( Op_Mouse ) {

		mousestate_s mousestate;
		int mouseavailable = INPs_MouseGetState( &mousestate );
		if ( mouseavailable ) {
			return ( mousestate.buttons[ MOUSE_BUTTON_LEFT ] == MOUSE_BUTTON_PRESSED );
		}
	}

	return FALSE;
}


// launch missile if activated ------------------------------------------------
//
int ISDL_ActivateMissile()
{
	// keyboard
	if ( DepressedKeys->key_LaunchMissile ) {
		return TRUE;
	}

	// joystick
	if ( QueryJoystick && Op_Joystick ) {

		if ( JoyState.Buttons[ 1 ] ) {
			return TRUE;
		}

	}
	// mouse
	if ( Op_Mouse ) {

		mousestate_s mousestate;
		int mouseavailable = INPs_MouseGetState( &mousestate );
		if ( mouseavailable ) {
			return ( mousestate.buttons[ MOUSE_BUTTON_RIGHT ] == MOUSE_BUTTON_PRESSED );
		}
	}

	return FALSE;
}


// ----------------------------------------------------------------------------
//
PRIVATE fixed_t ISDLm_target_speed = 0;
PRIVATE fixed_t ISDLm_OldCurSpeed = 0;
PRIVATE fixed_t ISDLm_oldSetSpeed = -1;


// ----------------------------------------------------------------------------
//
void ISDLm_SetTargetSpeed(fixed_t _targetSpeed)
{
	// constrain to the min/max speed of a ship
	if ( _targetSpeed > MyShip->MaxSpeed )
		_targetSpeed = MyShip->MaxSpeed;
	if ( _targetSpeed < 0 )
		_targetSpeed = 0;

	ISDLm_target_speed = _targetSpeed;
	ISDLm_OldCurSpeed  = MyShip->CurSpeed;
}


// ----------------------------------------------------------------------------
//
void ISDLm_DoAcceleration()
{
	if ( ISDLm_OldCurSpeed == MyShip->CurSpeed ) {
		fixed_t speedDiff = ISDLm_target_speed - MyShip->CurSpeed;

		if ( speedDiff > 0 ) {
			INP_UserAcceleration( MyShip->SpeedIncPerRefFrame * CurScreenRefFrames );
			if ( ISDLm_target_speed < MyShip->CurSpeed )
				ISDLm_target_speed = MyShip->CurSpeed;
		} else if ( speedDiff < 0 ) {
			INP_UserAcceleration( -MyShip->SpeedDecPerRefFrame * CurScreenRefFrames );
			if ( ISDLm_target_speed > MyShip->CurSpeed )
				ISDLm_target_speed = MyShip->CurSpeed;
		}

		ISDLm_OldCurSpeed = MyShip->CurSpeed;
	}
}


// process joystick input for spacecraft motion -------------------------------
//
PRIVATE
void ISDLm_ProcessMotionJoystick()
{
	// may be disabled
	if ( !QueryJoystick || !Op_Joystick ) {
		return;
	}

	bams_t	c_angle;
	int	c_speed;

	if ( ( ( JoyState.Buttons[ 1 ] & 0x80 ) == 0 ) || ( isdl_bHasRudder && isdl_bHasThrottle && !ObjCameraActive ) ) {

		// button 2 not pressed + up/down -> pitch
		c_angle  = (bams_t) -JoyState.Y;
		c_angle *= MyShip->PitchPerRefFrame * CurScreenRefFrames;
		c_angle = (bams_t)(c_angle * joy_pitch_corr_refframe);

		if ( c_angle != 0 ) {
			INP_UserRotX( c_angle );
		}

		// button 2 not pressed + left/right -> yaw
		c_angle  = (bams_t) -JoyState.X;
		c_angle *= MyShip->YawPerRefFrame * CurScreenRefFrames;
		c_angle = (bams_t)(c_angle * joy_yaw_corr_refframe);

		if ( c_angle != 0 ) {
			INP_UserRotY( c_angle );
		}

		if ( isdl_bHasRudder ) {

			// rudder
			c_angle  = (bams_t) -JoyState.Rz;
			c_angle *= MyShip->RollPerRefFrame * CurScreenRefFrames;
			c_angle = (bams_t)(c_angle * joy_roll_corr_refframe);

			if ( c_angle != 0 ) {
				INP_UserRotZ( c_angle );
			}
		}


		if ( isdl_bHasThrottle ) {

			// throttle only when in flight mode
			if ( !ObjCameraActive ) {
				//static fixed_t ILm_oldSetSpeed = -1;

				fixed_t SetSpeed = (JOY_THROTTLE_MAX - JoyState.Z);
				SetSpeed *= (MyShip->MaxSpeed>>6);
				SetSpeed = (SetSpeed / JOY_THROTTLE_MAX);
				SetSpeed &= JOY_THROTTLE_DEADZONE_MASK;
				SetSpeed = SetSpeed << 6;

				if ( ISDLm_oldSetSpeed == -1 ) {
					ISDLm_oldSetSpeed = SetSpeed;
				} else if ( ISDLm_oldSetSpeed != SetSpeed ) {
					ISDLm_SetTargetSpeed( SetSpeed );
					ISDLm_oldSetSpeed = SetSpeed;
				}
				ISDLm_DoAcceleration();
			}
		}

	} else {

		// button2 + left/right -> roll
		c_angle  = (bams_t) -JoyState.X;
		c_angle *= MyShip->RollPerRefFrame * CurScreenRefFrames;
		c_angle = (bams_t)(c_angle * joy_roll_corr_refframe);

		if ( c_angle != 0 ) {
			INP_UserRotZ( c_angle );
		}

		// button2 + up/down -> accelerate/decelerate
		c_speed  = (bams_t) -JoyState.Y;
		c_speed *= MyShip->SpeedIncPerRefFrame * CurScreenRefFrames;
		c_speed = (int)(c_speed * joy_acc_corr_refframe);

		INP_UserAcceleration( c_speed );
	}
}


#define MOUSE_EDGE_EPS  0.1f

// process mouse input for spacecraft motion ----------------------------------
//
PRIVATE
void ISDLm_ProcessMotionMouse()
{
	// may be disabled
	if ( !Op_Mouse ) {
		return;
	}

	mousestate_s mousestate;
	int mouseavailable = INPs_MouseGetState( &mousestate );

	// make sure mouse is there
	if ( !mouseavailable ) {
		return;
	}

	static float last_xpos = 0.5f;
	static float last_ypos = 0.5f;

	if ( mousestate.xpos >= ( 1.0f - MOUSE_EDGE_EPS ) ) {
	
		mousestate.xpos -= 0.5f;
		last_xpos -= 0.5f;
		
		INPs_MouseSetState( &mousestate );
	}

	if ( mousestate.ypos >= ( 1.0f - MOUSE_EDGE_EPS ) ) {
	
		mousestate.ypos -= 0.5f;
		last_ypos -= 0.5f;
		
		INPs_MouseSetState( &mousestate );
	}
	
	if ( mousestate.xpos <= ( 0.0f + MOUSE_EDGE_EPS ) ) {
	
		mousestate.xpos += 0.5f;
		last_xpos += 0.5f;
		
		INPs_MouseSetState( &mousestate );
	}

	if ( mousestate.ypos <= ( 0.0f + MOUSE_EDGE_EPS ) ) {
	
		mousestate.ypos += 0.5f;
		last_ypos += 0.5f;
		
		INPs_MouseSetState( &mousestate );
	}


	float mappedxpos = mousestate.xpos;
	float mappedypos = mousestate.ypos;

	mappedxpos = ( mappedxpos - last_xpos ) * ( inp_mouse_sensitivity / 100.0f ) + 0.5f;
	mappedypos = ( mappedypos - last_ypos ) * ( inp_mouse_sensitivity / 100.0f ) + 0.5f;

	if ( mappedxpos < 0.0f ) {
		mappedxpos = 0.0f;
	}
	if ( mappedxpos > 1.0f ) {
		mappedxpos = 1.0f;
	}

	if ( mappedypos < 0.0f ) {
		mappedypos = 0.0f;
	}
	if ( mappedypos > 1.0f ) {
		mappedypos = 1.0f;
	}

	bams_t	c_angle;

	c_angle  = (bams_t) -( JOY_Y_MIN + JOY_Y_RANGE * mappedypos );
	c_angle *= MyShip->PitchPerRefFrame * CurScreenRefFrames;
	c_angle = (bams_t)(c_angle * joy_pitch_corr_refframe);

	if ( c_angle != 0 ) {
		INP_UserRotX( inp_mouse_invert_yaxis ? -c_angle : c_angle );
	}

	c_angle  = (bams_t) -( JOY_X_MIN + JOY_X_RANGE * mappedxpos );
	c_angle *= MyShip->YawPerRefFrame * CurScreenRefFrames;
	c_angle = (bams_t)(c_angle * joy_yaw_corr_refframe);

	if ( c_angle != 0 ) {
		INP_UserRotY( c_angle );
	}

	/*
	static float last_xpos = 0.5f;
	static float last_ypos = 0.5f;

	float posdifx = mousestate.xpos - last_xpos;
	float posdify = mousestate.ypos - last_ypos;

	last_xpos = mousestate.xpos;
	last_ypos = mousestate.ypos;

	if ( ( posdifx < -0.01f ) || ( posdifx > 0.01f ) ) {
		return;
	}
	if ( ( posdify < -0.01f ) || ( posdify > 0.01f ) ) {
		return;
	}
	*/

	if ( inp_mouse_drift < 0 ) {
		inp_mouse_drift = 0;
	}
	if ( inp_mouse_drift > 100 ) {
		inp_mouse_drift = 100;
	}

	float mousedrift = ( inp_mouse_drift / 1000.0f ) * CurScreenRefFrames;
	if ( mousedrift > 1.0f ) {
		mousedrift = 1.0f;
	}

	if ( last_xpos > mousestate.xpos ) {
		last_xpos -= ( last_xpos - mousestate.xpos ) * mousedrift;
	} else {
		last_xpos += ( mousestate.xpos - last_xpos ) * mousedrift;
	}

	if ( last_ypos > mousestate.ypos ) {
		last_ypos -= ( last_ypos - mousestate.ypos ) * mousedrift;
	} else {
		last_ypos += ( mousestate.ypos - last_ypos ) * mousedrift;
	}

	/*
	float mousedrift = ( inp_mouse_drift / 1000.0f ) * CurScreenRefFrames;
	if ( mousedrift > 1.0f ) {
		mousedrift = 1.0f;
	}

	if ( mousestate.xpos > 0.5f ) {
		mousestate.xpos -= ( mousestate.xpos - 0.5f ) * mousedrift;
	} else {
		mousestate.xpos += ( 0.5f - mousestate.xpos ) * mousedrift;
	}

	if ( mousestate.ypos > 0.5f ) {
		mousestate.ypos -= ( mousestate.ypos - 0.5f ) * mousedrift;
	} else {
		mousestate.ypos += ( 0.5f - mousestate.ypos ) * mousedrift;
	}

	// set current drift destination
	INPs_MouseSetState( &mousestate );
	*/

}


// process additional input devices -------------------------------------------
//
void ISDL_UserProcessAuxInput()
{
#ifndef DISABLE_JOYSTICK_CODE
	ISDLm_ProcessMotionJoystick();
#endif
	ISDLm_ProcessMotionMouse();
}


// registration table for mouse config flags ----------------------------------
//
int_command_s inp_mouse_int_commands[] = {

	{ 0x01,	"inp.mouse_invert_yaxis", 0, 1,    &inp_mouse_invert_yaxis, NULL, NULL },
	{ 0x01,	"inp.mouse_sensitivity",  0, 2000, &inp_mouse_sensitivity,  NULL, NULL },
	{ 0x01,	"inp.mouse_drift",        0, 100,  &inp_mouse_drift,        NULL, NULL },
};

#define NUM_INP_MOUSE_INT_COMMANDS CALC_NUM_ARRAY_ENTRIES( inp_mouse_int_commands )


// module registration function -----------------------------------------------
//
REGISTER_MODULE( ISDL_SUPP )
{
	// register mouse config flags
	for ( unsigned int curcmd = 0; curcmd < NUM_INP_MOUSE_INT_COMMANDS; curcmd++ ) {
		CON_RegisterIntCommand( &inp_mouse_int_commands[ curcmd ] );
	}
}