void cTraits::LoadTraits(string filename) { ifstream in; in.open(filename.c_str()); char *pt, buffer[500]; sTrait* newTrait = 0; while (in.good()) { newTrait = new sTrait; if (in.peek() == '\n') in.ignore(1, '\n'); in.getline(buffer, sizeof(buffer), '\n'); // get the name if ((pt = strrchr(buffer, '\r'))) { *pt = 0; } newTrait->m_Name = new char[strlen(buffer) + 1]; strcpy(newTrait->m_Name, buffer); if (in.peek() == '\n') in.ignore(1, '\n'); if (in.peek() == '\r') in.ignore(1, '\r'); in.getline(buffer, sizeof(buffer), '\n'); // get the description if ((pt = strrchr(buffer, '\r'))) *pt = 0; if (strcmp(buffer, "na") != 0) { newTrait->m_Desc = new char[strlen(buffer) + 1]; strcpy(newTrait->m_Desc, buffer); } else newTrait->m_Desc = 0; AddTrait(newTrait); newTrait = 0; } in.close(); }
bool AddTrait (CNPWorld *pWorld, CXMLElement *pTraitTable, CXMLElement *pTrait, CSymbolTable &Symbols) { int i; // First check to see if the world has any of the traits // listed in the "unless" sections for (i = 0; i < pTrait->GetContentElementCount(); i++) { CXMLElement *pUnless = pTrait->GetContentElement(i); if (strCompare(pUnless->GetTag(), CONSTLIT("Unless")) != 0) continue; // If the world has the prohibited trait then we cannot // add the desired trait. if (pWorld->HasTrait(GetSymbolicAttribute(Symbols, pUnless, CONSTLIT("Trait")))) return false; } // Add the trait pWorld->SetTrait(GetSymbolicAttribute(Symbols, pTrait, CONSTLIT("Trait"))); // Add any traits that are implied by this trait for (i = 0; i < pTrait->GetContentElementCount(); i++) { CXMLElement *pImply = pTrait->GetContentElement(i); if (strCompare(pImply->GetTag(), CONSTLIT("Imply")) != 0) continue; // Random chance of actually having this trait if (mathRandom(1, 100) > pImply->GetAttributeInteger(CONSTLIT("Prob"))) continue; // If we already have this trait the don't bother if (pWorld->HasTrait(GetSymbolicAttribute(Symbols, pImply, CONSTLIT("Trait")))) continue; // Look for the implied trait in the table CXMLElement *pNewTrait = FindTraitInTable(pTraitTable, pImply->GetAttribute(CONSTLIT("Trait"))); // Add it. Note that we don't care if we cannot if (pNewTrait) AddTrait(pWorld, pTraitTable, pNewTrait, Symbols); } return true; }
void cTraits::LoadXMLTraits(string filename) { CLog l; l.ss() << "loading " << filename; l.ssend(); TiXmlDocument doc(filename); if (!doc.LoadFile()) { l.ss() << "can't load XML girls " << filename << endl; l.ss() << "Error: line " << doc.ErrorRow() << ", col " << doc.ErrorCol() << ": " << doc.ErrorDesc() << endl; l.ssend(); return; } const char *pt; sTrait* newTrait = 0; TiXmlElement *el, *root_el = doc.RootElement(); // loop over the elements attached to the root for (el = root_el->FirstChildElement(); el; el = el->NextSiblingElement()) { newTrait = new sTrait; if (pt = el->Attribute("Name")) newTrait->m_Name = n_strdup(pt); if (pt = el->Attribute("Desc")) newTrait->m_Desc = n_strdup(pt); if (pt = el->Attribute("Type")) newTrait->m_Type = n_strdup(pt); if (pt = el->Attribute("InheritChance")) { int ival = -1; pt = el->Attribute("InheritChance", &ival); newTrait->m_InheritChance = ival; } if (pt = el->Attribute("RandomChance")) { int ival = -1; pt = el->Attribute("RandomChance", &ival); newTrait->m_RandomChance = ival; } AddTrait(newTrait); newTrait = 0; } }
ALERROR CNPUniverse::AddRandomTrait (CreateCtx &Ctx, CNPWorld *pWorld, TraitType iSection, int iCount, CXMLElement *pTraitTable) // AddRandomTrait // // Adds traits from the given table { int iSectionStart; // Find the beginning of the section iSectionStart = 0; while (iSectionStart < pTraitTable->GetContentElementCount() && pTraitTable->GetContentElement(iSectionStart)->GetAttributeInteger(CONSTLIT("Table")) != iSection) iSectionStart++; // If not found, we fail if (iSectionStart == pTraitTable->GetContentElementCount()) { Ctx.sError = LITERAL("Unable to find appropriate trait table section"); return ERR_FAIL; } // Keep looping for each trait to add for (int i = 0; i < iCount; i++) { // Keep trying until we successfully add some trait or // until we get sick of trying. for (int j = 0; j < 10; j++) { int iRoll = mathRandom(1, 100); int iEntry = iSectionStart; CXMLElement *pEntry; while (iEntry < pTraitTable->GetContentElementCount() && (pEntry = pTraitTable->GetContentElement(iEntry)) && (pEntry->GetAttributeInteger(CONSTLIT("Table")) == iSection) && (iRoll = iRoll - pEntry->GetAttributeInteger(CONSTLIT("Prob"))) > 0) iEntry++; // If not found, we fail if (iRoll > 0) { Ctx.sError = LITERAL("Trait table probabilities do not add up to 100%"); return ERR_FAIL; } // Add the trait. If we add it successfully then we stop // the inner loop. if (AddTrait(pWorld, pTraitTable, pEntry, *Ctx.pSymbols)) break; } } return NOERROR; }