// 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: bl_lua_bindings.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/10 14:14:54  cavac
Removed "l2b" prefix from BlinkenLUA functions in scripting support

Revision 1.5  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.4  2006/02/26 14:35:55  cavac
Added docs for scripting

Revision 1.3  2006/02/24 20:55:03  cavac
Fixed some warnings

Revision 1.2  2006/02/24 20:25:39  cavac
Added sound-support to scripting engine

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

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

#include "globals.h"
#include "bl_lua_bindings.h"
#include "fgobjects.h"
#include "errorhandler.h"
#include "lua.h"
#include "sound.h"
#include "levelhandler.h"

static int l2bDie(lua_State *L) {
	if(!lua_isstring(L, 1)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		DIE(ERROR_LUAL2BDIE, (char *)lua_tostring(L, 1));
	}
	return 0;
}

static int l2bLoadGFX(lua_State *L) {
	char tmp[1000];
	if(!lua_isstring(L, 1)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		sprintf(tmp, "%s", lua_tostring(L, 1));
	}
	
	Uint32 idx = addFGObjGFX(tmp);
	lua_pushnumber(L, idx);
	
	return 1;
}

static int l2bAddObject(lua_State *L) {
	Uint32 gfxobj, x, y;
	gfxobj = x = y = 0;
	bool isBlocking = false;
	bool isVisible = false;
	bool isOnTop = false;
	if(!lua_isnumber(L, 1) || 
	   !lua_isnumber(L, 2) ||
	   !lua_isnumber(L, 3) ||
	   !lua_isnumber(L, 4) ||
	   !lua_isnumber(L, 5) ||
	   !lua_isnumber(L, 6)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		gfxobj = (Uint32)lua_tonumber(L, 1);
		x = (Uint32)lua_tonumber(L, 2) * TILESIZE;
		y = (Uint32)lua_tonumber(L, 3) * TILESIZE;
		if(lua_tonumber(L, 4) > 0) {
			isBlocking = true;
		}
		if(lua_tonumber(L, 5) > 0) {
			isVisible = true;
		}
		if(lua_tonumber(L, 6) > 0) {
			isOnTop = true;
		}
		
	}
	
	Uint32 idx = addFGObj(gfxobj, x, y, isBlocking, isVisible, isOnTop);
	lua_pushnumber(L, idx);
	
	return 1;
}


static int l2bSetVisible(lua_State *L) {
	Uint32 gfxobj = 0;
	bool isVisible = false;
	if(!lua_isnumber(L, 1) || 
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		gfxobj = (Uint32)lua_tonumber(L, 1);
		if(lua_tonumber(L, 2) > 0) {
			isVisible = true;
		}
		
	}
	
	setFGObjSetVisible(gfxobj, isVisible);

	return 0;
}

static int l2bSetBlocking(lua_State *L) {
	Uint32 gfxobj = 0;
	bool isBlocking = false;
	if(!lua_isnumber(L, 1) || 
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		gfxobj = (Uint32)lua_tonumber(L, 1);
		if(lua_tonumber(L, 2) > 0) {
			isBlocking = true;
		}
		
	}
	
	setFGObjSetBlocking(gfxobj, isBlocking);
	
	return 0;
}

static int l2bSetOnTop(lua_State *L) {
	Uint32 gfxobj = 0;
	bool isOnTop = false;
	if(!lua_isnumber(L, 1) || 
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		gfxobj = (Uint32)lua_tonumber(L, 1);
		if(lua_tonumber(L, 2) > 0) {
			isOnTop = true;
		}
		
	}
	
	setFGObjSetOnTop(gfxobj, isOnTop);
	
	return 0;
}

static int l2bSetGFX(lua_State *L) {
	Uint32 gfxobj, gfx;
	gfxobj = gfx = 0;
	if(!lua_isnumber(L, 1) || 
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		gfxobj = (Uint32)lua_tonumber(L, 1);
		gfx = (Uint32)lua_tonumber(L, 2);		
	}
	
	setFGObjSetGFX(gfxobj, gfx);
	
	return 0;
}

static int l2bLoadSoundFX(lua_State *L) {
	char tmp[1000];
	if(!lua_isstring(L, 1)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		sprintf(tmp, "%s", lua_tostring(L, 1));
	}
	
	Uint32 idx = addSoundFXLua(tmp);
	lua_pushnumber(L, idx);
	
	return 1;
}


//void soundPlayFXLua(Uint32 fx);
static int l2bPlaySoundFX(lua_State *L) {
	Uint32 fx = 0;
	if(!lua_isnumber(L, 1)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		fx = (Uint32)lua_tonumber(L, 1);		
	}
	
	soundPlayFXLua(fx);
	
	return 0;
}

static int l2bIncrementMaxPixels(lua_State *L) {
	Uint32 pixels = 0;
	if(!lua_isnumber(L, 1)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		pixels = (Uint32)lua_tonumber(L, 1);
	}
	
	incrementMaxPixels(pixels);
	
	return 0;
}

static int l2bAddPixel(lua_State *L) {
	Uint32 x = 0;
	Uint32 y = 0;
	if(!lua_isnumber(L, 1) ||
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		x = (Uint32)lua_tonumber(L, 1) * TILESIZE;
		y = (Uint32)lua_tonumber(L, 2) * TILESIZE;
	}
	
	addPixel(x, y, PIXELTYPE_PIXEL);
	
	return 0;
}

static int l2bAddBonusLive(lua_State *L) {
	Uint32 x = 0;
	Uint32 y = 0;
	if(!lua_isnumber(L, 1) ||
	   !lua_isnumber(L, 2)) {
		DIE(ERROR_LUAL2BTYPE, "");
	} else {
		x = (Uint32)lua_tonumber(L, 1) * TILESIZE;
		y = (Uint32)lua_tonumber(L, 2) * TILESIZE;
	}
	
	addPixel(x, y, PIXELTYPE_LIVE);
	
	return 0;
}


// To simplify registration of the binding function, use a macro
#define REGISTER(func,name) lua_pushcfunction(L, func); \
lua_setglobal(L, name);

void blRegisterLuaBindings(lua_State *L) {
	REGISTER(l2bDie, "Die");
	REGISTER(l2bLoadGFX, "LoadGFX");
	REGISTER(l2bAddObject, "AddObject");
	REGISTER(l2bSetVisible, "SetVisible");
	REGISTER(l2bSetBlocking, "SetBlocking");
	REGISTER(l2bSetOnTop, "SetOnTop");
	REGISTER(l2bSetGFX, "SetGFX");
	REGISTER(l2bLoadSoundFX, "LoadSoundFX");
	REGISTER(l2bPlaySoundFX, "PlaySoundFX");
	REGISTER(l2bIncrementMaxPixels, "IncMaxPixels");
	REGISTER(l2bAddPixel, "AddPixel");
	REGISTER(l2bAddBonusLive, "AddBonusLive");
}
