//////////////////////////////////////////////////
//  Author: Drammel								//
//  Date: 4/21/2009								//
//  Title: bot9s_preload						//
//  Description: Stores 2da on oToB				//
//	just after it has been created.  This file	//
//	is to make the Tome of Battle classes run a	//
//	bit more smoothly on Persistent Worlds.		//
//////////////////////////////////////////////////

void Set2DAStringColumn(string s2da, string sColumn, int nRows = 300, int i = 0)
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	if (GetIsObjectValid(oToB))
	{
		string sString;

		while (i <= nRows)
		{
			sString = Get2DAString(s2da, sColumn, i);

			if (sString != "") //The engine reads **** as a blank string.  Actually using "****" checks for "****".
			{
				SetLocalString(oToB, s2da + "_" + sColumn + IntToString(i), sString);
			}

			i++;
		}
	}
}

void Set2DAFloatColumn(string s2da, string sColumn, int nRows = 300, int i = 0)
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	if (GetIsObjectValid(oToB))
	{
		float fEntry;
		string sString;

		while (i <= nRows)
		{
			sString = Get2DAString(s2da, sColumn, i);
			fEntry = StringToFloat(sString);

			if (sString != "")
			{
				SetLocalFloat(oToB, s2da + "_" + sColumn + IntToString(i), fEntry);
			}

			i++;
		}
	}
}

void Set2DAIntColumn(string s2da, string sColumn, int nRows = 300, int i = 0)
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	if (GetIsObjectValid(oToB))
	{
		int nEntry;
		string sString;

		while (i <= nRows)
		{
			sString = Get2DAString(s2da, sColumn, i);
			nEntry = StringToInt(sString);

			if (sString != "")
			{
				SetLocalInt(oToB, s2da + "_" + sColumn + IntToString(i), nEntry);
			}

			i++;
		}
	}
}

void main()
{
	object oPC = OBJECT_SELF;
	string sToB = GetFirstName(oPC) + "tob";
	object oToB = GetObjectByTag(sToB);

	// Breaking up several loops to avoid 'To Many Instructions' errors.
	DelayCommand(0.1f, Set2DAStringColumn("maneuvers", "Script"));
	DelayCommand(0.1f, Set2DAStringColumn("maneuvers", "ICON"));
	DelayCommand(0.1f, Set2DAIntColumn("maneuvers", "StrRef"));
	DelayCommand(0.1f, Set2DAIntColumn("maneuvers", "Description"));
	DelayCommand(0.1f, Set2DAFloatColumn("baseitems", "PrefAttackDist"));// I use this 2da a lot.
	DelayCommand(0.1f, Set2DAIntColumn("baseitems", "NumDice"));
	DelayCommand(0.1f, Set2DAIntColumn("baseitems", "DieToRoll"));
	DelayCommand(0.1f, Set2DAIntColumn("baseitems", "CritThreat"));
	DelayCommand(0.1f, Set2DAIntColumn("baseitems", "CritHitMult"));
	DelayCommand(0.2f, Set2DAIntColumn("baseitems", "FEATImprCrit"));
	DelayCommand(0.2f, Set2DAIntColumn("baseitems", "FEATWpnFocus"));
	DelayCommand(0.2f, Set2DAIntColumn("baseitems", "FEATWpnSpec"));
	DelayCommand(0.2f, Set2DAIntColumn("baseitems", "FEATEpicDevCrit"));
	DelayCommand(0.2f, Set2DAIntColumn("baseitems", "FEATEpicWpnFocus"));
	DelayCommand(0.3f, Set2DAIntColumn("baseitems", "FEATEpicWpnSpec"));
	DelayCommand(0.3f, Set2DAIntColumn("baseitems", "FEATOverWhCrit"));
	DelayCommand(0.3f, Set2DAIntColumn("baseitems", "FEATWpnOfChoice"));
	DelayCommand(0.3f, Set2DAIntColumn("baseitems", "FEATGrtrWpnFocus"));
	DelayCommand(0.3f, Set2DAIntColumn("baseitems", "FEATGrtrWpnSpec"));
	DelayCommand(0.4f, Set2DAIntColumn("baseitems", "FEATPowerCrit"));
	DelayCommand(0.4f, Set2DAStringColumn("iprp_feats", "Label", 450));
	DelayCommand(0.4f, Set2DAIntColumn("iprp_feats", "FeatIndex", 450));
	DelayCommand(0.4f, Set2DAStringColumn("spells", "School", 500));
	
	int nBaseitemRows = GetNum2DARows("baseitems");
	
	if (nBaseitemRows > 200)
	{
		DelayCommand(0.4f, Set2DAFloatColumn("baseitems", "PrefAttackDist", nBaseitemRows, 200));
		DelayCommand(0.4f, Set2DAIntColumn("baseitems", "NumDice", nBaseitemRows, 200));
		DelayCommand(0.5f, Set2DAIntColumn("baseitems", "DieToRoll", nBaseitemRows, 200));
		DelayCommand(0.5f, Set2DAIntColumn("baseitems", "CritThreat", nBaseitemRows, 200));
		DelayCommand(0.5f, Set2DAIntColumn("baseitems", "CritHitMult", nBaseitemRows, 200));
		DelayCommand(0.5f, Set2DAIntColumn("baseitems", "FEATImprCrit", nBaseitemRows, 200));
		DelayCommand(0.5f, Set2DAIntColumn("baseitems", "FEATWpnFocus", nBaseitemRows, 200));
		DelayCommand(0.6f, Set2DAIntColumn("baseitems", "FEATWpnSpec", nBaseitemRows, 200));
		DelayCommand(0.6f, Set2DAIntColumn("baseitems", "FEATEpicDevCrit", nBaseitemRows, 200));
		DelayCommand(0.6f, Set2DAIntColumn("baseitems", "FEATEpicWpnFocus", nBaseitemRows, 200));
		DelayCommand(0.6f, Set2DAIntColumn("baseitems", "FEATEpicWpnSpec", nBaseitemRows, 200));
		DelayCommand(0.6f, Set2DAIntColumn("baseitems", "FEATOverWhCrit", nBaseitemRows, 200));
		DelayCommand(0.7f, Set2DAIntColumn("baseitems", "FEATWpnOfChoice", nBaseitemRows, 200));
		DelayCommand(0.7f, Set2DAIntColumn("baseitems", "FEATGrtrWpnFocus", nBaseitemRows, 200));
		DelayCommand(0.7f, Set2DAIntColumn("baseitems", "FEATGrtrWpnSpec", nBaseitemRows, 200));
		DelayCommand(0.7f, Set2DAIntColumn("baseitems", "FEATPowerCrit", nBaseitemRows, 200));
	}

	int niprpfeatsRows = GetNum2DARows("iprp_feats");
	
	if (niprpfeatsRows > 799)
	{
		DelayCommand(0.7f, Set2DAStringColumn("iprp_feats", "Label", niprpfeatsRows, 450));
		DelayCommand(0.8f, Set2DAIntColumn("iprp_feats", "FeatIndex", niprpfeatsRows, 450));
	}

	DelayCommand(0.5f, Set2DAStringColumn("spells", "School", 1000, 500));
	DelayCommand(0.6f, Set2DAStringColumn("spells", "School", 1500, 1000));
	DelayCommand(0.7f, Set2DAStringColumn("spells", "School", 2000, 2500));
	DelayCommand(0.8f, Set2DAStringColumn("spells", "School", 2500, 3000));
	DelayCommand(0.8f, Set2DAStringColumn("spells", "School", 3500, 3000));
	DelayCommand(0.8f, Set2DAStringColumn("spells", "School", 4000, 3500));
	DelayCommand(0.8f, Set2DAStringColumn("spells", "School", 4500, 4000));
	DelayCommand(0.9f, Set2DAStringColumn("spells", "School", 5000, 4500));
	DelayCommand(0.9f, Set2DAStringColumn("spells", "School", 5500, 5000));
	DelayCommand(0.9f, Set2DAStringColumn("spells", "School", 6000, 5500));
	DelayCommand(0.9f, Set2DAStringColumn("spells", "School", 6500, 6000));
	DelayCommand(0.9f, Set2DAStringColumn("spells", "School", 7000, 6500));

	int spellsRows = GetNum2DARows("spells");

	if (spellsRows > 7000)
	{
		DelayCommand(1.0f, Set2DAStringColumn("spells", "School", spellsRows, 7000));
	}

	AssignCommand(oPC, Set2DAFloatColumn("appearance", "PREFATCKDIST", 500));
	AssignCommand(oPC, Set2DAFloatColumn("appearance", "PREFATCKDIST", 1050, 500));

	int nappearanceRows = GetNum2DARows("appearance");

	if (nappearanceRows > 1050)
	{
		AssignCommand(oPC, Set2DAFloatColumn("appearance", "PREFATCKDIST", nappearanceRows, 1050));
	}

	SetLocalInt(oToB, "PreloadDone", 1);
}