/*=========================================================================
* SubMapPs.c :       This module contain all submap positions functions
* This is a part of the Game project.
*
* Version              1.0
* Creation date        08/09/98
* Revision date
*
*
* (c) Ubi Studios 1998
*=======================================================================*/
 
/*******************************************************/
/**** For the structures and variables declarations ****/
/*******************************************************/
#include "Options/Options.h"
#include "GAM/Header.h"
 
#include "ToolsCPA.h"
 
#include "ZeMem.h"
#include "GAM\Options\Specif\SubMapPs.h"
#include "Macros.h"
 
/*******************************************************/
/**** For the structures and variables declarations ****/
/*******************************************************/
 
tdstSubMapPosition *gs_hFirstSubMapPosition;
 
/*******************************************************/
 
extern long fn_lAToI(char *szString);
SCR_tde_Anl_ReturnValue GAM_fn_eCallBackLoadSubMapPosition(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType);
SCR_tde_Anl_ReturnValue GAM_fn_eCallBackLoadSuperObject(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType);
 
/*******************************************************/
/*****/
/*******************************************************/
void GAM_fn_vAddSubMapPosition(long _lSubMapNumber , long _lEntryNumber , POS_tdxHandleToPosition	_hPosition)
{
	tdstSubMapPosition *hSubMapPosition;
 
	hSubMapPosition = (tdstSubMapPosition *)M_p_GameMallocInHLM(sizeof(struct tdstSubMapPosition_));
 
	hSubMapPosition -> lSubMap		= GAM_M_MakeSubMapNumber(_lSubMapNumber,_lEntryNumber);
	hSubMapPosition -> hPosition	= _hPosition;
 
	if(gs_hFirstSubMapPosition == NULL)
	{
		hSubMapPosition -> p_stNextPosition = NULL;		
	}
	else
	{
		hSubMapPosition -> p_stNextPosition = gs_hFirstSubMapPosition;
	}
	gs_hFirstSubMapPosition = hSubMapPosition;
}
 
/*******************************************************/
/*****/
/*******************************************************/
POS_tdxHandleToPosition	GAM_fn_hGetSubMapPosition(long _lSubMapNumber , long _lEntryNumber)
{
	tdstSubMapPosition *hSubMapPosition;
	long				lAbsolute;
 
	lAbsolute       = GAM_M_MakeSubMapNumber(_lSubMapNumber,_lEntryNumber);
	hSubMapPosition = gs_hFirstSubMapPosition;
 
	while(hSubMapPosition)
	{
		if(hSubMapPosition -> lSubMap == lAbsolute)
			return hSubMapPosition -> hPosition;
 
		hSubMapPosition = hSubMapPosition -> p_stNextPosition;
	}
	return NULL;
}
 
/*******************************************************/
/*****/
/*******************************************************/
void GAM_fn_vInitSubMapPositionList()
{
	gs_hFirstSubMapPosition = NULL;
}
 
/*******************************************************/
/*****/
/*******************************************************/
void GAM_fn_vClearSubMapPositionList()
{
	tdstSubMapPosition *hSubMapPosition,*hNextSubMapPosition;
 
	hSubMapPosition = gs_hFirstSubMapPosition;
 
	while(hSubMapPosition)
	{
		hNextSubMapPosition = hSubMapPosition -> p_stNextPosition;
		POS_M_GameFree(hSubMapPosition -> hPosition);
		M_GameFreeInHLM(hSubMapPosition);
		hSubMapPosition = hNextSubMapPosition;
	}
	gs_hFirstSubMapPosition = NULL;
}
 
 
 
 
/*******************************************************/
/*****/
/*******************************************************/
void GAM_fn_vLoadSubMapPositions(char *_szFileName)
{
	SCR_tdst_Anl_Callback	*p_stCallBackSuperObject;
	SCR_tdpfn_Anl_Callback	pfn_CallBackSuperObject;
	SCR_tdst_Anl_Callback	*p_stCallBackSubMapSection;
	SCR_tdpfn_Anl_Callback	pfn_CallBackSubMapSection;
 
	if(SCR_fn_c_RdL0_IsSectionExists(_szFileName))
	{
		/* unregister callback*/
		p_stCallBackSuperObject		= SCR_fnp_st_RdL0_GetRegisterCallback( "SuperObject", SCR_CRC_c_RdL0_ForSection );
		pfn_CallBackSuperObject		= p_stCallBackSuperObject -> pfn_eCallback;
		/**/
		p_stCallBackSubMapSection	= SCR_fnp_st_RdL0_GetRegisterCallback( "CreateSubMaps", SCR_CRC_c_RdL0_ForSection );
		pfn_CallBackSubMapSection	= p_stCallBackSubMapSection -> pfn_eCallback;
 
		/* register new callback*/
		p_stCallBackSuperObject -> pfn_eCallback	= GAM_fn_eCallBackLoadSuperObject;
		p_stCallBackSubMapSection -> pfn_eCallback	= GAM_fn_eCallBackLoadSubMapPosition;
 
		/**/
		SCR_fnp_st_RdL0_AnalyseSection( _szFileName, SCR_CDF_uw_Anl_Normal);
		SCR_fn_v_RdL0_DeleteOpenSection( _szFileName, SCR_CDR_c_RdL0_Contains );	/* ???*/
 
		/* restore callback*/
		p_stCallBackSuperObject -> pfn_eCallback	= pfn_CallBackSuperObject;
		p_stCallBackSubMapSection -> pfn_eCallback	= pfn_CallBackSubMapSection;
	}
}
 
 
/*******************************************************/
/*****/
/*******************************************************/
SCR_tde_Anl_ReturnValue GAM_fn_eCallBackLoadSubMapPosition(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	POS_tdstCompletePosition	*p_stMatrix;
	long						lSubMapNumber,lEntryNumber;
 
	switch (cType)
	{
	case SCR_EA_Anl_BeginSection:
		break;
		
	case SCR_EA_Anl_Entry:
		break;
		
    case SCR_EA_Anl_BeginSubSection:
		break;
 
    case SCR_EA_Anl_EndSubSection:
		if(strcmpi(szParams[0],"Matrix")==0)
		{
			/* get matrix*/
			SCR_M_RdL0_GetContextLong(1,0,POS_tdstCompletePosition *,p_stMatrix);
 
			/* get SubMap Number*/
			lSubMapNumber = fn_lAToI(szAction + strlen("submap"));
 
			/* get Entry Number*/
			lEntryNumber  = fn_lAToI(szParams[1] + strlen("Position"));
 
			/* add position*/
			GAM_fn_vAddSubMapPosition( lSubMapNumber , lEntryNumber , p_stMatrix );
		}
		break;
 
	case C_SCRIPT_EndSection:
		break;
	}
	return  R_SCRIPT_NormalReturn;
}
 
SCR_tde_Anl_ReturnValue GAM_fn_eCallBackLoadSuperObject(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	return  R_SCRIPT_NormalReturn;
}
