
/*
	file : level.h

	Dragon Wings (c) 2005 Philipp Roßberger, Sebastian Roll
		www.einszunull.net
		www.sebastianroll.de

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any damages
	arising from the use of this software.

	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:

	1. The origin of this software must not be misrepresented; you must not
		claim that you wrote the original software. If you use this software
		in a product, an acknowledgment in the product documentation would be
		appreciated but is not required.
	2. Altered source versions must be plainly marked as such, and must not be
		misrepresented as being the original software.
	3. This notice may not be removed or altered from any source distribution.

	Used software libraries from other people (thank you very much):

	The Irrlicht Engine:
		Copyright (c) 2002-2005 Nikolaus Gebhardt
		www.irrlicht.sourceforge.net
  
	The Character Animation Library (cal3D):
		Copyright (c) 2001, 2002 Bruno 'Beosil' Heidelberger
		www.cal3d.sourceforge.net

	The FMOD audio library:
		Copyright (c), Firelight Technologies Pty, Ltd. 1999-2004
		www.fmod.org

	Article - Using Cal3D with Irrlicht:
		Copyright (c) Thomas Suter 2004
		www.thomas.webtracker.ch/jahia/Jahia/cache/off/pid/672

*/



#define LEVEL_DIMENSION_X			150.0f
#define LEVEL_DIMENSION_Y			150.0f
#define LEVEL_DIMENSION_Z			150.0f
#define LEVEL_DIMENSION_OFFSET		-128.0f
#define LEVEL_SCALE_X				0.5f
#define LEVEL_SCALE_Y				0.5f
#define LEVEL_SCALE_Z				0.5f
#define LEVEL_ROTATION_OFFSET		-10.0f
#define LEVEL_ROTATION_TRIM			0.1f



void parseXML(IXMLReader* xml)
{
	stringc nodeName = "";
	int objectCount = 0;

	while(xml && xml->read())
	{
		switch(xml->getNodeType())
		{
			case EXN_ELEMENT:
			{
				nodeName = xml->getNodeName();

				if ( nodeName.equals_ignore_case(stringc("level")) )
				{
					//levelName = xml->getAttributeValue(L"name");
					levelTime = xml->getAttributeValueAsInt(L"time");
					//levelPoints = xml->getAttributeValueAsInt(L"points");
				}
				else if ( nodeName.equals_ignore_case(stringc("config")) )
				{
					PLAYER_LEFTRIGHT_FLIP = xml->getAttributeValueAsInt(L"PLAYER_LEFTRIGHT_FLIP");
					PLAYER_WING_ROLL_FLIP = xml->getAttributeValueAsInt(L"PLAYER_WING_ROLL_FLIP");
					PLAYER_WING_ROLL_ARRAY_POS = xml->getAttributeValueAsInt(L"PLAYER_WING_ROLL_ARRAY_POS");
					PLAYER_LEFTRIGHT_DOWNGRADE = xml->getAttributeValueAsFloat(L"PLAYER_LEFTRIGHT_DOWNGRADE");
					PLAYER_WING_ANGLE_SCALE = xml->getAttributeValueAsFloat(L"PLAYER_WING_ANGLE_SCALE");
					PLAYER_WING_ROLL_SCALE = xml->getAttributeValueAsFloat(L"PLAYER_WING_ROLL_SCALE");
					PLAYER_LEFTRIGHT_SCALE = xml->getAttributeValueAsFloat(L"PLAYER_LEFTRIGHT_SCALE");
					CAMERA_Y_DIRECTION_MAX = xml->getAttributeValueAsFloat(L"CAMERA_Y_DIRECTION_MAX");
					TRACKER_BODY_ID_LEFTWING = xml->getAttributeValueAsInt(L"TRACKER_BODY_ID_LEFTWING");
					TRACKER_BODY_ID_RIGHTWING = xml->getAttributeValueAsInt(L"TRACKER_BODY_ID_RIGHTWING");
					CAMERA_FAR_VALUE = xml->getAttributeValueAsFloat(L"CAMERA_FAR_VALUE");
					TRACKER_BODY_ROLL_OFFSET = xml->getAttributeValueAsInt(L"TRACKER_BODY_ROLL_OFFSET");
					PLAYER_MAX_SPEED_Y = xml->getAttributeValueAsFloat(L"PLAYER_MAX_SPEED_Y");
					PLAYER_MAX_SPEED = xml->getAttributeValueAsFloat(L"PLAYER_MAX_SPEED");
				}
				else if ( nodeName.equals_ignore_case(stringc("player")) )
				{
					// Setup global cal3D Model
					stringc mesh = "";
					float x, y, z;
					float rx, ry, rz;
					float sx, sy, sz;
					mesh = xml->getAttributeValue(L"mesh");
					x = (xml->getAttributeValueAsFloat(L"pos_x") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_X;
					y = (xml->getAttributeValueAsFloat(L"pos_y") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_Y;
					z = (xml->getAttributeValueAsFloat(L"pos_z") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_Z;
					rx = xml->getAttributeValueAsFloat(L"rot_x") - LEVEL_ROTATION_OFFSET;
					ry = xml->getAttributeValueAsFloat(L"rot_y") - LEVEL_ROTATION_OFFSET;
					rz = xml->getAttributeValueAsFloat(L"rot_z") - LEVEL_ROTATION_OFFSET;
					sx = xml->getAttributeValueAsFloat(L"scale_x") * LEVEL_SCALE_X;
					sy = xml->getAttributeValueAsFloat(L"scale_y") * LEVEL_SCALE_X;
					sz = xml->getAttributeValueAsFloat(L"scale_z") * LEVEL_SCALE_X;

					ISceneNode* parent = smgr->getRootSceneNode();

					player.node = new CCal3DSceneNode(makeMediaString(mesh).c_str(), parent, smgr, -1);
					player.node->init();
					player.node->setPosition(v3(x,y,z));
					player.node->setRotation(v3(rx,ry,rz));
					player.node->setScale(v3(sx,sy,sz));

					// dbg:
					//player.node->setDebugDataVisible(true);
					//player.node->setMaterialFlag(EMF_LIGHTING, true);
				}
				else if ( nodeName.equals_ignore_case(stringc("object")) )
				{
					int objID = -1;
					stringc objType = "";
					stringc objMesh = "";
					float x, y, z;
					float rx, ry, rz;
					float sx, sy, sz;

					if (12 != xml->getAttributeCount())
					{
						std::cout << "parseXML() inconsistent object attributes: atrCnt=" << xml->getAttributeCount() << " (should be 12)" << endl;
					}

					objID = xml->getAttributeValueAsInt(L"id");
                    objectCount++;
					
					if (objectCount != objID)
					{
						cout << "parseXML() inconsistent object count: objCnt=" << objectCount << " objID=" << objID << endl;
					}

					objType = xml->getAttributeValue(L"type");
					objMesh = xml->getAttributeValue(L"mesh");
					x = (xml->getAttributeValueAsFloat(L"pos_x") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_X;
					y = (xml->getAttributeValueAsFloat(L"pos_y") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_Y;
					z = (xml->getAttributeValueAsFloat(L"pos_z") + LEVEL_DIMENSION_OFFSET) * LEVEL_DIMENSION_Z;
					rx = (xml->getAttributeValueAsFloat(L"rot_x") + LEVEL_ROTATION_OFFSET) * LEVEL_ROTATION_TRIM;
					ry = (xml->getAttributeValueAsFloat(L"rot_y") + LEVEL_ROTATION_OFFSET) * LEVEL_ROTATION_TRIM;
					rz = (xml->getAttributeValueAsFloat(L"rot_z") + LEVEL_ROTATION_OFFSET) * LEVEL_ROTATION_TRIM;
					sx = xml->getAttributeValueAsFloat(L"scale_x") * LEVEL_SCALE_X;
					sy = xml->getAttributeValueAsFloat(L"scale_y") * LEVEL_SCALE_Y;
					sz = xml->getAttributeValueAsFloat(L"scale_z") * LEVEL_SCALE_Z;

					IAnimatedMesh* mesh = smgr->getMesh(makeMediaString(objMesh).c_str());
					if (mesh)
					{
						// finaly, create an object
						ISceneNode* newNode = smgr->addMeshSceneNode(mesh->getMesh(0));
						//ISceneNode* newNode = smgr->addTestSceneNode(100.0f);
						//newNode->setMaterialTexture(0, driver->getTexture("media/wall.bmp"));

						//newNode->setDebugDataVisible(true);
						newNode->setMaterialFlag(EMF_LIGHTING, false);

						// set properties of object
						newNode->setID(objID);
						newNode->setPosition(v3(x, y, z));
						newNode->setRotation(v3(rx, ry, rz));
						newNode->setScale(v3(sx, sy, sz));

						// copy into our own structure
						Obj* newO = new Obj();
						newO->mesh = mesh->getMesh(0);
						newO->node = newNode;
						
						if (objType.equals_ignore_case(stringc("ring").c_str()))
						{
							// remove initial rotation on rings, because they must not rorate
							newO->rot = v3(0,0,0);
							newO->node->setMaterialFlag(EMF_LIGHTING, false);

							// do some fireworks around the ring
							newO->pssn = smgr->addParticleSystemSceneNode(false);
							newO->pssn->setPosition(newO->node->getPosition());
							newO->pssn->setParticleSize(dimension2d<f32>(40.0f, 40.0f));

							newO->pssn->setEmitter(0);
							IParticleEmitter* em = 0;
							em = newO->pssn->createBoxEmitter(aabbox3d<f32>(-7,0,-7,7,1,7), vector3df(0.0f,0.06f,0.0f), 80, 100, SColor(0,255,255,255), video::SColor(0,255,255,255), 3500, 6000, 45);
							newO->pssn->setEmitter(em);
                            em->drop(); 
                            
							newO->pssn->removeAllAffectors();
							IParticleAffector* paf = 0;
							paf = newO->pssn->createFadeOutParticleAffector();
							newO->pssn->addAffector(paf);
							paf->drop();
							
							newO->pssn->setMaterialFlag(EMF_LIGHTING, false);
							newO->pssn->setMaterialTexture(0, driver->getTexture("media/fire.bmp"));
							newO->pssn->setMaterialType(EMT_TRANSPARENT_VERTEX_ALPHA);

							// set initial position of emitter
							v3 circle(300.0f * sin(0.5f), 300.0f * cos(0.5f), 0);
							newO->pssn->setPosition(newO->node->getPosition() + circle);
														
							numLeftRings++;
							if (numLeftRings > 9)
								numLeftRings = 9;							
						}
						else
						{
							newO->rot = newNode->getRotation();
							newO->pssn = 0;
						}
						newO->name = objType;

						// insert into list
						objects.push_back(newO);
					}
				}
				else
				{
					cout << "parseXML() unknown node-name:" << nodeName.c_str() << endl;
				}
			}
		}
	}
}


void loadNextLevel()
{
	// todo: iterate through all levels in folder LEVELPATH
	IXMLReader* xml = device->getFileSystem()->createXMLReader(LEVELPATH);
	if (!xml)
	{
		cout << "loadNextLevel() cannot open file '" << LEVELPATH << "'" << endl;
	}
	else
	{
		cout << "loadNextLevel() parsing xml-file: " << LEVELPATH << endl;
		parseXML(xml);
	}

	if (xml)
	{
		xml->drop(); // don't forget to delete the xml reader 
	}
}
