#pragma once

#include "stdafx.h"

#include "Includes.h"

#include "Defines.h"

#include "BringUp.h"

#include "GUI_DefineFunctions.cpp"

using namespace std;

int Step_Max(int i,int j)
{
   return step_max[i];
}

int GetHexStep(int i,int j,int Step)
{
   if (i == 0) return 30;
	if (i == 1) return 52;
	if (i == 2) return 36;
	if (i >= 3 && i <= 19) return 36;
	if (i == 20) return 24;
	if (i == 21) return 2;
	if (i == 22) return 18;
   return 0;
}

int GetHexAdditive(int i,int j,int Step)
{
   if (i == 0) return Item_Power_Additives[Step];
	if (i == 1) return Building_Additives[Step];
	if (i == 2) return Spells_Additives[Step];
	if (i >= 3 && i <= 19) return Units_Additives[Step];
	if (i == 20) return Race_Additives[Step];
	if (i == 22) return HeroSpecial_Additives[Step];
	if (i == 24) return BookRetort_Additives[j];
	if (i == 25) return GoldMana_Additives[j];
	if (i == 26) return Picks_Additives[j];
	if (i == 27) return BookItems_Additives[j];
	if (i == 29) return Retort_Effects_Additives[j];
	if (i == 32) return Create_Caps_Additives[j];
	if (i == 34) return Spells_Per_Book_Additives[Step];
	if (i == 36) return Map_Generation_One_Additives[j];
	if (i == 37) return Research_From_Books_Additives[j];
	if (i == 38) return Hero_Join_Additives[j];
	if (i == 39) return Merc_Join_Additives[j];
	if (i == 41) return Misc_Additives[j];
	if (i == 42) return Food_Bonuses_Additives[j];

   return 0;
}

int GetHexAdditiveTotal(int i,int j,int Step)
{
   return GetHexAdditive(i,j,Step) + (GetHexStep(i,j,Step) * j);
}

int Get_Hex_Offset(int i,int j,int Step,int Hex = 0)
{
	return rec_ofs[i] + GetHexAdditiveTotal(i,j,Step);
}

int GetByteParameters(int i,int j,int Step)
{
   //0 = single byte
	//1 = 2 bytes LO
	//3 = Bit flag
   if (i == 0) {
		if (Step == 3 || Step == 4 || Step == 5) {
			return 1;
      } else if (Step == 1 || Step == 2 || Step == 6 || Step == 7 || Step == 8 || Step == 9) {
			return 3;
		} else {
			return 0;
		}
   }
   if (i == 1) {
		if (Step == 1 || Step == 5 || Step == 6 || Step == 7) {
			return 0;
	   } else  {
         return 1;
      }
   }
   if (i == 2) {
		if (Step == 8 || Step == 9) {
			return 1;
		} else {
			return 0;
		}
   }
   if (i >= 3 && i <= 19) {
		if (Step == 0 || Step == 9) {
			return 1;
		} else if (Step == 22 || Step == 24 || Step == 25 || Step == 26 || Step == 28 || Step == 29 || Step == 30 || Step == 31) {
			return 3;
		} else {
			return 0;
		}
   }
	if (i == 20) return 1;
   if (i == 22) {
		if (Step == 5 || Step == 6 || Step == 7 || Step == 8) {
			return 3;
		} else {
			return 0;
		}
   }
	if (i == 25) return 1;
	if (i == 27) return 1;
	if (i == 31) return 1;
	if (i == 32) return 1;
	if (i == 33) return 1;
	if (i == 34) return 1;
   if (i == 36) {
		if (j == 15 || j == 16 || j == 17) {
			return 1;
		} else {
			return 0;
		}
   }
   if (i == 37) {
		if (j == 1 || j == 4 || j == 7 || j == 10 || j == 13) {
			return 1;
		} else {
			return 0;
		}
   }

   return 0;
}

int GetSpaces(int i,int j,int Step)
{
   //0 = single byte
	//1 = 2 bytes LO
	//2 = 2 bytes not LO
	//3 = Bit flag
   int Bytes = GetByteParameters(i,j,Step);
   int Distance = 0;
   if (Bytes == 0) return 4;
   if (Bytes == 1) return 6;
   if (Bytes == 3) return 9;
   return 0;
}

int StringToBitFlag(string str)
{
   char c = '0';
   int value = 0;
   for (size_t i = 0;i < str.length();i++) {
      c = str.at(i);
	   if (i == 0 && c == '1') {
         value = value + 128;
	   }
	   if (i == 1 && c == '1') {
		   value = value + 64;
	   }
	   if (i == 2 && c == '1') {
		   value = value + 32;
	   }
	   if (i == 3 && c == '1') {
		   value = value + 16;
	   }
	   if (i == 4 && c == '1') {
		   value = value + 8;
	   }
	   if (i == 5 && c == '1') {
		   value = value + 4;
	   }
	   if (i == 6 && c == '1') {
		   value = value + 2;
	   }
	   if (i == 7 && c == '1'){
		   value = value + 1;
	   }
   }
   //debug << value << " return value " << '\n';
   //debug << str << " <-Start Str " << BitFlagToString(value) << " <-Processed " << value << " <-End Int " << value + value1 << " <-remainder " << '\n'; 
	return value;
}

string BitFlagToString(int i)
{ 
   string strvalue = "";
   int value = i;
   //Debug << value << '\n'; 
	if (value >=128) {
		value = value - 128;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=64) {
		value = value - 64;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=32) {
		value = value - 32;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=16) {
		value = value - 16;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=8) {
		value = value - 8;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if ( value >=4) {
		value = value - 4;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=2) {
		value = value - 2;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << value << '\n';
	if (value >=1) {
		value = value - 1;
      strvalue.append("1");
	} else {
      strvalue.append("0");
   }
   //Debug << strvalue << '\n';
   //Debug << i << " <-Start Int " << StringToBitFlag(strvalue) << " <-Processed " << strvalue << " <-End Str " << value << " <-remainder " << '\n';
	return strvalue;
}

int IsCharInt(char str)
{
   if (str == '0') {
      return 1;
   }
   if (str == '1') {
      return 1;
   }
   if (str == '2') {
      return 1;
   }
   if (str == '3') {
      return 1;
   }
   if (str == '4') {
      return 1;
   }
   if (str == '5') {
      return 1;
   }
   if (str == '6') {
      return 1;
   }
   if (str == '7') {
      return 1;
   }
   if (str == '8') {
      return 1;
   }
   if (str == '9') {
      return 1;
   }
   //AfxMessageBox("not an int",NULL,NULL);
   return 0;
}