/*===========================================================================
 *
 * DFCommon.CPP - Dave Humphrey (uesp@m0use.net), 2 November 2000
 *
 *=========================================================================*/

	/* Include files */
#include "dfcommon.h"
#include "dflandbuilder.h"
#include "dfblocks.h"


	/* Default game config file */
CDFConfig     DFConfigFile;

	/* Woods file object */
CDFWoodsFile  DFWoodsFile;

	/* World map information */
CDFMapsFile   DFMaps;

	/* Names of DF regions */
char DFRegionNames[62][33] = {
	"Alik'r Desert",			"Dragontail Mountains", 
	"Glenpoint Foothills*",			"Daggerfall Bluffs*",
 	"Yeorth Burrowland*",			"Dwynnen",
	"Ravennian Forest*",			"Devilrock*",
	"Malekna Forest*",			"Isle of Balfiera",
	"Bantha*",				"Dak'fron",
	"Islands in the Western Iliac Bay",	"Tamarilyn Point*",
	"Lainlyn Cliffs*",			"Bjoulsae River*",
	"Wrothgarian Mountains",		"Daggerfall",
	"Glenpoint",				"Betony",
	"Sentinel",				"Anticlere",
	"Lainlyn",				"Wayrest",
	"Gen Tem High Rock village*",		"Gen Rai Hammerfell village*",
	"Orsinium Area",			"Skeffington Wood*",
	"Hammerfell bay coast*",		"Hammerfell sea coast*",
	"High Rock bay coast*",			"High Rock sea coast",
	"Northmoor",				"Menevia",
	"Alcaire",				"Koegria",
	"Bhoriane",				"Kambria",
	"Phrygias",				"Urvaius",
	"Ykalon",				"Daenia",
	"Shalgora",				"Abibon-Gora",
	"Kairou",				"Pothago",
	"Myrkwasa",				"Ayasofya",
	"Tigonus",				"Kozanset",
	"Satakalaam",				"Totambu",
	"Mournoth",				"Ephesus",
	"Santaki",				"Antiphyllos",
	"Bergama",				"Gavaudon",
	"Tulune",				"Glenumbra Moors",
	"Ilessan Hills",			"Cybiades" 
 }; /* End DFRegionNames[] */


char DFRMBFilenames[45][5] = {
	  "TVRN",  "GENR",  "RESI",  "WEAP",  "ARMR",  "ALCH",  "BANK",  "BOOK",
	  "CLOT",  "FURN",  "GEMS",  "LIBR",  "PAWN",  "TEMP",  "TEMP",  "PALA",
	  "FARM",  "DUNG",  "CAST",  "MANR",  "SHRI",  "RUIN",  "SHCK",  "GRVE",
	  "FILL",  "KRAV",  "KDRA",  "KOWL",  "KMOO",  "KCAN",  "KFLA",  "KHOR",
	  "KROS",  "KWHE",  "KSCA",  "KHAW",  "MAGE",  "THIE",  "DARK",  "FIGH",
 	  "CUST",  "WALL",  "MARK",  "SHIP",  "WITC" };


/*===========================================================================
 *
 * Conversion Functions
 *
 * Convert a Map X-Z coordinate into a land pixel X-Y index and vice-versa.
 *
 *=========================================================================*/
#undef  __FUNC__
#define __FUNC__ "DFPixelToMapX()"

float DFPixelToMapX (const int PixelX) {
  return (float)((float)PixelX*DFLAND_PIXEL_COORSIZE);
 }


#undef  __FUNC__
#define __FUNC__ "DFPixelToMapZ()"

float DFPixelToMapZ (const int PixelY) {
  return (float)((float)(DFWOODS_MAP_HEIGHT - PixelY)*DFLAND_PIXEL_COORSIZE);
 }


#undef  __FUNC__
#define __FUNC__ "DFMapToPixelX()"

int   DFMapToPixelX (const float MapX) {
  return (int)(MapX/DFLAND_PIXEL_COORSIZE);
 }


#undef  __FUNC__
#define __FUNC__ "DFMapToPixelY()"

int   DFMapToPixelY (const float MapZ) {
  return  DFWOODS_MAP_HEIGHT - (int)((MapZ)/DFLAND_PIXEL_COORSIZE);
 }
/*===========================================================================
 *		End of DF Map-Pixel Coordinate Conversion
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "GetDFPath()"
/*===========================================================================
 *
 * Function - char* GetDFPath (void);
 *
 * Attempt to get the root DF directory from the config file.  Returns NULL
 * on any error.  The string returned should not be modified.
 *
 *=========================================================================*/
char* GetDFPath (void) {
  char* pPath;
  char* pBuffer; 

	/* Attempt to find the custom parameter RootPath */
  pPath = DFConfigFile.GetString(DF_CFGID_ROOTPATH);
  if (pPath != NULL) return (pPath);

	/* Attempt to get the Arena2 path */
  pPath = DFConfigFile.GetString(DF_CFGID_PATH);
  if (pPath == NULL) return (NULL);

	/* Create a temporary string to modify */
  pBuffer = CreateString(pPath);

	/* Remove the last path */
  pPath = strrchr(pBuffer, '\\');

  if (pPath) {
    *pPath = NULL_CHAR;

		/* Do again if this was at the end of the string */
    if (pPath == pBuffer + strlen(pBuffer) - 1) {
      pPath = strrchr(pBuffer, '\\');
      if (pPath) *pPath = NULL_CHAR;
     }
   }

	/* Add the string to the config file table */
  DFConfigFile.AddString("RootPath", pBuffer, DF_CFGID_ROOTPATH);
  DestroyPointer(pBuffer);

	/* Get the string just added and return it */
  pPath = DFConfigFile.GetString(DF_CFGID_ROOTPATH);
  return (pPath);
 }
/*===========================================================================
 *		End of Function GetDFPath()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "GetDFRMBFilename()"
/*===========================================================================
 *
 * Function - char* GetDFRMBFilename (Index);
 *
 * Returns the specified RMB filename,  Index should range from 0 to 44 or
 * NULL will be returned.
 *
 *=========================================================================*/
char* GetDFRMBFilename (const int Index) {

	/* Ensure a valid input */
  if (Index < 0 || Index >= DFBLK_MAX_FILEINDEX) return (NULL);
  return (DFRMBFilenames[Index]);
 }
/*===========================================================================
 *		End of Function GetDFArena2Path()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "GetDFArena2Path()"
/*===========================================================================
 *
 * Function - char* GetDFArena2Path (void);
 *
 * Attempt to get the Arena2 DF directory from the config file.  Returns NULL
 * on any error.  The string returned should not be modified.
 *
 *=========================================================================*/
char* GetDFArena2Path (void) {
  return (DFConfigFile.GetString(DF_CFGID_PATH));
 }
/*===========================================================================
 *		End of Function GetDFArena2Path()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "GetDFArena2CDPath()"
/*===========================================================================
 *
 * Function - char* GetDFArena2Path (void);
 *
 * Attempt to get the Arena2 DF CD path from the config file.  Returns NULL
 * on any error.  The string returned should not be modified.
 *
 *=========================================================================*/
char* GetDFArena2CDPath (void) {
  return (DFConfigFile.GetString(DF_CFGID_PATHCD));
 }
/*===========================================================================
 *		End of Function GetDFArena2Path()
 *=========================================================================*/


#undef  __FUNC__
#define __FUNC__ "PostDFErrors()"
/*===========================================================================
 *
 * Function - boolean PostDFErrors (void);
 *
 * Adds any Daggerfall specific error codes to the custom error handler.
 * Called automatically when this module is included in the project.
 *
 *=========================================================================*/
boolean PostDFErrors (void) {
  ErrorHandler.AddError (DFERR_NODATA,    "No data to save, NULL pointers found");
  ErrorHandler.AddError (DFERR_TOOMUCH,   "Too much unknown data in file");
  ErrorHandler.AddError (DFERR_64SIZE,    "Input exceeded the maximum of 64kb");
  ErrorHandler.AddError (DFERR_NOTAMD,    "File is not an AMD type");
  ErrorHandler.AddError (DFERR_MIDITRACK, "Did not find MIDI track text entry");
  ErrorHandler.AddError (DFERR_MIDISONG,  "Did not find MIDI song text entry");
  ErrorHandler.AddError (DFERR_ARTMIN,    "Artifact Index cannot be less than 23");
  ErrorHandler.AddError (DFERR_ARTMAX,    "Artifact Index cannot be greated than 255");
  ErrorHandler.AddError (DFERR_BADREC,    "Critical: Bad record found");
  ErrorHandler.AddError (DFERR_NOTEXT,    "Warning: Artifact has no matching text entry");
  ErrorHandler.AddError (DFERR_NOART,     "Warning: Text has no matching artifact entry");
  ErrorHandler.AddError (DFERR_MAXSPELL,  "Error: Spell data file contains too many spells to load!");
  ErrorHandler.AddError (DFERR_BSA_DIRTYPE, "Invalid BSA directory type!");
  ErrorHandler.AddError (DFERR_BSA_NOTFOUND, "Could not find the specified BSA directory entry!");
  return (TRUE);
 }

	/* Automatically add errors at startup */
static boolean DFResult = PostDFErrors();
/*===========================================================================
 *		End of Function PostDFErrors()
 *=========================================================================*/



	/* Generated by Hex Workshop
	 * Dagpic.pal - Starting Offset: 0 (0x00000000) Length: 768 (0x00000300) */
byte DFPalette[768] = {
	0x00, 0x00, 0x00, 0x3F, 0x39, 0x20, 0x3F, 0x33, 0x1A, 0x3F, 0x33, 0x18, 0x3D, 0x33, 0x1C, 0x3F, 
	0x33, 0x16, 0x3D, 0x33, 0x1A, 0x3B, 0x33, 0x1C, 0x39, 0x33, 0x1E, 0x3F, 0x31, 0x18, 0x3F, 0x31, 
	0x15, 0x39, 0x31, 0x1E, 0x37, 0x31, 0x20, 0x3D, 0x2F, 0x13, 0x34, 0x2E, 0x21, 0x39, 0x2C, 0x14, 
	0x2E, 0x2B, 0x24, 0x2C, 0x29, 0x25, 0x33, 0x27, 0x12, 0x2C, 0x28, 0x1E, 0x29, 0x27, 0x27, 0x2E, 
	0x25, 0x13, 0x28, 0x24, 0x1F, 0x29, 0x23, 0x17, 0x29, 0x20, 0x10, 0x23, 0x20, 0x1D, 0x22, 0x1E, 
	0x17, 0x21, 0x1D, 0x1A, 0x21, 0x1C, 0x14, 0x22, 0x1C, 0x10, 0x1D, 0x1A, 0x17, 0x1C, 0x17, 0x12, 
	0x3D, 0x32, 0x29, 0x38, 0x2D, 0x24, 0x33, 0x26, 0x1D, 0x30, 0x21, 0x19, 0x2D, 0x1C, 0x14, 0x29, 
	0x19, 0x11, 0x26, 0x17, 0x0F, 0x23, 0x15, 0x0D, 0x20, 0x13, 0x0C, 0x1E, 0x12, 0x0A, 0x1C, 0x11, 
	0x0A, 0x19, 0x10, 0x09, 0x16, 0x10, 0x09, 0x13, 0x0F, 0x0A, 0x10, 0x0D, 0x0A, 0x0D, 0x0C, 0x0A, 
	0x3A, 0x2F, 0x32, 0x37, 0x29, 0x2F, 0x33, 0x24, 0x2A, 0x2F, 0x1F, 0x27, 0x2B, 0x1B, 0x24, 0x26, 
	0x18, 0x20, 0x23, 0x15, 0x1D, 0x1F, 0x13, 0x1A, 0x1B, 0x11, 0x19, 0x19, 0x10, 0x18, 0x15, 0x0E, 
	0x13, 0x12, 0x0D, 0x11, 0x10, 0x0C, 0x0F, 0x0F, 0x0B, 0x0E, 0x0E, 0x0B, 0x0D, 0x0B, 0x0B, 0x0B, 
	0x3D, 0x35, 0x2B, 0x39, 0x30, 0x25, 0x35, 0x2B, 0x20, 0x31, 0x26, 0x1A, 0x2D, 0x23, 0x16, 0x2B, 
	0x1F, 0x13, 0x28, 0x1D, 0x12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 0x15, 0x26, 0x26, 0x13, 
	0x23, 0x23, 0x11, 0x21, 0x21, 0x0F, 0x1F, 0x1F, 0x0D, 0x1C, 0x1C, 0x0B, 0x19, 0x19, 0x09, 0x16, 
	0x16, 0x0A, 0x14, 0x14, 0x0B, 0x12, 0x12, 0x0B, 0x0F, 0x0F, 0x0C, 0x0D, 0x0D, 0x0B, 0x0C, 0x0C, 
	0x3F, 0x3D, 0x19, 0x3C, 0x3B, 0x0B, 0x38, 0x37, 0x00, 0x35, 0x32, 0x00, 0x31, 0x2E, 0x00, 0x2D, 
	0x2A, 0x00, 0x2A, 0x25, 0x00, 0x26, 0x21, 0x00, 0x22, 0x1C, 0x00, 0x1F, 0x1A, 0x01, 0x1D, 0x18, 
	0x01, 0x1A, 0x15, 0x02, 0x17, 0x13, 0x03, 0x14, 0x11, 0x04, 0x11, 0x0F, 0x05, 0x0E, 0x0C, 0x06, 
	0x32, 0x37, 0x31, 0x2B, 0x32, 0x2A, 0x25, 0x2C, 0x23, 0x1E, 0x27, 0x1D, 0x1A, 0x24, 0x1B, 0x17, 
	0x20, 0x17, 0x14, 0x1D, 0x15, 0x13, 0x1B, 0x13, 0x11, 0x18, 0x10, 0x0F, 0x16, 0x0D, 0x0D, 0x13, 
	0x0B, 0x0B, 0x11, 0x09, 0x09, 0x0F, 0x09, 0x07, 0x0D, 0x07, 0x08, 0x0C, 0x08, 0x0A, 0x0B, 0x0A, 
	0x2C, 0x1A, 0x14, 0x2B, 0x17, 0x12, 0x2B, 0x15, 0x10, 0x28, 0x13, 0x0E, 0x26, 0x12, 0x0C, 0x24, 
	0x11, 0x0B, 0x26, 0x16, 0x0B, 0x22, 0x14, 0x0A, 0x1F, 0x12, 0x09, 0x1C, 0x10, 0x08, 0x18, 0x0F, 
	0x07, 0x15, 0x0D, 0x06, 0x12, 0x0B, 0x05, 0x0E, 0x09, 0x04, 0x0B, 0x07, 0x03, 0x08, 0x05, 0x02, 
	0x36, 0x38, 0x28, 0x2E, 0x33, 0x1F, 0x27, 0x2D, 0x19, 0x20, 0x28, 0x13, 0x1B, 0x24, 0x10, 0x19, 
	0x22, 0x0F, 0x17, 0x1F, 0x0D, 0x15, 0x1D, 0x0C, 0x13, 0x1B, 0x0A, 0x10, 0x18, 0x09, 0x0D, 0x15, 
	0x08, 0x0C, 0x12, 0x08, 0x0B, 0x10, 0x09, 0x0A, 0x0E, 0x09, 0x09, 0x0C, 0x0A, 0x0A, 0x0B, 0x0B, 
	0x2C, 0x1C, 0x13, 0x2B, 0x1B, 0x12, 0x2A, 0x1A, 0x11, 0x29, 0x19, 0x10, 0x27, 0x18, 0x0F, 0x26, 
	0x17, 0x0E, 0x25, 0x16, 0x0D, 0x23, 0x15, 0x0C, 0x0A, 0x0A, 0x0A, 0x09, 0x09, 0x09, 0x08, 0x08, 
	0x08, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 
	0x3F, 0x3F, 0x31, 0x3F, 0x3D, 0x2E, 0x3F, 0x3A, 0x2A, 0x3F, 0x38, 0x27, 0x3F, 0x35, 0x23, 0x3F, 
	0x33, 0x1F, 0x3F, 0x30, 0x1C, 0x3F, 0x2E, 0x18, 0x3F, 0x2B, 0x14, 0x3C, 0x29, 0x0D, 0x3A, 0x26, 
	0x0C, 0x38, 0x23, 0x0B, 0x36, 0x20, 0x0A, 0x35, 0x1D, 0x09, 0x33, 0x1A, 0x08, 0x31, 0x17, 0x07, 
	0x2F, 0x15, 0x06, 0x2D, 0x12, 0x05, 0x2C, 0x0F, 0x04, 0x2A, 0x0C, 0x03, 0x28, 0x09, 0x03, 0x26, 
	0x06, 0x02, 0x24, 0x03, 0x01, 0x20, 0x05, 0x00, 0x1B, 0x08, 0x00, 0x19, 0x08, 0x00, 0x17, 0x08, 
	0x00, 0x14, 0x08, 0x02, 0x12, 0x09, 0x06, 0x10, 0x0A, 0x08, 0x0E, 0x0A, 0x09, 0x00, 0x00, 0x03, 
  }; /* End of DF palette */