itemdb.cpp 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "itemdb.hpp"
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <unordered_map>
  8. #include "../common/nullpo.hpp"
  9. #include "../common/random.hpp"
  10. #include "../common/showmsg.hpp"
  11. #include "../common/strlib.hpp"
  12. #include "../common/utils.hpp"
  13. #include "../common/utilities.hpp"
  14. #include "battle.hpp" // struct battle_config
  15. #include "cashshop.hpp"
  16. #include "clif.hpp"
  17. #include "intif.hpp"
  18. #include "log.hpp"
  19. #include "mob.hpp"
  20. #include "pc.hpp"
  21. #include "status.hpp"
  22. using namespace rathena;
  23. ComboDatabase itemdb_combo;
  24. ItemGroupDatabase itemdb_group;
  25. struct s_roulette_db rd;
  26. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active);
  27. const std::string ItemDatabase::getDefaultLocation() {
  28. return std::string(db_path) + "/item_db.yml";
  29. }
  30. /**
  31. * Reads and parses an entry from the item_db.
  32. * @param node: YAML node containing the entry.
  33. * @return count of successfully parsed rows
  34. */
  35. uint64 ItemDatabase::parseBodyNode(const ryml::NodeRef& node) {
  36. t_itemid nameid;
  37. if (!this->asUInt32(node, "Id", nameid))
  38. return 0;
  39. std::shared_ptr<item_data> item = this->find(nameid);
  40. bool exists = item != nullptr;
  41. if (!exists) {
  42. if (!this->nodesExist(node, { "AegisName", "Name" }))
  43. return 0;
  44. item = std::make_shared<item_data>();
  45. item->nameid = nameid;
  46. item->flag.available = true;
  47. }
  48. if (this->nodeExists(node, "AegisName")) {
  49. std::string name;
  50. if (!this->asString(node, "AegisName", name))
  51. return 0;
  52. if (name.length() > ITEM_NAME_LENGTH) {
  53. this->invalidWarning(node["AegisName"], "AegisName \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  54. }
  55. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  56. if (id != nullptr && id->nameid != nameid) {
  57. this->invalidWarning(node["AegisName"], "Found duplicate item Aegis name for %s, skipping.\n", name.c_str());
  58. return 0;
  59. }
  60. if( exists ){
  61. // Create a copy
  62. std::string aegisname = item->name;
  63. // Convert it to lower
  64. util::tolower( aegisname );
  65. // Remove old AEGIS name from lookup
  66. this->aegisNameToItemDataMap.erase( aegisname );
  67. }
  68. item->name.resize(ITEM_NAME_LENGTH);
  69. item->name = name.c_str();
  70. // Create a copy
  71. std::string aegisname = name;
  72. // Convert it to lower
  73. util::tolower( aegisname );
  74. this->aegisNameToItemDataMap[aegisname] = item;
  75. }
  76. if (this->nodeExists(node, "Name")) {
  77. std::string name;
  78. if (!this->asString(node, "Name", name))
  79. return 0;
  80. if (name.length() > ITEM_NAME_LENGTH) {
  81. this->invalidWarning(node["Name"], "Name \"%s\" exceeds maximum of %d characters, capping...\n", name.c_str(), ITEM_NAME_LENGTH - 1);
  82. }
  83. if( exists ){
  84. // Create a copy
  85. std::string ename = item->ename;
  86. // Convert it to lower
  87. util::tolower( ename );
  88. // Remove old name from lookup
  89. this->nameToItemDataMap.erase( ename );
  90. }
  91. item->ename.resize(ITEM_NAME_LENGTH);
  92. item->ename = name.c_str();
  93. // Create a copy
  94. std::string ename = name;
  95. // Convert it to lower
  96. util::tolower( ename );
  97. this->nameToItemDataMap[ename] = item;
  98. }
  99. if (this->nodeExists(node, "Type")) {
  100. std::string type;
  101. if (!this->asString(node, "Type", type))
  102. return 0;
  103. std::string type_constant = "IT_" + type;
  104. int64 constant;
  105. if (!script_get_constant(type_constant.c_str(), &constant) || constant < IT_HEALING || constant >= IT_MAX) {
  106. this->invalidWarning(node["Type"], "Invalid item type %s, defaulting to IT_ETC.\n", type.c_str());
  107. constant = IT_ETC;
  108. }
  109. item->type = static_cast<item_types>(constant);
  110. } else {
  111. if (!exists)
  112. item->type = IT_ETC;
  113. }
  114. if (this->nodeExists(node, "SubType")) {
  115. std::string type;
  116. if (!this->asString(node, "SubType", type))
  117. return 0;
  118. if (item->type == IT_WEAPON) {
  119. std::string type_constant = "W_" + type;
  120. int64 constant;
  121. if (!script_get_constant(type_constant.c_str(), &constant) || constant < W_FIST || constant >= MAX_WEAPON_TYPE) {
  122. this->invalidWarning(node["SubType"], "Invalid weapon type %s, defaulting to W_FIST.\n", type.c_str());
  123. item->subtype = W_FIST;
  124. }
  125. item->subtype = static_cast<weapon_type>(constant);
  126. } else if (item->type == IT_AMMO) {
  127. std::string type_constant = "AMMO_" + type;
  128. int64 constant;
  129. if (!script_get_constant(type_constant.c_str(), &constant) || constant <= AMMO_NONE || constant >= MAX_AMMO_TYPE) {
  130. this->invalidWarning(node["SubType"], "Invalid ammo type %s, defaulting to AMMO_NONE.\n", type.c_str());
  131. item->subtype = AMMO_NONE;
  132. }
  133. item->subtype = static_cast<e_ammo_type>(constant);
  134. } else if (item->type == IT_CARD) {
  135. std::string type_constant = "CARD_" + type;
  136. int64 constant;
  137. if (!script_get_constant(type_constant.c_str(), &constant) || constant < CARD_NORMAL || constant >= MAX_CARD_TYPE) {
  138. this->invalidWarning(node["SubType"], "Invalid card type %s, defaulting to CARD_NORMAL.\n", type.c_str());
  139. item->subtype = CARD_NORMAL;
  140. }
  141. item->subtype = static_cast<e_card_type>(constant);
  142. } else {
  143. this->invalidWarning(node["SubType"], "Item sub type is not supported for this item type.\n");
  144. return 0;
  145. }
  146. } else {
  147. if (!exists)
  148. item->subtype = 0;
  149. }
  150. if (this->nodeExists(node, "Buy")) {
  151. uint32 buy;
  152. if (!this->asUInt32(node, "Buy", buy))
  153. return 0;
  154. if( buy > MAX_ZENY ){
  155. this->invalidWarning( node["Buy"], "Buying price exceeds MAX_ZENY. Capping...\n" );
  156. buy = MAX_ZENY;
  157. }
  158. item->value_buy = buy;
  159. } else {
  160. if (!exists) {
  161. item->value_buy = 0;
  162. }
  163. }
  164. if (this->nodeExists(node, "Sell")) {
  165. uint32 sell;
  166. if (!this->asUInt32(node, "Sell", sell))
  167. return 0;
  168. if( sell > MAX_ZENY ){
  169. this->invalidWarning( node["Sell"], "Sell price exceeds MAX_ZENY. Capping...\n" );
  170. sell = MAX_ZENY;
  171. }
  172. item->value_sell = sell;
  173. } else {
  174. if (!exists) {
  175. item->value_sell = 0;
  176. }
  177. }
  178. if (this->nodeExists(node, "Weight")) {
  179. uint32 weight;
  180. if (!this->asUInt32(node, "Weight", weight))
  181. return 0;
  182. item->weight = weight;
  183. } else {
  184. if (!exists)
  185. item->weight = 0;
  186. }
  187. if (this->nodeExists(node, "Attack")) {
  188. uint32 atk;
  189. if (!this->asUInt32(node, "Attack", atk))
  190. return 0;
  191. item->atk = atk;
  192. } else {
  193. if (!exists)
  194. item->atk = 0;
  195. }
  196. #ifdef RENEWAL
  197. if (this->nodeExists(node, "MagicAttack")) {
  198. uint32 matk;
  199. if (!this->asUInt32(node, "MagicAttack", matk))
  200. return 0;
  201. item->matk = matk;
  202. } else {
  203. if (!exists)
  204. item->matk = 0;
  205. }
  206. #endif
  207. if (this->nodeExists(node, "Defense")) {
  208. uint32 def;
  209. if (!this->asUInt32(node, "Defense", def))
  210. return 0;
  211. if (def > DEFTYPE_MAX) {
  212. this->invalidWarning(node["Defense"], "Item defense %d exceeds DEFTYPE_MAX (%d), capping to DEFTYPE_MAX.\n", def, DEFTYPE_MAX);
  213. def = DEFTYPE_MAX;
  214. }
  215. item->def = def;
  216. } else {
  217. if (!exists)
  218. item->def = 0;
  219. }
  220. if (this->nodeExists(node, "Range")) {
  221. uint16 range;
  222. if (!this->asUInt16(node, "Range", range))
  223. return 0;
  224. if (range > AREA_SIZE) {
  225. this->invalidWarning(node["Range"], "Item attack range %d exceeds AREA_SIZE (%d), capping to AREA_SIZE.\n", range, AREA_SIZE);
  226. range = AREA_SIZE;
  227. }
  228. item->range = range;
  229. } else {
  230. if (!exists)
  231. item->range = 0;
  232. }
  233. if (this->nodeExists(node, "Slots")) {
  234. uint16 slots;
  235. if (!this->asUInt16(node, "Slots", slots))
  236. return 0;
  237. if (slots > MAX_SLOTS) {
  238. this->invalidWarning(node["Slots"], "Item slots %d exceeds MAX_SLOTS (%d), capping to MAX_SLOTS.\n", slots, MAX_SLOTS);
  239. slots = MAX_SLOTS;
  240. }
  241. item->slots = slots;
  242. } else {
  243. if (!exists)
  244. item->slots = 0;
  245. }
  246. if (this->nodeExists(node, "Jobs")) {
  247. const ryml::NodeRef& jobNode = node["Jobs"];
  248. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  249. if (this->nodeExists(jobNode, "All")) {
  250. bool active;
  251. if (!this->asBool(jobNode, "All", active))
  252. return 0;
  253. itemdb_jobid2mapid(item->class_base, MAPID_ALL, active);
  254. }
  255. for (const auto& jobit : jobNode) {
  256. std::string jobName;
  257. c4::from_chars(jobit.key(), &jobName);
  258. // Skipped because processed above the loop
  259. if (jobName.compare("All") == 0)
  260. continue;
  261. std::string jobName_constant = "EAJ_" + jobName;
  262. int64 constant;
  263. if (!script_get_constant(jobName_constant.c_str(), &constant)) {
  264. this->invalidWarning(jobNode[jobit.key()], "Invalid item job %s, defaulting to All.\n", jobName.c_str());
  265. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  266. break;
  267. }
  268. bool active;
  269. if (!this->asBool(jobNode, jobName, active))
  270. return 0;
  271. itemdb_jobid2mapid(item->class_base, static_cast<e_mapid>(constant), active);
  272. }
  273. } else {
  274. if (!exists) {
  275. item->class_base[0] = item->class_base[1] = item->class_base[2] = 0;
  276. itemdb_jobid2mapid(item->class_base, MAPID_ALL, true);
  277. }
  278. }
  279. if (this->nodeExists(node, "Classes")) {
  280. const auto& classNode = node["Classes"];
  281. if (this->nodeExists(classNode, "All")) {
  282. bool active;
  283. if (!this->asBool(classNode, "All", active))
  284. return 0;
  285. if (active)
  286. item->class_upper |= ITEMJ_ALL;
  287. else
  288. item->class_upper &= ~ITEMJ_ALL;
  289. }
  290. for (const auto& classit : classNode) {
  291. std::string className;
  292. c4::from_chars(classit.key(), &className);
  293. // Skipped because processed above the loop
  294. if (className.compare("All") == 0)
  295. continue;
  296. std::string className_constant = "ITEMJ_" + className;
  297. int64 constant;
  298. if (!script_get_constant(className_constant.c_str(), &constant)) {
  299. this->invalidWarning(classNode[classit.key()], "Invalid class upper %s, defaulting to All.\n", className.c_str());
  300. item->class_upper |= ITEMJ_ALL;
  301. break;
  302. }
  303. bool active;
  304. if (!this->asBool(classNode, className, active))
  305. return 0;
  306. if (active)
  307. item->class_upper |= constant;
  308. else
  309. item->class_upper &= ~constant;
  310. }
  311. } else {
  312. if (!exists)
  313. item->class_upper = ITEMJ_ALL;
  314. }
  315. if (this->nodeExists(node, "Gender")) {
  316. std::string gender;
  317. if (!this->asString(node, "Gender", gender))
  318. return 0;
  319. std::string gender_constant = "SEX_" + gender;
  320. int64 constant;
  321. if (!script_get_constant(gender_constant.c_str(), &constant) || constant < SEX_FEMALE || constant > SEX_BOTH) {
  322. this->invalidWarning(node["Gender"], "Invalid item gender %s, defaulting to SEX_BOTH.\n", gender.c_str());
  323. constant = SEX_BOTH;
  324. }
  325. item->sex = static_cast<e_sex>(constant);
  326. item->sex = this->defaultGender( node, item );
  327. } else {
  328. if (!exists) {
  329. item->sex = SEX_BOTH;
  330. item->sex = this->defaultGender( node, item );
  331. }
  332. }
  333. if (this->nodeExists(node, "Locations")) {
  334. const auto& locationNode = node["Locations"];
  335. for (const auto& locit : locationNode) {
  336. std::string equipName;
  337. c4::from_chars(locit.key(), &equipName);
  338. std::string equipName_constant = "EQP_" + equipName;
  339. int64 constant;
  340. if (!script_get_constant(equipName_constant.c_str(), &constant)) {
  341. this->invalidWarning(locationNode[locit.key()], "Invalid equip location %s, defaulting to IT_ETC.\n", equipName.c_str());
  342. item->type = IT_ETC;
  343. break;
  344. }
  345. bool active;
  346. if (!this->asBool(locationNode, equipName, active))
  347. return 0;
  348. if (active) {
  349. if (constant & EQP_SHADOW_GEAR && item->type != IT_SHADOWGEAR) {
  350. this->invalidWarning(node, "Invalid item equip location %s as it's not a Shadow Gear item type, defaulting to IT_ETC.\n", equipName.c_str());
  351. item->type = IT_ETC;
  352. }
  353. item->equip |= constant;
  354. } else
  355. item->equip &= ~constant;
  356. }
  357. } else {
  358. if (!exists) {
  359. if (itemdb_isequip2(item.get())) {
  360. this->invalidWarning(node, "Invalid item equip location as it has no equip location, defaulting to IT_ETC.\n");
  361. item->type = IT_ETC;
  362. } else
  363. item->equip = 0;
  364. }
  365. }
  366. if (this->nodeExists(node, "WeaponLevel")) {
  367. uint16 lv;
  368. if (!this->asUInt16(node, "WeaponLevel", lv))
  369. return 0;
  370. if (lv > MAX_WEAPON_LEVEL) {
  371. this->invalidWarning(node["WeaponLevel"], "Invalid weapon level %d, defaulting to 0.\n", lv);
  372. lv = 0;
  373. }
  374. if (item->type != IT_WEAPON) {
  375. this->invalidWarning(node["WeaponLevel"], "Item type is not a weapon, defaulting to 0.\n");
  376. lv = 0;
  377. }
  378. item->weapon_level = lv;
  379. } else {
  380. if (!exists)
  381. item->weapon_level = 0;
  382. }
  383. if( this->nodeExists( node, "ArmorLevel" ) ){
  384. uint16 level;
  385. if( !this->asUInt16( node, "ArmorLevel", level ) ){
  386. return 0;
  387. }
  388. if( level > MAX_ARMOR_LEVEL ){
  389. this->invalidWarning( node["ArmorLevel"], "Invalid armor level %d, defaulting to 0.\n", level );
  390. level = 0;
  391. }
  392. if( item->type != IT_ARMOR ){
  393. this->invalidWarning( node["ArmorLevel"], "Item type is not an armor, defaulting to 0.\n" );
  394. level = 0;
  395. }
  396. item->armor_level = level;
  397. }else{
  398. if( !exists ){
  399. item->armor_level = 0;
  400. }
  401. }
  402. if (this->nodeExists(node, "EquipLevelMin")) {
  403. uint16 lv;
  404. if (!this->asUInt16(node, "EquipLevelMin", lv))
  405. return 0;
  406. if (lv > MAX_LEVEL) {
  407. this->invalidWarning(node["EquipLevelMin"], "Minimum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  408. lv = MAX_LEVEL;
  409. }
  410. item->elv = lv;
  411. } else {
  412. if (!exists)
  413. item->elv = 0;
  414. }
  415. if (this->nodeExists(node, "EquipLevelMax")) {
  416. uint16 lv;
  417. if (!this->asUInt16(node, "EquipLevelMax", lv))
  418. return 0;
  419. if (lv < item->elv) {
  420. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d is less than minimum equip level %d, capping to minimum equip level.\n", lv, item->elv);
  421. lv = item->elv;
  422. }
  423. if (lv > MAX_LEVEL) {
  424. this->invalidWarning(node["EquipLevelMax"], "Maximum equip level %d exceeds MAX_LEVEL (%d), capping to MAX_LEVEL.\n", lv, MAX_LEVEL);
  425. lv = MAX_LEVEL;
  426. }
  427. item->elvmax = lv;
  428. } else {
  429. if (!exists)
  430. item->elvmax = MAX_LEVEL;
  431. }
  432. if (this->nodeExists(node, "Refineable")) {
  433. bool refine;
  434. if (!this->asBool(node, "Refineable", refine))
  435. return 0;
  436. item->flag.no_refine = !refine;
  437. } else {
  438. if (!exists)
  439. item->flag.no_refine = true;
  440. }
  441. if (this->nodeExists(node, "Gradable")) {
  442. bool gradable;
  443. if (!this->asBool(node, "Gradable", gradable))
  444. return 0;
  445. item->flag.gradable = gradable;
  446. } else {
  447. if (!exists)
  448. item->flag.gradable = false;
  449. }
  450. if (this->nodeExists(node, "View")) {
  451. uint32 look;
  452. if (!this->asUInt32(node, "View", look))
  453. return 0;
  454. item->look = look;
  455. } else {
  456. if (!exists)
  457. item->look = 0;
  458. }
  459. if (this->nodeExists(node, "AliasName")) {
  460. std::string view;
  461. if (!this->asString(node, "AliasName", view))
  462. return 0;
  463. std::shared_ptr<item_data> view_data = item_db.search_aegisname( view.c_str() );
  464. if (view_data == nullptr) {
  465. this->invalidWarning(node["AliasName"], "Unable to change the alias because %s is an unknown item.\n", view.c_str());
  466. return 0;
  467. }
  468. item->view_id = view_data->nameid;
  469. } else {
  470. if (!exists)
  471. item->view_id = 0;
  472. }
  473. if (this->nodeExists(node, "Flags")) {
  474. const auto& flagNode = node["Flags"];
  475. if (this->nodeExists(flagNode, "BuyingStore")) {
  476. bool active;
  477. if (!this->asBool(flagNode, "BuyingStore", active))
  478. return 0;
  479. if (!itemdb_isstackable2(item.get()) && active) {
  480. this->invalidWarning(flagNode["BuyingStore"], "Non-stackable item cannot be enabled for buying store.\n");
  481. active = false;
  482. }
  483. item->flag.buyingstore = active;
  484. } else {
  485. if (!exists)
  486. item->flag.buyingstore = false;
  487. }
  488. if (this->nodeExists(flagNode, "DeadBranch")) {
  489. bool active;
  490. if (!this->asBool(flagNode, "DeadBranch", active))
  491. return 0;
  492. item->flag.dead_branch = active;
  493. } else {
  494. if (!exists)
  495. item->flag.dead_branch = false;
  496. }
  497. if (this->nodeExists(flagNode, "Container")) {
  498. bool active;
  499. if (!this->asBool(flagNode, "Container", active))
  500. return 0;
  501. item->flag.group = active;
  502. } else {
  503. if (!exists)
  504. item->flag.group = false;
  505. }
  506. if (this->nodeExists(flagNode, "UniqueId")) {
  507. bool active;
  508. if (!this->asBool(flagNode, "UniqueId", active))
  509. return 0;
  510. if (!itemdb_isstackable2(item.get()) && active) {
  511. this->invalidWarning(flagNode["UniqueId"], "Non-stackable item cannot be enabled for UniqueId.\n");
  512. active = false;
  513. }
  514. item->flag.guid = active;
  515. } else {
  516. if (!exists)
  517. item->flag.guid = false;
  518. }
  519. if (this->nodeExists(flagNode, "BindOnEquip")) {
  520. bool active;
  521. if (!this->asBool(flagNode, "BindOnEquip", active))
  522. return 0;
  523. item->flag.bindOnEquip = active;
  524. } else {
  525. if (!exists)
  526. item->flag.bindOnEquip = false;
  527. }
  528. if (this->nodeExists(flagNode, "DropAnnounce")) {
  529. bool active;
  530. if (!this->asBool(flagNode, "DropAnnounce", active))
  531. return 0;
  532. item->flag.broadcast = active;
  533. } else {
  534. if (!exists)
  535. item->flag.broadcast = false;
  536. }
  537. if (this->nodeExists(flagNode, "NoConsume")) {
  538. bool active;
  539. if (!this->asBool(flagNode, "NoConsume", active))
  540. return 0;
  541. if (active)
  542. item->flag.delay_consume |= DELAYCONSUME_NOCONSUME;
  543. else
  544. item->flag.delay_consume &= ~DELAYCONSUME_NOCONSUME;
  545. } else {
  546. if (!exists) {
  547. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  548. item->flag.delay_consume = DELAYCONSUME_NONE;
  549. }
  550. }
  551. if (this->nodeExists(flagNode, "DropEffect")) {
  552. std::string effect;
  553. if (!this->asString(flagNode, "DropEffect", effect))
  554. return 0;
  555. std::string effect_constant = "DROPEFFECT_" + effect;
  556. int64 constant;
  557. if (!script_get_constant(effect_constant.c_str(), &constant) || constant < DROPEFFECT_NONE || constant > DROPEFFECT_MAX) {
  558. this->invalidWarning(flagNode["DropEffect"], "Invalid item drop effect %s, defaulting to DROPEFFECT_NONE.\n", effect.c_str());
  559. constant = DROPEFFECT_NONE;
  560. }
  561. item->flag.dropEffect = static_cast<e_item_drop_effect>(constant);
  562. } else {
  563. if (!exists)
  564. item->flag.dropEffect = DROPEFFECT_NONE;
  565. }
  566. } else {
  567. if (!exists) {
  568. item->flag.buyingstore = false;
  569. item->flag.dead_branch = false;
  570. item->flag.group = false;
  571. item->flag.guid = false;
  572. item->flag.bindOnEquip = false;
  573. item->flag.broadcast = false;
  574. if (!(item->flag.delay_consume & DELAYCONSUME_TEMP))
  575. item->flag.delay_consume = DELAYCONSUME_NONE;
  576. item->flag.dropEffect = DROPEFFECT_NONE;
  577. }
  578. }
  579. if (this->nodeExists(node, "Delay")) {
  580. const auto& delayNode = node["Delay"];
  581. if (this->nodeExists(delayNode, "Duration")) {
  582. uint32 duration;
  583. if (!this->asUInt32(delayNode, "Duration", duration))
  584. return 0;
  585. item->delay.duration = duration;
  586. } else {
  587. if (!exists)
  588. item->delay.duration = 0;
  589. }
  590. if (this->nodeExists(delayNode, "Status")) {
  591. std::string status;
  592. if (!this->asString(delayNode, "Status", status))
  593. return 0;
  594. std::string status_constant = "SC_" + status;
  595. int64 constant;
  596. if (!script_get_constant(status_constant.c_str(), &constant) || constant < SC_NONE || constant >= SC_MAX) {
  597. this->invalidWarning(delayNode[c4::to_csubstr(status)], "Invalid item delay status %s, defaulting to SC_NONE.\n", status.c_str());
  598. constant = SC_NONE;
  599. }
  600. item->delay.sc = static_cast<sc_type>(constant);
  601. } else {
  602. if (!exists)
  603. item->delay.sc = SC_NONE;
  604. }
  605. } else {
  606. if (!exists) {
  607. item->delay.duration = 0;
  608. item->delay.sc = SC_NONE;
  609. }
  610. }
  611. if (this->nodeExists(node, "Stack")) {
  612. const auto& stackNode = node["Stack"];
  613. if (this->nodeExists(stackNode, "Amount")) {
  614. uint16 amount;
  615. if (!this->asUInt16(stackNode, "Amount", amount))
  616. return 0;
  617. if (!itemdb_isstackable2(item.get())) {
  618. this->invalidWarning(stackNode["Amount"], "Non-stackable item cannot be enabled for stacking.\n");
  619. amount = 0;
  620. }
  621. item->stack.amount = amount;
  622. } else {
  623. if (!exists)
  624. item->stack.amount = 0;
  625. }
  626. if (this->nodeExists(stackNode, "Inventory")) {
  627. bool active;
  628. if (!this->asBool(stackNode, "Inventory", active))
  629. return 0;
  630. item->stack.inventory = active;
  631. } else {
  632. if (!exists)
  633. item->stack.inventory = false;
  634. }
  635. if (this->nodeExists(stackNode, "Cart")) {
  636. bool active;
  637. if (!this->asBool(stackNode, "Cart", active))
  638. return 0;
  639. item->stack.cart = active;
  640. } else {
  641. if (!exists)
  642. item->stack.cart = false;
  643. }
  644. if (this->nodeExists(stackNode, "Storage")) {
  645. bool active;
  646. if (!this->asBool(stackNode, "Storage", active))
  647. return 0;
  648. item->stack.storage = active;
  649. } else {
  650. if (!exists)
  651. item->stack.storage = false;
  652. }
  653. if (this->nodeExists(stackNode, "GuildStorage")) {
  654. bool active;
  655. if (!this->asBool(stackNode, "GuildStorage", active))
  656. return 0;
  657. item->stack.guild_storage = active;
  658. } else {
  659. if (!exists)
  660. item->stack.guild_storage = false;
  661. }
  662. } else {
  663. if (!exists) {
  664. item->stack.amount = 0;
  665. item->stack.inventory = false;
  666. item->stack.cart = false;
  667. item->stack.storage = false;
  668. item->stack.guild_storage = false;
  669. }
  670. }
  671. if (this->nodeExists(node, "NoUse")) {
  672. const auto& nouseNode = node["NoUse"];
  673. if (this->nodeExists(nouseNode, "Override")) {
  674. uint16 override;
  675. if (!this->asUInt16(nouseNode, "Override", override))
  676. return 0;
  677. if (override > 100) {
  678. this->invalidWarning(nouseNode["Override"], "Item no use override level %d exceeds 100, capping to 100.\n", override);
  679. override = 100;
  680. }
  681. item->item_usage.override = override;
  682. } else {
  683. if (!exists)
  684. item->item_usage.override = 100;
  685. }
  686. if (this->nodeExists(nouseNode, "Sitting")) {
  687. bool active;
  688. if (!this->asBool(nouseNode, "Sitting", active))
  689. return 0;
  690. item->item_usage.sitting = active;
  691. } else {
  692. if (!exists)
  693. item->item_usage.sitting = false;
  694. }
  695. } else {
  696. if (!exists) {
  697. item->item_usage.override = 100;
  698. item->item_usage.sitting = false;
  699. }
  700. }
  701. if (this->nodeExists(node, "Trade")) {
  702. const auto& tradeNode = node["Trade"];
  703. if (this->nodeExists(tradeNode, "Override")) {
  704. uint16 override;
  705. if (!this->asUInt16(tradeNode, "Override", override))
  706. return 0;
  707. if (override > 100) {
  708. this->invalidWarning(tradeNode["Override"], "Item trade override level %d exceeds 100, capping to 100.\n", override);
  709. override = 100;
  710. }
  711. item->gm_lv_trade_override = override;
  712. } else {
  713. if (!exists)
  714. item->gm_lv_trade_override = 100;
  715. }
  716. if (this->nodeExists(tradeNode, "NoDrop")) {
  717. bool active;
  718. if (!this->asBool(tradeNode, "NoDrop", active))
  719. return 0;
  720. item->flag.trade_restriction.drop = active;
  721. } else {
  722. if (!exists)
  723. item->flag.trade_restriction.drop = false;
  724. }
  725. if (this->nodeExists(tradeNode, "NoTrade")) {
  726. bool active;
  727. if (!this->asBool(tradeNode, "NoTrade", active))
  728. return 0;
  729. item->flag.trade_restriction.trade = active;
  730. } else {
  731. if (!exists)
  732. item->flag.trade_restriction.trade = false;
  733. }
  734. if (this->nodeExists(tradeNode, "TradePartner")) {
  735. bool active;
  736. if (!this->asBool(tradeNode, "TradePartner", active))
  737. return 0;
  738. item->flag.trade_restriction.trade_partner = active;
  739. } else {
  740. if (!exists)
  741. item->flag.trade_restriction.trade_partner = false;
  742. }
  743. if (this->nodeExists(tradeNode, "NoSell")) {
  744. bool active;
  745. if (!this->asBool(tradeNode, "NoSell", active))
  746. return 0;
  747. item->flag.trade_restriction.sell = active;
  748. } else {
  749. if (!exists)
  750. item->flag.trade_restriction.sell = false;
  751. }
  752. if (this->nodeExists(tradeNode, "NoCart")) {
  753. bool active;
  754. if (!this->asBool(tradeNode, "NoCart", active))
  755. return 0;
  756. item->flag.trade_restriction.cart = active;
  757. } else {
  758. if (!exists)
  759. item->flag.trade_restriction.cart = false;
  760. }
  761. if (this->nodeExists(tradeNode, "NoStorage")) {
  762. bool active;
  763. if (!this->asBool(tradeNode, "NoStorage", active))
  764. return 0;
  765. item->flag.trade_restriction.storage = active;
  766. } else {
  767. if (!exists)
  768. item->flag.trade_restriction.storage = false;
  769. }
  770. if (this->nodeExists(tradeNode, "NoGuildStorage")) {
  771. bool active;
  772. if (!this->asBool(tradeNode, "NoGuildStorage", active))
  773. return 0;
  774. item->flag.trade_restriction.guild_storage = active;
  775. } else {
  776. if (!exists)
  777. item->flag.trade_restriction.guild_storage = false;
  778. }
  779. if (this->nodeExists(tradeNode, "NoMail")) {
  780. bool active;
  781. if (!this->asBool(tradeNode, "NoMail", active))
  782. return 0;
  783. item->flag.trade_restriction.mail = active;
  784. } else {
  785. if (!exists)
  786. item->flag.trade_restriction.mail = false;
  787. }
  788. if (this->nodeExists(tradeNode, "NoAuction")) {
  789. bool active;
  790. if (!this->asBool(tradeNode, "NoAuction", active))
  791. return 0;
  792. item->flag.trade_restriction.auction = active;
  793. } else {
  794. if (!exists)
  795. item->flag.trade_restriction.auction = false;
  796. }
  797. } else {
  798. if (!exists) {
  799. item->gm_lv_trade_override = 100;
  800. item->flag.trade_restriction.drop = false;
  801. item->flag.trade_restriction.trade = false;
  802. item->flag.trade_restriction.trade_partner = false;
  803. item->flag.trade_restriction.sell = false;
  804. item->flag.trade_restriction.cart = false;
  805. item->flag.trade_restriction.storage = false;
  806. item->flag.trade_restriction.guild_storage = false;
  807. item->flag.trade_restriction.mail = false;
  808. item->flag.trade_restriction.auction = false;
  809. }
  810. }
  811. if (this->nodeExists(node, "Script")) {
  812. std::string script;
  813. if (!this->asString(node, "Script", script))
  814. return 0;
  815. if (exists && item->script) {
  816. script_free_code(item->script);
  817. item->script = nullptr;
  818. }
  819. item->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  820. } else {
  821. if (!exists)
  822. item->script = nullptr;
  823. }
  824. if (this->nodeExists(node, "EquipScript")) {
  825. std::string script;
  826. if (!this->asString(node, "EquipScript", script))
  827. return 0;
  828. if (exists && item->equip_script) {
  829. script_free_code(item->equip_script);
  830. item->equip_script = nullptr;
  831. }
  832. item->equip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["EquipScript"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  833. } else {
  834. if (!exists)
  835. item->equip_script = nullptr;
  836. }
  837. if (this->nodeExists(node, "UnEquipScript")) {
  838. std::string script;
  839. if (!this->asString(node, "UnEquipScript", script))
  840. return 0;
  841. if (exists && item->unequip_script) {
  842. script_free_code(item->unequip_script);
  843. item->unequip_script = nullptr;
  844. }
  845. item->unequip_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["UnEquipScript"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  846. } else {
  847. if (!exists)
  848. item->unequip_script = nullptr;
  849. }
  850. if (!exists)
  851. this->put(nameid, item);
  852. return 1;
  853. }
  854. void ItemDatabase::loadingFinished(){
  855. for (auto &tmp_item : item_db) {
  856. std::shared_ptr<item_data> item = tmp_item.second;
  857. // Items that are consumed only after target confirmation
  858. if (item->type == IT_DELAYCONSUME) {
  859. item->type = IT_USABLE;
  860. item->flag.delay_consume |= DELAYCONSUME_TEMP;
  861. } else {
  862. item->flag.delay_consume &= ~DELAYCONSUME_TEMP; // Remove delayed consumption flag if switching types
  863. }
  864. if( item->type == IT_WEAPON ){
  865. if( item->weapon_level == 0 ){
  866. ShowWarning( "Item %s is a weapon, but does not have a weapon level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  867. item->weapon_level = 1;
  868. }
  869. if( item->armor_level != 0 ){
  870. ShowWarning( "Item %s is a weapon, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  871. item->armor_level = 0;
  872. }
  873. }else if( item->type == IT_ARMOR ){
  874. if( item->armor_level == 0 ){
  875. ShowWarning( "Item %s is an armor, but does not have an armor level. Consider adding it. Defaulting to 1.\n", item->name.c_str() );
  876. item->armor_level = 1;
  877. }
  878. if( item->weapon_level != 0 ){
  879. ShowWarning( "Item %s is an armor, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  880. item->weapon_level = 0;
  881. }
  882. }else{
  883. if( item->weapon_level != 0 ){
  884. ShowWarning( "Item %s is not a weapon, but has a weapon level. Defaulting to 0.\n", item->name.c_str() );
  885. item->weapon_level = 0;
  886. }
  887. if( item->armor_level != 0 ){
  888. ShowWarning( "Item %s is not an armor, but has an armor level. Defaulting to 0.\n", item->name.c_str() );
  889. item->armor_level = 0;
  890. }
  891. }
  892. // When a particular price is not given, we should base it off the other one
  893. if (item->value_buy == 0 && item->value_sell > 0)
  894. item->value_buy = item->value_sell * 2;
  895. else if (item->value_buy > 0 && item->value_sell == 0)
  896. item->value_sell = item->value_buy / 2;
  897. if (item->value_buy / 124. < item->value_sell / 75.) {
  898. ShowWarning("Buying/Selling [%d/%d] price of %s (%u) allows Zeny making exploit through buying/selling at discounted/overcharged prices! Defaulting Sell to 1 Zeny.\n", item->value_buy, item->value_sell, item->name.c_str(), item->nameid);
  899. item->value_sell = 1;
  900. }
  901. // Shields need to have a view ID to be able to be recognized by ST_SHIELD check in skill.cpp
  902. if( item->type == IT_ARMOR && ( item->equip & EQP_SHIELD ) != 0 && item->look == 0 ){
  903. ShowWarning( "Item %s (%u) is a shield and should have a view id. Defaulting to Guard...\n", item->name.c_str(), item->nameid );
  904. item->look = 1;
  905. }
  906. }
  907. if( !this->exists( ITEMID_DUMMY ) ){
  908. // Create dummy item
  909. std::shared_ptr<item_data> dummy_item = std::make_shared<item_data>();
  910. dummy_item->nameid = ITEMID_DUMMY;
  911. dummy_item->weight = 1;
  912. dummy_item->value_sell = 1;
  913. dummy_item->type = IT_ETC;
  914. dummy_item->name = "UNKNOWN_ITEM";
  915. dummy_item->ename = "Unknown Item";
  916. dummy_item->view_id = UNKNOWN_ITEM_ID;
  917. item_db.put( ITEMID_DUMMY, dummy_item );
  918. }
  919. TypesafeCachedYamlDatabase::loadingFinished();
  920. }
  921. /**
  922. * Applies gender restrictions according to settings.
  923. * @param node: YAML node containing the entry.
  924. * @param node: the already parsed item data.
  925. * @return gender that should be used.
  926. */
  927. e_sex ItemDatabase::defaultGender( const ryml::NodeRef& node, std::shared_ptr<item_data> id ){
  928. if (id->nameid == WEDDING_RING_M) //Grom Ring
  929. return SEX_MALE;
  930. if (id->nameid == WEDDING_RING_F) //Bride Ring
  931. return SEX_FEMALE;
  932. if( id->type == IT_WEAPON ){
  933. if( id->subtype == W_MUSICAL ){
  934. if( id->sex != SEX_MALE ){
  935. this->invalidWarning( node, "Musical instruments are always male-only, defaulting to SEX_MALE.\n" );
  936. }
  937. return SEX_MALE;
  938. }
  939. if( id->subtype == W_WHIP ){
  940. if( id->sex != SEX_FEMALE ){
  941. this->invalidWarning( node, "Whips are always female-only, defaulting to SEX_FEMALE.\n" );
  942. }
  943. return SEX_FEMALE;
  944. }
  945. }
  946. return static_cast<e_sex>( id->sex );
  947. }
  948. std::shared_ptr<item_data> ItemDatabase::search_aegisname( const char* name ){
  949. // Create a copy
  950. std::string lowername = name;
  951. // Convert it to lower
  952. util::tolower( lowername );
  953. return util::umap_find( this->aegisNameToItemDataMap, lowername );
  954. }
  955. std::shared_ptr<item_data> ItemDatabase::searchname( const char *name ){
  956. // Create a copy
  957. std::string lowername = name;
  958. // Convert it to lower
  959. util::tolower( lowername );
  960. std::shared_ptr<item_data> result = util::umap_find( this->aegisNameToItemDataMap, lowername );
  961. if( result != nullptr ){
  962. return result;
  963. }
  964. return util::umap_find( this->nameToItemDataMap, lowername );
  965. }
  966. /**
  967. * Generates an item link string
  968. * @param data: Item info
  969. * @return <ITEML> string for the item
  970. * @author [Cydh]
  971. **/
  972. std::string ItemDatabase::create_item_link( struct item& item ){
  973. std::shared_ptr<item_data> data = this->find( item.nameid );
  974. if( data == nullptr ){
  975. ShowError( "Tried to create itemlink for unknown item %u.\n", item.nameid );
  976. return "Unknown item";
  977. }
  978. // All these dates are unconfirmed
  979. #if PACKETVER >= 20100000
  980. if( !battle_config.feature_itemlink ){
  981. // Feature is disabled
  982. return data->ename;
  983. }
  984. struct item_data* id = data.get();
  985. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  986. const std::string start_tag = "<ITEML>";
  987. const std::string closing_tag = "</ITEML>";
  988. #else // PACKETVER >= 20100000
  989. const std::string start_tag = "<ITEMLINK>";
  990. const std::string closing_tag = "</ITEMLINK>";
  991. #endif
  992. std::string itemstr = start_tag;
  993. itemstr += util::string_left_pad(util::base62_encode(id->equip), '0', 5);
  994. itemstr += itemdb_isequip2(id) ? "1" : "0";
  995. itemstr += util::base62_encode(item.nameid);
  996. if (item.refine > 0) {
  997. itemstr += "%" + util::string_left_pad(util::base62_encode(item.refine), '0', 2);
  998. }
  999. if (itemdb_isequip2(id)) {
  1000. itemstr += "&" + util::string_left_pad(util::base62_encode(id->look), '0', 2);
  1001. }
  1002. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  1003. itemstr += "'" + util::string_left_pad(util::base62_encode(item.enchantgrade), '0', 2);
  1004. #endif
  1005. #if PACKETVER_MAIN_NUM >= 20200916 || PACKETVER_RE_NUM >= 20200724
  1006. const std::string card_sep = ")";
  1007. const std::string optid_sep = "+";
  1008. const std::string optpar_sep = ",";
  1009. const std::string optval_sep = "-";
  1010. #else
  1011. const std::string card_sep = "(";
  1012. const std::string optid_sep = "*";
  1013. const std::string optpar_sep = "+";
  1014. const std::string optval_sep = ",";
  1015. #endif
  1016. for (uint8 i = 0; i < MAX_SLOTS; ++i) {
  1017. itemstr += card_sep + util::string_left_pad(util::base62_encode(item.card[i]), '0', 2);
  1018. }
  1019. #if PACKETVER >= 20150225
  1020. for (uint8 i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  1021. if (item.option[i].id == 0) {
  1022. break; // ignore options including ones beyond this one since the client won't even display them
  1023. }
  1024. // Option ID
  1025. itemstr += optid_sep + util::string_left_pad(util::base62_encode(item.option[i].id), '0', 2);
  1026. // Param
  1027. itemstr += optpar_sep + util::string_left_pad(util::base62_encode(item.option[i].param), '0', 2);
  1028. // Value
  1029. itemstr += optval_sep + util::string_left_pad(util::base62_encode(item.option[i].value), '0', 2);
  1030. }
  1031. #endif
  1032. itemstr += closing_tag;
  1033. return itemstr;
  1034. #else
  1035. // Did not exist before that
  1036. return data->ename;
  1037. #endif
  1038. }
  1039. std::string ItemDatabase::create_item_link( t_itemid id ){
  1040. struct item it = {};
  1041. it.nameid = id;
  1042. return this->create_item_link( it );
  1043. }
  1044. ItemDatabase item_db;
  1045. /**
  1046. * Check if an item exists in a group
  1047. * @param group_id: Item Group ID
  1048. * @param nameid: Item to check for in group
  1049. * @return True if item is in group, else false
  1050. */
  1051. bool ItemGroupDatabase::item_exists(uint16 group_id, t_itemid nameid)
  1052. {
  1053. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1054. if (group == nullptr)
  1055. return false;
  1056. for (const auto &random : group->random) {
  1057. for (const auto &it : random.second->data) {
  1058. if (it.second->nameid == nameid)
  1059. return true;
  1060. }
  1061. }
  1062. return false;
  1063. }
  1064. /**
  1065. * Check if an item exists from a group in a player's inventory
  1066. * @param group_id: Item Group ID
  1067. * @return Item's index if found or -1 otherwise
  1068. */
  1069. int16 ItemGroupDatabase::item_exists_pc(map_session_data *sd, uint16 group_id)
  1070. {
  1071. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  1072. if (group == nullptr || group->random.empty())
  1073. return -1;
  1074. for (const auto &random : group->random) {
  1075. for (const auto &it : random.second->data) {
  1076. int16 item_position = pc_search_inventory(sd, it.second->nameid);
  1077. if (item_position != -1)
  1078. return item_position;
  1079. }
  1080. }
  1081. return -1;
  1082. }
  1083. const std::string LaphineSynthesisDatabase::getDefaultLocation(){
  1084. return std::string( db_path ) + "/laphine_synthesis.yml";
  1085. }
  1086. uint64 LaphineSynthesisDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1087. t_itemid item_id;
  1088. {
  1089. std::string name;
  1090. if( !this->asString( node, "Item", name ) ){
  1091. return 0;
  1092. }
  1093. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1094. if( id == nullptr ){
  1095. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1096. return 0;
  1097. }
  1098. item_id = id->nameid;
  1099. }
  1100. std::shared_ptr<s_laphine_synthesis> entry = this->find( item_id );
  1101. bool exists = entry != nullptr;
  1102. if( !exists ){
  1103. if( !this->nodesExist( node, { "RewardGroup", "Requirements" } ) ){
  1104. return 0;
  1105. }
  1106. entry = std::make_shared<s_laphine_synthesis>();
  1107. entry->item_id = item_id;
  1108. }
  1109. if( this->nodeExists( node, "RewardGroup" ) ){
  1110. std::string name;
  1111. if( !this->asString( node, "RewardGroup", name ) ){
  1112. return 0;
  1113. }
  1114. int64 constant;
  1115. if( !script_get_constant( ( "IG_" + name ).c_str(), &constant ) ){
  1116. this->invalidWarning( node["RewardGroup"], "Unknown reward group \"%s\".\n", name.c_str() );
  1117. return 0;
  1118. }
  1119. if( !itemdb_group.exists( (uint16)constant ) ){
  1120. this->invalidWarning( node["RewardGroup"], "Unknown reward group ID \"%" PRId64 "\".\n", constant );
  1121. return 0;
  1122. }
  1123. entry->rewardGroupId = (uint16)constant;
  1124. }else{
  1125. if( !exists ){
  1126. entry->rewardGroupId = 0;
  1127. }
  1128. }
  1129. if( this->nodeExists( node, "RequiredRequirementsCount" ) ){
  1130. uint16 amount;
  1131. if( !this->asUInt16( node, "RequiredRequirementsCount", amount ) ){
  1132. return 0;
  1133. }
  1134. entry->requiredRequirements = amount;
  1135. }else{
  1136. if( !exists ){
  1137. entry->requiredRequirements = 1;
  1138. }
  1139. }
  1140. if( this->nodeExists( node, "Requirements" ) ){
  1141. for( const ryml::NodeRef& requirementNode : node["Requirements"] ){
  1142. std::string name;
  1143. if( !this->asString( requirementNode, "Item", name ) ){
  1144. return 0;
  1145. }
  1146. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1147. if( id == nullptr ){
  1148. this->invalidWarning( requirementNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1149. return 0;
  1150. }
  1151. std::shared_ptr<s_laphine_synthesis_requirement> requirement = util::umap_find( entry->requirements, id->nameid );
  1152. bool requirement_exists = requirement != nullptr;
  1153. if( !requirement_exists ){
  1154. requirement = std::make_shared<s_laphine_synthesis_requirement>();
  1155. requirement->item_id = id->nameid;
  1156. }
  1157. if( this->nodeExists( requirementNode, "Amount" ) ){
  1158. uint16 amount;
  1159. if( !this->asUInt16( requirementNode, "Amount", amount ) ){
  1160. return 0;
  1161. }
  1162. if( amount > MAX_AMOUNT ){
  1163. this->invalidWarning( requirementNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1164. amount = MAX_AMOUNT;
  1165. }
  1166. requirement->amount = amount;
  1167. }else{
  1168. if( !requirement_exists ){
  1169. requirement->amount = 1;
  1170. }
  1171. }
  1172. if( !requirement_exists ){
  1173. entry->requirements[id->nameid] = requirement;
  1174. }
  1175. }
  1176. }
  1177. if( this->nodeExists( node, "MinimumRefine" ) ){
  1178. uint16 refine;
  1179. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1180. return 0;
  1181. }
  1182. if( refine > MAX_REFINE ){
  1183. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1184. refine = MAX_REFINE;
  1185. }
  1186. entry->minimumRefine = refine;
  1187. }else{
  1188. if( !exists ){
  1189. entry->minimumRefine = 0;
  1190. }
  1191. }
  1192. if( this->nodeExists( node, "MaximumRefine" ) ){
  1193. uint16 refine;
  1194. if( !this->asUInt16( node, "MaximumRefine", refine ) ){
  1195. return 0;
  1196. }
  1197. if( refine > MAX_REFINE ){
  1198. this->invalidWarning( node["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1199. refine = MAX_REFINE;
  1200. }
  1201. entry->maximumRefine = refine;
  1202. }else{
  1203. if( !exists ){
  1204. entry->maximumRefine = MAX_REFINE;
  1205. }
  1206. }
  1207. if( !exists ){
  1208. this->put( entry->item_id, entry );
  1209. }
  1210. return 1;
  1211. }
  1212. LaphineSynthesisDatabase laphine_synthesis_db;
  1213. const std::string LaphineUpgradeDatabase::getDefaultLocation(){
  1214. return std::string( db_path ) + "/laphine_upgrade.yml";
  1215. }
  1216. uint64 LaphineUpgradeDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1217. t_itemid item_id;
  1218. {
  1219. std::string name;
  1220. if( !this->asString( node, "Item", name ) ){
  1221. return 0;
  1222. }
  1223. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1224. if( id == nullptr ){
  1225. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1226. return 0;
  1227. }
  1228. item_id = id->nameid;
  1229. }
  1230. std::shared_ptr<s_laphine_upgrade> entry = this->find( item_id );
  1231. bool exists = entry != nullptr;
  1232. if( !exists ){
  1233. if( !this->nodesExist( node, { "TargetItems" } ) ){
  1234. return 0;
  1235. }
  1236. entry = std::make_shared<s_laphine_upgrade>();
  1237. entry->item_id = item_id;
  1238. }
  1239. if( this->nodeExists( node, "RandomOptionGroup" ) ){
  1240. std::string name;
  1241. if( !this->asString( node, "RandomOptionGroup", name ) ){
  1242. return 0;
  1243. }
  1244. uint16 id;
  1245. if( !random_option_group.option_get_id( name, id ) ){
  1246. this->invalidWarning( node["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  1247. return 0;
  1248. }
  1249. entry->randomOptionGroup = random_option_group.find( id );
  1250. }else{
  1251. if( !exists ){
  1252. entry->randomOptionGroup = nullptr;
  1253. }
  1254. }
  1255. if( this->nodeExists( node, "TargetItems" ) ){
  1256. for( const ryml::NodeRef& targetNode : node["TargetItems"] ){
  1257. std::string name;
  1258. if( !this->asString( targetNode, "Item", name ) ){
  1259. return 0;
  1260. }
  1261. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1262. if( id == nullptr ){
  1263. this->invalidWarning( targetNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1264. return 0;
  1265. }
  1266. if( !util::vector_exists( entry->target_item_ids, id->nameid ) ){
  1267. entry->target_item_ids.push_back( id->nameid );
  1268. }
  1269. }
  1270. }
  1271. if( this->nodeExists( node, "MinimumRefine" ) ){
  1272. uint16 refine;
  1273. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1274. return 0;
  1275. }
  1276. if( refine > MAX_REFINE ){
  1277. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1278. refine = MAX_REFINE;
  1279. }
  1280. entry->minimumRefine = refine;
  1281. }else{
  1282. if( !exists ){
  1283. entry->minimumRefine = 0;
  1284. }
  1285. }
  1286. if( this->nodeExists( node, "MaximumRefine" ) ){
  1287. uint16 refine;
  1288. if( !this->asUInt16( node, "MaximumRefine", refine ) ){
  1289. return 0;
  1290. }
  1291. if( refine > MAX_REFINE ){
  1292. this->invalidWarning( node["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1293. refine = MAX_REFINE;
  1294. }
  1295. entry->maximumRefine = refine;
  1296. }else{
  1297. if( !exists ){
  1298. entry->maximumRefine = MAX_REFINE;
  1299. }
  1300. }
  1301. if( this->nodeExists( node, "RequiredRandomOptions" ) ){
  1302. uint16 amount;
  1303. if( !this->asUInt16( node, "RequiredRandomOptions", amount ) ){
  1304. return 0;
  1305. }
  1306. if( amount > MAX_ITEM_RDM_OPT ){
  1307. this->invalidWarning( node["RequiredRandomOptions"], "Required random option amount %hu is too high, capping to MAX_ITEM_RDM_OPT...\n", amount );
  1308. amount = MAX_ITEM_RDM_OPT;
  1309. }
  1310. entry->requiredRandomOptions = amount;
  1311. }else{
  1312. if( !exists ){
  1313. entry->requiredRandomOptions = 0;
  1314. }
  1315. }
  1316. if( this->nodeExists( node, "CardsAllowed" ) ){
  1317. bool allowed;
  1318. if( !this->asBool( node, "CardsAllowed", allowed ) ){
  1319. return 0;
  1320. }
  1321. entry->cardsAllowed = allowed;
  1322. }else{
  1323. if( !exists ){
  1324. entry->cardsAllowed = false;
  1325. }
  1326. }
  1327. if( this->nodeExists( node, "ResultRefine" ) ){
  1328. uint16 refine;
  1329. if( !this->asUInt16( node, "ResultRefine", refine ) ){
  1330. return 0;
  1331. }
  1332. if( refine > MAX_REFINE ){
  1333. this->invalidWarning( node["ResultRefine"], "Result refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1334. refine = MAX_REFINE;
  1335. }
  1336. entry->resultRefine = refine;
  1337. }else{
  1338. if( !exists ){
  1339. entry->resultRefine = 0;
  1340. }
  1341. }
  1342. if( this->nodeExists( node, "ResultRefineMinimum" ) ){
  1343. uint16 refine;
  1344. if( !this->asUInt16( node, "ResultRefineMinimum", refine ) ){
  1345. return 0;
  1346. }
  1347. if( refine > MAX_REFINE ){
  1348. this->invalidWarning( node["ResultRefineMinimum"], "Result refine minimum %hu is too high, capping to MAX_REFINE...\n", refine );
  1349. refine = MAX_REFINE;
  1350. }
  1351. entry->resultRefineMinimum = refine;
  1352. }else{
  1353. if( !exists ){
  1354. entry->resultRefineMinimum = 0;
  1355. }
  1356. }
  1357. if( this->nodeExists( node, "ResultRefineMaximum" ) ){
  1358. uint16 refine;
  1359. if( !this->asUInt16( node, "ResultRefineMaximum", refine ) ){
  1360. return 0;
  1361. }
  1362. if( refine > MAX_REFINE ){
  1363. this->invalidWarning( node["ResultRefineMaximum"], "Result refine maximum %hu is too high, capping to MAX_REFINE...\n", refine );
  1364. refine = MAX_REFINE;
  1365. }
  1366. entry->resultRefineMaximum = refine;
  1367. }else{
  1368. if( !exists ){
  1369. entry->resultRefineMaximum = 0;
  1370. }
  1371. }
  1372. if( !exists ){
  1373. this->put( entry->item_id, entry );
  1374. }
  1375. return 1;
  1376. }
  1377. LaphineUpgradeDatabase laphine_upgrade_db;
  1378. const std::string ItemReformDatabase::getDefaultLocation(){
  1379. return std::string( db_path ) + "/item_reform.yml";
  1380. }
  1381. uint64 ItemReformDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1382. t_itemid item_id;
  1383. {
  1384. std::string name;
  1385. if( !this->asString( node, "Item", name ) ){
  1386. return 0;
  1387. }
  1388. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1389. if( id == nullptr ){
  1390. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  1391. return 0;
  1392. }
  1393. item_id = id->nameid;
  1394. }
  1395. std::shared_ptr<s_item_reform> entry = this->find( item_id );
  1396. bool exists = entry != nullptr;
  1397. if( !exists ){
  1398. if( !this->nodesExist( node, { "BaseItems" } ) ){
  1399. return 0;
  1400. }
  1401. entry = std::make_shared<s_item_reform>();
  1402. entry->item_id = item_id;
  1403. }
  1404. if( this->nodeExists( node, "BaseItems" ) ){
  1405. for( const ryml::NodeRef& baseNode : node["BaseItems"] ){
  1406. t_itemid base_itemid;
  1407. {
  1408. std::string name;
  1409. if( !this->asString( baseNode, "BaseItem", name ) ){
  1410. return 0;
  1411. }
  1412. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1413. if( id == nullptr ){
  1414. this->invalidWarning( baseNode["BaseItem"], "Unknown item \"%s\".\n", name.c_str() );
  1415. return 0;
  1416. }
  1417. base_itemid = id->nameid;
  1418. }
  1419. std::shared_ptr<s_item_reform_base> base = util::umap_find( entry->base_items, base_itemid );
  1420. bool base_exists = base != nullptr;
  1421. if( !base_exists ){
  1422. if( !this->nodesExist( baseNode, { "ResultItem" } ) ){
  1423. return 0;
  1424. }
  1425. base = std::make_shared<s_item_reform_base>();
  1426. base->item_id = base_itemid;
  1427. }
  1428. if( this->nodeExists( baseNode, "MinimumRefine" ) ){
  1429. uint16 refine;
  1430. if( !this->asUInt16( baseNode, "MinimumRefine", refine ) ){
  1431. return 0;
  1432. }
  1433. if( refine > MAX_REFINE ){
  1434. this->invalidWarning( baseNode["MinimumRefine"], "Minimum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1435. refine = MAX_REFINE;
  1436. }
  1437. base->minimumRefine = refine;
  1438. }else{
  1439. if( !base_exists ){
  1440. base->minimumRefine = 0;
  1441. }
  1442. }
  1443. if( this->nodeExists( baseNode, "MaximumRefine" ) ){
  1444. uint16 refine;
  1445. if( !this->asUInt16( baseNode, "MaximumRefine", refine ) ){
  1446. return 0;
  1447. }
  1448. if( refine > MAX_REFINE ){
  1449. this->invalidWarning( baseNode["MaximumRefine"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  1450. refine = MAX_REFINE;
  1451. }
  1452. base->maximumRefine = refine;
  1453. }else{
  1454. if( !base_exists ){
  1455. base->maximumRefine = MAX_REFINE;
  1456. }
  1457. }
  1458. if( this->nodeExists( baseNode, "RequiredRandomOptions" ) ){
  1459. uint16 amount;
  1460. if( !this->asUInt16( baseNode, "RequiredRandomOptions", amount ) ){
  1461. return 0;
  1462. }
  1463. if( amount > MAX_ITEM_RDM_OPT ){
  1464. this->invalidWarning( baseNode["RequiredRandomOptions"], "Required random option amount %hu is too high, capping to MAX_ITEM_RDM_OPT...\n", amount );
  1465. amount = MAX_ITEM_RDM_OPT;
  1466. }
  1467. base->requiredRandomOptions = amount;
  1468. }else{
  1469. if( !base_exists ){
  1470. base->requiredRandomOptions = 0;
  1471. }
  1472. }
  1473. if( this->nodeExists( baseNode, "CardsAllowed" ) ){
  1474. bool allowed;
  1475. if( !this->asBool( baseNode, "CardsAllowed", allowed ) ){
  1476. return 0;
  1477. }
  1478. base->cardsAllowed = allowed;
  1479. }else{
  1480. if( !base_exists ){
  1481. base->cardsAllowed = true;
  1482. }
  1483. }
  1484. if( this->nodeExists( baseNode, "Materials" ) ){
  1485. for( const ryml::NodeRef& materialNode : baseNode["Materials"] ){
  1486. std::string name;
  1487. if( !this->asString( materialNode, "Material", name ) ){
  1488. return 0;
  1489. }
  1490. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1491. if( id == nullptr ){
  1492. this->invalidWarning( materialNode["Material"], "Unknown item \"%s\".\n", name.c_str() );
  1493. return 0;
  1494. }
  1495. t_itemid material_id = id->nameid;
  1496. bool material_exists = util::umap_find( base->materials, material_id ) != nullptr;
  1497. uint16 amount;
  1498. if( this->nodeExists( materialNode, "Amount" ) ){
  1499. if( !this->asUInt16( materialNode, "Amount", amount ) ){
  1500. return 0;
  1501. }
  1502. if( amount > MAX_AMOUNT ){
  1503. this->invalidWarning( materialNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1504. amount = MAX_AMOUNT;
  1505. }
  1506. }else{
  1507. if( !material_exists ){
  1508. amount = 1;
  1509. }
  1510. }
  1511. if( amount > 0 ){
  1512. base->materials[material_id] = amount;
  1513. }else{
  1514. base->materials.erase( material_id );
  1515. }
  1516. }
  1517. }
  1518. if( this->nodeExists( baseNode, "ResultItem" ) ){
  1519. std::string name;
  1520. if( !this->asString( baseNode, "ResultItem", name ) ){
  1521. return 0;
  1522. }
  1523. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  1524. if( id == nullptr ){
  1525. this->invalidWarning( baseNode["ResultItem"], "Unknown item \"%s\".\n", name.c_str() );
  1526. return 0;
  1527. }
  1528. base->resultItemId = id->nameid;
  1529. }
  1530. if( this->nodeExists( baseNode, "ChangeRefine" ) ){
  1531. int16 refine;
  1532. if( !this->asInt16( baseNode, "ChangeRefine", refine ) ){
  1533. return 0;
  1534. }
  1535. if( refine > MAX_REFINE ){
  1536. this->invalidWarning( baseNode["MaximumRefine"], "Refine change %hu is too high, capping to MAX_REFINE...\n", refine );
  1537. refine = MAX_REFINE;
  1538. }else if( refine < -MAX_REFINE ){
  1539. this->invalidWarning( baseNode["MaximumRefine"], "Refine change %hu is too low, capping to -MAX_REFINE...\n", refine );
  1540. refine = -MAX_REFINE;
  1541. }
  1542. base->refineChange = refine;
  1543. }else{
  1544. if( !base_exists ){
  1545. base->refineChange = 0;
  1546. }
  1547. }
  1548. if( this->nodeExists( baseNode, "RandomOptionGroup" ) ){
  1549. std::string name;
  1550. if( !this->asString( baseNode, "RandomOptionGroup", name ) ){
  1551. return 0;
  1552. }
  1553. uint16 id;
  1554. if( !random_option_group.option_get_id( name, id ) ){
  1555. this->invalidWarning( baseNode["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  1556. return 0;
  1557. }
  1558. base->randomOptionGroup = random_option_group.find( id );
  1559. }else{
  1560. if( !base_exists ){
  1561. base->randomOptionGroup = nullptr;
  1562. }
  1563. }
  1564. if( this->nodeExists( baseNode, "ClearSlots" ) ){
  1565. bool clear;
  1566. if( !this->asBool( baseNode, "ClearSlots", clear ) ){
  1567. return 0;
  1568. }
  1569. base->clearSlots = clear;
  1570. }else{
  1571. if( !base_exists ){
  1572. base->clearSlots = false;
  1573. }
  1574. }
  1575. if( this->nodeExists( baseNode, "RemoveEnchantgrade" ) ){
  1576. bool clear;
  1577. if( !this->asBool( baseNode, "RemoveEnchantgrade", clear ) ){
  1578. return 0;
  1579. }
  1580. base->removeEnchantgrade = clear;
  1581. }else{
  1582. if( !base_exists ){
  1583. base->removeEnchantgrade = false;
  1584. }
  1585. }
  1586. if( !base_exists ){
  1587. entry->base_items[base_itemid] = base;
  1588. }
  1589. }
  1590. }
  1591. if( !exists ){
  1592. this->put( entry->item_id, entry );
  1593. }
  1594. return 1;
  1595. }
  1596. ItemReformDatabase item_reform_db;
  1597. const std::string ItemEnchantDatabase::getDefaultLocation(){
  1598. return std::string( db_path ) + "/item_enchant.yml";
  1599. }
  1600. bool ItemEnchantDatabase::parseMaterials( const ryml::NodeRef& node, std::unordered_map<t_itemid, uint16>& materials ){
  1601. if( this->nodeExists( node, "Materials" ) ){
  1602. for( const ryml::NodeRef& materialNode : node["Materials"] ){
  1603. std::string name;
  1604. if( !this->asString( materialNode, "Material", name ) ){
  1605. return false;
  1606. }
  1607. std::shared_ptr<item_data> item = item_db.search_aegisname( name.c_str() );
  1608. if( item == nullptr ){
  1609. this->invalidWarning( materialNode["Material"], "Unknown item \"%s\".\n", name.c_str() );
  1610. return false;
  1611. }
  1612. t_itemid material_id = item->nameid;
  1613. bool material_exists = util::umap_find( materials, material_id ) != nullptr;
  1614. uint16 amount;
  1615. if( this->nodeExists( materialNode, "Amount" ) ){
  1616. if( !this->asUInt16( materialNode, "Amount", amount ) ){
  1617. return false;
  1618. }
  1619. if( amount > MAX_AMOUNT ){
  1620. this->invalidWarning( materialNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  1621. amount = MAX_AMOUNT;
  1622. }
  1623. }else{
  1624. if( !material_exists ){
  1625. amount = 1;
  1626. }else{
  1627. amount = materials[material_id];
  1628. }
  1629. }
  1630. if( amount > 0 ){
  1631. materials[material_id] = amount;
  1632. }else{
  1633. materials.erase( material_id );
  1634. }
  1635. }
  1636. }
  1637. return true;
  1638. }
  1639. uint64 ItemEnchantDatabase::parseBodyNode( const ryml::NodeRef& node ){
  1640. uint64 id;
  1641. if( !this->asUInt64( node, "Id", id ) ){
  1642. return 0;
  1643. }
  1644. std::shared_ptr<s_item_enchant> enchant = this->find( id );
  1645. bool exists = enchant != nullptr;
  1646. if( !exists ){
  1647. if( !this->nodesExist( node, { "TargetItems", "Order", "Slots" } ) ){
  1648. return 0;
  1649. }
  1650. enchant = std::make_shared<s_item_enchant>();
  1651. enchant->id = id;
  1652. }
  1653. if( this->nodeExists( node, "TargetItems" ) ){
  1654. const ryml::NodeRef& targetItemsNode = node["TargetItems"];
  1655. for( const auto& it : targetItemsNode ){
  1656. std::string name;
  1657. c4::from_chars( it.key(), &name );
  1658. std::shared_ptr<item_data> item = item_db.search_aegisname( name.c_str() );
  1659. if( item == nullptr ){
  1660. this->invalidWarning( it, "Unknown item \"%s\".\n", name.c_str() );
  1661. return 0;
  1662. }
  1663. bool enable;
  1664. if( !this->asBool( targetItemsNode, name, enable ) ){
  1665. return 0;
  1666. }
  1667. if( enable ){
  1668. if( util::vector_exists( enchant->target_item_ids, item->nameid ) ){
  1669. this->invalidWarning( it, "Target item \"%s\" is already in the list.\n", name.c_str() );
  1670. }else{
  1671. enchant->target_item_ids.push_back( item->nameid );
  1672. }
  1673. }else{
  1674. if( !util::vector_exists( enchant->target_item_ids, item->nameid ) ){
  1675. this->invalidWarning( it, "Target item \"%s\" is not in the list.\n", name.c_str() );
  1676. }else{
  1677. util::vector_erase_if_exists( enchant->target_item_ids, item->nameid );
  1678. }
  1679. }
  1680. }
  1681. }
  1682. if( this->nodeExists( node, "MinimumRefine" ) ){
  1683. uint16 refine;
  1684. if( !this->asUInt16( node, "MinimumRefine", refine ) ){
  1685. return 0;
  1686. }
  1687. if( refine > MAX_REFINE ){
  1688. this->invalidWarning( node["MinimumRefine"], "Minimum refine %hu exceeds MAX_REFINE. Capping...\n", refine );
  1689. refine = MAX_REFINE;
  1690. }
  1691. enchant->minimumRefine = refine;
  1692. }else{
  1693. if( !exists ){
  1694. enchant->minimumRefine = 0;
  1695. }
  1696. }
  1697. if( this->nodeExists( node, "MinimumEnchantgrade" ) ){
  1698. uint16 enchantgrade;
  1699. if( !this->asUInt16( node, "MinimumEnchantgrade", enchantgrade ) ){
  1700. return 0;
  1701. }
  1702. if( enchantgrade > MAX_ENCHANTGRADE ){
  1703. this->invalidWarning( node["MinimumEnchantgrade"], "Minimum enchantgrade %hu exceeds MAX_ENCHANTGRADE. Capping...\n", enchantgrade );
  1704. enchantgrade = MAX_ENCHANTGRADE;
  1705. }
  1706. enchant->minimumEnchantgrade = enchantgrade;
  1707. }else{
  1708. if( !exists ){
  1709. enchant->minimumEnchantgrade = 0;
  1710. }
  1711. }
  1712. if( this->nodeExists( node, "AllowRandomOptions" ) ){
  1713. bool allow;
  1714. if( !this->asBool( node, "AllowRandomOptions", allow ) ){
  1715. return 0;
  1716. }
  1717. enchant->allowRandomOptions = allow;
  1718. }else{
  1719. if( !exists ){
  1720. enchant->allowRandomOptions = true;
  1721. }
  1722. }
  1723. if( this->nodeExists( node, "Reset" ) ){
  1724. const ryml::NodeRef& resetNode = node["Reset"];
  1725. if( this->nodeExists( resetNode, "Chance" ) ){
  1726. uint32 chance;
  1727. if( !this->asUInt32Rate( resetNode, "Chance", chance, 100000 ) ){
  1728. return 0;
  1729. }
  1730. enchant->reset.chance = chance;
  1731. }else{
  1732. if( !exists ){
  1733. enchant->reset.chance = 0;
  1734. }
  1735. }
  1736. if( this->nodeExists( resetNode, "Price" ) ){
  1737. uint32 zeny;
  1738. if( !this->asUInt32( resetNode, "Price", zeny ) ){
  1739. return 0;
  1740. }
  1741. if( zeny > MAX_ZENY ){
  1742. this->invalidWarning( resetNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1743. zeny = MAX_ZENY;
  1744. }
  1745. enchant->reset.zeny = zeny;
  1746. }else{
  1747. if( !exists ){
  1748. enchant->reset.zeny = 0;
  1749. }
  1750. }
  1751. if( !this->parseMaterials( resetNode, enchant->reset.materials ) ){
  1752. return 0;
  1753. }
  1754. }else{
  1755. if( !exists ){
  1756. enchant->reset = {};
  1757. }
  1758. }
  1759. if( this->nodeExists( node, "Order" ) ){
  1760. enchant->order.clear();
  1761. for( const auto& it : node["Order"] ){
  1762. uint16 slot;
  1763. if( !this->asUInt16( it, "Slot", slot ) ){
  1764. return 0;
  1765. }
  1766. if( slot >= MAX_SLOTS ){
  1767. this->invalidWarning( it, "Slot %hu exceeds MAX_SLOTS...\n", slot );
  1768. return 0;
  1769. }
  1770. enchant->order.push_back( slot );
  1771. }
  1772. }
  1773. if( this->nodeExists( node, "Slots" ) ){
  1774. for( const ryml::NodeRef& slotNode : node["Slots"] ){
  1775. uint16 slot;
  1776. if( !this->asUInt16( slotNode, "Slot", slot ) ){
  1777. return 0;
  1778. }
  1779. if( slot >= MAX_SLOTS ){
  1780. this->invalidWarning( slotNode["Slot"], "Slot %hu exceeds MAX_SLOTS...\n", slot );
  1781. return 0;
  1782. }
  1783. std::shared_ptr<s_item_enchant_slot> enchant_slot = util::umap_find( enchant->slots, slot );
  1784. bool slot_exists = enchant_slot != nullptr;
  1785. if( !slot_exists ){
  1786. enchant_slot = std::make_shared<s_item_enchant_slot>();
  1787. enchant_slot->slot = slot;
  1788. for( int i = 0; i <= MAX_ENCHANTGRADE; i++ ){
  1789. enchant_slot->normal.enchantgradeChanceIncrease[i] = 0;
  1790. }
  1791. }
  1792. if( this->nodeExists( slotNode, "Price" ) ){
  1793. uint32 zeny;
  1794. if( !this->asUInt32( slotNode, "Price", zeny ) ){
  1795. return 0;
  1796. }
  1797. if( zeny > MAX_ZENY ){
  1798. this->invalidWarning( slotNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1799. zeny = MAX_ZENY;
  1800. }
  1801. enchant_slot->normal.zeny = zeny;
  1802. }else{
  1803. if( !slot_exists ){
  1804. enchant_slot->normal.zeny = 0;
  1805. }
  1806. }
  1807. if( !this->parseMaterials( slotNode, enchant_slot->normal.materials ) ){
  1808. return 0;
  1809. }
  1810. if( this->nodeExists( slotNode, "Chance" ) ){
  1811. uint32 chance;
  1812. if( !this->asUInt32Rate( slotNode, "Chance", chance, 100000 ) ){
  1813. return 0;
  1814. }
  1815. enchant_slot->normal.chance = chance;
  1816. }else{
  1817. if( !slot_exists ){
  1818. enchant_slot->normal.chance = 100000;
  1819. }
  1820. }
  1821. if( this->nodeExists( slotNode, "EnchantgradeBonus" ) ){
  1822. for( const ryml::NodeRef& enchantgradeNode : slotNode["EnchantgradeBonus"] ){
  1823. uint16 enchantgrade;
  1824. if( !this->asUInt16( enchantgradeNode, "Enchantgrade", enchantgrade ) ){
  1825. return 0;
  1826. }
  1827. if( enchantgrade > MAX_ENCHANTGRADE ){
  1828. this->invalidWarning( enchantgradeNode["Enchantgrade"], "Enchant grade %hu exceeds MAX_ENCHANTGRADE.\n", enchantgrade );
  1829. return 0;
  1830. }
  1831. uint32 chance;
  1832. if( !this->asUInt32Rate( slotNode, "Chance", chance, 100000 ) ){
  1833. return 0;
  1834. }
  1835. enchant_slot->normal.enchantgradeChanceIncrease[enchantgrade] = chance;
  1836. }
  1837. }
  1838. if( this->nodeExists( slotNode, "Enchants" ) ){
  1839. for( const ryml::NodeRef& enchantNode : slotNode["Enchants"] ){
  1840. uint16 enchantgrade;
  1841. if( !this->asUInt16( enchantNode, "Enchantgrade", enchantgrade ) ){
  1842. return 0;
  1843. }
  1844. if( enchantgrade > MAX_ENCHANTGRADE ){
  1845. this->invalidWarning( enchantNode["Enchantgrade"], "Enchant grade %hu exceeds MAX_ENCHANTGRADE...\n", enchantgrade );
  1846. return 0;
  1847. }
  1848. std::shared_ptr<s_item_enchant_normal> enchants_for_enchantgrade = util::umap_find( enchant_slot->normal.enchants, enchantgrade );
  1849. bool enchants_for_enchantgrade_exists = enchants_for_enchantgrade != nullptr;
  1850. if( !enchants_for_enchantgrade_exists ){
  1851. enchants_for_enchantgrade = std::make_shared<s_item_enchant_normal>();
  1852. enchants_for_enchantgrade->enchantgrade = enchantgrade;
  1853. }
  1854. if( this->nodeExists( enchantNode, "Items" ) ){
  1855. for( const ryml::NodeRef& itemNode : enchantNode["Items"] ){
  1856. std::string enchant_name;
  1857. if( !this->asString( itemNode, "Item", enchant_name ) ){
  1858. return 0;
  1859. }
  1860. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1861. if( enchant_item == nullptr ){
  1862. this->invalidWarning( itemNode["Item"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1863. return 0;
  1864. }
  1865. std::shared_ptr<s_item_enchant_normal_sub> enchant = util::umap_find( enchants_for_enchantgrade->enchants, enchant_item->nameid );
  1866. bool enchant_exists = enchant != nullptr;
  1867. if( !enchant_exists ){
  1868. if( !this->nodesExist( itemNode, { "Chance" } ) ){
  1869. return 0;
  1870. }
  1871. enchant = std::make_shared<s_item_enchant_normal_sub>();
  1872. enchant->item_id = enchant_item->nameid;
  1873. }
  1874. if( this->nodeExists( itemNode, "Chance" ) ){
  1875. uint32 chance;
  1876. if( !this->asUInt32Rate( itemNode, "Chance", chance, 100000 ) ){
  1877. return 0;
  1878. }
  1879. enchant->chance = chance;
  1880. }
  1881. if( !enchant_exists ){
  1882. enchants_for_enchantgrade->enchants[enchant->item_id] = enchant;
  1883. }
  1884. }
  1885. }
  1886. if( !enchants_for_enchantgrade_exists ){
  1887. enchant_slot->normal.enchants[enchantgrade] = enchants_for_enchantgrade;
  1888. }
  1889. }
  1890. }
  1891. if( this->nodeExists( slotNode, "PerfectEnchants" ) ){
  1892. for( const ryml::NodeRef& enchantNode : slotNode["PerfectEnchants"] ){
  1893. std::string enchant_name;
  1894. if( !this->asString( enchantNode, "Item", enchant_name ) ){
  1895. return 0;
  1896. }
  1897. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1898. if( enchant_item == nullptr ){
  1899. this->invalidWarning( enchantNode["Item"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1900. return 0;
  1901. }
  1902. std::shared_ptr<s_item_enchant_perfect> enchant = util::umap_find( enchant_slot->perfect.enchants, enchant_item->nameid );
  1903. bool enchant_exists = enchant != nullptr;
  1904. if( !enchant_exists ){
  1905. enchant = std::make_shared<s_item_enchant_perfect>();
  1906. enchant->item_id = enchant_item->nameid;
  1907. }
  1908. if( this->nodeExists( enchantNode, "Price" ) ){
  1909. uint32 zeny;
  1910. if( !this->asUInt32( enchantNode, "Price", zeny ) ){
  1911. return 0;
  1912. }
  1913. if( zeny > MAX_ZENY ){
  1914. this->invalidWarning( enchantNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1915. zeny = MAX_ZENY;
  1916. }
  1917. enchant->zeny = zeny;
  1918. }else{
  1919. if( !slot_exists ){
  1920. enchant->zeny = 0;
  1921. }
  1922. }
  1923. if( !this->parseMaterials( enchantNode, enchant->materials ) ){
  1924. return 0;
  1925. }
  1926. if( !enchant_exists ){
  1927. enchant_slot->perfect.enchants[enchant->item_id] = enchant;
  1928. }
  1929. }
  1930. }
  1931. if( this->nodeExists( slotNode, "Upgrades" ) ){
  1932. for( const ryml::NodeRef& upgradeNode : slotNode["Upgrades"] ){
  1933. std::string enchant_name;
  1934. if( !this->asString( upgradeNode, "Enchant", enchant_name ) ){
  1935. return 0;
  1936. }
  1937. std::shared_ptr<item_data> enchant_item = item_db.search_aegisname( enchant_name.c_str() );
  1938. if( enchant_item == nullptr ){
  1939. this->invalidWarning( upgradeNode["Enchant"], "Unknown item \"%s\".\n", enchant_name.c_str() );
  1940. return 0;
  1941. }
  1942. std::shared_ptr<s_item_enchant_upgrade> enchant_upgrade = util::umap_find( enchant_slot->upgrade.enchants, enchant_item->nameid );
  1943. bool enchant_upgrade_exists = enchant_upgrade != nullptr;
  1944. if( !enchant_upgrade_exists ){
  1945. if( !this->nodesExist( upgradeNode, { "Upgrade" } ) ){
  1946. return 0;
  1947. }
  1948. enchant_upgrade = std::make_shared<s_item_enchant_upgrade>();
  1949. enchant_upgrade->enchant_item_id = enchant_item->nameid;
  1950. }
  1951. if( this->nodeExists( upgradeNode, "Upgrade" ) ){
  1952. std::string upgrade_name;
  1953. if( !this->asString( upgradeNode, "Upgrade", upgrade_name ) ){
  1954. return 0;
  1955. }
  1956. std::shared_ptr<item_data> upgrade_item = item_db.search_aegisname( upgrade_name.c_str() );
  1957. if( upgrade_item == nullptr ){
  1958. this->invalidWarning( upgradeNode["Upgrade"], "Unknown item \"%s\".\n", upgrade_name.c_str() );
  1959. return 0;
  1960. }
  1961. enchant_upgrade->upgrade_item_id = upgrade_item->nameid;
  1962. }
  1963. if( this->nodeExists( upgradeNode, "Price" ) ){
  1964. uint32 zeny;
  1965. if( !this->asUInt32( upgradeNode, "Price", zeny ) ){
  1966. return 0;
  1967. }
  1968. if( zeny > MAX_ZENY ){
  1969. this->invalidWarning( upgradeNode["Price"], "Price %u exceeds MAX_ZENY. Capping...\n", zeny );
  1970. zeny = MAX_ZENY;
  1971. }
  1972. enchant_upgrade->zeny = zeny;
  1973. }else{
  1974. if( !enchant_upgrade_exists ){
  1975. enchant_upgrade->zeny = 0;
  1976. }
  1977. }
  1978. if( !this->parseMaterials( upgradeNode, enchant_upgrade->materials ) ){
  1979. return 0;
  1980. }
  1981. if( !enchant_upgrade_exists ){
  1982. enchant_slot->upgrade.enchants[enchant_upgrade->enchant_item_id] = enchant_upgrade;
  1983. }
  1984. }
  1985. }
  1986. if( !slot_exists ){
  1987. enchant->slots[slot] = enchant_slot;
  1988. }
  1989. }
  1990. }
  1991. if( !exists ){
  1992. this->put( enchant->id, enchant );
  1993. }
  1994. return 1;
  1995. }
  1996. ItemEnchantDatabase item_enchant_db;
  1997. const std::string ItemPackageDatabase::getDefaultLocation(){
  1998. return std::string( db_path ) + "/item_packages.yml";
  1999. }
  2000. uint64 ItemPackageDatabase::parseBodyNode( const ryml::NodeRef& node ){
  2001. t_itemid item_id;
  2002. {
  2003. std::string name;
  2004. if( !this->asString( node, "Item", name ) ){
  2005. return 0;
  2006. }
  2007. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  2008. if( id == nullptr ){
  2009. this->invalidWarning( node["Item"], "Unknown item \"%s\".\n", name.c_str() );
  2010. return 0;
  2011. }
  2012. item_id = id->nameid;
  2013. }
  2014. std::shared_ptr<s_item_package> entry = this->find( item_id );
  2015. bool exists = entry != nullptr;
  2016. if( !exists ){
  2017. if( !this->nodesExist( node, { "Groups" } ) ){
  2018. return 0;
  2019. }
  2020. entry = std::make_shared<s_item_package>();
  2021. entry->item_id = item_id;
  2022. }
  2023. if( this->nodeExists( node, "Groups" ) ){
  2024. for( const ryml::NodeRef& groupNode : node["Groups"] ){
  2025. uint32 groupIndex;
  2026. if( !this->asUInt32( groupNode, "Group", groupIndex) ){
  2027. return 0;
  2028. }
  2029. std::shared_ptr<s_item_package_group> group = util::umap_find( entry->groups, groupIndex );
  2030. bool group_exists = group != nullptr;
  2031. if( !group_exists ){
  2032. if( !this->nodesExist( groupNode, { "Items" } ) ){
  2033. return 0;
  2034. }
  2035. group = std::make_shared<s_item_package_group>();
  2036. group->groupIndex = groupIndex;
  2037. }
  2038. for( const ryml::NodeRef& itemNode : groupNode["Items"] ){
  2039. std::string name;
  2040. if( !this->asString( itemNode, "Item", name ) ){
  2041. return 0;
  2042. }
  2043. std::shared_ptr<item_data> id = item_db.search_aegisname( name.c_str() );
  2044. if( id == nullptr ){
  2045. this->invalidWarning( itemNode["Item"], "Unknown item \"%s\".\n", name.c_str() );
  2046. return 0;
  2047. }
  2048. std::shared_ptr<s_item_package_item> package_item = util::umap_find( group->items, id->nameid );
  2049. bool package_item_exists = package_item != nullptr;
  2050. if( !package_item_exists ){
  2051. package_item = std::make_shared<s_item_package_item>();
  2052. package_item->item_id = id->nameid;
  2053. }
  2054. if( this->nodeExists( itemNode, "Amount" ) ){
  2055. uint16 amount;
  2056. if( !this->asUInt16( itemNode, "Amount", amount ) ){
  2057. return 0;
  2058. }
  2059. if( amount > MAX_AMOUNT ){
  2060. this->invalidWarning( itemNode["Amount"], "Amount %hu is too high, capping to MAX_AMOUNT...\n", amount );
  2061. amount = MAX_AMOUNT;
  2062. }else if( amount == 0 ){
  2063. if( !package_item_exists ){
  2064. this->invalidWarning( itemNode["Amount"], "Trying to remove non existant item \"%s\".\n", name.c_str() );
  2065. return 0;
  2066. }else{
  2067. group->items.erase( id->nameid );
  2068. }
  2069. }
  2070. package_item->amount = amount;
  2071. }else{
  2072. if( !package_item_exists ){
  2073. package_item->amount = 1;
  2074. }
  2075. }
  2076. if( this->nodeExists( itemNode, "RentalHours" ) ){
  2077. uint16 rentalhours;
  2078. if( !this->asUInt16( itemNode, "RentalHours", rentalhours ) ){
  2079. return 0;
  2080. }
  2081. package_item->rentalhours = rentalhours;
  2082. }else{
  2083. if( !package_item_exists ){
  2084. package_item->rentalhours = 0;
  2085. }
  2086. }
  2087. if( this->nodeExists( itemNode, "Refine" ) ){
  2088. uint16 refine;
  2089. if( !this->asUInt16( itemNode, "Refine", refine ) ){
  2090. return 0;
  2091. }
  2092. if( refine > MAX_REFINE ){
  2093. this->invalidWarning( itemNode["Refine"], "Refine %hu is too high, capping to MAX_REFINE...\n", refine );
  2094. refine = MAX_REFINE;
  2095. }
  2096. package_item->refine = refine;
  2097. }else{
  2098. if( !package_item_exists ){
  2099. package_item->refine = 0;
  2100. }
  2101. }
  2102. if( this->nodeExists( itemNode, "RandomOptionGroup" ) ){
  2103. std::string name;
  2104. if( !this->asString( itemNode, "RandomOptionGroup", name ) ){
  2105. return 0;
  2106. }
  2107. uint16 option_group_id;
  2108. if( !random_option_group.option_get_id( name, option_group_id ) ){
  2109. this->invalidWarning( itemNode["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  2110. return 0;
  2111. }
  2112. package_item->randomOptionGroup = random_option_group.find( option_group_id );
  2113. }else{
  2114. if( !package_item_exists ){
  2115. package_item->randomOptionGroup = nullptr;
  2116. }
  2117. }
  2118. if( !package_item_exists ){
  2119. group->items[package_item->item_id] = package_item;
  2120. }
  2121. }
  2122. if( !group_exists ){
  2123. entry->groups[group->groupIndex] = group;
  2124. }
  2125. }
  2126. }
  2127. if( !exists ){
  2128. this->put( entry->item_id, entry );
  2129. }
  2130. return 1;
  2131. }
  2132. ItemPackageDatabase item_package_db;
  2133. /*==========================================
  2134. * Finds up to N matches. Returns number of matches [Skotlex]
  2135. * @param *data
  2136. * @param size
  2137. * @param str
  2138. * @return Number of matches item
  2139. *------------------------------------------*/
  2140. uint16 itemdb_searchname_array(std::map<t_itemid, std::shared_ptr<item_data>> &data, uint16 size, const char *str)
  2141. {
  2142. for (const auto &item : item_db) {
  2143. std::shared_ptr<item_data> id = item.second;
  2144. if (id == nullptr)
  2145. continue;
  2146. if (stristr(id->name.c_str(), str) != nullptr || stristr(id->ename.c_str(), str) != nullptr || strcmpi(id->ename.c_str(), str) == 0)
  2147. data[id->nameid] = id;
  2148. }
  2149. if (data.size() > size)
  2150. util::map_resize(data, size);
  2151. return static_cast<uint16>(data.size());
  2152. }
  2153. std::shared_ptr<s_item_group_entry> get_random_itemsubgroup(std::shared_ptr<s_item_group_random> random) {
  2154. if (random == nullptr)
  2155. return nullptr;
  2156. for (size_t j = 0, max = random->data.size() * 3; j < max; j++) {
  2157. std::shared_ptr<s_item_group_entry> entry = util::umap_random(random->data);
  2158. if (entry->rate == 0 || rnd() % random->total_rate < entry->rate) // always return entry for rate 0 ('must' item)
  2159. return entry;
  2160. }
  2161. return util::umap_random(random->data);
  2162. }
  2163. /**
  2164. * Return a random group entry from Item Group
  2165. * @param group_id
  2166. * @param sub_group: 0 is 'must' item group, random groups start from 1
  2167. * @return Item group entry or NULL on fail
  2168. */
  2169. std::shared_ptr<s_item_group_entry> ItemGroupDatabase::get_random_entry(uint16 group_id, uint8 sub_group) {
  2170. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  2171. if (group == nullptr) {
  2172. ShowError("get_random_entry: Invalid group id %hu.\n", group_id);
  2173. return nullptr;
  2174. }
  2175. if (group->random.empty()) {
  2176. ShowError("get_random_entry: No item entries for group id %hu.\n", group_id);
  2177. return nullptr;
  2178. }
  2179. if (group->random.count(sub_group) == 0) {
  2180. ShowError("get_random_entry: No item entries for group id %hu and sub group %hu.\n", group_id, sub_group);
  2181. return nullptr;
  2182. }
  2183. return get_random_itemsubgroup(group->random[sub_group]);
  2184. }
  2185. /**
  2186. * Return a random Item ID from Item Group
  2187. * @param group_id
  2188. * @param sub_group: 0 is 'must' item group, random groups start from 1
  2189. * @return Item ID or UNKNOWN_ITEM_ID on fail
  2190. */
  2191. t_itemid ItemGroupDatabase::get_random_item_id(uint16 group_id, uint8 sub_group) {
  2192. std::shared_ptr<s_item_group_entry> entry = this->get_random_entry(group_id, sub_group);
  2193. return entry != nullptr ? entry->nameid : UNKNOWN_ITEM_ID;
  2194. }
  2195. /** [Cydh]
  2196. * Gives item(s) to the player based on item group
  2197. * @param sd: Player that obtains item from item group
  2198. * @param group_id: The group ID of item that obtained by player
  2199. * @param *group: struct s_item_group from itemgroup_db[group_id].random[idx] or itemgroup_db[group_id].must[sub_group][idx]
  2200. */
  2201. static void itemdb_pc_get_itemgroup_sub(map_session_data *sd, bool identify, std::shared_ptr<s_item_group_entry> data) {
  2202. if (data == nullptr)
  2203. return;
  2204. item tmp = {};
  2205. tmp.nameid = data->nameid;
  2206. tmp.bound = data->bound;
  2207. tmp.identify = identify ? identify : itemdb_isidentified(data->nameid);
  2208. tmp.expire_time = (data->duration) ? (unsigned int)(time(NULL) + data->duration*60) : 0;
  2209. if (data->isNamed) {
  2210. tmp.card[0] = itemdb_isequip(data->nameid) ? CARD0_FORGE : CARD0_CREATE;
  2211. tmp.card[1] = 0;
  2212. tmp.card[2] = GetWord(sd->status.char_id, 0);
  2213. tmp.card[3] = GetWord(sd->status.char_id, 1);
  2214. }
  2215. uint16 get_amt = 0;
  2216. if (itemdb_isstackable(data->nameid) && data->isStacked)
  2217. get_amt = data->amount;
  2218. else
  2219. get_amt = 1;
  2220. tmp.amount = get_amt;
  2221. // Do loop for non-stackable item
  2222. for (uint16 i = 0; i < data->amount; i += get_amt) {
  2223. char flag = 0;
  2224. tmp.unique_id = data->GUID ? pc_generate_unique_id(sd) : 0; // Generate GUID
  2225. if( itemdb_isequip( data->nameid ) ){
  2226. if( data->refineMinimum > 0 && data->refineMaximum > 0 ){
  2227. tmp.refine = rnd_value( data->refineMinimum, data->refineMaximum );
  2228. }else if( data->refineMinimum > 0 ){
  2229. tmp.refine = rnd_value( data->refineMinimum, MAX_REFINE );
  2230. }else if( data->refineMaximum > 0 ){
  2231. tmp.refine = rnd_value( 1, data->refineMaximum );
  2232. }else{
  2233. tmp.refine = 0;
  2234. }
  2235. if( data->randomOptionGroup != nullptr ){
  2236. memset( tmp.option, 0, sizeof( tmp.option ) );
  2237. data->randomOptionGroup->apply( tmp );
  2238. }
  2239. }
  2240. if ((flag = pc_additem(sd, &tmp, get_amt, LOG_TYPE_SCRIPT))) {
  2241. clif_additem(sd, 0, 0, flag);
  2242. if (pc_candrop(sd, &tmp))
  2243. map_addflooritem(&tmp, tmp.amount, sd->bl.m, sd->bl.x,sd->bl.y, 0, 0, 0, 0, 0);
  2244. }
  2245. else if (!flag && data->isAnnounced)
  2246. intif_broadcast_obtain_special_item(sd, data->nameid, sd->itemid, ITEMOBTAIN_TYPE_BOXITEM);
  2247. }
  2248. }
  2249. /** [Cydh]
  2250. * Find item(s) that will be obtained by player based on Item Group
  2251. * @param group_id: The group ID that will be gained by player
  2252. * @param nameid: The item that trigger this item group
  2253. * @return val: 0:success, 1:no sd, 2:invalid item group
  2254. */
  2255. uint8 ItemGroupDatabase::pc_get_itemgroup(uint16 group_id, bool identify, map_session_data *sd) {
  2256. nullpo_retr(1,sd);
  2257. std::shared_ptr<s_item_group_db> group = this->find(group_id);
  2258. if (group == nullptr) {
  2259. ShowError("pc_get_itemgroup: Invalid group id '%d' specified.\n",group_id);
  2260. return 2;
  2261. }
  2262. if (group->random.empty())
  2263. return 0;
  2264. // Get all the 'must' item(s) (subgroup 0)
  2265. uint16 subgroup = 0;
  2266. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  2267. if (random != nullptr && !random->data.empty()) {
  2268. for (const auto &it : random->data)
  2269. itemdb_pc_get_itemgroup_sub(sd, identify, it.second);
  2270. }
  2271. // Get 1 'random' item from each subgroup
  2272. for (const auto &random : group->random) {
  2273. if (random.first == 0 || random.second->data.empty())
  2274. continue;
  2275. itemdb_pc_get_itemgroup_sub(sd, identify, get_random_itemsubgroup(random.second));
  2276. }
  2277. return 0;
  2278. }
  2279. /** Searches for the item_data. Use this to check if item exists or not.
  2280. * @param nameid
  2281. * @return *item_data if item is exist, or NULL if not
  2282. */
  2283. std::shared_ptr<item_data> itemdb_exists(t_itemid nameid) {
  2284. return item_db.find(nameid);
  2285. }
  2286. /// Returns name type of ammunition [Cydh]
  2287. const char *itemdb_typename_ammo (e_ammo_type ammo) {
  2288. switch (ammo) {
  2289. case AMMO_ARROW: return "Arrow";
  2290. case AMMO_DAGGER: return "Throwable Dagger";
  2291. case AMMO_BULLET: return "Bullet";
  2292. case AMMO_SHELL: return "Shell";
  2293. case AMMO_GRENADE: return "Grenade";
  2294. case AMMO_SHURIKEN: return "Shuriken";
  2295. case AMMO_KUNAI: return "Kunai";
  2296. case AMMO_CANNONBALL: return "Cannonball";
  2297. case AMMO_THROWWEAPON: return "Throwable Item/Sling Item";
  2298. }
  2299. return "Ammunition";
  2300. }
  2301. /// Returns human readable name for given item type.
  2302. /// @param type Type id to retrieve name for ( IT_* ).
  2303. const char* itemdb_typename(enum item_types type)
  2304. {
  2305. switch(type)
  2306. {
  2307. case IT_HEALING: return "Potion/Food";
  2308. case IT_USABLE: return "Usable";
  2309. case IT_ETC: return "Etc.";
  2310. case IT_WEAPON: return "Weapon";
  2311. case IT_ARMOR: return "Armor";
  2312. case IT_CARD: return "Card";
  2313. case IT_PETEGG: return "Pet Egg";
  2314. case IT_PETARMOR: return "Pet Accessory";
  2315. case IT_AMMO: return "Arrow/Ammunition";
  2316. case IT_DELAYCONSUME: return "Delay-Consume Usable";
  2317. case IT_SHADOWGEAR: return "Shadow Equipment";
  2318. case IT_CASH: return "Cash Usable";
  2319. }
  2320. return "Unknown Type";
  2321. }
  2322. /**
  2323. * Converts the jobmask from the format in itemdb to the format used by the map server.
  2324. * @param bclass: Pointer to store itemdb format
  2325. * @param jobmask: Job Mask to convert
  2326. * @param active: Whether the flag is active or not
  2327. * @author: Skotlex
  2328. */
  2329. static void itemdb_jobid2mapid(uint64 bclass[3], e_mapid jobmask, bool active)
  2330. {
  2331. uint64 temp_mask[3] = { 0 };
  2332. if (jobmask != MAPID_ALL) {
  2333. // Calculate the required bit to set
  2334. uint64 job = 1ULL << ( jobmask & MAPID_BASEMASK );
  2335. // 2-1
  2336. if( ( jobmask & JOBL_2_1 ) != 0 ){
  2337. temp_mask[1] |= job;
  2338. // 2-2
  2339. }else if( ( jobmask & JOBL_2_2 ) != 0 ){
  2340. temp_mask[2] |= job;
  2341. // Basejob
  2342. }else{
  2343. temp_mask[0] |= job;
  2344. }
  2345. } else {
  2346. temp_mask[0] = temp_mask[1] = temp_mask[2] = MAPID_ALL;
  2347. }
  2348. for (int i = 0; i < ARRAYLENGTH(temp_mask); i++) {
  2349. if (temp_mask[i] == 0)
  2350. continue;
  2351. if (active)
  2352. bclass[i] |= temp_mask[i];
  2353. else
  2354. bclass[i] &= ~temp_mask[i];
  2355. }
  2356. }
  2357. /*==========================================
  2358. * Loads an item from the db. If not found, it will return the dummy item.
  2359. * @param nameid
  2360. * @return *item_data or *dummy_item if item not found
  2361. *------------------------------------------*/
  2362. struct item_data* itemdb_search(t_itemid nameid) {
  2363. std::shared_ptr<item_data> id;
  2364. if (!(id = item_db.find(nameid))) {
  2365. ShowWarning("itemdb_search: Item ID %u does not exists in the item_db. Using dummy data.\n", nameid);
  2366. id = item_db.find(ITEMID_DUMMY);
  2367. }
  2368. return id.get();
  2369. }
  2370. /** Checks if item is equip type or not
  2371. * @param id Item data
  2372. * @return True if item is equip, false otherwise
  2373. */
  2374. bool itemdb_isequip2(struct item_data *id) {
  2375. nullpo_ret(id);
  2376. switch (id->type) {
  2377. case IT_WEAPON:
  2378. case IT_ARMOR:
  2379. case IT_AMMO:
  2380. case IT_SHADOWGEAR:
  2381. return true;
  2382. default:
  2383. return false;
  2384. }
  2385. }
  2386. /** Checks if item is stackable or not
  2387. * @param id Item data
  2388. * @return True if item is stackable, false otherwise
  2389. */
  2390. bool itemdb_isstackable2(struct item_data *id)
  2391. {
  2392. nullpo_ret(id);
  2393. return id->isStackable();
  2394. }
  2395. /*==========================================
  2396. * Trade Restriction functions [Skotlex]
  2397. *------------------------------------------*/
  2398. bool itemdb_isdropable_sub(struct item_data *item, int gmlv, int unused) {
  2399. return (item && (!(item->flag.trade_restriction.drop) || gmlv >= item->gm_lv_trade_override));
  2400. }
  2401. bool itemdb_cantrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  2402. return (item && (!(item->flag.trade_restriction.trade) || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  2403. }
  2404. bool itemdb_canpartnertrade_sub(struct item_data* item, int gmlv, int gmlv2) {
  2405. return (item && (item->flag.trade_restriction.trade_partner || gmlv >= item->gm_lv_trade_override || gmlv2 >= item->gm_lv_trade_override));
  2406. }
  2407. bool itemdb_cansell_sub(struct item_data* item, int gmlv, int unused) {
  2408. return (item && (!(item->flag.trade_restriction.sell) || gmlv >= item->gm_lv_trade_override));
  2409. }
  2410. bool itemdb_cancartstore_sub(struct item_data* item, int gmlv, int unused) {
  2411. return (item && (!(item->flag.trade_restriction.cart) || gmlv >= item->gm_lv_trade_override));
  2412. }
  2413. bool itemdb_canstore_sub(struct item_data* item, int gmlv, int unused) {
  2414. return (item && (!(item->flag.trade_restriction.storage) || gmlv >= item->gm_lv_trade_override));
  2415. }
  2416. bool itemdb_canguildstore_sub(struct item_data* item, int gmlv, int unused) {
  2417. return (item && (!(item->flag.trade_restriction.guild_storage) || gmlv >= item->gm_lv_trade_override));
  2418. }
  2419. bool itemdb_canmail_sub(struct item_data* item, int gmlv, int unused) {
  2420. return (item && (!(item->flag.trade_restriction.mail) || gmlv >= item->gm_lv_trade_override));
  2421. }
  2422. bool itemdb_canauction_sub(struct item_data* item, int gmlv, int unused) {
  2423. return (item && (!(item->flag.trade_restriction.auction) || gmlv >= item->gm_lv_trade_override));
  2424. }
  2425. bool itemdb_isrestricted(struct item* item, int gmlv, int gmlv2, bool (*func)(struct item_data*, int, int))
  2426. {
  2427. struct item_data* item_data = itemdb_search(item->nameid);
  2428. int i;
  2429. if (!func(item_data, gmlv, gmlv2))
  2430. return false;
  2431. if(item_data->slots == 0 || itemdb_isspecial(item->card[0]))
  2432. return true;
  2433. for(i = 0; i < item_data->slots; i++) {
  2434. if (!item->card[i]) continue;
  2435. if (!func(itemdb_search(item->card[i]), gmlv, gmlv2))
  2436. return false;
  2437. }
  2438. return true;
  2439. }
  2440. bool itemdb_ishatched_egg(struct item* item) {
  2441. if (item && item->card[0] == CARD0_PET && item->attribute == 1)
  2442. return true;
  2443. return false;
  2444. }
  2445. /** Specifies if item-type should drop unidentified.
  2446. * @param nameid ID of item
  2447. */
  2448. char itemdb_isidentified(t_itemid nameid) {
  2449. int type=itemdb_type(nameid);
  2450. switch (type) {
  2451. case IT_WEAPON:
  2452. case IT_ARMOR:
  2453. case IT_PETARMOR:
  2454. case IT_SHADOWGEAR:
  2455. return 0;
  2456. default:
  2457. return 1;
  2458. }
  2459. }
  2460. const std::string ItemGroupDatabase::getDefaultLocation() {
  2461. return std::string(db_path) + "/item_group_db.yml";
  2462. }
  2463. /**
  2464. * Reads and parses an entry from the item_group_db.
  2465. * @param node: YAML node containing the entry.
  2466. * @return count of successfully parsed rows
  2467. */
  2468. uint64 ItemGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  2469. std::string group_name;
  2470. if (!this->asString(node, "Group", group_name))
  2471. return 0;
  2472. std::string group_name_constant = "IG_" + group_name;
  2473. int64 constant;
  2474. if (!script_get_constant(group_name_constant.c_str(), &constant) || constant < IG_BLUEBOX) {
  2475. if (strncasecmp(group_name.c_str(), "IG_", 3) != 0)
  2476. this->invalidWarning(node["Group"], "Invalid group %s.\n", group_name.c_str());
  2477. else
  2478. this->invalidWarning(node["Group"], "Invalid group %s. Note that 'IG_' is automatically appended to the group name.\n", group_name.c_str());
  2479. return 0;
  2480. }
  2481. uint16 id = static_cast<uint16>(constant);
  2482. std::shared_ptr<s_item_group_db> group = this->find(id);
  2483. bool exists = group != nullptr;
  2484. if (!exists) {
  2485. group = std::make_shared<s_item_group_db>();
  2486. group->id = id;
  2487. }
  2488. if (this->nodeExists(node, "SubGroups")) {
  2489. const auto& subNode = node["SubGroups"];
  2490. for (const auto& subit : subNode) {
  2491. if (this->nodeExists(subit, "Clear")) {
  2492. uint16 id;
  2493. if (!this->asUInt16(subit, "Clear", id))
  2494. continue;
  2495. if (group->random.erase(id) == 0)
  2496. this->invalidWarning(subit["Clear"], "The SubGroup %hu doesn't exist in the group %s. Clear failed.\n", id, group_name.c_str());
  2497. continue;
  2498. }
  2499. if (!this->nodesExist(subit, { "SubGroup", "List" })) {
  2500. continue;
  2501. }
  2502. uint16 subgroup;
  2503. if (this->nodeExists(subit, "SubGroup")) {
  2504. if (!this->asUInt16(subit, "SubGroup", subgroup))
  2505. continue;
  2506. } else {
  2507. subgroup = 1;
  2508. }
  2509. std::shared_ptr<s_item_group_random> random = util::umap_find(group->random, subgroup);
  2510. if (random == nullptr) {
  2511. random = std::make_shared<s_item_group_random>();
  2512. group->random[subgroup] = random;
  2513. }
  2514. const auto& listNode = subit["List"];
  2515. for (const auto& listit : listNode) {
  2516. if (this->nodeExists(listit, "Clear")) {
  2517. std::string item_name;
  2518. if (!this->asString(listit, "Clear", item_name))
  2519. continue;
  2520. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  2521. if (item == nullptr) {
  2522. this->invalidWarning(listit["Clear"], "Unknown Item %s. Clear failed.\n", item_name.c_str());
  2523. continue;
  2524. }
  2525. if (random->data.erase(item->nameid) == 0)
  2526. this->invalidWarning(listit["Clear"], "Item %hu doesn't exist in the SubGroup %hu (group %s). Clear failed.\n", item->nameid, subgroup, group_name.c_str());
  2527. continue;
  2528. }
  2529. std::string item_name;
  2530. if (!this->asString(listit, "Item", item_name))
  2531. continue;
  2532. std::shared_ptr<item_data> item = item_db.search_aegisname( item_name.c_str() );
  2533. if (item == nullptr) {
  2534. this->invalidWarning(listit["Item"], "Unknown Item %s.\n", item_name.c_str());
  2535. continue;
  2536. }
  2537. std::shared_ptr<s_item_group_entry> entry = util::umap_find(random->data, item->nameid);
  2538. bool entry_exists = entry != nullptr;
  2539. if (!entry_exists) {
  2540. entry = std::make_shared<s_item_group_entry>();
  2541. random->data[item->nameid] = entry;
  2542. }
  2543. entry->nameid = item->nameid;
  2544. if (this->nodeExists(listit, "Rate")) {
  2545. uint16 rate;
  2546. if (!this->asUInt16(listit, "Rate", rate))
  2547. continue;
  2548. entry->rate = rate;
  2549. } else {
  2550. if (!entry_exists)
  2551. entry->rate = 0;
  2552. }
  2553. if (subgroup == 0 && entry->rate > 0) {
  2554. this->invalidWarning(listit["Item"], "SubGroup 0 is reserved for item without Rate ('must' item). Defaulting Rate to 0.\n");
  2555. entry->rate = 0;
  2556. }
  2557. if (subgroup != 0 && entry->rate == 0) {
  2558. this->invalidWarning(listit["Item"], "Entry must have a Rate for group above 0, skipping.\n");
  2559. continue;
  2560. }
  2561. if (this->nodeExists(listit, "Amount")) {
  2562. uint16 amount;
  2563. if (!this->asUInt16(listit, "Amount", amount))
  2564. continue;
  2565. entry->amount = cap_value(amount, 1, MAX_AMOUNT);
  2566. } else {
  2567. if (!entry_exists)
  2568. entry->amount = 1;
  2569. }
  2570. if (this->nodeExists(listit, "Duration")) {
  2571. uint16 duration;
  2572. if (!this->asUInt16(listit, "Duration", duration))
  2573. continue;
  2574. entry->duration = duration;
  2575. } else {
  2576. if (!entry_exists)
  2577. entry->duration = 0;
  2578. }
  2579. if (this->nodeExists(listit, "Announced")) {
  2580. bool isAnnounced;
  2581. if (!this->asBool(listit, "Announced", isAnnounced))
  2582. continue;
  2583. entry->isAnnounced = isAnnounced;
  2584. } else {
  2585. if (!entry_exists)
  2586. entry->isAnnounced = false;
  2587. }
  2588. if (this->nodeExists(listit, "UniqueId")) {
  2589. bool guid;
  2590. if (!this->asBool(listit, "UniqueId", guid))
  2591. continue;
  2592. entry->GUID = guid;
  2593. } else {
  2594. if (!entry_exists)
  2595. entry->GUID = item->flag.guid;
  2596. }
  2597. if (this->nodeExists(listit, "Stacked")) {
  2598. bool isStacked;
  2599. if (!this->asBool(listit, "Stacked", isStacked))
  2600. continue;
  2601. entry->isStacked = isStacked;
  2602. } else {
  2603. if (!entry_exists)
  2604. entry->isStacked = true;
  2605. }
  2606. if (this->nodeExists(listit, "Named")) {
  2607. bool named;
  2608. if (!this->asBool(listit, "Named", named))
  2609. continue;
  2610. entry->isNamed = named;
  2611. } else {
  2612. if (!entry_exists)
  2613. entry->isNamed = false;
  2614. }
  2615. if (this->nodeExists(listit, "Bound")) {
  2616. std::string bound_name;
  2617. if (!this->asString(listit, "Bound", bound_name))
  2618. continue;
  2619. std::string bound_name_constant = "BOUND_" + bound_name;
  2620. int64 bound;
  2621. if (!script_get_constant(bound_name_constant.c_str(), &bound) || bound < BOUND_NONE || bound >= BOUND_MAX) {
  2622. this->invalidWarning(listit["Group"], "Invalid Bound %s.\n", bound_name.c_str());
  2623. continue;
  2624. }
  2625. entry->bound = static_cast<uint8>(bound);
  2626. } else {
  2627. if (!entry_exists)
  2628. entry->bound = BOUND_NONE;
  2629. }
  2630. if( this->nodeExists( listit, "RandomOptionGroup" ) ){
  2631. std::string name;
  2632. if( !this->asString( listit, "RandomOptionGroup", name ) ){
  2633. return 0;
  2634. }
  2635. uint16 id;
  2636. if( !random_option_group.option_get_id( name, id ) ){
  2637. this->invalidWarning( listit["RandomOptionGroup"], "Unknown random option group \"%s\".\n", name.c_str() );
  2638. return 0;
  2639. }
  2640. entry->randomOptionGroup = random_option_group.find( id );
  2641. }else{
  2642. if( !entry_exists ){
  2643. entry->randomOptionGroup = nullptr;
  2644. }
  2645. }
  2646. if( this->nodeExists( listit, "RefineMinimum" ) ){
  2647. uint16 refine;
  2648. if( !this->asUInt16( listit, "RefineMinimum", refine ) ){
  2649. return 0;
  2650. }
  2651. if( refine > MAX_REFINE ){
  2652. this->invalidWarning( listit["RefineMinimum"], "Minimum refine % hu is too high, capping to MAX_REFINE...\n", refine );
  2653. refine = MAX_REFINE;
  2654. }
  2655. entry->refineMinimum = refine;
  2656. }else{
  2657. if( !exists ){
  2658. entry->refineMinimum = 0;
  2659. }
  2660. }
  2661. if( this->nodeExists( listit, "RefineMaximum" ) ){
  2662. uint16 refine;
  2663. if( !this->asUInt16( listit, "RefineMaximum", refine ) ){
  2664. return 0;
  2665. }
  2666. if( refine > MAX_REFINE ){
  2667. this->invalidWarning( listit["RefineMaximum"], "Maximum refine %hu is too high, capping to MAX_REFINE...\n", refine );
  2668. refine = MAX_REFINE;
  2669. }
  2670. entry->refineMaximum = refine;
  2671. }else{
  2672. if( !exists ){
  2673. entry->refineMaximum = 0;
  2674. }
  2675. }
  2676. }
  2677. }
  2678. }
  2679. if (!exists)
  2680. this->put(id, group);
  2681. return 1;
  2682. }
  2683. void ItemGroupDatabase::loadingFinished() {
  2684. for (const auto &group : *this) {
  2685. for (const auto &random : group.second->random) {
  2686. random.second->total_rate = 0;
  2687. for (const auto &it : random.second->data) {
  2688. random.second->total_rate += it.second->rate;
  2689. }
  2690. }
  2691. }
  2692. TypesafeYamlDatabase::loadingFinished();
  2693. }
  2694. /** Read item forbidden by mapflag (can't equip item)
  2695. * Structure: <nameid>,<mode>
  2696. */
  2697. static bool itemdb_read_noequip(char* str[], int columns, int current) {
  2698. t_itemid nameid;
  2699. int flag;
  2700. nameid = strtoul(str[0], nullptr, 10);
  2701. flag = atoi(str[1]);
  2702. std::shared_ptr<item_data> id = item_db.find(nameid);
  2703. if( id == nullptr )
  2704. {
  2705. ShowWarning("itemdb_read_noequip: Invalid item id %u.\n", nameid);
  2706. return false;
  2707. }
  2708. if (flag >= 0)
  2709. id->flag.no_equip |= flag;
  2710. else
  2711. id->flag.no_equip &= ~abs(flag);
  2712. return true;
  2713. }
  2714. const std::string ComboDatabase::getDefaultLocation() {
  2715. return std::string(db_path) + "/item_combos.yml";
  2716. }
  2717. uint16 ComboDatabase::find_combo_id( const std::vector<t_itemid>& items ){
  2718. for (const auto &it : *this) {
  2719. if (it.second->nameid == items) {
  2720. return it.first;
  2721. }
  2722. }
  2723. return 0;
  2724. }
  2725. /**
  2726. * Reads and parses an entry from the item_combos.
  2727. * @param node: YAML node containing the entry.
  2728. * @return count of successfully parsed rows
  2729. */
  2730. uint64 ComboDatabase::parseBodyNode(const ryml::NodeRef& node) {
  2731. std::vector<std::vector<t_itemid>> items_list;
  2732. if( !this->nodesExist( node, { "Combos" } ) ){
  2733. return 0;
  2734. }
  2735. const ryml::NodeRef& combosNode = node["Combos"];
  2736. for (const auto& comboit : combosNode) {
  2737. static const std::string nodeName = "Combo";
  2738. if (!this->nodesExist(comboit, { nodeName })) {
  2739. return 0;
  2740. }
  2741. const ryml::NodeRef& comboNode = comboit["Combo"];
  2742. if (!comboNode.is_seq()) {
  2743. this->invalidWarning(comboNode, "%s should be a sequence.\n", nodeName.c_str());
  2744. return 0;
  2745. }
  2746. std::vector<t_itemid> items = {};
  2747. for (const auto it : comboNode) {
  2748. std::string item_name;
  2749. c4::from_chars(it.val(), &item_name);
  2750. std::shared_ptr<item_data> item = item_db.search_aegisname(item_name.c_str());
  2751. if (item == nullptr) {
  2752. this->invalidWarning(it, "Invalid item %s, skipping.\n", item_name.c_str());
  2753. return 0;
  2754. }
  2755. items.push_back(item->nameid);
  2756. }
  2757. if (items.empty()) {
  2758. this->invalidWarning(comboNode, "Empty combo, skipping.\n");
  2759. return 0;
  2760. }
  2761. if (items.size() < 2) {
  2762. this->invalidWarning(comboNode, "Not enough item to make a combo (need at least 2). Skipping.\n");
  2763. return 0;
  2764. }
  2765. std::sort(items.begin(), items.end());
  2766. items_list.push_back(items);
  2767. }
  2768. if (items_list.empty()) {
  2769. this->invalidWarning(combosNode, "No combos defined, skipping.\n");
  2770. return 0;
  2771. }
  2772. if (this->nodeExists(node, "Clear")) {
  2773. bool clear = false;
  2774. if (!this->asBool(node, "Clear", clear))
  2775. return 0;
  2776. // Remove the combo (if exists)
  2777. if (clear) {
  2778. for (const auto& itemsit : items_list) {
  2779. uint16 id = this->find_combo_id(itemsit);
  2780. if (id == 0) {
  2781. this->invalidWarning(node["Clear"], "Unable to clear the combo.\n");
  2782. return 0;
  2783. }
  2784. this->erase(id);
  2785. }
  2786. return 1;
  2787. }
  2788. }
  2789. uint64 count = 0;
  2790. for (const auto &itemsit : items_list) {
  2791. // Find the id when the combo exists
  2792. uint16 id = this->find_combo_id(itemsit);
  2793. std::shared_ptr<s_item_combo> combo = this->find(id);
  2794. bool exists = combo != nullptr;
  2795. if (!exists) {
  2796. combo = std::make_shared<s_item_combo>();
  2797. combo->nameid.insert(combo->nameid.begin(), itemsit.begin(), itemsit.end());
  2798. combo->id = ++this->combo_num;
  2799. }
  2800. if (this->nodeExists(node, "Script")) {
  2801. std::string script;
  2802. if (!this->asString(node, "Script", script))
  2803. return 0;
  2804. if (exists) {
  2805. script_free_code(combo->script);
  2806. combo->script = nullptr;
  2807. }
  2808. combo->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  2809. } else {
  2810. if (!exists) {
  2811. combo->script = nullptr;
  2812. }
  2813. }
  2814. if (!exists)
  2815. this->put( combo->id, combo );
  2816. count++;
  2817. }
  2818. return count;
  2819. }
  2820. void ComboDatabase::loadingFinished() {
  2821. // Populate item_data to refer to the combo
  2822. for (const auto &combo : *this) {
  2823. for (const auto &itm : combo.second->nameid) {
  2824. std::shared_ptr<item_data> it = item_db.find(itm);
  2825. if (it != nullptr)
  2826. it->combos.push_back(combo.second);
  2827. }
  2828. }
  2829. TypesafeYamlDatabase::loadingFinished();
  2830. }
  2831. /**
  2832. * Process Roulette items
  2833. */
  2834. bool itemdb_parse_roulette_db(void)
  2835. {
  2836. int i, j;
  2837. uint32 count = 0;
  2838. // retrieve all rows from the item database
  2839. if (SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", roulette_table)) {
  2840. Sql_ShowDebug(mmysql_handle);
  2841. return false;
  2842. }
  2843. for (i = 0; i < MAX_ROULETTE_LEVEL; i++)
  2844. rd.items[i] = 0;
  2845. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2846. int k, limit = MAX_ROULETTE_COLUMNS - i;
  2847. for (k = 0; k < limit && SQL_SUCCESS == Sql_NextRow(mmysql_handle); k++) {
  2848. char* data;
  2849. t_itemid item_id;
  2850. unsigned short amount;
  2851. int level, flag;
  2852. Sql_GetData(mmysql_handle, 1, &data, NULL); level = atoi(data);
  2853. Sql_GetData(mmysql_handle, 2, &data, NULL); item_id = strtoul(data, nullptr, 10);
  2854. Sql_GetData(mmysql_handle, 3, &data, NULL); amount = atoi(data);
  2855. Sql_GetData(mmysql_handle, 4, &data, NULL); flag = atoi(data);
  2856. if (!item_db.exists(item_id)) {
  2857. ShowWarning("itemdb_parse_roulette_db: Unknown item ID '%u' in level '%d'\n", item_id, level);
  2858. continue;
  2859. }
  2860. if (amount < 1 || amount > MAX_AMOUNT){
  2861. ShowWarning("itemdb_parse_roulette_db: Unsupported amount '%hu' for item ID '%u' in level '%d'\n", amount, item_id, level);
  2862. continue;
  2863. }
  2864. if (flag < 0 || flag > 1) {
  2865. ShowWarning("itemdb_parse_roulette_db: Unsupported flag '%d' for item ID '%u' in level '%d'\n", flag, item_id, level);
  2866. continue;
  2867. }
  2868. j = rd.items[i];
  2869. RECREATE(rd.nameid[i], t_itemid, ++rd.items[i]);
  2870. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  2871. RECREATE(rd.flag[i], int, rd.items[i]);
  2872. rd.nameid[i][j] = item_id;
  2873. rd.qty[i][j] = amount;
  2874. rd.flag[i][j] = flag;
  2875. ++count;
  2876. }
  2877. }
  2878. // free the query result
  2879. Sql_FreeResult(mmysql_handle);
  2880. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2881. int limit = MAX_ROULETTE_COLUMNS - i;
  2882. if (rd.items[i] == limit)
  2883. continue;
  2884. if (rd.items[i] > limit) {
  2885. ShowWarning("itemdb_parse_roulette_db: level %d has %d items, only %d supported, capping...\n", i + 1, rd.items[i], limit);
  2886. rd.items[i] = limit;
  2887. continue;
  2888. }
  2889. /** this scenario = rd.items[i] < limit **/
  2890. ShowWarning("itemdb_parse_roulette_db: Level %d has %d items, %d are required. Filling with Apples...\n", i + 1, rd.items[i], limit);
  2891. rd.items[i] = limit;
  2892. RECREATE(rd.nameid[i], t_itemid, rd.items[i]);
  2893. RECREATE(rd.qty[i], unsigned short, rd.items[i]);
  2894. RECREATE(rd.flag[i], int, rd.items[i]);
  2895. for (j = 0; j < MAX_ROULETTE_COLUMNS - i; j++) {
  2896. if (rd.qty[i][j])
  2897. continue;
  2898. rd.nameid[i][j] = ITEMID_APPLE;
  2899. rd.qty[i][j] = 1;
  2900. rd.flag[i][j] = 0;
  2901. }
  2902. }
  2903. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, roulette_table);
  2904. return true;
  2905. }
  2906. /**
  2907. * Free Roulette items
  2908. */
  2909. static void itemdb_roulette_free(void) {
  2910. int i;
  2911. for (i = 0; i < MAX_ROULETTE_LEVEL; i++) {
  2912. if (rd.nameid[i])
  2913. aFree(rd.nameid[i]);
  2914. if (rd.qty[i])
  2915. aFree(rd.qty[i]);
  2916. if (rd.flag[i])
  2917. aFree(rd.flag[i]);
  2918. rd.nameid[i] = NULL;
  2919. rd.qty[i] = NULL;
  2920. rd.flag[i] = NULL;
  2921. rd.items[i] = 0;
  2922. }
  2923. }
  2924. /**
  2925. * Convert SQL data to YAML Node
  2926. * @param str: Array of parsed SQL data
  2927. * @return True on success or false otherwise
  2928. */
  2929. static bool itemdb_read_sqldb_sub(std::vector<std::string> str) {
  2930. ryml::Tree tree;
  2931. ryml::NodeRef rootNode = tree.rootref();
  2932. rootNode |= ryml::MAP;
  2933. int32 index = -1;
  2934. rootNode["Id"] << str[++index];
  2935. if (!str[++index].empty())
  2936. rootNode["AegisName"] << str[index];
  2937. if (!str[++index].empty())
  2938. rootNode["Name"] << str[index];
  2939. if (!str[++index].empty())
  2940. rootNode["Type"] << str[index];
  2941. if (!str[++index].empty())
  2942. rootNode["SubType"] << str[index];
  2943. if (!str[++index].empty())
  2944. rootNode["Buy"] << str[index];
  2945. if (!str[++index].empty())
  2946. rootNode["Sell"] << str[index];
  2947. if (!str[++index].empty())
  2948. rootNode["Weight"] << str[index];
  2949. if (!str[++index].empty())
  2950. rootNode["Attack"] << str[index];
  2951. if (!str[++index].empty())
  2952. rootNode["Defense"] << str[index];
  2953. if (!str[++index].empty())
  2954. rootNode["Range"] << str[index];
  2955. if (!str[++index].empty())
  2956. rootNode["Slots"] << str[index];
  2957. ryml::NodeRef jobs = rootNode["Jobs"];
  2958. jobs |= ryml::MAP;
  2959. if (!str[++index].empty())
  2960. jobs["All"] << (std::stoi(str[index]) ? "true" : "false");
  2961. if (!str[++index].empty())
  2962. jobs["Acolyte"] << (std::stoi(str[index]) ? "true" : "false");
  2963. if (!str[++index].empty())
  2964. jobs["Alchemist"] << (std::stoi(str[index]) ? "true" : "false");
  2965. if (!str[++index].empty())
  2966. jobs["Archer"] << (std::stoi(str[index]) ? "true" : "false");
  2967. if (!str[++index].empty())
  2968. jobs["Assassin"] << (std::stoi(str[index]) ? "true" : "false");
  2969. if (!str[++index].empty())
  2970. jobs["BardDancer"] << (std::stoi(str[index]) ? "true" : "false");
  2971. if (!str[++index].empty())
  2972. jobs["Blacksmith"] << (std::stoi(str[index]) ? "true" : "false");
  2973. if (!str[++index].empty())
  2974. jobs["Crusader"] << (std::stoi(str[index]) ? "true" : "false");
  2975. if (!str[++index].empty())
  2976. jobs["Gunslinger"] << (std::stoi(str[index]) ? "true" : "false");
  2977. if (!str[++index].empty())
  2978. jobs["Hunter"] << (std::stoi(str[index]) ? "true" : "false");
  2979. if (!str[++index].empty())
  2980. jobs["Knight"] << (std::stoi(str[index]) ? "true" : "false");
  2981. if (!str[++index].empty())
  2982. jobs["Mage"] << (std::stoi(str[index]) ? "true" : "false");
  2983. if (!str[++index].empty())
  2984. jobs["Merchant"] << (std::stoi(str[index]) ? "true" : "false");
  2985. if (!str[++index].empty())
  2986. jobs["Monk"] << (std::stoi(str[index]) ? "true" : "false");
  2987. if (!str[++index].empty())
  2988. jobs["Ninja"] << (std::stoi(str[index]) ? "true" : "false");
  2989. if (!str[++index].empty())
  2990. jobs["Novice"] << (std::stoi(str[index]) ? "true" : "false");
  2991. if (!str[++index].empty())
  2992. jobs["Priest"] << (std::stoi(str[index]) ? "true" : "false");
  2993. if (!str[++index].empty())
  2994. jobs["Rogue"] << (std::stoi(str[index]) ? "true" : "false");
  2995. if (!str[++index].empty())
  2996. jobs["Sage"] << (std::stoi(str[index]) ? "true" : "false");
  2997. if (!str[++index].empty())
  2998. jobs["SoulLinker"] << (std::stoi(str[index]) ? "true" : "false");
  2999. if (!str[++index].empty())
  3000. jobs["StarGladiator"] << (std::stoi(str[index]) ? "true" : "false");
  3001. if (!str[++index].empty())
  3002. jobs["SuperNovice"] << (std::stoi(str[index]) ? "true" : "false");
  3003. if (!str[++index].empty())
  3004. jobs["Swordman"] << (std::stoi(str[index]) ? "true" : "false");
  3005. if (!str[++index].empty())
  3006. jobs["Taekwon"] << (std::stoi(str[index]) ? "true" : "false");
  3007. if (!str[++index].empty())
  3008. jobs["Thief"] << (std::stoi(str[index]) ? "true" : "false");
  3009. if (!str[++index].empty())
  3010. jobs["Wizard"] << (std::stoi(str[index]) ? "true" : "false");
  3011. ryml::NodeRef classes = rootNode["Classes"];
  3012. classes |= ryml::MAP;
  3013. if (!str[++index].empty())
  3014. classes["All"] << (std::stoi(str[index]) ? "true" : "false");
  3015. if (!str[++index].empty())
  3016. classes["Normal"] << (std::stoi(str[index]) ? "true" : "false");
  3017. if (!str[++index].empty())
  3018. classes["Upper"] << (std::stoi(str[index]) ? "true" : "false");
  3019. if (!str[++index].empty())
  3020. classes["Baby"] << (std::stoi(str[index]) ? "true" : "false");
  3021. if (!str[++index].empty())
  3022. rootNode["Gender"] << str[index];
  3023. ryml::NodeRef locations = rootNode["Locations"];
  3024. locations |= ryml::MAP;
  3025. if (!str[++index].empty())
  3026. locations["Head_Top"] << (std::stoi(str[index]) ? "true" : "false");
  3027. if (!str[++index].empty())
  3028. locations["Head_Mid"] << (std::stoi(str[index]) ? "true" : "false");
  3029. if (!str[++index].empty())
  3030. locations["Head_Low"] << (std::stoi(str[index]) ? "true" : "false");
  3031. if (!str[++index].empty())
  3032. locations["Armor"] << (std::stoi(str[index]) ? "true" : "false");
  3033. if (!str[++index].empty())
  3034. locations["Right_Hand"] << (std::stoi(str[index]) ? "true" : "false");
  3035. if (!str[++index].empty())
  3036. locations["Left_Hand"] << (std::stoi(str[index]) ? "true" : "false");
  3037. if (!str[++index].empty())
  3038. locations["Garment"] << (std::stoi(str[index]) ? "true" : "false");
  3039. if (!str[++index].empty())
  3040. locations["Shoes"] << (std::stoi(str[index]) ? "true" : "false");
  3041. if (!str[++index].empty())
  3042. locations["Right_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3043. if (!str[++index].empty())
  3044. locations["Left_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3045. if (!str[++index].empty())
  3046. locations["Costume_Head_Top"] << (std::stoi(str[index]) ? "true" : "false");
  3047. if (!str[++index].empty())
  3048. locations["Costume_Head_Mid"] << (std::stoi(str[index]) ? "true" : "false");
  3049. if (!str[++index].empty())
  3050. locations["Costume_Head_Low"] << (std::stoi(str[index]) ? "true" : "false");
  3051. if (!str[++index].empty())
  3052. locations["Costume_Garment"] << (std::stoi(str[index]) ? "true" : "false");
  3053. if (!str[++index].empty())
  3054. locations["Ammo"] << (std::stoi(str[index]) ? "true" : "false");
  3055. if (!str[++index].empty())
  3056. locations["Shadow_Armor"] << (std::stoi(str[index]) ? "true" : "false");
  3057. if (!str[++index].empty())
  3058. locations["Shadow_Weapon"] << (std::stoi(str[index]) ? "true" : "false");
  3059. if (!str[++index].empty())
  3060. locations["Shadow_Shield"] << (std::stoi(str[index]) ? "true" : "false");
  3061. if (!str[++index].empty())
  3062. locations["Shadow_Shoes"] << (std::stoi(str[index]) ? "true" : "false");
  3063. if (!str[++index].empty())
  3064. locations["Shadow_Right_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3065. if (!str[++index].empty())
  3066. locations["Shadow_Left_Accessory"] << (std::stoi(str[index]) ? "true" : "false");
  3067. if (!str[++index].empty())
  3068. rootNode["WeaponLevel"] << str[index];
  3069. if (!str[++index].empty())
  3070. rootNode["ArmorLevel"] << str[index];
  3071. if (!str[++index].empty())
  3072. rootNode["EquipLevelMin"] << str[index];
  3073. if (!str[++index].empty())
  3074. rootNode["EquipLevelMax"] << str[index];
  3075. if (!str[++index].empty())
  3076. rootNode["Refineable"] << (std::stoi(str[index]) ? "true" : "false");
  3077. if (!str[++index].empty())
  3078. rootNode["View"] << str[index];
  3079. if (!str[++index].empty())
  3080. rootNode["AliasName"] << str[index];
  3081. ryml::NodeRef flags = rootNode["Flags"];
  3082. flags |= ryml::MAP;
  3083. if (!str[++index].empty())
  3084. flags["BuyingStore"] << (std::stoi(str[index]) ? "true" : "false");
  3085. if (!str[++index].empty())
  3086. flags["DeadBranch"] << (std::stoi(str[index]) ? "true" : "false");
  3087. if (!str[++index].empty())
  3088. flags["Container"] << (std::stoi(str[index]) ? "true" : "false");
  3089. if (!str[++index].empty())
  3090. flags["UniqueId"] << (std::stoi(str[index]) ? "true" : "false");
  3091. if (!str[++index].empty())
  3092. flags["BindOnEquip"] << (std::stoi(str[index]) ? "true" : "false");
  3093. if (!str[++index].empty())
  3094. flags["DropAnnounce"] << (std::stoi(str[index]) ? "true" : "false");
  3095. if (!str[++index].empty())
  3096. flags["NoConsume"] << (std::stoi(str[index]) ? "true" : "false");
  3097. if (!str[++index].empty())
  3098. flags["DropEffect"] << str[index];
  3099. rootNode["Flags"] = flags;
  3100. ryml::NodeRef delay = rootNode["Delay"];
  3101. delay |= ryml::MAP;
  3102. if (!str[++index].empty())
  3103. delay["Duration"] << str[index];
  3104. if (!str[++index].empty())
  3105. delay["Status"] << str[index];
  3106. ryml::NodeRef stack = rootNode["Stack"];
  3107. stack |= ryml::MAP;
  3108. if (!str[++index].empty())
  3109. stack["Amount"] << str[index];
  3110. if (!str[++index].empty())
  3111. stack["Inventory"] << (std::stoi(str[index]) ? "true" : "false");
  3112. if (!str[++index].empty())
  3113. stack["Cart"] << (std::stoi(str[index]) ? "true" : "false");
  3114. if (!str[++index].empty())
  3115. stack["Storage"] << (std::stoi(str[index]) ? "true" : "false");
  3116. if (!str[++index].empty())
  3117. stack["GuildStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3118. ryml::NodeRef nouse = rootNode["NoUse"];
  3119. nouse |= ryml::MAP;
  3120. if (!str[++index].empty())
  3121. nouse["Override"] << str[index];
  3122. if (!str[++index].empty())
  3123. nouse["Sitting"] << (std::stoi(str[index]) ? "true" : "false");
  3124. ryml::NodeRef trade = rootNode["Trade"];
  3125. trade |= ryml::MAP;
  3126. if (!str[++index].empty())
  3127. trade["Override"] << str[index];
  3128. if (!str[++index].empty())
  3129. trade["NoDrop"] << (std::stoi(str[index]) ? "true" : "false");
  3130. if (!str[++index].empty())
  3131. trade["NoTrade"] << (std::stoi(str[index]) ? "true" : "false");
  3132. if (!str[++index].empty())
  3133. trade["TradePartner"] << (std::stoi(str[index]) ? "true" : "false");
  3134. if (!str[++index].empty())
  3135. trade["NoSell"] << (std::stoi(str[index]) ? "true" : "false");
  3136. if (!str[++index].empty())
  3137. trade["NoCart"] << (std::stoi(str[index]) ? "true" : "false");
  3138. if (!str[++index].empty())
  3139. trade["NoStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3140. if (!str[++index].empty())
  3141. trade["NoGuildStorage"] << (std::stoi(str[index]) ? "true" : "false");
  3142. if (!str[++index].empty())
  3143. trade["NoMail"] << (std::stoi(str[index]) ? "true" : "false");
  3144. if (!str[++index].empty())
  3145. trade["NoAuction"] << (std::stoi(str[index]) ? "true" : "false");
  3146. if (!str[++index].empty())
  3147. rootNode["Script"] << str[index];
  3148. if (!str[++index].empty())
  3149. rootNode["EquipScript"] << str[index];
  3150. if (!str[++index].empty())
  3151. rootNode["UnEquipScript"] << str[index];
  3152. #ifdef RENEWAL
  3153. if (!str[++index].empty())
  3154. rootNode["MagicAttack"] << str[index];
  3155. if (!str[++index].empty())
  3156. classes["Third"] << (std::stoi(str[index]) ? "true" : "false");
  3157. if (!str[++index].empty())
  3158. classes["Third_Upper"] << (std::stoi(str[index]) ? "true" : "false");
  3159. if (!str[++index].empty())
  3160. classes["Third_Baby"] << (std::stoi(str[index]) ? "true" : "false");
  3161. if (!str[++index].empty())
  3162. classes["Fourth"] << (std::stoi(str[index]) ? "true" : "false");
  3163. if (!str[++index].empty())
  3164. jobs["KagerouOboro"] << (std::stoi(str[index]) ? "true" : "false");
  3165. if (!str[++index].empty())
  3166. jobs["Rebellion"] << (std::stoi(str[index]) ? "true" : "false");
  3167. if (!str[++index].empty())
  3168. jobs["Summoner"] << (std::stoi(str[index]) ? "true" : "false");
  3169. if (!str[++index].empty())
  3170. jobs["Spirit_Handler"] << (std::stoi(str[index]) ? "true" : "false");
  3171. if (!str[++index].empty())
  3172. rootNode["Gradable"] << (std::stoi(str[index]) ? "true" : "false");
  3173. #endif
  3174. if( !jobs.has_children() ){
  3175. rootNode.remove_child( jobs );
  3176. }
  3177. if( !classes.has_children() ){
  3178. rootNode.remove_child( classes );
  3179. }
  3180. if( !locations.has_children() ){
  3181. rootNode.remove_child( locations );
  3182. }
  3183. if( !flags.has_children() ){
  3184. rootNode.remove_child( flags );
  3185. }
  3186. if( !delay.has_children() ){
  3187. rootNode.remove_child( delay );
  3188. }
  3189. if( !stack.has_children() ){
  3190. rootNode.remove_child( stack );
  3191. }
  3192. if( !nouse.has_children() ){
  3193. rootNode.remove_child( nouse );
  3194. }
  3195. if( !trade.has_children() ){
  3196. rootNode.remove_child( trade );
  3197. }
  3198. return item_db.parseBodyNode(rootNode) > 0;
  3199. }
  3200. /**
  3201. * Read SQL item_db table
  3202. */
  3203. static int itemdb_read_sqldb(void) {
  3204. const char* item_db_name[] = {
  3205. item_table,
  3206. item2_table
  3207. };
  3208. for( uint8 fi = 0; fi < ARRAYLENGTH(item_db_name); ++fi ) {
  3209. // retrieve all rows from the item database
  3210. if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT `id`,`name_aegis`,`name_english`,`type`,`subtype`,`price_buy`,`price_sell`,`weight`,`attack`,`defense`,`range`,`slots`,"
  3211. "`job_all`,`job_acolyte`,`job_alchemist`,`job_archer`,`job_assassin`,`job_barddancer`,`job_blacksmith`,`job_crusader`,`job_gunslinger`,`job_hunter`,`job_knight`,`job_mage`,`job_merchant`,"
  3212. "`job_monk`,`job_ninja`,`job_novice`,`job_priest`,`job_rogue`,`job_sage`,`job_soullinker`,`job_stargladiator`,`job_supernovice`,`job_swordman`,`job_taekwon`,`job_thief`,`job_wizard`,"
  3213. "`class_all`,`class_normal`,`class_upper`,`class_baby`,`gender`,"
  3214. "`location_head_top`,`location_head_mid`,`location_head_low`,`location_armor`,`location_right_hand`,`location_left_hand`,`location_garment`,`location_shoes`,`location_right_accessory`,`location_left_accessory`,"
  3215. "`location_costume_head_top`,`location_costume_head_mid`,`location_costume_head_low`,`location_costume_garment`,`location_ammo`,`location_shadow_armor`,`location_shadow_weapon`,`location_shadow_shield`,`location_shadow_shoes`,`location_shadow_right_accessory`,`location_shadow_left_accessory`,"
  3216. "`weapon_level`,`armor_level`,`equip_level_min`,`equip_level_max`,`refineable`,`view`,`alias_name`,"
  3217. "`flag_buyingstore`,`flag_deadbranch`,`flag_container`,`flag_uniqueid`,`flag_bindonequip`,`flag_dropannounce`,`flag_noconsume`,`flag_dropeffect`,"
  3218. "`delay_duration`,`delay_status`,`stack_amount`,`stack_inventory`,`stack_cart`,`stack_storage`,`stack_guildstorage`,`nouse_override`,`nouse_sitting`,"
  3219. "`trade_override`,`trade_nodrop`,`trade_notrade`,`trade_tradepartner`,`trade_nosell`,`trade_nocart`,`trade_nostorage`,`trade_noguildstorage`,`trade_nomail`,`trade_noauction`,`script`,`equip_script`,`unequip_script`"
  3220. #ifdef RENEWAL
  3221. ",`magic_attack`,`class_third`,`class_third_upper`,`class_third_baby`,`class_fourth`,`job_kagerouoboro`,`job_rebellion`,`job_summoner`,`job_spirit_handler`,`gradable`"
  3222. #endif
  3223. " FROM `%s`", item_db_name[fi]) ) {
  3224. Sql_ShowDebug(mmysql_handle);
  3225. continue;
  3226. }
  3227. uint32 total_columns = Sql_NumColumns(mmysql_handle);
  3228. uint64 total_rows = Sql_NumRows(mmysql_handle), rows = 0, count = 0;
  3229. // process rows one by one
  3230. while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) ) {
  3231. ShowStatus( "Loading [%" PRIu64 "/%" PRIu64 "] rows from '" CL_WHITE "%s" CL_RESET "'" CL_CLL "\r", ++rows, total_rows, item_db_name[fi] );
  3232. std::vector<std::string> data = {};
  3233. for( uint32 i = 0; i < total_columns; ++i ) {
  3234. char *str;
  3235. Sql_GetData(mmysql_handle, i, &str, nullptr);
  3236. if (str == nullptr)
  3237. data.push_back("");
  3238. else
  3239. data.push_back(str);
  3240. }
  3241. if (!itemdb_read_sqldb_sub(data))
  3242. continue;
  3243. ++count;
  3244. }
  3245. // free the query result
  3246. Sql_FreeResult(mmysql_handle);
  3247. ShowStatus("Done reading '" CL_WHITE "%" PRIu64 CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", count, item_db_name[fi]);
  3248. }
  3249. item_db.loadingFinished();
  3250. return 0;
  3251. }
  3252. /** Check if the item is restricted by item_noequip.txt
  3253. * @param id Item that will be checked
  3254. * @param m Map ID
  3255. * @return true: can't be used; false: can be used
  3256. */
  3257. bool itemdb_isNoEquip(struct item_data *id, uint16 m) {
  3258. if (!id->flag.no_equip)
  3259. return false;
  3260. struct map_data *mapdata = map_getmapdata(m);
  3261. if ((id->flag.no_equip&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  3262. (id->flag.no_equip&2 && mapdata->flag[MF_PVP]) || // PVP
  3263. (id->flag.no_equip&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  3264. (id->flag.no_equip&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  3265. (id->flag.no_equip&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  3266. (id->flag.no_equip&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  3267. )
  3268. return true;
  3269. return false;
  3270. }
  3271. const std::string RandomOptionDatabase::getDefaultLocation() {
  3272. return std::string(db_path) + "/item_randomopt_db.yml";
  3273. }
  3274. /**
  3275. * Reads and parses an entry from the item_randomopt_db.
  3276. * @param node: YAML node containing the entry.
  3277. * @return count of successfully parsed rows
  3278. */
  3279. uint64 RandomOptionDatabase::parseBodyNode(const ryml::NodeRef& node) {
  3280. uint16 id;
  3281. if (!this->asUInt16(node, "Id", id))
  3282. return 0;
  3283. std::shared_ptr<s_random_opt_data> randopt = this->find(id);
  3284. bool exists = randopt != nullptr;
  3285. if (!exists) {
  3286. if (!this->nodesExist(node, { "Option", "Script" }))
  3287. return 0;
  3288. randopt = std::make_shared<s_random_opt_data>();
  3289. randopt->id = id;
  3290. }
  3291. if (this->nodeExists(node, "Option")) {
  3292. std::string name;
  3293. if (!this->asString(node, "Option", name))
  3294. return 0;
  3295. if (randopt->name.compare(name) != 0 && random_option_db.option_exists(name)) {
  3296. this->invalidWarning(node["Option"], "Found duplicate random option name for %s, skipping.\n", name.c_str());
  3297. return 0;
  3298. }
  3299. randopt->name = name;
  3300. }
  3301. if (this->nodeExists(node, "Script")) {
  3302. std::string script;
  3303. if (!this->asString(node, "Script", script))
  3304. return 0;
  3305. if (randopt->script) {
  3306. aFree(randopt->script);
  3307. randopt->script = nullptr;
  3308. }
  3309. randopt->script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Script"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  3310. }
  3311. if (!exists)
  3312. this->put(id, randopt);
  3313. return 1;
  3314. }
  3315. void RandomOptionDatabase::loadingFinished(){
  3316. const char* prefix = "RDMOPT_";
  3317. for( const auto& pair : *this ){
  3318. std::string name = prefix + pair.second->name;
  3319. int64 constant;
  3320. // Check if it has already been set
  3321. if( script_get_constant( name.c_str(), &constant ) ){
  3322. // It is already the same
  3323. if( constant == pair.first ){
  3324. continue;
  3325. }else{
  3326. // Export it to the script engine -> will issue a warning
  3327. }
  3328. }
  3329. script_set_constant( name.c_str(), pair.first, false, false );
  3330. }
  3331. TypesafeYamlDatabase::loadingFinished();
  3332. }
  3333. RandomOptionDatabase random_option_db;
  3334. /**
  3335. * Check if the given random option name exists.
  3336. * @param name: Random option name
  3337. * @return True on success or false on failure
  3338. */
  3339. bool RandomOptionDatabase::option_exists(std::string name) {
  3340. for (const auto &opt : random_option_db) {
  3341. if (opt.second->name.compare(name) == 0)
  3342. return true;
  3343. }
  3344. return false;
  3345. }
  3346. /**
  3347. * Return the constant value of the given random option.
  3348. * @param name: Random option name
  3349. * @param id: Random option ID
  3350. * @return True on success or false on failure
  3351. */
  3352. bool RandomOptionDatabase::option_get_id(std::string name, uint16 &id) {
  3353. for (const auto &opt : random_option_db) {
  3354. if (opt.second->name.compare(name) == 0) {
  3355. id = opt.first;
  3356. return true;
  3357. }
  3358. }
  3359. return false;
  3360. }
  3361. const std::string RandomOptionGroupDatabase::getDefaultLocation() {
  3362. return std::string(db_path) + "/item_randomopt_group.yml";
  3363. }
  3364. bool RandomOptionGroupDatabase::add_option(const ryml::NodeRef& node, std::shared_ptr<s_random_opt_group_entry> &entry) {
  3365. uint16 option_id;
  3366. if (this->nodeExists(node, "Option")) {
  3367. std::string opt_name;
  3368. if (!this->asString(node, "Option", opt_name))
  3369. return false;
  3370. if (!random_option_db.option_get_id(opt_name, option_id)) {
  3371. this->invalidWarning(node["Option"], "Invalid Random Option name %s given.\n", opt_name.c_str());
  3372. return false;
  3373. }
  3374. } else {
  3375. this->invalidWarning(node, "Missing Option node.\n");
  3376. return false;
  3377. }
  3378. entry = std::make_shared<s_random_opt_group_entry>();
  3379. entry->id = option_id;
  3380. if (this->nodeExists(node, "MinValue")) {
  3381. int16 value;
  3382. if (!this->asInt16(node, "MinValue", value))
  3383. return false;
  3384. entry->min_value = value;
  3385. } else {
  3386. entry->min_value = 0;
  3387. }
  3388. if (this->nodeExists(node, "MaxValue")) {
  3389. int16 value;
  3390. if (!this->asInt16(node, "MaxValue", value))
  3391. return false;
  3392. entry->max_value = value;
  3393. } else {
  3394. entry->max_value = 0;
  3395. }
  3396. if (entry->min_value > entry->max_value) {
  3397. this->invalidWarning(node["MaxValue"], "MinValue %d is greater than MaxValue %d, setting MaxValue to MinValue + 1.\n", entry->min_value, entry->max_value);
  3398. entry->max_value = entry->min_value + 1;
  3399. }
  3400. if (this->nodeExists(node, "Param")) {
  3401. int16 value;
  3402. if (!this->asInt16(node, "Param", value))
  3403. return false;
  3404. entry->param = static_cast<int8>(value);
  3405. } else {
  3406. entry->param = 0;
  3407. }
  3408. if (this->nodeExists(node, "Chance")) {
  3409. uint16 chance;
  3410. if (!this->asUInt16Rate(node, "Chance", chance))
  3411. return false;
  3412. entry->chance = chance;
  3413. } else {
  3414. entry->chance = 0;
  3415. }
  3416. return true;
  3417. }
  3418. void s_random_opt_group::apply( struct item& item ){
  3419. auto apply_sub = []( s_item_randomoption& item_option, const std::shared_ptr<s_random_opt_group_entry>& option ){
  3420. item_option.id = option->id;
  3421. item_option.value = rnd_value( option->min_value, option->max_value );
  3422. item_option.param = option->param;
  3423. };
  3424. // (Re)initialize all the options
  3425. for( size_t i = 0; i < MAX_ITEM_RDM_OPT; i++ ){
  3426. item.option[i].id = 0;
  3427. item.option[i].value = 0;
  3428. item.option[i].param = 0;
  3429. };
  3430. // Apply Must options
  3431. for( size_t i = 0; i < this->slots.size(); i++ ){
  3432. // Try to apply an entry
  3433. for( size_t j = 0, max = this->slots[static_cast<uint16>(i)].size() * 3; j < max; j++ ){
  3434. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->slots[static_cast<uint16>(i)] );
  3435. if( rnd() % 10000 < option->chance ){
  3436. apply_sub( item.option[i], option );
  3437. break;
  3438. }
  3439. }
  3440. // If no entry was applied, assign one
  3441. if( item.option[i].id == 0 ){
  3442. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->slots[static_cast<uint16>(i)] );
  3443. // Apply an entry without checking the chance
  3444. apply_sub( item.option[i], option );
  3445. }
  3446. }
  3447. // Apply Random options (if available)
  3448. if( this->max_random > 0 ){
  3449. for( size_t i = 0; i < min( this->max_random, MAX_ITEM_RDM_OPT ); i++ ){
  3450. // If item already has an option in this slot, skip it
  3451. if( item.option[i].id > 0 ){
  3452. continue;
  3453. }
  3454. std::shared_ptr<s_random_opt_group_entry> option = util::vector_random( this->random_options );
  3455. if( rnd() % 10000 < option->chance ){
  3456. apply_sub( item.option[i], option );
  3457. }
  3458. }
  3459. }
  3460. }
  3461. /**
  3462. * Reads and parses an entry from the item_randomopt_group.
  3463. * @param node: YAML node containing the entry.
  3464. * @return count of successfully parsed rows
  3465. */
  3466. uint64 RandomOptionGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  3467. uint16 id;
  3468. if (!this->asUInt16(node, "Id", id))
  3469. return 0;
  3470. std::shared_ptr<s_random_opt_group> randopt = this->find(id);
  3471. bool exists = randopt != nullptr;
  3472. if (!exists) {
  3473. if (!this->nodesExist(node, { "Group" }))
  3474. return 0;
  3475. randopt = std::make_shared<s_random_opt_group>();
  3476. randopt->id = id;
  3477. }
  3478. if (this->nodeExists(node, "Group")) {
  3479. std::string name;
  3480. if (!this->asString(node, "Group", name))
  3481. return 0;
  3482. if (randopt->name.compare(name) != 0 && random_option_group.option_exists(name)) {
  3483. this->invalidWarning(node["Group"], "Found duplicate random option group name for %s, skipping.\n", name.c_str());
  3484. return 0;
  3485. }
  3486. randopt->name = name;
  3487. }
  3488. if (this->nodeExists(node, "Slots")) {
  3489. const auto& slotsNode = node["Slots"];
  3490. for (const ryml::NodeRef& slotNode : slotsNode) {
  3491. if (randopt->slots.size() >= MAX_ITEM_RDM_OPT) {
  3492. this->invalidWarning(slotNode, "Reached maximum of %d Random Option group options. Skipping the remaining slots...\n", MAX_ITEM_RDM_OPT);
  3493. break;
  3494. }
  3495. uint16 slot;
  3496. if (!this->asUInt16(slotNode, "Slot", slot))
  3497. return 0;
  3498. if (slot < 1 || slot > MAX_ITEM_RDM_OPT) {
  3499. this->invalidWarning(slotNode["Slot"], "Invalid Random Opton Slot number %hu given, must be between 1~%d, skipping.\n", slot, MAX_ITEM_RDM_OPT);
  3500. return 0;
  3501. }
  3502. if (!this->nodeExists(slotNode, "Options")) {
  3503. this->invalidWarning(slotNode, "Random option slot does not contain Options node, skipping.\n");
  3504. return 0;
  3505. }
  3506. std::vector<std::shared_ptr<s_random_opt_group_entry>> entries;
  3507. const auto& optionsNode = slotNode["Options"];
  3508. for (const auto& optionNode : optionsNode) {
  3509. std::shared_ptr<s_random_opt_group_entry> entry;
  3510. if (!this->add_option(optionNode, entry))
  3511. return 0;
  3512. entries.push_back(entry);
  3513. }
  3514. randopt->slots[slot - 1] = entries;
  3515. }
  3516. }
  3517. if (this->nodeExists(node, "MaxRandom")) {
  3518. uint16 max;
  3519. if (!this->asUInt16(node, "MaxRandom", max))
  3520. return 0;
  3521. if (max > MAX_ITEM_RDM_OPT) {
  3522. this->invalidWarning(node["MaxRandom"], "Exceeds the maximum of %d Random Option group options, capping to MAX_ITEM_RDM_OPT.\n", MAX_ITEM_RDM_OPT);
  3523. max = MAX_ITEM_RDM_OPT;
  3524. }
  3525. randopt->max_random = max;
  3526. } else {
  3527. if (!exists)
  3528. randopt->max_random = 0;
  3529. }
  3530. if (this->nodeExists(node, "Random")) {
  3531. randopt->random_options.clear();
  3532. const auto& randomNode = node["Random"];
  3533. for (const auto& randomNode : randomNode) {
  3534. std::shared_ptr<s_random_opt_group_entry> entry;
  3535. if (!this->add_option(randomNode, entry))
  3536. return 0;
  3537. randopt->random_options.push_back(entry);
  3538. }
  3539. }
  3540. if (!exists)
  3541. this->put(id, randopt);
  3542. return 1;
  3543. }
  3544. RandomOptionGroupDatabase random_option_group;
  3545. /**
  3546. * Check if the given random option group name exists.
  3547. * @param name: Random option name
  3548. * @return True on success or false on failure
  3549. */
  3550. bool RandomOptionGroupDatabase::option_exists(std::string name) {
  3551. for (const auto &opt : random_option_group) {
  3552. if (opt.second->name.compare(name) == 0)
  3553. return true;
  3554. }
  3555. return false;
  3556. }
  3557. /**
  3558. * Return the constant value of the given random option group.
  3559. * @param name: Random option group name
  3560. * @param id: Random option group ID
  3561. * @return True on success or false on failure
  3562. */
  3563. bool RandomOptionGroupDatabase::option_get_id(std::string name, uint16 &id) {
  3564. for (const auto &opt : random_option_group) {
  3565. if (opt.second->name.compare(name) == 0) {
  3566. id = opt.first;
  3567. return true;
  3568. }
  3569. }
  3570. return false;
  3571. }
  3572. /**
  3573. * Read all item-related databases
  3574. */
  3575. static void itemdb_read(void) {
  3576. int i;
  3577. const char* dbsubpath[] = {
  3578. "",
  3579. "/" DBIMPORT,
  3580. };
  3581. if (db_use_sqldbs)
  3582. itemdb_read_sqldb();
  3583. else
  3584. item_db.load();
  3585. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  3586. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  3587. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  3588. char* dbsubpath1 = (char*)aMalloc(n1+1);
  3589. char* dbsubpath2 = (char*)aMalloc(n2+1);
  3590. if(i==0) {
  3591. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  3592. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  3593. }
  3594. else {
  3595. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  3596. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  3597. }
  3598. sv_readdb(dbsubpath2, "item_noequip.txt", ',', 2, 2, -1, &itemdb_read_noequip, i > 0);
  3599. aFree(dbsubpath1);
  3600. aFree(dbsubpath2);
  3601. }
  3602. random_option_db.load();
  3603. random_option_group.load();
  3604. itemdb_group.load();
  3605. itemdb_combo.load();
  3606. laphine_synthesis_db.load();
  3607. laphine_upgrade_db.load();
  3608. item_reform_db.load();
  3609. item_enchant_db.load();
  3610. item_package_db.load();
  3611. if (battle_config.feature_roulette)
  3612. itemdb_parse_roulette_db();
  3613. }
  3614. /*==========================================
  3615. * Initialize / Finalize
  3616. *------------------------------------------*/
  3617. bool item_data::isStackable()
  3618. {
  3619. switch (this->type) {
  3620. case IT_WEAPON:
  3621. case IT_ARMOR:
  3622. case IT_PETEGG:
  3623. case IT_PETARMOR:
  3624. case IT_SHADOWGEAR:
  3625. return false;
  3626. }
  3627. return true;
  3628. }
  3629. int item_data::inventorySlotNeeded(int quantity)
  3630. {
  3631. return (this->flag.guid || !this->isStackable()) ? quantity : 1;
  3632. }
  3633. /**
  3634. * Reload Item DB
  3635. */
  3636. void itemdb_reload(void) {
  3637. struct s_mapiterator* iter;
  3638. struct map_session_data* sd;
  3639. do_final_itemdb();
  3640. // read new data
  3641. itemdb_read();
  3642. cashshop_reloaddb();
  3643. mob_reload_itemmob_data();
  3644. // readjust itemdb pointer cache for each player
  3645. iter = mapit_geteachpc();
  3646. for( sd = (struct map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (struct map_session_data*)mapit_next(iter) ) {
  3647. memset(sd->item_delay, 0, sizeof(sd->item_delay)); // reset item delays
  3648. sd->combos.clear(); // clear combo bonuses
  3649. pc_setinventorydata(sd);
  3650. pc_check_available_item(sd, ITMCHK_ALL); // Check for invalid(ated) items.
  3651. pc_load_combo(sd); // Check to see if new combos are available
  3652. status_calc_pc(sd, SCO_FORCE); //
  3653. }
  3654. mapit_free(iter);
  3655. }
  3656. /**
  3657. * Finalizing Item DB
  3658. */
  3659. void do_final_itemdb(void) {
  3660. item_db.clear();
  3661. itemdb_combo.clear();
  3662. itemdb_group.clear();
  3663. random_option_db.clear();
  3664. random_option_group.clear();
  3665. laphine_synthesis_db.clear();
  3666. laphine_upgrade_db.clear();
  3667. item_reform_db.clear();
  3668. item_enchant_db.clear();
  3669. item_package_db.clear();
  3670. if (battle_config.feature_roulette)
  3671. itemdb_roulette_free();
  3672. }
  3673. /**
  3674. * Initializing Item DB
  3675. */
  3676. void do_init_itemdb(void) {
  3677. itemdb_read();
  3678. }