📄 treasure.cpp
字号:
//////////////////////////////////////////////////////////////////////////////// Filename : Treasure.cpp// Written by : excel96// Description ://////////////////////////////////////////////////////////////////////////////#include "Treasure.h"#include "Assert.h"#include "StringStream.h"#include "OptionInfo.h"#include "ItemInfoManager.h"#include "VariableManager.h"#include "UniqueItemManager.h"#include "ItemUtil.h"#include <string.h>#include "SXml.h"const uint TREASURE_RATIO_MODULUS = 100000;//////////////////////////////////////////////////////////////////////////////// class TreasureOptionType member methods//////////////////////////////////////////////////////////////////////////////TreasureOptionType::TreasureOptionType() throw(){ __BEGIN_TRY m_Ratio = 0; m_OptionType = 0; __END_CATCH}TreasureOptionType::~TreasureOptionType() throw(){ __BEGIN_TRY __END_CATCH}void TreasureOptionType::loadFromFile(ifstream& file) throw(){ __BEGIN_TRY file.read((char*)&m_OptionType, sizeof(int)); file.read((char*)&m_Ratio, sizeof(int)); try { OptionInfo* pInfo = g_pOptionInfoManager->getOptionInfo(m_OptionType); Assert(pInfo!=NULL); } catch (NoSuchElementException & nsee) { cerr << "TreasureOptionType::loadFromFile() : Unknown Option" << endl; throw ("TreasureOptionType::loadFromFile() : Unknown Option"); } __END_CATCH}void TreasureOptionType::parseString(const string& text) throw(){ __BEGIN_TRY //cout << "OptionTypeText:" << text << endl; if (text.size() < 3) return; uint a = text.find_first_of(',', 0); if (a != string::npos) { string optionString = trim(text.substr(0, a)); m_Ratio = atoi(text.substr(a+1, text.size()-a-1).c_str()); // 可记 胶飘傅阑 捞侩秦辑, 可记 鸥涝阑 掘绢辰促. try { OptionInfo* pInfo = g_pOptionInfoManager->getOptionInfo(optionString); Assert(pInfo!=NULL); m_OptionType = pInfo->getType(); } catch (NoSuchElementException & nsee) { cerr << "TreasureOptionType::parseString() : Unknown Option String[" << optionString << "]" << endl; throw ("TreasureOptionType::parseString() : Unknown Option String"); } } else { cerr << "TreasureOptionType::parseString() : Error[" << text << "]" << endl; throw ("TreasureOptionType::parseString() : Error"); } __END_CATCH}string TreasureOptionType::toString(void) const throw(){ __BEGIN_TRY StringStream msg; msg << "TreasureOptionType(" << "OptionType:" << m_OptionType << ",Ratio:" << m_Ratio << ")"; return msg.toString(); __END_CATCH}/*XMLTree* TreasureOptionType::makeXMLTree() const{ XMLTree* ret = NULL; OptionInfo* pOptionInfo = g_pOptionInfoManager->getOptionInfo( m_OptionType ); if ( pOptionInfo != NULL ) { ret = new XMLTree("OptionType"); ret->AddAttribute("Option", pOptionInfo->getNickname()); ret->AddAttribute("Ratio", m_Ratio); } return ret;}*///////////////////////////////////////////////////////////////////////////////// class TreasureItemType member methods//////////////////////////////////////////////////////////////////////////////TreasureItemType::TreasureItemType() throw(){ __BEGIN_TRY m_ItemType = 0; m_Ratio = 0; __END_CATCH}TreasureItemType::~TreasureItemType() throw(){ __BEGIN_TRY for (uint i=0; i<m_TreasureOptionTypes.size(); i++) { TreasureOptionType* pTOT = m_TreasureOptionTypes[i]; SAFE_DELETE(pTOT); } m_TreasureOptionTypes.clear(); __END_CATCH}void TreasureItemType::loadFromFile(int itemClass, ifstream& file) throw(){ __BEGIN_TRY int OptionTypeCount = 0; file.read((char*)&m_ItemType, sizeof(int)); file.read((char*)&m_Ratio, sizeof(int)); file.read((char*)&OptionTypeCount, sizeof(int)); // 瘤沥等 酒捞袍 鸥涝捞 粮犁窍绰瘤甫 眉农茄促. list<OptionType_t> optionNULL; if (!g_pItemInfoManager->isPossibleItem((Item::ItemClass)itemClass, m_ItemType, optionNULL)) { StringStream msg; msg << "TreasureItemType::loadFromFile() : Invalid Item Type!\n" << "ItemClass:" << ItemClass2String[itemClass] << ",ItemType:" << (int)m_ItemType << "\n"; cerr << msg.toString(); throw (msg.toString()); } for (int i=0; i<OptionTypeCount; i++) { TreasureOptionType* pTOT = new TreasureOptionType; pTOT->loadFromFile(file); m_TreasureOptionTypes.push_back(pTOT); } // 犬伏钦拌甫 拌魂秦 敌促. m_OptionTypeTotalRatio = 0; for (uint i=0; i<m_TreasureOptionTypes.size(); i++) { TreasureOptionType* pTOT = m_TreasureOptionTypes[i]; m_OptionTypeTotalRatio += pTOT->getRatio(); } __END_CATCH}void TreasureItemType::parseString(int itemClass, const string& text) throw(){ __BEGIN_TRY //////////////////////////////////////////////////////////// // (1,50) (STR+1,50)(STR+2,30)(STR+30) //////////////////////////////////////////////////////////// // 刚历 item type阑 佬绢甸牢促. uint i = text.find_first_of('(', 0); uint j = text.find_first_of(',', i+1); uint k = text.find_first_of(')', j+1); if (i == string::npos || j == string::npos || k == string::npos) { cerr << "TreasureItemType::parseString() : Error" << endl; throw ("TreasureItemType::parseString() : Error"); } m_ItemType = atoi(trim(text.substr(i+1, j-i-1)).c_str()); m_Ratio = atoi(trim(text.substr(j+1, k-j-1)).c_str()); // 瘤沥等 酒捞袍 鸥涝捞 粮犁窍绰瘤甫 眉农茄促. list<OptionType_t> optionNULL; if (!g_pItemInfoManager->isPossibleItem((Item::ItemClass)itemClass, m_ItemType, optionNULL)) { StringStream msg; msg << "TreasureItemType::parseString() : Invalid Item Type!\n" << "ItemClass:" << ItemClass2String[itemClass] << ",ItemType:" << (int)m_ItemType << "\n"; cerr << msg.toString(); throw (msg.toString()); } // 可记 沥焊甫 佬绢甸牢促. string newText = text.substr(k+1, text.size()-k-1); uint a = 0; uint b = 0; if (newText.size() > 0) { while (b < newText.size()-1) { //////////////////////////////////////////////////////////// // (STR+1,50)(STR+2,30)(STR+30) //////////////////////////////////////////////////////////// a = newText.find_first_of('(', b); b = newText.find_first_of(')', a); if (a == string::npos || b == string::npos) break; string substring = trim(newText.substr(a+1, b-a-1)); //cout << "TIT::substring\n" << substring << endl << endl; TreasureOptionType* pTOT = new TreasureOptionType; pTOT->parseString(substring); m_TreasureOptionTypes.push_back(pTOT); substring.clear(); } } newText.clear(); // 颇教捞 场车栏聪, 犬伏钦拌甫 拌魂秦 敌促. m_OptionTypeTotalRatio = 0; for (uint i=0; i<m_TreasureOptionTypes.size(); i++) { TreasureOptionType* pTOT = m_TreasureOptionTypes[i]; m_OptionTypeTotalRatio += pTOT->getRatio(); } __END_CATCH}bool TreasureItemType::getRandomOption(ITEM_TEMPLATE* pTemplate) throw(){ __BEGIN_TRY if (m_OptionTypeTotalRatio == 0) { //pTemplate->OptionType.clear(); //cout << "OptionTypeTotalRatio = 0" << endl; return false; } for (int k=0; k<2; k++) { bool bOneMore = false; int optionRatio = rand()%m_OptionTypeTotalRatio; int ratioSum = 0; //cout << "numOption=" << m_TreasureOptionTypes.size() // << ", Ratio=" << optionRatio << "/" << m_OptionTypeTotalRatio << endl; for (uint i=0; i<m_TreasureOptionTypes.size(); i++) { TreasureOptionType* pTOT = m_TreasureOptionTypes[i]; ratioSum += pTOT->getRatio(); if (optionRatio < ratioSum) { if (pTemplate->bCreateOption) { // 捞固 弊 可记捞 嘿绢乐绰 版快 if (hasOptionClass( pTemplate->OptionType, pTOT->getOptionType() )) { // 歹 捞惑 茫瘤 富磊. //cout << "has Same OptionClass(" << pTOT->getOptionType() << ")" << endl; return true; } if (pTOT->getOptionType()!=0) { //cout << "add Option: " << pTOT->getOptionType() << endl; pTemplate->OptionType.push_back( pTOT->getOptionType() ); } } //else { //pTemplate->OptionType = 0; } // 可记捞 窍唱 歹 嘿阑 犬伏 眉农 秦辑 烹苞窍搁 // 茄锅歹 可记狼 犬伏阑 眉农秦焊磊. if (isPossibleNextOption(pTemplate)) { bOneMore = true; break; } return true; } } if (!bOneMore) { // 可记阑 急琶窍瘤 给茄 版快? break; } } //while (1); //cout << "Cannot Select Option: " // << optionRatio << ", " // << ratioSum << endl; return false; __END_CATCH}string TreasureItemType::toString(void) const throw(){ __BEGIN_TRY StringStream msg; msg << "\tTreasureItemType\n\t\t\t(" << "\n\t\t\t\tItemType:" << m_ItemType << ",Ratio:" << m_Ratio << "\n"; for (uint i=0; i<m_TreasureOptionTypes.size(); i++) { msg << "\t\t\t\t" << m_TreasureOptionTypes[i]->toString() << "\n"; } msg << "\n\t\t\t)"; return msg.toString(); __END_CATCH}/*XMLTree* TreasureItemType::makeXMLTree() const{ XMLTree* ret = new XMLTree("ItemType"); ret->AddAttribute("Type", m_ItemType); ret->AddAttribute("Ratio", m_Ratio); vector<TreasureOptionType*>::const_iterator itr = m_TreasureOptionTypes.begin(); vector<TreasureOptionType*>::const_iterator endItr = m_TreasureOptionTypes.end(); for ( ; itr != endItr ; ++itr ) { ret->AddChild( (*itr)->makeXMLTree() ); } return ret;}*///////////////////////////////////////////////////////////////////////////////// class TreasureItemClass member methods//////////////////////////////////////////////////////////////////////////////TreasureItemClass::TreasureItemClass() throw(){ __BEGIN_TRY m_ItemClass = Item::ITEM_CLASS_MAX; m_Ratio = 0; __END_CATCH}TreasureItemClass::~TreasureItemClass() throw(){ __BEGIN_TRY for (uint i=0; i<m_TreasureItemTypes.size(); i++) { TreasureItemType* pTIT = m_TreasureItemTypes[i]; SAFE_DELETE(pTIT); } m_TreasureItemTypes.clear(); __END_CATCH}void TreasureItemClass::loadFromFile(ifstream& file) throw(){ __BEGIN_TRY int ItemTypeCount = 0; file.read((char*)&m_ItemClass, sizeof(int)); file.read((char*)&m_Ratio, sizeof(int)); file.read((char*)&ItemTypeCount, sizeof(int)); for (int i=0; i<ItemTypeCount; i++) { TreasureItemType* pTIT = new TreasureItemType; pTIT->loadFromFile(m_ItemClass, file); m_TreasureItemTypes.push_back(pTIT); } // 傈眉 犬伏狼 钦阑 拌魂秦 敌促. m_ItemTypeTotalRatio = 0; for (uint i=0; i<m_TreasureItemTypes.size(); i++) { TreasureItemType* pTIT = m_TreasureItemTypes[i]; m_ItemTypeTotalRatio += pTIT->getRatio(); } __END_CATCH}void TreasureItemClass::parseString(const string& text) throw(){ __BEGIN_TRY //////////////////////////////////////////////////////////// // (SWORD, 50) // <(1,50) (STR+1,50)(STR+2,30)(STR+3,20)> // <(2,30) (STR+1,50)(STR+2,30)(STR+3,20)> // <(3,20) (STR+1,50)(STR+2,30)(STR+3,20)> //////////////////////////////////////////////////////////// // 酒捞袍 努贰胶 棺 犬伏阑 佬绢甸牢促. uint i = text.find_first_of('(', 0); uint j = text.find_first_of(',', i+1); uint k = text.find_first_of(')', j+1); string itemClassString = trim(text.substr(i+1, j-i-1)); m_Ratio = atoi(trim(text.substr(j+1, k-j-1)).c_str()); // 巩磊凯肺何磐 酒捞袍 努贰胶甫 掘绢辰促. m_ItemClass = getItemClassFromString(itemClassString); string newText = text.substr(k+1, text.size()-k-1); //cout << "TIC::newText\n" << newText << endl << endl; uint a = 0; uint b = 0; while (b < newText.size()-1) { //////////////////////////////////////////////////////////// // <(1,50,50) (STR+1,50)(STR+2,30)(STR+3,20)> // <(2,30,50) (STR+1,50)(STR+2,30)(STR+3,20)> // <(3,20,50) (STR+1,50)(STR+2,30)(STR+3,20)> //////////////////////////////////////////////////////////// a = newText.find_first_of('<', b); b = newText.find_first_of('>', a); if (a == string::npos || b == string::npos) break; string substring = trim(newText.substr(a+1, b-a-1)); //cout << "TIC::substring\n" << substring << endl << endl; TreasureItemType* pTIT = new TreasureItemType; pTIT->parseString(m_ItemClass, substring); m_TreasureItemTypes.push_back(pTIT); substring.clear(); } newText.clear(); // 颇教捞 场车栏搁, 傈眉 犬伏狼 钦阑 拌魂秦 敌促. m_ItemTypeTotalRatio = 0; for (uint i=0; i<m_TreasureItemTypes.size(); i++) { TreasureItemType* pTIT = m_TreasureItemTypes[i]; m_ItemTypeTotalRatio += pTIT->getRatio(); } __END_CATCH}bool TreasureItemClass::getRandomItem(ITEM_TEMPLATE* pTemplate) throw(){ __BEGIN_TRY int itemTypeRatio = rand()%TREASURE_RATIO_MODULUS; int ratioSum = 0; for (uint i=0; i<m_TreasureItemTypes.size(); i++) { TreasureItemType* pTIT = m_TreasureItemTypes[i]; ratioSum += pTIT->getRatio(); if (itemTypeRatio < ratioSum) { pTemplate->ItemType = pTIT->getItemType(); // 蜡聪农 酒捞袍俊 包茄 眉农甫 秦具茄促. by sigi. 2002.8.16 ItemInfo* pItemInfo = g_pItemInfoManager->getItemInfo( pTemplate->ItemClass, pTemplate->ItemType ); Assert(pItemInfo!=NULL); // 蜡聪农 酒捞袍牢 版快 if (pItemInfo->isUnique()) { int okRatio = rand()%10000; int uniqueRatio = g_pVariableManager->getUniqueItemRatio(); if (okRatio < uniqueRatio && UniqueItemManager::isPossibleCreate( pTemplate->ItemClass, pTemplate->ItemType)) { // unique绰 option捞 嘿瘤 臼绰促. return true; } // 蜡聪农 犬伏俊 吧啡绰单.. // 蜡聪农 箭磊力茄 锭巩俊 积己 阂啊瓷茄 版快捞促. return false; } else { pTIT->getRandomOption(pTemplate); return true; } } } return false; __END_CATCH}string TreasureItemClass::toString(void) const throw(){ __BEGIN_TRY StringStream msg; msg << "\tTreasureItemClass\n\t\t(" << "\n\t\t\tItemClass:" << m_ItemClass << ",Ratio:" << m_Ratio << "\n"; for (uint i=0; i<m_TreasureItemTypes.size(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -