DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

i_input.cpp (9222B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #include "Precompiled.h"
     30 #include "globaldata.h"
     31 
     32 #include <stdlib.h>
     33 #include <stdarg.h>
     34 #include <sys/types.h>
     35 
     36 #include "i_video.h"
     37 #include "i_system.h"
     38 
     39 #include "doomstat.h"
     40 #include "v_video.h"
     41 #include "m_argv.h"
     42 #include "d_main.h"
     43 
     44 #include "doomdef.h"
     45 
     46 #include "sys/sys_public.h"
     47 
     48 #define ALLOW_CHEATS	1
     49 
     50 
     51 
     52 extern int PLAYERCOUNT;
     53 
     54 #define NUM_BUTTONS 4
     55 
     56 static bool Cheat_God( void ) {
     57 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
     58 		return false;
     59 	}
     60 	::g->plyr->cheats ^= CF_GODMODE;
     61 	if (::g->plyr->cheats & CF_GODMODE)
     62 	{
     63 		if (::g->plyr->mo)
     64 			::g->plyr->mo->health = 100;
     65 
     66 		::g->plyr->health = 100;
     67 		::g->plyr->message = STSTR_DQDON;
     68 	}
     69 	else 
     70 		::g->plyr->message = STSTR_DQDOFF;
     71 	return true;
     72 }
     73 
     74 #include "g_game.h"
     75 static bool Cheat_NextLevel( void ) {
     76 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
     77 		return false;
     78 	}
     79 	G_ExitLevel();
     80 
     81 	return true;
     82 }
     83 
     84 static bool Cheat_GiveAll( void ) {
     85 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
     86 		return false;
     87 	}
     88 
     89 	::g->plyr->armorpoints = 200;
     90 	::g->plyr->armortype = 2;
     91 
     92 	int i;
     93 	for (i=0;i<NUMWEAPONS;i++)
     94 		::g->plyr->weaponowned[i] = true;
     95 
     96 	for (i=0;i<NUMAMMO;i++)
     97 		::g->plyr->ammo[i] = ::g->plyr->maxammo[i];
     98 
     99 	for (i=0;i<NUMCARDS;i++)
    100 		::g->plyr->cards[i] = true;
    101 
    102 	::g->plyr->message = STSTR_KFAADDED;
    103 	return true;
    104 }
    105 
    106 static bool Cheat_GiveAmmo( void ) {
    107 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    108 		return false;
    109 	}
    110 	::g->plyr->armorpoints = 200;
    111 	::g->plyr->armortype = 2;
    112 
    113 	int i;
    114 	for (i=0;i<NUMWEAPONS;i++)
    115 		::g->plyr->weaponowned[i] = true;
    116 
    117 	for (i=0;i<NUMAMMO;i++)
    118 		::g->plyr->ammo[i] = ::g->plyr->maxammo[i];
    119 
    120 	::g->plyr->message = STSTR_KFAADDED;
    121 	return true;
    122 }
    123 
    124 static bool Cheat_Choppers( void ) {
    125 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    126 		return false;
    127 	}
    128 	::g->plyr->weaponowned[wp_chainsaw] = true;
    129 	::g->plyr->message = "Chainsaw!";
    130 	return true;
    131 }
    132 
    133 extern qboolean P_GivePower ( player_t*	player, int /*powertype_t*/	power );
    134 
    135 static void TogglePowerUp( int i ) {
    136 	if (!::g->plyr->powers[i])
    137 		P_GivePower( ::g->plyr, i);
    138 	else if (i!=pw_strength)
    139 		::g->plyr->powers[i] = 1;
    140 	else
    141 		::g->plyr->powers[i] = 0;
    142 
    143 	::g->plyr->message = STSTR_BEHOLDX;
    144 }
    145 
    146 static bool Cheat_GiveInvul( void ) {
    147 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    148 		return false;
    149 	}
    150 
    151 	TogglePowerUp( 0 );
    152 	return true;
    153 }
    154 
    155 static bool Cheat_GiveBerserk( void ) {
    156 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    157 		return false;
    158 	}
    159 
    160 	TogglePowerUp( 1 );
    161 	return true;
    162 }
    163 
    164 static bool Cheat_GiveBlur( void ) {
    165 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    166 		return false;
    167 	}
    168 
    169 	TogglePowerUp( 2 );
    170 	return true;
    171 }
    172 
    173 static bool Cheat_GiveRad( void ) {
    174 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    175 		return false;
    176 	}
    177 
    178 	TogglePowerUp( 3 );
    179 	return true;
    180 }
    181 
    182 static bool Cheat_GiveMap( void ) {
    183 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    184 		return false;
    185 	}
    186 
    187 	TogglePowerUp( 4 );
    188 	return true;
    189 }
    190 
    191 static bool Cheat_GiveLight( void ) {
    192 	if( PLAYERCOUNT != 1 || ::g->netgame ) {
    193 		return false;
    194 	}
    195 
    196 	TogglePowerUp( 5 );
    197 	return true;
    198 }
    199 
    200 
    201 
    202 #ifndef __PS3__
    203 
    204 static bool			tracking		= false;
    205 static int			currentCode[NUM_BUTTONS];
    206 static int			currentCheatLength;
    207 
    208 #endif
    209 
    210 typedef bool(*cheat_command)(void);
    211 struct cheatcode_t
    212 {
    213 	int			code[NUM_BUTTONS];
    214 	cheat_command	function;
    215 };
    216 
    217 static cheatcode_t codes[] = {
    218 	{ {0, 1, 1, 0}, Cheat_God }, // a b b a
    219 	{ {0, 0, 1, 1}, Cheat_NextLevel }, // a a b b
    220 	{ {1, 0, 1, 0}, Cheat_GiveAmmo }, // b a b a
    221 	{ {1, 1, 0, 0}, Cheat_Choppers}, // b b a a
    222 	{ {0, 1, 0, 1}, Cheat_GiveAll },  // a b a b
    223 	{ {2, 3, 3, 2}, Cheat_GiveInvul }, // x y y x
    224 	{ {2, 2, 2, 3}, Cheat_GiveBerserk }, // x x x y
    225 	{ {2, 2, 3, 3}, Cheat_GiveBlur }, // x x y y
    226 	{ {2, 3, 3, 3}, Cheat_GiveRad }, // x y y y
    227 	{ {3, 2, 3, 2}, Cheat_GiveMap }, // y x y x
    228 	{ {3, 3, 3, 2}, Cheat_GiveLight}, // y y y x
    229 };
    230 
    231 const static int numberOfCodes = sizeof(codes) / sizeof(codes[0]);
    232 
    233 
    234 void BeginTrackingCheat( void ) {
    235 #if ALLOW_CHEATS
    236 	tracking = true;
    237 	currentCheatLength = 0;
    238 	memset( currentCode, 0, sizeof( currentCode ) );
    239 #endif
    240 }
    241 
    242 void EndTrackingCheat( void ) {
    243 #if ALLOW_CHEATS
    244 	tracking = false;
    245 #endif
    246 }
    247 
    248 extern void S_StartSound ( void*		origin, int		sfx_id );
    249 
    250 void CheckCheat( int button ) {
    251 #if ALLOW_CHEATS
    252 	if( tracking && !::g->netgame ) {
    253 
    254 		currentCode[ currentCheatLength++ ] = button;
    255 
    256 		if( currentCheatLength == NUM_BUTTONS ) {
    257 			for( int i = 0; i < numberOfCodes; ++i) {
    258 				if( memcmp( &codes[i].code[0], &currentCode[0], sizeof(currentCode) ) == 0 ) {
    259 					if(codes[i].function()) {
    260 						S_StartSound(0, sfx_cybsit);
    261 					}
    262 				}
    263 			}
    264 			// reset the code
    265 			memset( currentCode, 0, sizeof( currentCode ) );
    266 			currentCheatLength = 0;
    267 		}
    268 	}
    269 #endif
    270 }
    271 
    272 
    273 
    274 float xbox_deadzone = 0.28f;
    275 
    276 // input event storage
    277 //PRIVATE TO THE INPUT THREAD!
    278 
    279 
    280 
    281 void I_InitInput(void)
    282 {
    283 }
    284 
    285 void I_ShutdownInput( void ) 
    286 {
    287 }
    288 
    289 
    290 static float _joyAxisConvert(short x, float xbxScale, float dScale, float deadZone)
    291 {
    292 	//const float signConverted = x - 127;
    293 	float y = x - 127;
    294 	y		= y / xbxScale;
    295 	return (fabs(y) < deadZone) ? 0.f : (y * dScale);
    296 }
    297 
    298 
    299 int I_PollMouseInputEvents( controller_t *con) 
    300 {
    301 	int numEvents = 0;
    302 
    303 	return numEvents;
    304 }
    305 
    306 int I_ReturnMouseInputEvent( const int n, event_t* e) {
    307 	e->type = ev_mouse;
    308 	e->data1 = e->data2 = e->data3 = 0;
    309 
    310 	switch(::g->mouseEvents[n].type) {
    311 	case IETAxis:
    312 		switch (::g->mouseEvents[n].action)
    313 		{
    314 		case M_DELTAX:
    315 			e->data2 = ::g->mouseEvents[n].data;
    316 			break;
    317 		case M_DELTAY:
    318 			e->data3 = ::g->mouseEvents[n].data;
    319 			break;
    320 		}
    321 		return 1;
    322 
    323 	default:
    324 		break;
    325 	}
    326 	return 0;
    327 }
    328 
    329 int I_PollJoystickInputEvents( controller_t *con ) {
    330 	int numEvents	= 0;
    331 
    332 	return numEvents;
    333 }
    334 
    335 //
    336 //  Translates the key currently in X_event
    337 //
    338 static int xlatekey(int key)
    339 {
    340 	int rc = KEY_F1;
    341 	
    342 	switch (key)
    343 	{
    344 	case 0:	// A
    345 		//rc = KEY_ENTER;
    346 		rc = ' ';
    347 		break;
    348 	case 3: // Y
    349 		rc = '1';
    350 		break;
    351 	case 1:	// B
    352 		if( ::g->menuactive ) {
    353 			rc = KEY_BACKSPACE;
    354 		}
    355 		else {
    356 			rc = '2';
    357 		}
    358 		break;
    359 	case 2: // X
    360 		//rc = ' ';
    361 		rc = KEY_TAB;
    362 		break;
    363 	case 4:	// White
    364 		rc = KEY_MINUS;
    365 		break;
    366 	case 5: // Black	
    367 		rc = KEY_EQUALS;
    368 		break;
    369 	case 6: // Left triggers
    370 		rc = KEY_RSHIFT;
    371 		break;
    372 	case 7: // Right
    373 		rc = KEY_RCTRL;
    374 		break;
    375 	case 8:	// Up
    376 		if( ::g->menuactive ) {
    377 			rc = KEY_UPARROW;
    378 		}
    379 		else {
    380 			//rc = KEY_ENTER;
    381 			rc = '3';
    382 		}
    383 		break;
    384 	case 9:
    385 		if( ::g->menuactive ) {
    386 			rc = KEY_DOWNARROW;
    387 		}
    388 		else {
    389 			//rc = KEY_TAB;
    390 			rc = '5';
    391 		}
    392 		break;
    393 	case 10:
    394 		if( ::g->menuactive ) {
    395 			rc = KEY_UPARROW;
    396 		}
    397 		else {
    398 			//rc = '1';
    399 			rc = '6';
    400 		}
    401 		break;
    402 	case 11:
    403 		if( ::g->menuactive ) {
    404 			rc = KEY_DOWNARROW;
    405 		}
    406 		else {
    407 			//rc = '2';
    408 			rc = '4';
    409 		}
    410 		break;
    411 	case 12:	// start
    412 		rc = KEY_ESCAPE;
    413 		break;
    414 	case 13:	//select
    415 		//rc = KEY_ESCAPE;
    416 		break;
    417 	case 14:	// lclick
    418 	case 15:	// rclick
    419 		//rc = ' ';
    420 		break;
    421 	}
    422     return rc;
    423 }
    424 
    425 int I_ReturnJoystickInputEvent( const int n, event_t* e) {
    426 
    427 	e->data1 = e->data2 = e->data3 = 0;
    428 
    429 	switch(::g->joyEvents[n].type)
    430 	{
    431 	case IETAxis:
    432 		e->type = ev_joystick;//ev_mouse;
    433 		switch (::g->joyEvents[n].action)
    434 		{
    435 		case J_DELTAX:
    436 /*
    437 			if (::g->joyEvents[n].data < 0) 
    438 				e->data2 = -1;
    439 			else if (::g->joyEvents[n].data > 0)
    440 				e->data2 = 1;
    441 */
    442 			e->data2 = ::g->joyEvents[n].data;
    443 			break;
    444 		case J_DELTAY:
    445 			e->type = ev_mouse;
    446 			e->data3 = ::g->joyEvents[n].data;
    447 			break;
    448 		}
    449 		return 1;
    450 	case IETButtonAnalog:
    451 	case IETButtonDigital:
    452 		if (::g->joyEvents[n].data) 
    453 			e->type = ev_keydown;
    454 		else
    455 			e->type = ev_keyup;
    456 		e->data1 = xlatekey(::g->joyEvents[n].action);
    457 		return 1;
    458 
    459 	case IETNone:
    460 		break;
    461 	}
    462 
    463 	return 0;
    464 }
    465 
    466 void I_EndJoystickInputEvents( void ) {
    467 	int i;
    468 	for(i = 0; i < 18; i++)
    469 	{
    470 		::g->joyEvents[i].type = IETNone;
    471 	}
    472 
    473 }
    474