float PHackValue(std::string sValue) { float f = 0; bool fp = false; const char *cStr = sValue.c_str(); char *c = new char[strlen(cStr)+1]; std::istringstream sTof(""); for (unsigned int i=0; i<=strlen(cStr); ++i) { if (i == 20) { c[i] = '\0'; break; } c[i] = (cStr[i] == ',') ? '.' : *(cStr+i); if (c[i] == '.') fp = true; } cStr = c; sTof.str(cStr); sTof >> f; if (!fp) f /= 0xF4240; delete [] c; return f; }
Potion Potion::randomPotion(){ Potion pot = Potion(); std::string fileName("../resources/items/potionType.txt"); std::ifstream file(fileName.c_str()); std::string p,d,prob; if (!file){ std::cout << "Impossible d'ouvrir le fichier :" << fileName << std::endl; }else if(!file.fail()){ std::string line; float rSelection = random(0.0f,1.0f); float sumP = 0; while ( sumP < rSelection && getline( file, line )){ std::stringstream iss(line); getline (iss, d, ','); getline (iss, p, 'k'); getline (iss, prob, 'p'); sumP += sTof(prob); } pot.setPrice(sToi(p)); pot.setDescription(" : " + d); }else{ std::cout << "Fichier non lisible : " << fileName <<"\n"; } file.close(); return pot; }
void Weapon::loadRandomType(){ std::string fileName; if (random(0,1) == 0){ fileName = "../resources/items/weaponMeleeType.txt"; std::ifstream file(fileName.c_str()); std::string n,d1,d2,c,p,prob; if (!file){ std::cout << "Impossible d'ouvrir le fichier :" << fileName << std::endl; }else if(!file.fail()){ std::string line; float rSelection = random(0.0f,1.0f); float sumP = 0; while (sumP < rSelection && getline( file, line )){ std::stringstream iss(line); getline (iss, n, ','); getline (iss, d1, 'd'); getline (iss, d2, '-'); getline (iss, c, 'C'); getline (iss, p, 'k'); getline (iss, prob, 'p'); sumP += sTof(prob); } cc = "cc" + c; price = sToi(p); dd1 = sToi(d1); dd2 = sToi(d2); name = n; }else{ std::cout << "Fichier non lisible : " << fileName <<"\n"; } file.close(); }else{ fileName = "../resources/items/weaponRangeType.txt"; std::ifstream file(fileName.c_str()); std::string n,d1,d2,c,p,po,prob; if (!file){ std::cout << "Impossible d'ouvrir le fichier :" << fileName << std::endl; }else if(!file.fail()){ std::string line; float rSelection = random(0.0f,1.0f); float sumP = 0; while (sumP < rSelection && getline( file, line )){ std::stringstream iss(line); getline (iss, n, ','); getline (iss, d1, 'd'); getline (iss, d2, '-'); getline (iss, c, 'C'); getline (iss, po, 'm'); getline (iss, p, 'k'); getline (iss, prob, 'p'); sumP += sTof(prob); } cc = "cc" + c; price = sToi(p); range = sToi(po); dd1 = sToi(d1); dd2 = sToi(d2); name = n; }else{ std::cout << "Fichier non lisible : " << fileName <<"\n"; } file.close(); } }