itemdb.cpp 127 KB

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