changeset 2282:f5680ba9c96d

SpellStats::Initialize using tokenizer
author Grumpy7
date Fri, 14 Mar 2014 23:06:24 +0100
parents d8b4a36f54a4
children 53d4e8539ca1
files Items.cpp Spells.cpp texts.h
diffstat 3 files changed, 60 insertions(+), 166 deletions(-) [+]
line wrap: on
line diff
--- a/Items.cpp	Fri Mar 14 23:55:50 2014 +0600
+++ b/Items.cpp	Fri Mar 14 23:06:24 2014 +0100
@@ -15,23 +15,6 @@
 #include "texts.h"
 #include "mm7_data.h"
 
-struct ci_less : std::binary_function<std::string, std::string, bool>
-{
-  // case-independent (ci) compare_less binary function
-  struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool> 
-  {
-    bool operator() (const unsigned char& c1, const unsigned char& c2) const {
-      return tolower (c1) < tolower (c2); 
-    }
-  };
-  bool operator() (const std::string & s1, const std::string & s2) const {
-    return std::lexicographical_compare 
-      (s1.begin (), s1.end (),   // source range
-      s2.begin (), s2.end (),   // dest range
-      nocase_compare ());  // comparison
-  }
-};
-
 
 struct ITEM_VARIATION
 	{
@@ -372,7 +355,6 @@
   materialMap["relic"] = MATERIAL_RELIC;
   materialMap["special"] = MATERIAL_SPECIAL;
 
-	int i,j;
   char* test_string;
   int item_counter;
 
@@ -402,7 +384,7 @@
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
 	//Standard Bonuses by Group	
-	for (i=0;i<24;++i)
+	for (int i=0;i<24;++i)
 		{
 		test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -415,9 +397,9 @@
 	}
 
 	memset(&pEnchantmentsSumm, 0, 36);
-	for(i=0;i<9;++i)
+	for(int i=0;i<9;++i)
 		{
-		for (j=0;j<24;++j)
+		for (int j=0;j<24;++j)
 			pEnchantmentsSumm[i]+=pEnchantments[j].to_item[i];
 		}
 
@@ -427,7 +409,7 @@
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
-	for(i=0;i<6;++i) //counted from 1
+	for(int i=0;i<6;++i) //counted from 1
   {
     test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -443,7 +425,7 @@
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
-	for (i=0;i<72;++i)
+	for (int i=0;i<72;++i)
 	{
     test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -469,7 +451,7 @@
 
 	pSpecialEnchantments_count = 71;
 	memset(&pSpecialEnchantmentsSumm, 0, 96);
-	for(i=0;i<12;++i)
+	for(int i=0;i<12;++i)
 		{
 		for (unsigned int j=0;j<=pSpecialEnchantments_count;++j)
 			pSpecialEnchantmentsSumm[i]+=pSpecialEnchantments[j].to_item_apply[i];
@@ -582,9 +564,9 @@
 
 	//ChanceByTreasureLvl Summ - to calculate chance
 	memset(&uChanceByTreasureLvlSumm, 0, 24);
-	for(i=0;i<6;++i)
+	for(int i=0;i<6;++i)
 		{
-		for (j=1;j<item_counter;++j)
+		for (int j=1;j<item_counter;++j)
 			uChanceByTreasureLvlSumm[i]+=pItems[j].uChanceByTreasureLvl[i];
 		}
 
@@ -593,7 +575,7 @@
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
 	strtok(NULL, "\r");
-	for (i=0;i<3;++i)
+	for (int i=0;i<3;++i)
 		{
 		test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -636,7 +618,7 @@
 	pSkillDescTXT_Raw = NULL;
 	pSkillDescTXT_Raw = (char *)pEvents_LOD->LoadRaw("skilldes.txt", 0);
 	strtok(pSkillDescTXT_Raw, "\r");
-	for (i=0; i<37; ++i)
+	for (int i=0; i<37; ++i)
 		{
 		test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -651,7 +633,7 @@
 	pStatsTXT_Raw = 0;
 	pStatsTXT_Raw = (char *)pEvents_LOD->LoadRaw("stats.txt", 0);
 	strtok(pStatsTXT_Raw, "\r");
-	for (i=0; i<26; ++i)
+	for (int i=0; i<26; ++i)
 		{
 		test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
@@ -730,7 +712,7 @@
 	pClassTXT_Raw = 0;
 	pClassTXT_Raw = (char *)pEvents_LOD->LoadRaw("class.txt", 0);
 	strtok(pClassTXT_Raw, "\r");
-	for (i=0; i<36; ++i)
+	for (int i=0; i<36; ++i)
 		{
 		test_string = strtok(NULL, "\r") + 1;
     auto tokens = Tokenize(test_string, '\t');
--- a/Spells.cpp	Fri Mar 14 23:55:50 2014 +0600
+++ b/Spells.cpp	Fri Mar 14 23:06:24 2014 +0100
@@ -289,12 +289,19 @@
 //----- (0045384A) --------------------------------------------------------
 void SpellStats::Initialize()
 {
-	int decode_step;
+  std::map<std::string, SPELL_SCHOOL, ci_less> spellSchoolMaps;
+  spellSchoolMaps["fire"] = SPELL_SCHOOL_FIRE;
+  spellSchoolMaps["air"] = SPELL_SCHOOL_AIR;
+  spellSchoolMaps["water"] = SPELL_SCHOOL_WATER;
+  spellSchoolMaps["earth"] = SPELL_SCHOOL_EARTH;
+  spellSchoolMaps["spirit"] = SPELL_SCHOOL_SPIRIT;
+  spellSchoolMaps["mind"] = SPELL_SCHOOL_MIND;
+  spellSchoolMaps["body"] = SPELL_SCHOOL_BODY;
+  spellSchoolMaps["light"] = SPELL_SCHOOL_LIGHT;
+  spellSchoolMaps["dark"] = SPELL_SCHOOL_DARK;
+  spellSchoolMaps["magic"] = SPELL_SCHOOL_MAGIC;
+
 	char* test_string;
-	bool break_loop;
-	unsigned char c;
-	unsigned int temp_str_len;
-	char* tmp_pos;
 
 	if ( pSpellsTXT_Raw )
 		free(pSpellsTXT_Raw);
@@ -304,138 +311,27 @@
 	strtok(pSpellsTXT_Raw, "\r");
 	strtok(NULL, "\r");
 	for(int i=1; i<100; ++i) 
-		{
-		test_string=strtok(NULL, "\r")+1;
-		decode_step =0;
-		break_loop=false;
-		do 
-			{
-			c = *(unsigned char*)test_string;
-			temp_str_len = 0;
-			while((c!='\t')&&(c>0))
-				{
-				++temp_str_len;
-				c=test_string[temp_str_len];
-				}		
-			tmp_pos=test_string+temp_str_len;
-			if (*tmp_pos == 0)
-				break_loop = true;
-			*tmp_pos = 0;
-			if (temp_str_len)
-				{		
-				switch (decode_step)
-					{	
-				case 2:
-					pInfos[i].pName=RemoveQuotes(test_string);
-					break;
-				case 3:
-					pInfos[i].uSchool = SPELL_SCHOOL_NONE;
-					if (!_stricmp("fire", test_string) )
-						{
-						pInfos[i].uSchool =SPELL_SCHOOL_FIRE;
-						break;
-						}
-					if ( !_stricmp("air", test_string) )	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_AIR;
-						break;
-						}
-					if ( !_stricmp("water", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_WATER;
-						break;
-						}	
-					if ( !_stricmp("earth", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_EARTH;
-						break;
-						}	
-					if ( !_stricmp("spirit", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_SPIRIT;
-						break;
-						}
-					if ( !_stricmp("mind", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_MIND;
-						break;
-						}
-					if ( !_stricmp("body", test_string) )	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_BODY;
-						break;
-						}			
-					if ( !_stricmp("light", test_string) )	
-						{
-						pInfos[i].uSchool =  SPELL_SCHOOL_LIGHT;
-						break;
-						}					
-					if ( !_stricmp("dark", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_DARK;
-						break;
-						}					
+	{
+    test_string=strtok(NULL, "\r")+1;
+    auto tokens = Tokenize(test_string, '\t');
+    pInfos[i].pName=RemoveQuotes(tokens[2]);
+    auto findResult = spellSchoolMaps.find(tokens[3]);
+    pInfos[i].uSchool = findResult == spellSchoolMaps.end() ? SPELL_SCHOOL_NONE : findResult->second;
+    pInfos[i].pShortName=RemoveQuotes(tokens[4]);
+    pInfos[i].pDescription=RemoveQuotes(tokens[5]);
+    pInfos[i].pBasicSkillDesc=RemoveQuotes(tokens[6]);
+    pInfos[i].pExpertSkillDesc=RemoveQuotes(tokens[7]);
+    pInfos[i].pMasterSkillDesc=RemoveQuotes(tokens[8]);
+    pInfos[i].pGrandmasterSkillDesc=RemoveQuotes(tokens[9]);
+    pSpellDatas[i].stats |= strchr(tokens[10], 'm') || strchr(tokens[10], 'M') ? 1 : 0;
+    pSpellDatas[i].stats |= strchr(tokens[10], 'e') || strchr(tokens[10], 'E') ? 2 : 0;
+    pSpellDatas[i].stats |= strchr(tokens[10], 'c') || strchr(tokens[10], 'C') ? 4 : 0;
+    pSpellDatas[i].stats |= strchr(tokens[10], 'x') || strchr(tokens[10], 'X') ? 8 : 0;
+    if ((i % 11)==0)
+      strtok(NULL, "\r");
+  }
 
-					if ( !_stricmp("magic", test_string))	
-						{
-						pInfos[i].uSchool = SPELL_SCHOOL_MAGIC;
-						break;
-						}								
-					break;
-				case 4:
-					pInfos[i].pShortName=RemoveQuotes(test_string);
-					break;
-				case 5:
-					pInfos[i].pDescription=RemoveQuotes(test_string);
-					break;
-				case 6:
-					pInfos[i].pBasicSkillDesc=RemoveQuotes(test_string);
-					break;
-				case 7:
-					pInfos[i].pExpertSkillDesc=RemoveQuotes(test_string);
-					break;
-				case 8:
-					pInfos[i].pMasterSkillDesc=RemoveQuotes(test_string);
-					break;
-				case 9:
-					pInfos[i].pGrandmasterSkillDesc=RemoveQuotes(test_string);
-					break;
-				case 10:
-					int in_string_pos=0;
-					unsigned char test;
-					int in_str_len=strlen(test_string);
-					for (int j=0; j<in_str_len; ++j)
-						{
-							test= tolower(test_string[j]);
-							switch (test)
-							{
-							case 'm':
-								pSpellDatas[i].stats|=1;
-								break;
-							case 'e':
-								pSpellDatas[i].stats|=2;
-								break;
-							case 'c':
-								pSpellDatas[i].stats|=4;
-								break;
-							case 'x':
-								pSpellDatas[i].stats|=8;
-								break;
-							}					
-						}
-					break;
-					}
-				}
-			else
-				break_loop=true;
-			++decode_step;
-			test_string=tmp_pos+1;	
-			} while ((decode_step<11)&&!break_loop);
-			if ((i%11)==0)
-				strtok(NULL, "\r");
-		}
-
-	}
+}
 //----- (00448DF8) --------------------------------------------------------
 void __fastcall EventCastSpell(int uSpellID, int uSkillLevel, int uSkill, int fromx, int fromy, int fromz, int tox, int toy, int toz)//sub_448DF8
 {
--- a/texts.h	Fri Mar 14 23:55:50 2014 +0600
+++ b/texts.h	Fri Mar 14 23:06:24 2014 +0100
@@ -10,6 +10,22 @@
 char *RemoveQuotes(char *Str);
 void  InitializeGameText();
 
+struct ci_less : std::binary_function<std::string, std::string, bool>   //case insensitive comparator for dictionaries
+{
+  // case-independent (ci) compare_less binary function
+  struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool> 
+  {
+    bool operator() (const unsigned char& c1, const unsigned char& c2) const {
+      return tolower (c1) < tolower (c2); 
+    }
+  };
+  bool operator() (const std::string & s1, const std::string & s2) const {
+    return std::lexicographical_compare 
+      (s1.begin (), s1.end (),   // source range
+      s2.begin (), s2.end (),   // dest range
+      nocase_compare ());  // comparison
+  }
+};
 std::vector<char*> Tokenize(char* input, const char separator);
 
 enum GLOBAL_LOCALIZ_INDEX