// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/15 22:54:08 $
$Name: rel-0-3-patches $
$Revision: 1.7 $
$State: Exp $

$Log: colission.cpp,v $
Revision 1.7  2006/03/15 22:54:08  cavac
* Added property/methods for setting FG objects as "ontop" which renders
them on top of everything.
* Changed levelhandler.* so that it uses the newly added "AddPixel" function
instead of doing it internally in the file parser state machine.
* Added functionality for bonus live collectables
* Adapted lua bindings so it can utilize above properties
* Removed l2b prefix from lua bindings and adapted scripts
* Added temporary 32x32 explosion graphics
* Adapted level0.bsl (Testlevel/scripting) so it has a small riddle:
  You have to turn on all switches and press the emergency stop button
  while the neon sign is ON (e.g. it lights up yellow). two small
  explosions leave one pixel and one bonus live collectible behind.
  (You can NOT finish the level without collecting the second pixel!)

Revision 1.6  2006/03/02 13:03:41  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.5  2006/02/24 18:48:26  cavac
Finally added real scripting support
Added Testlevel (level0) which can be enabled from globals.h

Revision 1.4  2006/02/17 19:07:59  cavac
Added rudimentary sound effects (FX) support

Revision 1.3  2006/01/23 18:47:55  cavac
Patches for unix make system for building on Linux  and other
   Unix systems (thanks to: Dauti)
Tests for new colission detection (default: Use old one)
Added Joystick/Gamepad support (only tested with a cheap logitech gamepad)
Fixed some warnings on linux/gcc3
Added true-type fonts for planned menu system
Added library SDL_ttf in xcode for planned menu-system

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include "globals.h"
#include "colission.h"
#include "levelhandler.h"
#include "engine.h"
#include "monstersprites.h"
#include "gameengine.h"
#include "sound.h"
#include "fgobjects.h"

#ifdef USE_NEWCOLDETECT
// Detects wich quadrant the colission is in, if on an edge, which is the MAIN direction of movement
//				 +---+----------------+----+
//				 |C1 |	  C2		|C3  |
//				 +---+----------------+----+
//				 |   |				|	|
//				 |   |				|	|
//				 |C4 |	 C5		 |C6  |
//				 |   |				|	|
//				 +---+----------------+----+
//				 |C7 |	 C8		 |C9  |
//				 +---+----------------+----+

COL_TYPE checkMainColDir(Sint32 srcx, Sint32 srcy, double srcvx, double srcvy, Sint32 destx, Sint32 desty) {
	Uint32 sect = 0;
	COL_TYPE coldir = COL_NONE;
	
	sect = 0;
	coldir = COL_NONE;
	
	// Sector for C1 C2 C3
	if((srcy + TILESIZE) < (desty/* + SPRITE_MAXSPEED + 1*/)) {
		sect = 1;
	// Sector for C7 C8 C9
	} else if((srcy/* - SPRITE_MAXSPEED -1*/) >= (desty + TILESIZE)) {
		sect = 7;
	// Sector for C4 C5 C6
	} else {
		sect = 4;
	}

	// Sector for C1 C4 C7
	if((srcx + TILESIZE) < (destx/* + SPRITE_MAXSPEED + 1*/)) {
		//sect += 0;
		// Sector for C3 C6 C9
	} else if((srcx /*- SPRITE_MAXSPEED -1*/) >= (destx + TILESIZE)) {
		sect += 2;
		// Sector for C2 C5 C8
	} else {
		sect += 1;
	}
	
	switch(sect) {
		case 0:
			coldir = COL_NONE;
			break;
		case 1:
			if(srcvx > 0 && srcvx > srcvy) {
				coldir = COL_RIGHT;
			} else {
				coldir = COL_DOWN;
			}
			break;
		case 2:
			coldir = COL_DOWN;
			break;
		case 3:
			if(srcvx < 0 && (-srcvx) > srcvy) {
				coldir = COL_LEFT;
			} else {
				coldir = COL_DOWN;
			}
			break;
		case 4:
			coldir = COL_RIGHT;
			break;
		case 5:
			// No colission possible
			printf("ERROR: Collission is more than SPRITE_MAXSPEED in middle of tile\n");
			break;
		case 6:
			coldir = COL_LEFT;
			break;
		case 7:
			if(srcvx > 0 && (-srcvy) > srcvx) {
				coldir = COL_RIGHT;
			} else {
				coldir = COL_UP;
			}
			break;
		case 8:
			coldir = COL_UP;
			break;
		case 9:
			if(srcvx < 0 && (-srcvy) > (-srcvx)) {
				coldir = COL_LEFT;
			} else {
				coldir = COL_RIGHT;
			}
			break;
		default:
			printf("ERROR: UNKNOWN SECTION %d\n", sect);
			exit(1);
	}
	
	switch(coldir) {
		case COL_UP:
			printf("COL: UP\n");
			break;
		case COL_DOWN:
			printf("COL: DOWN\n");
			break;
		case COL_LEFT:
			printf("COL: LEFT\n");
			break;
		case COL_RIGHT:
			printf("COL: RIGHT\n");
			break;
		default:
			break;
	}
	
	return coldir;
}
#endif // USE_NEWCOLDETECT				
	

Uint32 getTilesColission(Uint32 playerx, Uint32 playery) {
	//Uint32 cols = COL_NONE;
	Uint32 cols = getFGObjsColission(playerx, playery);
	
	Uint32 playerxmax = playerx + TILESIZE;
	Uint32 playerymax = playery + TILESIZE;
	Uint32 playerxmid = playerx + TILESIZE / 2;
	Uint32 playerymid = playery + TILESIZE / 2;
	
	DISPLAYLIST *tthis = lhandle.dlist;
	while(tthis) {
		
		// COL_DOWN
		if(playerxmax > tthis->x && playerx < (tthis->x + TILESIZE) && playerymax > tthis->y && playerymid < tthis->y && spritevy > 0) {
			isJumping = 0;
			// Ignore Gravity when down on the ground;
			if(spritevy <= SPRITE_FALLSILENTTHRESHOLD && spritevy > 0) {  // FIXME
				spritevy = 0;
				spritey -= SPRITE_GRAVITY*2;
				return getTilesColission((Uint32)spritex, (Uint32)spritey);
			} else {
				cols =  cols | COL_DOWN;
			}
		}
		
		// COL_UP
		if(playerxmax > tthis->x && playerx < (tthis->x + TILESIZE) && playery < (tthis->y + TILESIZE) && playerymid > tthis->y && spritevy < 0) {
			cols = cols | COL_UP;
		}
		
		// COL_RIGHT
		if(playerymax > tthis->y && playery < (tthis->y + TILESIZE) && playerxmax >= tthis->x && playerxmid < tthis->x && spritevx > 0) {
			cols = cols | COL_RIGHT;
		}
		// COL_LEFT
		if(playerymax > tthis->y && playery < (tthis->y + TILESIZE) && playerx <= (tthis->x + TILESIZE) && playerxmid > tthis->x && spritevx < 0) {
			cols = cols | COL_LEFT;
		}
		
		tthis = tthis->next;
	}
	
	return (COL_TYPE)cols;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// FG Object colission is in fgobjects.cpp
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Uint32 getPlayerMonsterColission(Uint32 playerx, Uint32 playery) {
	Uint32 cols = COL_NONE;
	
	Uint32 playerxmax = playerx + TILESIZE;
	Uint32 playerymax = playery + TILESIZE- 1;
	Uint32 playerxmid = playerx + TILESIZE / 2;
	Uint32 playerymid = playery + TILESIZE / 2;
	
	MONSTERS *tthis = lhandle.monsters;
	while(tthis) {
		if(tthis->isAlive) {
			// COL_DOWN
			if(playerxmax > tthis->monsterx && playerx < (tthis->monsterx + TILESIZE) && playerymax > tthis->monstery && playerymid < tthis->monstery && spritevy > 0) {
				tthis->isAlive = false;
				tthis->isDying = true;
				gamedata.player->score += tthis->score;
				cols =  cols | COL_DOWN;
				soundPlayFX(FX_KILL_MONSTER);
			}
			
			// COL_UP
			else if(playerxmax > tthis->monsterx && playerx < (tthis->monsterx + TILESIZE) && playery < (tthis->monstery + TILESIZE) && playerymid > tthis->monstery && spritevy < 0) {
				cols = cols | COL_UP;
			}
			
			// COL_RIGHT
			else if(playerymax > tthis->monstery && playery < (tthis->monstery + TILESIZE) && playerxmax >= tthis->monsterx && playerxmid < tthis->monsterx) {
				cols = cols | COL_RIGHT;
			}
			// COL_LEFT
			else if(playerymax > tthis->monstery && playery < (tthis->monstery + TILESIZE) && playerx <= (tthis->monsterx + TILESIZE) && playerxmid > tthis->monsterx) {
				cols = cols | COL_LEFT;
			}
		}
		tthis = tthis->next;
	}
	
	return (COL_TYPE)cols;
}


Uint32 getTilesMonsterColission(MONSTERS* monster) {
	Uint32 cols = COL_NONE;
	
	Uint32 monsterx = (Uint32)monster->monsterx;
	Uint32 monstery = (Uint32)monster->monstery;
	Uint32 monsterxmax = monsterx + TILESIZE;
	Uint32 monsterymax = monstery + TILESIZE;
	Uint32 monsterxmid = monsterx + TILESIZE / 2;
	//Uint32 playerymid = playery + TILESIZE / 2;
	
	DISPLAYLIST *tthis = lhandle.dlist;
	while(tthis) {		
		// COL_RIGHT
		if(monsterymax > tthis->y && monstery < (tthis->y + TILESIZE) && monsterxmax >= tthis->x && monsterxmid < tthis->x && monster->monstervx > 0) {
			cols = cols | COL_RIGHT;
		}
		// COL_LEFT
		if(monsterymax > tthis->y && monstery < (tthis->y + TILESIZE) && monsterx <= (tthis->x + TILESIZE) && monsterxmid > tthis->x && monster->monstervx < 0) {
			cols = cols | COL_LEFT;
		}
		
		tthis = tthis->next;
	}
	
	return (COL_TYPE)cols;
}


void handlePixelCollission(Uint32 playerx, Uint32 playery) {
	DISPLAYLIST *tthis = lhandle.pixels;
	Uint32 playerxmax = playerx + TILESIZE;
	Uint32 playerymax = playery + TILESIZE;
	
	while(tthis) {
		if(tthis->type) {
			if(playerxmax >= tthis->x && playerx < (tthis->x + TILESIZE) && playerymax >= tthis->y && playery < (tthis->y + TILESIZE)) {
				if(tthis->type == PIXELTYPE_PIXEL) {
					foundPixels++;
					gamedata.player->score += PIXEL_SCORE;
					soundPlayFX(FX_COLLECT_PIXEL);
				} else {
					gamedata.player->lives++;
					soundPlayFX(FX_LEVEL_FINISHED);
				}
				
				tthis->type = PIXELTYPE_NONE;
			}
		}
		tthis = tthis->next;
	}
}
