intif.cpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "intif.hpp"
  4. #include <cstdlib>
  5. #include <common/malloc.hpp>
  6. #include <common/mmo.hpp>
  7. #include <common/nullpo.hpp>
  8. #include <common/showmsg.hpp>
  9. #include <common/socket.hpp>
  10. #include <common/strlib.hpp>
  11. #include <common/timer.hpp>
  12. #include "achievement.hpp"
  13. #include "battle.hpp"
  14. #include "chrif.hpp"
  15. #include "clan.hpp"
  16. #include "clif.hpp"
  17. #include "elemental.hpp"
  18. #include "guild.hpp"
  19. #include "homunculus.hpp"
  20. #include "log.hpp"
  21. #include "mail.hpp"
  22. #include "map.hpp"
  23. #include "mercenary.hpp"
  24. #include "party.hpp"
  25. #include "pc.hpp"
  26. #include "pc_groups.hpp"
  27. #include "pet.hpp"
  28. #include "quest.hpp"
  29. #include "status.hpp"
  30. #include "storage.hpp"
  31. /// Received packet Lengths from inter-server
  32. static const int packet_len_table[] = {
  33. -1,-1,27,-1, -1, 0,37,-1, 10+NAME_LENGTH,-1, 0, 0, 0, 0, 0, 0, //0x3800-0x380f
  34. 0, 0, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0, //0x3810
  35. 39,-1,15,15, 15+NAME_LENGTH,17+MAP_NAME_LENGTH_EXT, 7,-1, 0, 0, 0, 0, 0, 0, 0, 0, //0x3820
  36. 10,-1,15, 0, 79,19, 7,-1, 0,-1,-1,-1, 14,67,186,-1, //0x3830
  37. -1,10, 0,18, 0, 0, 0, 0, -1,75,-1,11, 11,-1, 38, 0, //0x3840
  38. -1,-1, 7, 7, 7,11, 8,-1, 0, 0, 0, 0, 0, 0, 0, 0, //0x3850 Auctions [Zephyrus] itembound[Akinari]
  39. -1, 7,-1, 7, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3860 Quests [Kevin] [Inkfish] / Achievements [Aleos]
  40. -1, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 3, 3, 0, //0x3870 Mercenaries [Zephyrus] / Elemental [pakpil]
  41. 12,-1, 7, 3, 0, 0, 0, 0, 0, 0,-1, 9, -1, 0, 0, 0, //0x3880 Pet System, Storages
  42. -1,-1, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3890 Homunculus [albator]
  43. -1,-1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x38A0 Clans
  44. };
  45. extern int char_fd; // inter server Fd used for char_fd
  46. #define inter_fd char_fd // alias
  47. //-----------------------------------------------------------------
  48. // Send to inter server
  49. /**
  50. * Verify the char-serv is up and running
  51. * @return 0=no, 1=ok
  52. */
  53. int CheckForCharServer(void)
  54. {
  55. return ((char_fd <= 0) || session[char_fd] == nullptr || session[char_fd]->wdata == nullptr);
  56. }
  57. /**
  58. * Get sd from pc_db (map_id2db) or auth_db (in case if parsing packet from inter-server when sd not added to pc_db yet)
  59. * @param account_id
  60. * @param char_id
  61. * @return sd Found sd or nullptr if not found
  62. */
  63. map_session_data *inter_search_sd(uint32 account_id, uint32 char_id)
  64. {
  65. map_session_data *sd = nullptr;
  66. struct auth_node *node = chrif_auth_check(account_id, char_id, ST_LOGIN);
  67. if (node)
  68. sd = node->sd;
  69. else
  70. sd = map_id2sd(account_id);
  71. return sd;
  72. }
  73. /**
  74. * Request the char-serv to create a pet (to register it actually)
  75. * @param account_id
  76. * @param char_id
  77. * @param pet_class
  78. * @param pet_lv
  79. * @param pet_egg_id
  80. * @param pet_equip
  81. * @param intimate
  82. * @param hungry
  83. * @param rename_flag
  84. * @param incubate
  85. * @param pet_name
  86. * @return
  87. */
  88. int intif_create_pet(uint32 account_id,uint32 char_id,short pet_class,short pet_lv, t_itemid pet_egg_id, t_itemid pet_equip,short intimate,short hungry,char rename_flag,char incubate,const char *pet_name)
  89. {
  90. if (CheckForCharServer())
  91. return 0;
  92. WFIFOHEAD(inter_fd, 28 + NAME_LENGTH);
  93. WFIFOW(inter_fd, 0) = 0x3080;
  94. WFIFOL(inter_fd, 2) = account_id;
  95. WFIFOL(inter_fd, 6) = char_id;
  96. WFIFOW(inter_fd, 10) = pet_class;
  97. WFIFOW(inter_fd, 12) = pet_lv;
  98. WFIFOL(inter_fd, 14) = pet_egg_id;
  99. WFIFOL(inter_fd, 18) = pet_equip;
  100. WFIFOW(inter_fd, 22) = intimate;
  101. WFIFOW(inter_fd, 24) = hungry;
  102. WFIFOB(inter_fd, 26) = rename_flag;
  103. WFIFOB(inter_fd, 27) = incubate;
  104. safestrncpy(WFIFOCP(inter_fd, 28), pet_name, NAME_LENGTH);
  105. WFIFOSET(inter_fd, 28 + NAME_LENGTH);
  106. return 1;
  107. }
  108. /**
  109. * Request char-serv to load a pet from persistence (SQL)
  110. * @param account_id
  111. * @param char_id
  112. * @param pet_id
  113. * @return
  114. */
  115. int intif_request_petdata(uint32 account_id,uint32 char_id,int pet_id)
  116. {
  117. if (CheckForCharServer())
  118. return 0;
  119. WFIFOHEAD(inter_fd, 14);
  120. WFIFOW(inter_fd,0) = 0x3081;
  121. WFIFOL(inter_fd,2) = account_id;
  122. WFIFOL(inter_fd,6) = char_id;
  123. WFIFOL(inter_fd,10) = pet_id;
  124. WFIFOSET(inter_fd,14);
  125. return 1;
  126. }
  127. /**
  128. * Request char-serv to save a pet in persistence (SQL)
  129. * @param account_id
  130. * @param p
  131. * @return
  132. */
  133. int intif_save_petdata(uint32 account_id,struct s_pet *p)
  134. {
  135. if (CheckForCharServer())
  136. return 0;
  137. WFIFOHEAD(inter_fd, sizeof(struct s_pet) + 8);
  138. WFIFOW(inter_fd,0) = 0x3082;
  139. WFIFOW(inter_fd,2) = sizeof(struct s_pet) + 8;
  140. WFIFOL(inter_fd,4) = account_id;
  141. memcpy(WFIFOP(inter_fd,8),p,sizeof(struct s_pet));
  142. WFIFOSET(inter_fd,WFIFOW(inter_fd,2));
  143. return 1;
  144. }
  145. /**
  146. * Request char-serv to delete the entry for this pet-char association
  147. * @param pet_id
  148. * @return
  149. */
  150. int intif_delete_petdata(int pet_id)
  151. {
  152. if (CheckForCharServer())
  153. return 0;
  154. WFIFOHEAD(inter_fd,6);
  155. WFIFOW(inter_fd,0) = 0x3083;
  156. WFIFOL(inter_fd,2) = pet_id;
  157. WFIFOSET(inter_fd,6);
  158. return 1;
  159. }
  160. /**
  161. * Ask char-serv to rename a CHAR (PC|PET|HOM)
  162. * @param sd
  163. * @param type
  164. * @param name
  165. * @return
  166. */
  167. int intif_rename(map_session_data *sd, int type, char *name)
  168. {
  169. if (CheckForCharServer())
  170. return 0;
  171. WFIFOHEAD(inter_fd,NAME_LENGTH+12);
  172. WFIFOW(inter_fd,0) = 0x3006;
  173. WFIFOL(inter_fd,2) = sd->status.account_id;
  174. WFIFOL(inter_fd,6) = sd->status.char_id;
  175. WFIFOB(inter_fd,10) = type; //Type: 0 - PC, 1 - PET, 2 - HOM
  176. safestrncpy(WFIFOCP(inter_fd,11),name, NAME_LENGTH);
  177. WFIFOSET(inter_fd,NAME_LENGTH+12);
  178. return 1;
  179. }
  180. /**
  181. * Request to broadcast a message via char-serv to reach all map-serv connected
  182. * @param mes : Message to send
  183. * @param len ; Size of the message
  184. * @param type : Color of msg
  185. * @return 0=error occured, 1=msg sent
  186. */
  187. int intif_broadcast( const char* mes, size_t len, int type ){
  188. nullpo_ret(mes);
  189. if (len < 2)
  190. return 0;
  191. int lp = (type|BC_COLOR_MASK) ? 4 : 0;
  192. // Send to the local players
  193. clif_broadcast(nullptr, mes, len, type, ALL_CLIENT);
  194. if (CheckForCharServer())
  195. return 0;
  196. if (other_mapserver_count < 1)
  197. return 0; //No need to send.
  198. WFIFOHEAD(inter_fd, 16 + lp + len);
  199. WFIFOW(inter_fd,0) = 0x3000;
  200. WFIFOW( inter_fd, 2 ) = static_cast<int16>( 16 + lp + len );
  201. WFIFOL(inter_fd,4) = 0xFF000000; // 0xFF000000 color signals standard broadcast
  202. WFIFOW(inter_fd,8) = 0; // fontType not used with standard broadcast
  203. WFIFOW(inter_fd,10) = 0; // fontSize not used with standard broadcast
  204. WFIFOW(inter_fd,12) = 0; // fontAlign not used with standard broadcast
  205. WFIFOW(inter_fd,14) = 0; // fontY not used with standard broadcast
  206. if (type|BC_BLUE)
  207. WFIFOL(inter_fd,16) = 0x65756c62; //If there's "blue" at the beginning of the message, game client will display it in blue instead of yellow.
  208. else if (type|BC_WOE)
  209. WFIFOL(inter_fd,16) = 0x73737373; //If there's "ssss", game client will recognize message as 'WoE broadcast'.
  210. safestrncpy(WFIFOCP(inter_fd,16 + lp), mes, len);
  211. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  212. return 1;
  213. }
  214. /**
  215. * Request to char-serv to broadcast a message to all map-serv
  216. * @param mes : Message to brodcast
  217. * @param len : Size of message
  218. * @param fontColor : color to display message
  219. * @param fontType :
  220. * @param fontSize :
  221. * @param fontAlign :
  222. * @param fontY :
  223. * @return 0=not send to char-serv, 1=send to char-serv
  224. */
  225. int intif_broadcast2( const char* mes, size_t len, unsigned long fontColor, short fontType, short fontSize, short fontAlign, short fontY ){
  226. nullpo_ret(mes);
  227. if (len < 2)
  228. return 0;
  229. // Send to the local players
  230. clif_broadcast2(nullptr, mes, len, fontColor, fontType, fontSize, fontAlign, fontY, ALL_CLIENT);
  231. if (CheckForCharServer())
  232. return 0;
  233. if (other_mapserver_count < 1)
  234. return 0; //No need to send.
  235. WFIFOHEAD(inter_fd, 16 + len);
  236. WFIFOW(inter_fd,0) = 0x3000;
  237. WFIFOW( inter_fd, 2 ) = static_cast<int16>( 16 + len );
  238. WFIFOL(inter_fd,4) = fontColor;
  239. WFIFOW(inter_fd,8) = fontType;
  240. WFIFOW(inter_fd,10) = fontSize;
  241. WFIFOW(inter_fd,12) = fontAlign;
  242. WFIFOW(inter_fd,14) = fontY;
  243. safestrncpy(WFIFOCP(inter_fd,16), mes, len);
  244. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  245. return 1;
  246. }
  247. /**
  248. * send a message using the main chat system
  249. * @param sd : Player source of message
  250. * @param message : the message to sent
  251. * @return
  252. */
  253. int intif_main_message(map_session_data* sd, const char* message)
  254. {
  255. char output[256];
  256. nullpo_ret(sd);
  257. // format the message for main broadcasting
  258. snprintf( output, sizeof(output), msg_txt(sd,386), sd->status.name, message );
  259. // send the message using the inter-server broadcast service
  260. intif_broadcast2( output, strlen(output) + 1, 0xFE000000, 0, 0, 0, 0 );
  261. // log the chat message
  262. log_chat( LOG_CHAT_MAINCHAT, 0, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, nullptr, message );
  263. return 1;
  264. }
  265. /**
  266. * Request char-serv to transmit a whisper message. (Private message from one player to another)
  267. * (player not found on this server)
  268. * @param sd : Player ending message
  269. * @param nick : Name of receiver
  270. * @param mes : Message to send
  271. * @param mes_len : Size of message
  272. * @return 0=Message not send, 1=Message send
  273. */
  274. int intif_wis_message(map_session_data *sd, char *nick, char *mes, size_t mes_len)
  275. {
  276. int headersize = 8 + 2 * NAME_LENGTH;
  277. nullpo_ret(sd);
  278. if (CheckForCharServer())
  279. return 0;
  280. if (other_mapserver_count < 1)
  281. { //Character not found.
  282. clif_wis_end( *sd, ACKWHISPER_TARGET_OFFLINE );
  283. return 0;
  284. }
  285. WFIFOHEAD(inter_fd,mes_len + headersize);
  286. WFIFOW(inter_fd,0) = 0x3001;
  287. WFIFOW(inter_fd,2) = static_cast<int16>( mes_len + headersize);
  288. WFIFOL(inter_fd,4) = pc_get_group_level(sd);
  289. safestrncpy(WFIFOCP(inter_fd,8), sd->status.name, NAME_LENGTH);
  290. safestrncpy(WFIFOCP(inter_fd,8+NAME_LENGTH), nick, NAME_LENGTH);
  291. safestrncpy(WFIFOCP(inter_fd,8+2*NAME_LENGTH), mes, mes_len);
  292. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  293. if (battle_config.etc_log)
  294. ShowInfo("intif_wis_message from %s to %s (message: '%s')\n", sd->status.name, nick, mes);
  295. return 1;
  296. }
  297. /**
  298. * Inform the char-serv of the result of the whisper
  299. * @param id : Character ID
  300. * @param flag : 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
  301. * @return 0=no char-serv connected, 1=msg sent
  302. */
  303. int intif_wis_reply(int id, int flag)
  304. {
  305. if (CheckForCharServer())
  306. return 0;
  307. WFIFOHEAD(inter_fd,7);
  308. WFIFOW(inter_fd,0) = 0x3002;
  309. WFIFOL(inter_fd,2) = id;
  310. WFIFOB(inter_fd,6) = flag; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
  311. WFIFOSET(inter_fd,7);
  312. if (battle_config.etc_log)
  313. ShowInfo("intif_wis_reply: id: %d, flag:%d\n", id, flag);
  314. return 1;
  315. }
  316. /**
  317. * Whisper message from player to all GM from map-server to inter-server (@request)
  318. * @param wisp_name
  319. * @param permission
  320. * @param mes
  321. * @return 0:no char-serv connected, 1:transfered
  322. */
  323. int intif_wis_message_to_gm(char *wisp_name, int permission, char *mes)
  324. {
  325. if (CheckForCharServer())
  326. return 0;
  327. // + null
  328. size_t mes_len = strlen( mes ) + 1;
  329. WFIFOHEAD(inter_fd, mes_len + 8 + NAME_LENGTH);
  330. WFIFOW(inter_fd,0) = 0x3003;
  331. WFIFOW( inter_fd, 2 ) = static_cast<int16>( mes_len + 32 );
  332. safestrncpy(WFIFOCP(inter_fd,4), wisp_name, NAME_LENGTH);
  333. WFIFOL(inter_fd,4+NAME_LENGTH) = permission;
  334. safestrncpy(WFIFOCP(inter_fd,8+NAME_LENGTH), mes, mes_len);
  335. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  336. if (battle_config.etc_log)
  337. ShowNotice("intif_wis_message_to_gm: from: '%s', required permission: %d, message: '%s'.\n", wisp_name, permission, mes);
  338. return 1;
  339. }
  340. /**
  341. * Request for saving registry values.
  342. * @param sd : Player to save registry
  343. * @return 1=msg sent, -1=error
  344. */
  345. int intif_saveregistry(map_session_data *sd)
  346. {
  347. DBIterator *iter;
  348. DBKey key;
  349. DBData *data;
  350. int plen = 0;
  351. size_t len;
  352. if (CheckForCharServer() || !sd->regs.vars)
  353. return -1;
  354. WFIFOHEAD(inter_fd, 60000 + 300);
  355. WFIFOW(inter_fd,0) = 0x3004;
  356. // 0x2 = length (set later)
  357. WFIFOL(inter_fd,4) = sd->status.account_id;
  358. WFIFOL(inter_fd,8) = sd->status.char_id;
  359. WFIFOW(inter_fd,12) = 0; // count
  360. plen = 14;
  361. iter = db_iterator(sd->regs.vars);
  362. for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) ) {
  363. const char *varname = nullptr;
  364. struct script_reg_state *src = nullptr;
  365. bool lValid = false;
  366. if( data->type != DB_DATA_PTR ) // it's a @number
  367. continue;
  368. varname = get_str(script_getvarid(key.i64));
  369. if( varname[0] == '@' ) // @string$ can get here, so we skip
  370. continue;
  371. src = (struct script_reg_state *)db_data2ptr(data);
  372. if( !src->update )
  373. continue;
  374. src->update = false;
  375. lValid = script_check_RegistryVariableLength(0,varname,&len);
  376. ++len;
  377. if (!lValid) { //this is sql colum size, must be retrive from config
  378. ShowError("intif_saveregistry: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR "\n", sd->status.account_id, sd->status.char_id, varname, len);
  379. continue;
  380. }
  381. WFIFOB(inter_fd, plen) = (unsigned char)len; // won't be higher; the column size is 32
  382. plen += 1;
  383. safestrncpy(WFIFOCP(inter_fd,plen), varname, len); //the key
  384. plen += static_cast<decltype(plen)>( len );
  385. WFIFOL(inter_fd, plen) = script_getvaridx(key.i64);
  386. plen += 4;
  387. if( src->type ) {
  388. struct script_reg_str *p = (struct script_reg_str *)src;
  389. WFIFOB(inter_fd, plen) = p->value ? 2 : 3; //var type
  390. plen += 1;
  391. if( p->value ) {
  392. lValid = script_check_RegistryVariableLength(1,p->value,&len);
  393. ++len;
  394. if ( !lValid ) { // error can't be higher; the column size is 254. (nb the transmission limit with be fixed with protobuf revamp)
  395. ShowDebug( "intif_saveregistry: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%" PRIuPTR " to be saved with current system and will be truncated\n",sd->status.account_id, sd->status.char_id,p->value,len);
  396. len = 254;
  397. p->value[len - 1] = '\0'; //this is backward for old char-serv but new one doesn't need this
  398. }
  399. WFIFOB(inter_fd, plen) = (uint8)len;
  400. plen += 1;
  401. safestrncpy(WFIFOCP(inter_fd,plen), p->value, len);
  402. plen += static_cast<decltype(plen)>( len );
  403. } else {
  404. script_reg_destroy_single(sd,key.i64,&p->flag);
  405. }
  406. } else {
  407. struct script_reg_num *p = (struct script_reg_num *)src;
  408. WFIFOB(inter_fd, plen) = p->value ? 0 : 1;
  409. plen += 1;
  410. if( p->value ) {
  411. WFIFOQ(inter_fd, plen) = p->value;
  412. plen += 8;
  413. } else {
  414. script_reg_destroy_single(sd,key.i64,&p->flag);
  415. }
  416. }
  417. WFIFOW(inter_fd,12) += 1;
  418. if( plen > 60000 ) {
  419. WFIFOW(inter_fd, 2) = plen;
  420. WFIFOSET(inter_fd, plen);
  421. // prepare follow up
  422. WFIFOHEAD(inter_fd, 60000 + 300);
  423. WFIFOW(inter_fd,0) = 0x3004;
  424. // 0x2 = length (set later)
  425. WFIFOL(inter_fd,4) = sd->status.account_id;
  426. WFIFOL(inter_fd,8) = sd->status.char_id;
  427. WFIFOW(inter_fd,12) = 0; // count
  428. plen = 14;
  429. }
  430. }
  431. dbi_destroy(iter);
  432. WFIFOW(inter_fd, 2) = plen;
  433. WFIFOSET(inter_fd, plen);
  434. sd->vars_dirty = false;
  435. return 0;
  436. }
  437. /**
  438. * Request the registries for this player.
  439. * @param sd : Player to load registry
  440. * @param flag : Type of registry to load, &1=acc (login-serv), &2=acc (char-serv), &4=char
  441. * @return
  442. */
  443. int intif_request_registry(map_session_data *sd, int flag)
  444. {
  445. nullpo_ret(sd);
  446. if (CheckForCharServer())
  447. return 0;
  448. WFIFOHEAD(inter_fd,13);
  449. WFIFOW(inter_fd,0) = 0x3005;
  450. WFIFOL(inter_fd,2) = sd->status.account_id;
  451. WFIFOL(inter_fd,6) = sd->status.char_id;
  452. WFIFOB(inter_fd,10) = (flag&1?1:0); //Request Acc Reg 2 (from login-serv))
  453. WFIFOB(inter_fd,11) = (flag&2?1:0); //Request Acc Reg (from char-serv)
  454. WFIFOB(inter_fd,12) = (flag&4?1:0); //Request Char Reg
  455. WFIFOSET(inter_fd,13);
  456. return 1;
  457. }
  458. /**
  459. * Request to load guild storage from char-serv
  460. * @param account_id: Player account identification
  461. * @param guild_id: Guild of player
  462. * @return false - error, true - message sent
  463. */
  464. bool intif_request_guild_storage(uint32 account_id, int guild_id)
  465. {
  466. if (CheckForCharServer())
  467. return false;
  468. WFIFOHEAD(inter_fd,10);
  469. WFIFOW(inter_fd,0) = 0x3018;
  470. WFIFOL(inter_fd,2) = account_id;
  471. WFIFOL(inter_fd,6) = guild_id;
  472. WFIFOSET(inter_fd,10);
  473. return true;
  474. }
  475. /**
  476. * Request to save guild storage
  477. * @param account_id: account requesting the save
  478. * @param gstor: Guild storage struct to save
  479. * @return false - error, true - message sent
  480. */
  481. bool intif_send_guild_storage(uint32 account_id, struct s_storage *gstor)
  482. {
  483. if (CheckForCharServer())
  484. return false;
  485. WFIFOHEAD(inter_fd,sizeof(struct s_storage)+12);
  486. WFIFOW(inter_fd,0) = 0x3019;
  487. WFIFOW(inter_fd,2) = (unsigned short)sizeof(struct s_storage)+12;
  488. WFIFOL(inter_fd,4) = account_id;
  489. WFIFOL(inter_fd,8) = gstor->id;
  490. memcpy( WFIFOP(inter_fd,12),gstor, sizeof(struct s_storage) );
  491. WFIFOSET(inter_fd,WFIFOW(inter_fd,2));
  492. return true;
  493. }
  494. /**
  495. * Party creation request
  496. * @param member : Struct of 1 party member
  497. * @param name : Party name
  498. * @param item : item pickup rule
  499. * @param item2 : item share rule
  500. * @return 0=error, 1=msg sent
  501. */
  502. int intif_create_party(struct party_member *member,char *name,int item,int item2)
  503. {
  504. if (CheckForCharServer())
  505. return 0;
  506. nullpo_ret(member);
  507. WFIFOHEAD(inter_fd, 6+NAME_LENGTH+sizeof(struct party_member));
  508. WFIFOW(inter_fd,0) = 0x3020;
  509. WFIFOW(inter_fd,2) = 6+NAME_LENGTH+sizeof(struct party_member);
  510. safestrncpy(WFIFOCP(inter_fd,4),name, NAME_LENGTH);
  511. WFIFOB(inter_fd,28)= item;
  512. WFIFOB(inter_fd,29)= item2;
  513. memcpy(WFIFOP(inter_fd,30), member, sizeof(struct party_member));
  514. WFIFOSET(inter_fd,WFIFOW(inter_fd, 2));
  515. return 1;
  516. }
  517. /**
  518. * Party information request
  519. * @param party_id : Party id to request info
  520. * @param char_id : Player id requesting
  521. * @return 0=error, 1=msg sent
  522. */
  523. int intif_request_partyinfo(int party_id, uint32 char_id)
  524. {
  525. if (CheckForCharServer())
  526. return 0;
  527. WFIFOHEAD(inter_fd,10);
  528. WFIFOW(inter_fd,0) = 0x3021;
  529. WFIFOL(inter_fd,2) = party_id;
  530. WFIFOL(inter_fd,6) = char_id;
  531. WFIFOSET(inter_fd,10);
  532. return 1;
  533. }
  534. /**
  535. * Request to add a member to party
  536. * @param party_id : Party to add member to
  537. * @param member : member to add to party
  538. * @return
  539. */
  540. int intif_party_addmember(int party_id,struct party_member *member)
  541. {
  542. if (CheckForCharServer())
  543. return 0;
  544. WFIFOHEAD(inter_fd,8+sizeof(struct party_member));
  545. WFIFOW(inter_fd,0)=0x3022;
  546. WFIFOW(inter_fd,2)=8+sizeof(struct party_member);
  547. WFIFOL(inter_fd,4)=party_id;
  548. memcpy(WFIFOP(inter_fd,8),member,sizeof(struct party_member));
  549. WFIFOSET(inter_fd,WFIFOW(inter_fd, 2));
  550. return 1;
  551. }
  552. /**
  553. * Request to change party configuration (exp,item share)
  554. * @param party_id : Party to alter
  555. * @param account_id : account requesting change
  556. * @param exp : sharing exp option
  557. * @param item : sharing item option
  558. * @return 0=error, 1=msg sent
  559. */
  560. int intif_party_changeoption(int party_id,uint32 account_id,int exp,int item)
  561. {
  562. if (CheckForCharServer())
  563. return 0;
  564. WFIFOHEAD(inter_fd,14);
  565. WFIFOW(inter_fd,0)=0x3023;
  566. WFIFOL(inter_fd,2)=party_id;
  567. WFIFOL(inter_fd,6)=account_id;
  568. WFIFOW(inter_fd,10)=exp;
  569. WFIFOW(inter_fd,12)=item;
  570. WFIFOSET(inter_fd,14);
  571. return 1;
  572. }
  573. /**
  574. * Ask the char-serv to make aid,cid quit party
  575. * @param party_id : Party to leave
  576. * @param account_id : aid of player to leave
  577. * @param char_id : cid of player to leave
  578. * @return 0:char-serv disconected, 1=msg sent
  579. */
  580. int intif_party_leave(int party_id, uint32 account_id, uint32 char_id, const char *name, enum e_party_member_withdraw type)
  581. {
  582. if (CheckForCharServer())
  583. return 0;
  584. WFIFOHEAD(inter_fd,15+NAME_LENGTH);
  585. WFIFOW(inter_fd,0) = 0x3024;
  586. WFIFOL(inter_fd,2) = party_id;
  587. WFIFOL(inter_fd,6) = account_id;
  588. WFIFOL(inter_fd,10) = char_id;
  589. safestrncpy(WFIFOCP(inter_fd,14), name, NAME_LENGTH);
  590. WFIFOB(inter_fd,14+NAME_LENGTH) = type;
  591. WFIFOSET(inter_fd,15+NAME_LENGTH);
  592. return 1;
  593. }
  594. /**
  595. * Inform char-serv that a member as quit/lvlup change map, therefore changing party state
  596. * @param sd : Player that moved
  597. * @param online : If the player will stay online or no
  598. * @return 0=error, 1=msg sent
  599. */
  600. int intif_party_changemap(map_session_data *sd,int online)
  601. {
  602. int16 m, mapindex;
  603. if (CheckForCharServer())
  604. return 0;
  605. if(!sd)
  606. return 0;
  607. if ((m = map_mapindex2mapid(sd->mapindex)) >= 0) {
  608. struct map_data *mapdata = map_getmapdata(m);
  609. if (mapdata->instance_id)
  610. mapindex = map_getmapdata(mapdata->instance_src_map)->index;
  611. else
  612. mapindex = sd->mapindex;
  613. } else
  614. mapindex = sd->mapindex;
  615. WFIFOHEAD( inter_fd, 17 + MAP_NAME_LENGTH_EXT );
  616. WFIFOW(inter_fd,0)=0x3025;
  617. WFIFOL(inter_fd,2)=sd->status.party_id;
  618. WFIFOL(inter_fd,6)=sd->status.account_id;
  619. WFIFOL(inter_fd,10)=sd->status.char_id;
  620. WFIFOB(inter_fd,14)=online;
  621. WFIFOW(inter_fd,15)=sd->status.base_level;
  622. safestrncpy( WFIFOCP( inter_fd, 17 ), mapindex_id2name( mapindex ), MAP_NAME_LENGTH_EXT );
  623. WFIFOSET( inter_fd, 17 + MAP_NAME_LENGTH_EXT );
  624. return 1;
  625. }
  626. /**
  627. * Request breaking party
  628. * @param party_id : Party to delete
  629. * @return 0=error, 1=msg sent
  630. */
  631. int intif_break_party(int party_id)
  632. {
  633. if (CheckForCharServer())
  634. return 0;
  635. WFIFOHEAD(inter_fd,6);
  636. WFIFOW(inter_fd,0)=0x3026;
  637. WFIFOL(inter_fd,2)=party_id;
  638. WFIFOSET(inter_fd,6);
  639. return 0;
  640. }
  641. //
  642. /**
  643. * Request sending party chat
  644. * (we using this in case we have multiple map-serv attached
  645. * to be sure all party get the message)
  646. * @param party_id : Party identification
  647. * @param account_id : Player sending the message
  648. * @param mes : Message to send
  649. * @param len : Size of the message
  650. * @return 0=error, 1=msg sent
  651. */
  652. int intif_party_message(int party_id, uint32 account_id, const char *mes, size_t len)
  653. {
  654. if (CheckForCharServer())
  655. return 0;
  656. if (other_mapserver_count < 1)
  657. return 0; //No need to send.
  658. WFIFOHEAD(inter_fd,len + 12);
  659. WFIFOW(inter_fd,0)=0x3027;
  660. WFIFOW(inter_fd,2)=static_cast<int16>( len + 12 );
  661. WFIFOL(inter_fd,4)=party_id;
  662. WFIFOL(inter_fd,8)=account_id;
  663. safestrncpy(WFIFOCP(inter_fd,12),mes,len);
  664. WFIFOSET(inter_fd,len+12);
  665. return 1;
  666. }
  667. /**
  668. * Request a new leader for party
  669. * @param party_id : Party to alter
  670. * @param account_id : player to set as new leader
  671. * @param char_id : player to set as new leader
  672. * @return 0=error, 1=msg sent
  673. */
  674. int intif_party_leaderchange(int party_id,uint32 account_id,uint32 char_id)
  675. {
  676. if (CheckForCharServer())
  677. return 0;
  678. WFIFOHEAD(inter_fd,14);
  679. WFIFOW(inter_fd,0)=0x3029;
  680. WFIFOL(inter_fd,2)=party_id;
  681. WFIFOL(inter_fd,6)=account_id;
  682. WFIFOL(inter_fd,10)=char_id;
  683. WFIFOSET(inter_fd,14);
  684. return 1;
  685. }
  686. /**
  687. * Request to update party share level
  688. * @param share_lvl : Max level number of difference to share exp
  689. * @return 0=error, 1=msg sent
  690. */
  691. int intif_party_sharelvlupdate(unsigned int share_lvl)
  692. {
  693. if (CheckForCharServer())
  694. return 0;
  695. WFIFOHEAD(inter_fd,6);
  696. WFIFOW(inter_fd,0)=0x302A;
  697. WFIFOL(inter_fd,2)=share_lvl;
  698. WFIFOSET(inter_fd,6);
  699. return 1;
  700. }
  701. /**
  702. * Request a Guild creation
  703. * @param name : Name of the guild
  704. * @param master : Guild_member info of master
  705. * @return 0=error, 1=msg_sent
  706. */
  707. int intif_guild_create(const char *name,const struct guild_member *master)
  708. {
  709. if (CheckForCharServer())
  710. return 0;
  711. nullpo_ret(master);
  712. WFIFOHEAD(inter_fd,sizeof(struct guild_member)+(8+NAME_LENGTH));
  713. WFIFOW(inter_fd,0)=0x3030;
  714. WFIFOW(inter_fd,2)=sizeof(struct guild_member)+(8+NAME_LENGTH);
  715. WFIFOL(inter_fd,4)=master->account_id;
  716. safestrncpy(WFIFOCP(inter_fd,8),name,NAME_LENGTH);
  717. memcpy(WFIFOP(inter_fd,8+NAME_LENGTH),master,sizeof(struct guild_member));
  718. WFIFOSET(inter_fd,WFIFOW(inter_fd,2));
  719. return 1;
  720. }
  721. /**
  722. * Request Guild information
  723. * @param guild_id : guild to get info from
  724. * @return 0=error, 1=msg_sent
  725. */
  726. int intif_guild_request_info(int guild_id)
  727. {
  728. if (CheckForCharServer())
  729. return 0;
  730. WFIFOHEAD(inter_fd,6);
  731. WFIFOW(inter_fd,0) = 0x3031;
  732. WFIFOL(inter_fd,2) = guild_id;
  733. WFIFOSET(inter_fd,6);
  734. return 1;
  735. }
  736. /**
  737. * Request to add member to the guild
  738. * @param guild_id : Guild to alter
  739. * @param m : Member to add to the guild
  740. * @return 0=error, 1=msg_sent
  741. */
  742. int intif_guild_addmember( int guild_id, struct guild_member& m ){
  743. if (CheckForCharServer())
  744. return 0;
  745. WFIFOHEAD(inter_fd,sizeof(struct guild_member)+8);
  746. WFIFOW(inter_fd,0) = 0x3032;
  747. WFIFOW(inter_fd,2) = sizeof(struct guild_member)+8;
  748. WFIFOL(inter_fd,4) = guild_id;
  749. memcpy( WFIFOP( inter_fd, 8 ), &m, sizeof( struct guild_member ) );
  750. WFIFOSET(inter_fd,WFIFOW(inter_fd,2));
  751. return 1;
  752. }
  753. /**
  754. * Request a new leader for guild
  755. * @param guild_id : guild to alter
  756. * @param name : name of the new master
  757. * @param len : size of the name
  758. * @return 0=error, 1=msg_sent
  759. */
  760. bool intif_guild_change_gm( int guild_id, const char* name, size_t len ){
  761. if (CheckForCharServer())
  762. return false;
  763. WFIFOHEAD(inter_fd, len + 8);
  764. WFIFOW(inter_fd, 0)=0x3033;
  765. WFIFOW( inter_fd, 2 ) = static_cast<int16>( len + 8 );
  766. WFIFOL(inter_fd, 4)=guild_id;
  767. safestrncpy(WFIFOCP(inter_fd,8),name,len);
  768. WFIFOSET(inter_fd,len+8);
  769. return true;
  770. }
  771. /**
  772. * Request to make a player leave a guild
  773. * @param guild_id : guild to alter
  774. * @param account_id : player aid to kick
  775. * @param char_id : player cid to kick
  776. * @param flag : 0:normal quit, 1=expulsion
  777. * @param mes : quitting message (max 40)
  778. * @return 0=error, 1=msg_sent
  779. */
  780. bool intif_guild_leave( int guild_id, uint32 account_id, uint32 char_id, int flag, const char *mes ){
  781. if (CheckForCharServer())
  782. return false;
  783. WFIFOHEAD(inter_fd, 55);
  784. WFIFOW(inter_fd, 0) = 0x3034;
  785. WFIFOL(inter_fd, 2) = guild_id;
  786. WFIFOL(inter_fd, 6) = account_id;
  787. WFIFOL(inter_fd,10) = char_id;
  788. WFIFOB(inter_fd,14) = flag;
  789. safestrncpy(WFIFOCP(inter_fd,15),mes,40);
  790. WFIFOSET(inter_fd,55);
  791. return true;
  792. }
  793. /**
  794. * Update request / Lv online status of the guild members
  795. * @param guild_id : guild to alter
  796. * @param account_id : player aid to alter
  797. * @param char_id : player cid to alter
  798. * @param online : does the player is online (or will stay online)
  799. * @param lv : player lv
  800. * @param class_ : player class
  801. * @return 0=error, 1=msg_sent
  802. */
  803. int intif_guild_memberinfoshort(int guild_id,uint32 account_id,uint32 char_id,int online,int lv,int class_)
  804. {
  805. if (CheckForCharServer())
  806. return 0;
  807. WFIFOHEAD(inter_fd, 19);
  808. WFIFOW(inter_fd, 0) = 0x3035;
  809. WFIFOL(inter_fd, 2) = guild_id;
  810. WFIFOL(inter_fd, 6) = account_id;
  811. WFIFOL(inter_fd,10) = char_id;
  812. WFIFOB(inter_fd,14) = online;
  813. WFIFOW(inter_fd,15) = lv;
  814. WFIFOW(inter_fd,17) = class_;
  815. WFIFOSET(inter_fd,19);
  816. return 1;
  817. }
  818. /**
  819. * Guild disbanded notification
  820. * @param guild_id : guild to disband
  821. * @return 0=error, 1=msg_sent
  822. */
  823. int intif_guild_break(int guild_id)
  824. {
  825. if (CheckForCharServer())
  826. return 0;
  827. WFIFOHEAD(inter_fd, 6);
  828. WFIFOW(inter_fd, 0) = 0x3036;
  829. WFIFOL(inter_fd, 2) = guild_id;
  830. WFIFOSET(inter_fd,6);
  831. return 1;
  832. }
  833. /**
  834. * Send a guild message
  835. * (This is goign throught char-serv in case of multi-map setup)
  836. * @param guild_id : Guild id to send the message to
  837. * @param account_id : Player sending the msg
  838. * @param mes : Message to send
  839. * @param len : Size of the message
  840. * @return 0=error, 1=msg_sent
  841. */
  842. int intif_guild_message(int guild_id, uint32 account_id, const char *mes, size_t len)
  843. {
  844. if (CheckForCharServer())
  845. return 0;
  846. if (other_mapserver_count < 1)
  847. return 0; //No need to send.
  848. WFIFOHEAD(inter_fd, len + 12);
  849. WFIFOW(inter_fd,0)=0x3037;
  850. WFIFOW(inter_fd,2)=static_cast<int16>( len + 12 );
  851. WFIFOL(inter_fd,4)=guild_id;
  852. WFIFOL(inter_fd,8)=account_id;
  853. safestrncpy(WFIFOCP(inter_fd,12),mes,len);
  854. WFIFOSET(inter_fd,len+12);
  855. return 1;
  856. }
  857. /**
  858. * Request a change of Guild basic information
  859. * @param guild_id : Guild to alter
  860. * @param type : type of data to change (currently only guildlv)
  861. * @param data : new value for type
  862. * @param len : size of data
  863. * @return 0=error, 1=msg_sent
  864. */
  865. int intif_guild_change_basicinfo(int guild_id,int type,const void *data,int len)
  866. {
  867. if (CheckForCharServer())
  868. return 0;
  869. WFIFOHEAD(inter_fd, len + 10);
  870. WFIFOW(inter_fd,0)=0x3039;
  871. WFIFOW(inter_fd,2)=len+10;
  872. WFIFOL(inter_fd,4)=guild_id;
  873. WFIFOW(inter_fd,8)=type;
  874. memcpy(WFIFOP(inter_fd,10),data,len);
  875. WFIFOSET(inter_fd,len+10);
  876. return 1;
  877. }
  878. /**
  879. * Request a change of Guild member information
  880. * @param guild_id : Guild to alter
  881. * @param account_id : Player aid to alter
  882. * @param char_id : Player cid to alter
  883. * @param type : Type of modification
  884. * @param data : Value of modification
  885. * @param len : Size of value
  886. * @return 0=error, 1=msg_sent
  887. */
  888. int intif_guild_change_memberinfo(int guild_id,uint32 account_id,uint32 char_id,
  889. int type,const void *data,int len)
  890. {
  891. if (CheckForCharServer())
  892. return 0;
  893. WFIFOHEAD(inter_fd, len + 18);
  894. WFIFOW(inter_fd, 0)=0x303a;
  895. WFIFOW(inter_fd, 2)=len+18;
  896. WFIFOL(inter_fd, 4)=guild_id;
  897. WFIFOL(inter_fd, 8)=account_id;
  898. WFIFOL(inter_fd,12)=char_id;
  899. WFIFOW(inter_fd,16)=type;
  900. memcpy(WFIFOP(inter_fd,18),data,len);
  901. WFIFOSET(inter_fd,len+18);
  902. return 1;
  903. }
  904. /**
  905. * Request a change of Guild title
  906. * @param guild_id : guild to alter
  907. * @param idx : Position Index
  908. * @param p : Position data { <mode>.L <ranking>.L <pay rate>.L <name>.24B }
  909. * @return 0=error, 1=msg_sent
  910. */
  911. int intif_guild_position(int guild_id,int idx,struct guild_position *p)
  912. {
  913. if (CheckForCharServer())
  914. return 0;
  915. WFIFOHEAD(inter_fd, sizeof(struct guild_position)+12);
  916. WFIFOW(inter_fd,0)=0x303b;
  917. WFIFOW(inter_fd,2)=sizeof(struct guild_position)+12;
  918. WFIFOL(inter_fd,4)=guild_id;
  919. WFIFOL(inter_fd,8)=idx;
  920. memcpy(WFIFOP(inter_fd,12),p,sizeof(struct guild_position));
  921. WFIFOSET(inter_fd,WFIFOW(inter_fd,2));
  922. return 1;
  923. }
  924. /**
  925. * Request an update of Guildskill skill_id
  926. * @param guild_id : Guild to alter
  927. * @param skill_id : Skill to lvl up
  928. * @param account_id : aid requesting update
  929. * @param max : skill max level
  930. * @return 0=error, 1=msg_sent
  931. */
  932. int intif_guild_skillup(int guild_id, uint16 skill_id, uint32 account_id, int max)
  933. {
  934. if( CheckForCharServer() )
  935. return 0;
  936. WFIFOHEAD(inter_fd, 18);
  937. WFIFOW(inter_fd, 0) = 0x303c;
  938. WFIFOL(inter_fd, 2) = guild_id;
  939. WFIFOL(inter_fd, 6) = skill_id;
  940. WFIFOL(inter_fd, 10) = account_id;
  941. WFIFOL(inter_fd, 14) = max;
  942. WFIFOSET(inter_fd, 18);
  943. return 1;
  944. }
  945. /**
  946. * Request a new guild relationship
  947. * @param guild_id1 : Guild to associate 1
  948. * @param guild_id2 : Guild to associate 2
  949. * @param account_id1 : aid of player in guild1
  950. * @param account_id2 : aid of player in guild2
  951. * @param flag : (GUILD_ALLIANCE_REMOVE|0|1)
  952. * @return 0=error, 1=msg_sent
  953. */
  954. int intif_guild_alliance(int guild_id1,int guild_id2,uint32 account_id1,uint32 account_id2,int flag)
  955. {
  956. if (CheckForCharServer())
  957. return 0;
  958. WFIFOHEAD(inter_fd,19);
  959. WFIFOW(inter_fd, 0)=0x303d;
  960. WFIFOL(inter_fd, 2)=guild_id1;
  961. WFIFOL(inter_fd, 6)=guild_id2;
  962. WFIFOL(inter_fd,10)=account_id1;
  963. WFIFOL(inter_fd,14)=account_id2;
  964. WFIFOB(inter_fd,18)=flag;
  965. WFIFOSET(inter_fd,19);
  966. return 1;
  967. }
  968. /**
  969. * Request to change guild notice
  970. * @param guild_id : Guild to alter
  971. * @param mes1 : Notice title (max 60)
  972. * @param mes2 : Notice body (max 120)
  973. * @return 0=error, 1=msg_sent
  974. */
  975. int intif_guild_notice(int guild_id,const char *mes1,const char *mes2)
  976. {
  977. if (CheckForCharServer())
  978. return 0;
  979. WFIFOHEAD(inter_fd,186);
  980. WFIFOW(inter_fd,0)=0x303e;
  981. WFIFOL(inter_fd,2)=guild_id;
  982. safestrncpy(WFIFOCP(inter_fd,6),mes1,MAX_GUILDMES1);
  983. safestrncpy(WFIFOCP(inter_fd,66),mes2,MAX_GUILDMES2);
  984. WFIFOSET(inter_fd,186);
  985. return 1;
  986. }
  987. /**
  988. * Request to change guild emblem
  989. * @param guild_id
  990. * @param len
  991. * @param data
  992. * @return
  993. */
  994. int intif_guild_emblem(int guild_id,int len,const char *data)
  995. {
  996. if (CheckForCharServer())
  997. return 0;
  998. if(guild_id<=0 || len<0 || len>2000)
  999. return 0;
  1000. WFIFOHEAD(inter_fd,len + 12);
  1001. WFIFOW(inter_fd,0)=0x303f;
  1002. WFIFOW(inter_fd,2)=len+12;
  1003. WFIFOL(inter_fd,4)=guild_id;
  1004. WFIFOL(inter_fd,8)=0;
  1005. memcpy(WFIFOP(inter_fd,12),data,len);
  1006. WFIFOSET(inter_fd,len+12);
  1007. return 1;
  1008. }
  1009. int intif_guild_emblem_version(int guild_id, int emblem_id)
  1010. {
  1011. if (CheckForCharServer())
  1012. return 0;
  1013. if (guild_id <= 0)
  1014. return 0;
  1015. WFIFOHEAD(inter_fd, 10);
  1016. WFIFOW(inter_fd, 0) = 0x3042;
  1017. WFIFOL(inter_fd, 2) = guild_id;
  1018. WFIFOL(inter_fd, 6) = emblem_id;
  1019. WFIFOSET(inter_fd, 10);
  1020. return 1;
  1021. }
  1022. /**
  1023. * Requests guild castles data from char-server.
  1024. * @param num Number of castles, size of castle_ids array.
  1025. * @param castle_ids Pointer to array of castle IDs.
  1026. */
  1027. bool intif_guild_castle_dataload( const std::vector<int32>& castle_ids ){
  1028. if( CheckForCharServer() ){
  1029. return false;
  1030. }
  1031. uint16 size = (uint16)( 4 + castle_ids.size() * sizeof( int32 ) );
  1032. WFIFOHEAD( inter_fd, size );
  1033. WFIFOW(inter_fd, 0) = 0x3040;
  1034. WFIFOW( inter_fd, 2 ) = size;
  1035. for( size_t i = 0; i < castle_ids.size(); i++ ){
  1036. WFIFOL( inter_fd, 4 + i * sizeof( int32 ) ) = castle_ids[i];
  1037. }
  1038. WFIFOSET( inter_fd, size );
  1039. return true;
  1040. }
  1041. /**
  1042. * Request change castle guild owner and save data
  1043. * @param castle_id
  1044. * @param index
  1045. * @param value
  1046. * @return
  1047. */
  1048. int intif_guild_castle_datasave(int castle_id,int index, int value)
  1049. {
  1050. if (CheckForCharServer())
  1051. return 0;
  1052. WFIFOHEAD(inter_fd,9);
  1053. WFIFOW(inter_fd,0)=0x3041;
  1054. WFIFOW(inter_fd,2)=castle_id;
  1055. WFIFOB(inter_fd,4)=index;
  1056. WFIFOL(inter_fd,5)=value;
  1057. WFIFOSET(inter_fd,9);
  1058. return 1;
  1059. }
  1060. //-----------------------------------------------------------------
  1061. // Homunculus Packets send to Inter server [albator]
  1062. //-----------------------------------------------------------------
  1063. /**
  1064. * Request to create/register homonculus
  1065. * @param account_id : player requesting
  1066. * @param sh : TMp homunlus data
  1067. * @return 0=error, 1=msg_sent
  1068. */
  1069. int intif_homunculus_create(uint32 account_id, struct s_homunculus *sh)
  1070. {
  1071. if (CheckForCharServer())
  1072. return 0;
  1073. WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8);
  1074. WFIFOW(inter_fd,0) = 0x3090;
  1075. WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8;
  1076. WFIFOL(inter_fd,4) = account_id;
  1077. memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus));
  1078. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  1079. return 1;
  1080. }
  1081. /**
  1082. * Request to load homunculus from char-serv
  1083. * @param account_id
  1084. * @param homun_id
  1085. * @return 0=error, 1=msg sent
  1086. */
  1087. int intif_homunculus_requestload(uint32 account_id, int homun_id)
  1088. {
  1089. if (CheckForCharServer())
  1090. return 0;
  1091. WFIFOHEAD(inter_fd, 10);
  1092. WFIFOW(inter_fd,0) = 0x3091;
  1093. WFIFOL(inter_fd,2) = account_id;
  1094. WFIFOL(inter_fd,6) = homun_id;
  1095. WFIFOSET(inter_fd, 10);
  1096. return 1;
  1097. }
  1098. /**
  1099. * Request to save homunculus
  1100. * @param account_id : Player asking save
  1101. * @param sh : homunculus struct
  1102. * @return : 0=error, 1=msg sent
  1103. */
  1104. int intif_homunculus_requestsave(uint32 account_id, struct s_homunculus* sh)
  1105. {
  1106. if (CheckForCharServer())
  1107. return 0;
  1108. WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8);
  1109. WFIFOW(inter_fd,0) = 0x3092;
  1110. WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8;
  1111. WFIFOL(inter_fd,4) = account_id;
  1112. memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus));
  1113. WFIFOSET(inter_fd, WFIFOW(inter_fd,2));
  1114. return 1;
  1115. }
  1116. /**
  1117. * request to delete homunculus
  1118. * @param homun_id
  1119. * @return 0=error, 1=msg sent
  1120. */
  1121. int intif_homunculus_requestdelete(int homun_id)
  1122. {
  1123. if (CheckForCharServer())
  1124. return 0;
  1125. WFIFOHEAD(inter_fd, 6);
  1126. WFIFOW(inter_fd, 0) = 0x3093;
  1127. WFIFOL(inter_fd,2) = homun_id;
  1128. WFIFOSET(inter_fd,6);
  1129. return 1;
  1130. }
  1131. //-----------------------------------------------------------------
  1132. // Packets receive from inter server
  1133. /**
  1134. * Receive a whisper request from char-serv transmit it to player
  1135. * @author : rewritten by [Yor]
  1136. * @param fd : char-serv link
  1137. * @return 0=not found or ignored, 1=transmited
  1138. */
  1139. int intif_parse_WisMessage(int fd)
  1140. {
  1141. map_session_data* sd;
  1142. char *wisp_source;
  1143. char name[NAME_LENGTH];
  1144. int id, i, gmlvl;
  1145. id=RFIFOL(fd,4);
  1146. gmlvl=RFIFOL(fd,8);
  1147. safestrncpy(name, RFIFOCP(fd,12+NAME_LENGTH), NAME_LENGTH);
  1148. sd = map_nick2sd(name,false);
  1149. if(sd == nullptr || strcmp(sd->status.name, name) != 0)
  1150. { //Not found
  1151. intif_wis_reply(id,1);
  1152. return 0;
  1153. }
  1154. if(sd->state.ignoreAll) {
  1155. intif_wis_reply(id, (pc_has_permission(sd, PC_PERM_HIDE_SESSION))?1:2);
  1156. return 0;
  1157. }
  1158. wisp_source = RFIFOCP(fd,12); // speed up [Yor]
  1159. for(i=0; i < MAX_IGNORE_LIST &&
  1160. sd->ignore[i].name[0] != '\0' &&
  1161. strcmp(sd->ignore[i].name, wisp_source) != 0
  1162. ; i++);
  1163. if (i < MAX_IGNORE_LIST && sd->ignore[i].name[0] != '\0')
  1164. { //Ignored
  1165. intif_wis_reply(id, (pc_has_permission(sd, PC_PERM_HIDE_SESSION))?1:2);
  1166. return 0;
  1167. }
  1168. //Success to send whisper.
  1169. clif_wis_message(sd, wisp_source, RFIFOCP(fd,12+2*NAME_LENGTH),RFIFOW(fd,2)-12+2*NAME_LENGTH, gmlvl);
  1170. intif_wis_reply(id,0); // success
  1171. return 1;
  1172. }
  1173. /**
  1174. * Wisp/page transmission result reception
  1175. * @param fd : char-serv link
  1176. * @return 1
  1177. */
  1178. int intif_parse_WisEnd(int fd)
  1179. {
  1180. if (battle_config.etc_log)
  1181. ShowInfo("intif_parse_wisend: player: %s, flag: %d\n", RFIFOP(fd,2), RFIFOB(fd,26)); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
  1182. map_session_data* sd = map_nick2sd( RFIFOCP( fd, 2 ), false );
  1183. if (sd != nullptr) {
  1184. uint8 result = RFIFOB( fd, 26 );
  1185. if ( result >= ACKWHISPER_SUCCESS && result <= ACKWHISPER_ALL_IGNORED )
  1186. clif_wis_end( *sd, static_cast<e_ack_whisper>(result) );
  1187. }
  1188. return 1;
  1189. }
  1190. /**
  1191. * Transmit a whisper message to sd
  1192. * @param sd : Player to transmit the message to
  1193. * @param va : list of arguments ( wisp_name, message, len)
  1194. * @return 0=error, 1=msg sent
  1195. */
  1196. static int mapif_parse_WisToGM_sub(map_session_data* sd,va_list va)
  1197. {
  1198. e_pc_permission permission = static_cast<e_pc_permission>(va_arg(va, uint32));
  1199. char *wisp_name;
  1200. char *message;
  1201. int len;
  1202. if (!pc_has_permission(sd, permission))
  1203. return 0;
  1204. wisp_name = va_arg(va, char*);
  1205. message = va_arg(va, char*);
  1206. len = va_arg(va, int);
  1207. clif_wis_message(sd, wisp_name, message, len,0);
  1208. return 1;
  1209. }
  1210. /**
  1211. * Received wisp message from map-server via char-server for ALL gm
  1212. * 0x3003/0x3803 <packet_len>.w <wispname>.24B <permission>.l <message>.?B
  1213. * @see mapif_parse_WisToGM_sub, for 1 transmission
  1214. * @param fd : char-serv link
  1215. * @return 1
  1216. */
  1217. int mapif_parse_WisToGM(int fd)
  1218. {
  1219. int permission, mes_len;
  1220. char Wisp_name[NAME_LENGTH];
  1221. char *message;
  1222. mes_len = RFIFOW(fd,2) - 8+NAME_LENGTH;
  1223. message = (char *) aMalloc(mes_len+1);
  1224. safestrncpy(Wisp_name, RFIFOCP(fd,4), NAME_LENGTH);
  1225. permission = RFIFOL(fd, 4 + NAME_LENGTH);
  1226. safestrncpy(message, RFIFOCP(fd,8+NAME_LENGTH), mes_len+1);
  1227. // information is sent to all online GM
  1228. map_foreachpc(mapif_parse_WisToGM_sub, permission, Wisp_name, message, mes_len);
  1229. aFree(message);
  1230. return 1;
  1231. }
  1232. /**
  1233. * Request player registry
  1234. * @param fd : char-serv link
  1235. * @return 0=error, 1=sucess
  1236. */
  1237. void intif_parse_Registers(int fd)
  1238. {
  1239. int flag;
  1240. map_session_data *sd;
  1241. uint32 account_id = RFIFOL(fd,4), char_id = RFIFOL(fd,8);
  1242. struct auth_node *node = chrif_auth_check(account_id, char_id, ST_LOGIN);
  1243. char type = RFIFOB(fd, 13);
  1244. if (node)
  1245. sd = node->sd;
  1246. else { //Normally registries should arrive for in log-in chars.
  1247. sd = map_id2sd(account_id);
  1248. }
  1249. if (!sd || sd->status.char_id != char_id) {
  1250. return; //Character registry from another character.
  1251. }
  1252. flag = ( sd->vars_received&PRL_ACCG && sd->vars_received&PRL_ACCL && sd->vars_received&PRL_CHAR ) ? 0 : 1;
  1253. switch (RFIFOB(fd,12)) {
  1254. case 3: //Character Registry
  1255. sd->vars_received |= PRL_CHAR;
  1256. break;
  1257. case 2: //Account Registry
  1258. sd->vars_received |= PRL_ACCL;
  1259. break;
  1260. case 1: //Account2 Registry
  1261. sd->vars_received |= PRL_ACCG;
  1262. break;
  1263. case 0:
  1264. break;
  1265. default:
  1266. ShowError("intif_parse_Registers: Unrecognized type %d\n",RFIFOB(fd,12));
  1267. return;
  1268. }
  1269. // have it not complain about insertion of vars before loading, and not set those vars as new or modified
  1270. pc_set_reg_load(true);
  1271. if( RFIFOW(fd, 14) ) {
  1272. char key[32];
  1273. uint32 index;
  1274. int max = RFIFOW(fd, 14), cursor = 16, i;
  1275. /**
  1276. * Vessel!char_reg_num_db
  1277. *
  1278. * str type
  1279. * { keyLength(B), key(<keyLength>), index(L), valLength(B), val(<valLength>) }
  1280. **/
  1281. if (type) {
  1282. for(i = 0; i < max; i++) {
  1283. char sval[254];
  1284. safestrncpy(key, RFIFOCP(fd, cursor + 1), RFIFOB(fd, cursor));
  1285. cursor += RFIFOB(fd, cursor) + 1;
  1286. index = RFIFOL(fd, cursor);
  1287. cursor += 4;
  1288. safestrncpy(sval, RFIFOCP(fd, cursor + 1), RFIFOB(fd, cursor));
  1289. cursor += RFIFOB(fd, cursor) + 1;
  1290. set_reg_str( nullptr, sd, reference_uid( add_str( key ), index ), key, sval, nullptr );
  1291. }
  1292. /**
  1293. * Vessel!
  1294. *
  1295. * int type
  1296. * { keyLength(B), key(<keyLength>), index(L), value(L) }
  1297. **/
  1298. } else {
  1299. for(i = 0; i < max; i++) {
  1300. int64 ival;
  1301. safestrncpy(key, RFIFOCP(fd, cursor + 1), RFIFOB(fd, cursor));
  1302. cursor += RFIFOB(fd, cursor) + 1;
  1303. index = RFIFOL(fd, cursor);
  1304. cursor += 4;
  1305. ival = RFIFOQ(fd, cursor);
  1306. cursor += 8;
  1307. set_reg_num( nullptr, sd, reference_uid( add_str( key ), index ), key, ival, nullptr );
  1308. }
  1309. }
  1310. }
  1311. pc_set_reg_load(false);
  1312. if (flag && sd->vars_received&PRL_ACCG && sd->vars_received&PRL_ACCL && sd->vars_received&PRL_CHAR)
  1313. pc_reg_received(sd); //Received all registry values, execute init scripts and what-not. [Skotlex]
  1314. }
  1315. /**
  1316. * Received a guild storage
  1317. * @param fd : char-serv link
  1318. * @return 0=error, 1=sucess
  1319. */
  1320. int intif_parse_LoadGuildStorage(int fd)
  1321. {
  1322. struct s_storage *gstor;
  1323. map_session_data *sd;
  1324. int guild_id, flag;
  1325. guild_id = RFIFOL(fd,8);
  1326. flag = RFIFOL(fd,12);
  1327. if (guild_id <= 0)
  1328. return 0;
  1329. sd = map_id2sd( RFIFOL(fd,4) );
  1330. if (flag){ //If flag != 0, we attach a player and open the storage
  1331. if(sd == nullptr){
  1332. ShowError("intif_parse_LoadGuildStorage: user not found (AID: %d)\n",RFIFOL(fd,4));
  1333. return 0;
  1334. }
  1335. }
  1336. gstor = guild2storage(guild_id);
  1337. if (!gstor) {
  1338. ShowWarning("intif_parse_LoadGuildStorage: error guild_id %d not exist\n",guild_id);
  1339. return 0;
  1340. }
  1341. if (gstor->status) { // Already open.. lets ignore this update
  1342. ShowWarning("intif_parse_LoadGuildStorage: storage received for a client already open (User %d:%d)\n", flag?sd->status.account_id:1, flag?sd->status.char_id:1);
  1343. return 0;
  1344. }
  1345. if (gstor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit! [Skotlex]
  1346. ShowWarning("intif_parse_LoadGuildStorage: received storage for an already modified non-saved storage! (User %d:%d)\n", flag?sd->status.account_id:1, flag?sd->status.char_id:1);
  1347. return 0;
  1348. }
  1349. if (RFIFOW(fd,2)-13 != sizeof(struct s_storage)) {
  1350. ShowError("intif_parse_LoadGuildStorage: data size error %d %" PRIuPTR "\n",RFIFOW(fd,2)-13 , sizeof(struct s_storage));
  1351. gstor->status = false;
  1352. return 0;
  1353. }
  1354. memcpy(gstor,RFIFOP(fd,13),sizeof(struct s_storage));
  1355. if( flag )
  1356. storage_guild_storageopen(sd);
  1357. return 1;
  1358. }
  1359. /**
  1360. * ACK guild_storage saved
  1361. * @param fd : char-serv link
  1362. * @return 1
  1363. */
  1364. int intif_parse_SaveGuildStorage(int fd)
  1365. {
  1366. storage_guild_storagesaved(/*RFIFOL(fd,2), */RFIFOL(fd,6));
  1367. return 1;
  1368. }
  1369. /**
  1370. * ACK party creation
  1371. * @param fd : char-serv link
  1372. * @return 1
  1373. */
  1374. int intif_parse_PartyCreated(int fd)
  1375. {
  1376. if(battle_config.etc_log)
  1377. ShowInfo("intif: party created by account %d\n\n", RFIFOL(fd,2));
  1378. party_created(RFIFOL(fd,2), RFIFOL(fd,6),RFIFOB(fd,10),RFIFOL(fd,11), RFIFOCP(fd,15));
  1379. return 1;
  1380. }
  1381. /**
  1382. * Receive party info
  1383. * @param fd : char-serv link
  1384. * @return 0=error, 1=sucess
  1385. */
  1386. int intif_parse_PartyInfo(int fd)
  1387. {
  1388. if( RFIFOW(fd,2) == 12 ){
  1389. ShowWarning("intif: party noinfo (char_id=%d party_id=%d)\n", RFIFOL(fd,4), RFIFOL(fd,8));
  1390. party_recv_noinfo(RFIFOL(fd,8), RFIFOL(fd,4));
  1391. return 0;
  1392. }
  1393. if( RFIFOW(fd,2) != 8+sizeof(struct party) )
  1394. ShowError("intif: party info : data size error (char_id=%d party_id=%d packet_len=%d expected_len=%" PRIuPTR ")\n", RFIFOL(fd,4), RFIFOL(fd,8), RFIFOW(fd,2), 8+sizeof(struct party));
  1395. party_recv_info((struct party *)RFIFOP(fd,8), RFIFOL(fd,4));
  1396. return 1;
  1397. }
  1398. /**
  1399. * ACK adding party member
  1400. * @param fd : char-serv link
  1401. * @return 1
  1402. */
  1403. int intif_parse_PartyMemberAdded(int fd)
  1404. {
  1405. if(battle_config.etc_log)
  1406. ShowInfo("intif: party member added Party (%d), Account(%d), Char(%d)\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10));
  1407. party_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10), RFIFOB(fd, 14));
  1408. return 1;
  1409. }
  1410. /**
  1411. * ACK changing party option
  1412. * @param fd : char-serv link
  1413. * @return 1
  1414. */
  1415. int intif_parse_PartyOptionChanged(int fd)
  1416. {
  1417. party_optionchanged(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOW(fd,10),RFIFOW(fd,12),RFIFOB(fd,14));
  1418. return 1;
  1419. }
  1420. /**
  1421. * ACK member leaving party
  1422. * @param fd : char-serv link
  1423. * @return 1
  1424. */
  1425. int intif_parse_PartyMemberWithdraw(int fd)
  1426. {
  1427. if(battle_config.etc_log)
  1428. ShowInfo("intif: party member withdraw: Type(%d) Party(%d), Account(%d), Char(%d), Name(%s)\n",RFIFOB(fd,14+NAME_LENGTH),RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOCP(fd,14));
  1429. party_member_withdraw(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOCP(fd,14),(enum e_party_member_withdraw)RFIFOB(fd,14+NAME_LENGTH));
  1430. return 1;
  1431. }
  1432. /**
  1433. * ACK party break
  1434. * @param fd : char-serv link
  1435. * @return 1
  1436. */
  1437. int intif_parse_PartyBroken(int fd)
  1438. {
  1439. party_broken(RFIFOL(fd,2));
  1440. return 1;
  1441. }
  1442. /**
  1443. * ACK party on new map
  1444. * @param fd : char-serv link
  1445. * @return 1
  1446. */
  1447. int intif_parse_PartyMove(int fd)
  1448. {
  1449. party_recv_movemap( RFIFOL( fd, 2 ), RFIFOL( fd, 6 ), RFIFOL( fd, 10 ), RFIFOB( fd, 14 ), RFIFOW( fd, 15 ), RFIFOCP( fd, 17 ) );
  1450. return 1;
  1451. }
  1452. /**
  1453. * ACK party messages
  1454. * @param fd : char-serv link
  1455. * @return 1
  1456. */
  1457. int intif_parse_PartyMessage(int fd)
  1458. {
  1459. party_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),RFIFOCP(fd,12),RFIFOW(fd,2)-12);
  1460. return 1;
  1461. }
  1462. /**
  1463. * ACK guild creation
  1464. * @param fd : char-serv link
  1465. * @return 1
  1466. */
  1467. int intif_parse_GuildCreated(int fd)
  1468. {
  1469. guild_created(RFIFOL(fd,2),RFIFOL(fd,6));
  1470. return 1;
  1471. }
  1472. /**
  1473. * ACK guild infos
  1474. * @param fd : char-serv link
  1475. * @return 0=error, 1=sucess
  1476. */
  1477. int intif_parse_GuildInfo(int fd)
  1478. {
  1479. if(RFIFOW(fd,2) == 8) {
  1480. ShowWarning("intif: guild noinfo %d\n",RFIFOL(fd,4));
  1481. guild_recv_noinfo(RFIFOL(fd,4));
  1482. return 0;
  1483. }
  1484. if( RFIFOW(fd,2)!=sizeof(struct mmo_guild)+4 )
  1485. ShowError("intif: guild info : data size error Gid: %d recv size: %d Expected size: %" PRIuPTR "\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct mmo_guild)+4);
  1486. guild_recv_info(*(struct mmo_guild *)RFIFOP(fd,4));
  1487. return 1;
  1488. }
  1489. /**
  1490. * ACK adding guild member
  1491. * @param fd : char-serv link
  1492. * @return 1
  1493. */
  1494. int intif_parse_GuildMemberAdded(int fd)
  1495. {
  1496. if(battle_config.etc_log)
  1497. ShowInfo("intif: guild member added %d %d %d %d\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14));
  1498. guild_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14));
  1499. return 1;
  1500. }
  1501. /**
  1502. * ACK member leaving guild
  1503. * @param fd : char-serv link
  1504. * @return 1
  1505. */
  1506. int intif_parse_GuildMemberWithdraw(int fd)
  1507. {
  1508. guild_member_withdraw(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOCP(fd,55),RFIFOCP(fd,15));
  1509. return 1;
  1510. }
  1511. /**
  1512. * ACK guild member basic info
  1513. * @param fd : char-serv link
  1514. * @return 1
  1515. */
  1516. int intif_parse_GuildMemberInfoShort(int fd)
  1517. {
  1518. guild_recv_memberinfoshort(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOW(fd,15),RFIFOW(fd,17));
  1519. return 1;
  1520. }
  1521. /**
  1522. * ACK guild break
  1523. * @param fd : char-serv link
  1524. * @return 1
  1525. */
  1526. int intif_parse_GuildBroken(int fd)
  1527. {
  1528. guild_broken(RFIFOL(fd,2),RFIFOB(fd,6));
  1529. return 1;
  1530. }
  1531. /**
  1532. * basic guild info change notice
  1533. * 0x3839 <packet len>.w <guild id>.l <type>.w <data>.?b
  1534. * @param fd : char-serv link
  1535. * @return 0=error, 1=sucess
  1536. */
  1537. int intif_parse_GuildBasicInfoChanged(int fd)
  1538. {
  1539. //int len = RFIFOW(fd,2) - 10;
  1540. int guild_id = RFIFOL(fd,4);
  1541. int type = RFIFOW(fd,8);
  1542. //void* data = RFIFOP(fd,10);
  1543. auto g = guild_search(guild_id);
  1544. if( g == nullptr )
  1545. return 0;
  1546. switch(type) {
  1547. case GBI_EXP: g->guild.exp = RFIFOQ(fd,10); break;
  1548. case GBI_GUILDLV: g->guild.guild_lv = RFIFOW(fd,10); break;
  1549. case GBI_SKILLPOINT: g->guild.skill_point = RFIFOL(fd,10); break;
  1550. }
  1551. return 1;
  1552. }
  1553. /**
  1554. * guild member info change notice
  1555. * 0x383a <packet len>.w <guild id>.l <account id>.l <char id>.l <type>.w <data>.?b
  1556. * @param fd : char-serv link
  1557. * @return 0=error, 1=sucess
  1558. */
  1559. int intif_parse_GuildMemberInfoChanged(int fd)
  1560. {
  1561. //int len = RFIFOW(fd,2) - 18;
  1562. int guild_id = RFIFOL(fd,4);
  1563. uint32 account_id = RFIFOL(fd,8);
  1564. uint32 char_id = RFIFOL(fd,12);
  1565. int type = RFIFOW(fd,16);
  1566. //void* data = RFIFOP(fd,18);
  1567. int idx;
  1568. auto g = guild_search(guild_id);
  1569. if( g == nullptr )
  1570. return 0;
  1571. idx = guild_getindex(g->guild,account_id,char_id);
  1572. if( idx == -1 )
  1573. return 0;
  1574. switch( type ) {
  1575. case GMI_POSITION: g->guild.member[idx].position = RFIFOW(fd,18); guild_memberposition_changed(g->guild,idx,RFIFOW(fd,18)); break;
  1576. case GMI_EXP: g->guild.member[idx].exp = RFIFOQ(fd,18); break;
  1577. case GMI_HAIR: g->guild.member[idx].hair = RFIFOW(fd,18); break;
  1578. case GMI_HAIR_COLOR: g->guild.member[idx].hair_color = RFIFOW(fd,18); break;
  1579. case GMI_GENDER: g->guild.member[idx].gender = RFIFOW(fd,18); break;
  1580. case GMI_CLASS: g->guild.member[idx].class_ = RFIFOW(fd,18); break;
  1581. case GMI_LEVEL: g->guild.member[idx].lv = RFIFOW(fd,18); break;
  1582. }
  1583. return 1;
  1584. }
  1585. /**
  1586. * ACK change of guild title
  1587. * @param fd : char-serv link
  1588. * @return 1
  1589. */
  1590. int intif_parse_GuildPosition(int fd)
  1591. {
  1592. if( RFIFOW(fd,2)!=sizeof(struct guild_position)+12 )
  1593. ShowError("intif: guild info : data size error %d %d %" PRIuPTR "\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct guild_position)+12);
  1594. guild_position_changed(RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12));
  1595. return 1;
  1596. }
  1597. /**
  1598. * ACK change of guild skill update
  1599. * @param fd : char-serv link
  1600. * @return 1
  1601. */
  1602. int intif_parse_GuildSkillUp(int fd)
  1603. {
  1604. guild_skillupack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10));
  1605. return 1;
  1606. }
  1607. /**
  1608. * ACK change of guild relationship
  1609. * @param fd : char-serv link
  1610. * @return 1
  1611. */
  1612. int intif_parse_GuildAlliance(int fd)
  1613. {
  1614. guild_allianceack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14),RFIFOB(fd,18),RFIFOCP(fd,19),RFIFOCP(fd,43));
  1615. return 1;
  1616. }
  1617. /**
  1618. * ACK change of guild notice
  1619. * @param fd : char-serv link
  1620. * @return 1
  1621. */
  1622. int intif_parse_GuildNotice(int fd)
  1623. {
  1624. guild_notice_changed(RFIFOL(fd,2),RFIFOCP(fd,6),RFIFOCP(fd,66));
  1625. return 1;
  1626. }
  1627. /**
  1628. * ACK change of guild emblem
  1629. * @param fd : char-serv link
  1630. * @return 1
  1631. */
  1632. int intif_parse_GuildEmblem(int fd)
  1633. {
  1634. guild_emblem_changed(RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8), RFIFOCP(fd,12));
  1635. return 1;
  1636. }
  1637. int intif_parse_GuildEmblemVersionChanged(int fd)
  1638. {
  1639. guild_emblem_changed(0, RFIFOL(fd, 2), RFIFOL(fd, 6), nullptr); // Doesn't need emblem length and data
  1640. return 1;
  1641. }
  1642. /**
  1643. * ACK guild message
  1644. * @param fd : char-serv link
  1645. * @return 1
  1646. */
  1647. int intif_parse_GuildMessage(int fd)
  1648. {
  1649. guild_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),RFIFOCP(fd,12),RFIFOW(fd,2)-12);
  1650. return 1;
  1651. }
  1652. /**
  1653. * Reply guild castle data request
  1654. * @param fd : char-serv link
  1655. * @return ?
  1656. */
  1657. int intif_parse_GuildCastleDataLoad(int fd)
  1658. {
  1659. return guild_castledataloadack(RFIFOW(fd,2), (struct guild_castle *)RFIFOP(fd,4));
  1660. }
  1661. /**
  1662. * ACK change of guildmaster
  1663. * @param fd : char-serv link
  1664. * @return ?
  1665. */
  1666. int intif_parse_GuildMasterChanged(int fd)
  1667. {
  1668. return guild_gm_changed(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14));
  1669. }
  1670. /**
  1671. * Request pet creation
  1672. * @param fd : char-serv link
  1673. * @return 1
  1674. */
  1675. int intif_parse_CreatePet(int fd)
  1676. {
  1677. pet_get_egg(RFIFOL(fd,2),RFIFOW(fd,6),RFIFOL(fd,8));
  1678. return 1;
  1679. }
  1680. /**
  1681. * ACK pet data
  1682. * @param fd : char-serv link
  1683. * @return 1
  1684. */
  1685. int intif_parse_RecvPetData(int fd)
  1686. {
  1687. struct s_pet p;
  1688. int len;
  1689. len=RFIFOW(fd,2);
  1690. if(sizeof(struct s_pet)!=len-9) {
  1691. if(battle_config.etc_log)
  1692. ShowError("intif: pet data: data size error %" PRIuPTR " %d\n",sizeof(struct s_pet),len-9);
  1693. }
  1694. else{
  1695. memcpy(&p,RFIFOP(fd,9),sizeof(struct s_pet));
  1696. pet_recv_petdata(RFIFOL(fd,4),&p,RFIFOB(fd,8));
  1697. }
  1698. return 1;
  1699. }
  1700. /**
  1701. * ACK pet save data
  1702. * @param fd : char-serv link
  1703. * @return 1
  1704. */
  1705. int intif_parse_SavePetOk(int fd)
  1706. {
  1707. if(RFIFOB(fd,6) == 1)
  1708. ShowError("pet data save failure\n");
  1709. return 1;
  1710. }
  1711. /**
  1712. * ACK deleting pet
  1713. * @param fd : char-serv link
  1714. * @return 1
  1715. */
  1716. int intif_parse_DeletePetOk(int fd)
  1717. {
  1718. if(RFIFOB(fd,2) == 1)
  1719. ShowError("pet data delete failure\n");
  1720. return 1;
  1721. }
  1722. /**
  1723. * ACK changing name resquest, players,pets,hommon
  1724. * @param fd : char-serv link
  1725. * @return 0=error, 1=sucess
  1726. */
  1727. int intif_parse_ChangeNameOk(int fd)
  1728. {
  1729. map_session_data *sd = nullptr;
  1730. if((sd=map_id2sd(RFIFOL(fd,2)))==nullptr ||
  1731. sd->status.char_id != RFIFOL(fd,6))
  1732. return 0;
  1733. switch (RFIFOB(fd,10)) {
  1734. case 0: //Players [NOT SUPPORTED YET]
  1735. break;
  1736. case 1: //Pets
  1737. pet_change_name_ack(sd, RFIFOCP(fd,12), RFIFOB(fd,11));
  1738. break;
  1739. case 2: //Hom
  1740. hom_change_name_ack(sd, RFIFOCP(fd,12), RFIFOB(fd,11));
  1741. break;
  1742. }
  1743. return 1;
  1744. }
  1745. //----------------------------------------------------------------
  1746. // Homunculus recv packets [albator]
  1747. /**
  1748. * ACK Homunculus creation
  1749. * @param fd : char-serv link
  1750. * @return 0=error, 1=sucess
  1751. */
  1752. int intif_parse_CreateHomunculus(int fd)
  1753. {
  1754. int len;
  1755. len=RFIFOW(fd,2)-9;
  1756. if(sizeof(struct s_homunculus)!=len) {
  1757. if(battle_config.etc_log)
  1758. ShowError("intif: create homun data: data size error %" PRIuPTR " != %d\n",sizeof(struct s_homunculus),len);
  1759. return 0;
  1760. }
  1761. hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8)) ;
  1762. return 1;
  1763. }
  1764. /**
  1765. * ACK homunculus get data (load homun from char)
  1766. * @param fd : char-serv link
  1767. * @return 0=error, 1=sucess
  1768. */
  1769. int intif_parse_RecvHomunculusData(int fd)
  1770. {
  1771. int len;
  1772. len=RFIFOW(fd,2)-9;
  1773. if(sizeof(struct s_homunculus)!=len) {
  1774. if(battle_config.etc_log)
  1775. ShowError("intif: homun data: data size error %" PRIuPTR " %d\n",sizeof(struct s_homunculus),len);
  1776. return 0;
  1777. }
  1778. hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8));
  1779. return 1;
  1780. }
  1781. /**
  1782. * ACK save Homun
  1783. * @param fd : char-serv link
  1784. * @return 1
  1785. */
  1786. int intif_parse_SaveHomunculusOk(int fd)
  1787. {
  1788. if(RFIFOB(fd,6) != 1)
  1789. ShowError("homunculus data save failure for account %d\n", RFIFOL(fd,2));
  1790. return 1;
  1791. }
  1792. /**
  1793. * ACK delete Homun
  1794. * @param fd : char-serv link
  1795. * @return 1
  1796. */
  1797. int intif_parse_DeleteHomunculusOk(int fd)
  1798. {
  1799. if(RFIFOB(fd,2) != 1)
  1800. ShowError("Homunculus data delete failure\n");
  1801. return 1;
  1802. }
  1803. /**************************************
  1804. QUESTLOG SYSTEM FUNCTIONS
  1805. ***************************************/
  1806. /**
  1807. * Requests a character's quest log entries to the inter server.
  1808. * @param sd Character's data
  1809. */
  1810. void intif_request_questlog(map_session_data *sd)
  1811. {
  1812. if (CheckForCharServer())
  1813. return;
  1814. WFIFOHEAD(inter_fd,6);
  1815. WFIFOW(inter_fd,0) = 0x3060;
  1816. WFIFOL(inter_fd,2) = sd->status.char_id;
  1817. WFIFOSET(inter_fd,6);
  1818. }
  1819. /**
  1820. * Receive a char quest log
  1821. * @param fd : char-serv link
  1822. */
  1823. void intif_parse_questlog(int fd)
  1824. {
  1825. uint32 char_id = RFIFOL(fd,4), num_received = (RFIFOW(fd,2) - 8) / sizeof(struct quest);
  1826. TBL_PC *sd = map_charid2sd(char_id);
  1827. if(!sd) // User not online anymore
  1828. return;
  1829. sd->num_quests = sd->avail_quests = 0;
  1830. if(num_received == 0) {
  1831. if(sd->quest_log) {
  1832. aFree(sd->quest_log);
  1833. sd->quest_log = nullptr;
  1834. }
  1835. } else {
  1836. struct quest *received = (struct quest *)RFIFOP(fd,8);
  1837. int k = num_received;
  1838. if(sd->quest_log)
  1839. RECREATE(sd->quest_log, struct quest, num_received);
  1840. else
  1841. CREATE(sd->quest_log, struct quest, num_received);
  1842. for(int i = 0; i < num_received; i++) {
  1843. if(!quest_search(received[i].quest_id)) {
  1844. ShowError("intif_parse_QuestLog: quest %d not found in DB.\n", received[i].quest_id);
  1845. continue;
  1846. }
  1847. if(received[i].state != Q_COMPLETE) // Insert at the beginning
  1848. memcpy(&sd->quest_log[sd->avail_quests++], &received[i], sizeof(struct quest));
  1849. else // Insert at the end
  1850. memcpy(&sd->quest_log[--k], &received[i], sizeof(struct quest));
  1851. sd->num_quests++;
  1852. }
  1853. if(sd->avail_quests < k) {
  1854. // sd->avail_quests and k didn't meet in the middle: some entries were skipped
  1855. if(k < num_received) // Move the entries at the end to fill the gap
  1856. memmove(&sd->quest_log[k], &sd->quest_log[sd->avail_quests], sizeof(struct quest) * (num_received - k));
  1857. sd->quest_log = (struct quest *)aRealloc(sd->quest_log, sizeof(struct quest) * sd->num_quests);
  1858. }
  1859. }
  1860. quest_pc_login(sd);
  1861. }
  1862. /**
  1863. * Parses the quest log save ack for a character from the inter server.
  1864. * Received in reply to the requests made by intif_quest_save.
  1865. * @see intif_parse
  1866. * @param fd : char-serv link
  1867. */
  1868. void intif_parse_questsave(int fd)
  1869. {
  1870. int cid = RFIFOL(fd, 2);
  1871. TBL_PC *sd = map_id2sd(cid);
  1872. if( !RFIFOB(fd, 6) )
  1873. ShowError("intif_parse_questsave: Failed to save quest(s) for character %d!\n", cid);
  1874. else if( sd )
  1875. sd->save_quest = false;
  1876. }
  1877. /**
  1878. * Requests to the inter server to save a character's quest log entries.
  1879. * @param sd Character's data
  1880. * @return 0 in case of success, nonzero otherwise
  1881. */
  1882. int intif_quest_save(map_session_data *sd)
  1883. {
  1884. int len = sizeof(struct quest) * sd->num_quests + 8;
  1885. if(CheckForCharServer())
  1886. return 0;
  1887. WFIFOHEAD(inter_fd, len);
  1888. WFIFOW(inter_fd,0) = 0x3061;
  1889. WFIFOW(inter_fd,2) = len;
  1890. WFIFOL(inter_fd,4) = sd->status.char_id;
  1891. if( sd->num_quests )
  1892. memcpy(WFIFOP(inter_fd,8), sd->quest_log, sizeof(struct quest)*sd->num_quests);
  1893. WFIFOSET(inter_fd, len);
  1894. return 1;
  1895. }
  1896. /*==========================================
  1897. * Achievement System
  1898. *------------------------------------------*/
  1899. /**
  1900. * Requests a character's achievement log entries to the inter server.
  1901. * @param char_id: Character ID
  1902. */
  1903. void intif_request_achievements(uint32 char_id)
  1904. {
  1905. if (CheckForCharServer())
  1906. return;
  1907. WFIFOHEAD(inter_fd, 6);
  1908. WFIFOW(inter_fd, 0) = 0x3062;
  1909. WFIFOL(inter_fd, 2) = char_id;
  1910. WFIFOSET(inter_fd, 6);
  1911. }
  1912. /**
  1913. * Receive a character's achievements
  1914. * @param fd: char-serv link
  1915. */
  1916. void intif_parse_achievements(int fd)
  1917. {
  1918. uint32 char_id = RFIFOL(fd, 4), num_received = (RFIFOW(fd, 2) - 8) / sizeof(struct achievement);
  1919. map_session_data *sd = map_charid2sd(char_id);
  1920. if (!sd) // User not online anymore
  1921. return;
  1922. if (num_received == 0) {
  1923. if (sd->achievement_data.achievements) {
  1924. aFree(sd->achievement_data.achievements);
  1925. sd->achievement_data.achievements = nullptr;
  1926. sd->achievement_data.incompleteCount = 0;
  1927. sd->achievement_data.count = 0;
  1928. }
  1929. } else {
  1930. struct achievement *received = (struct achievement *)RFIFOP(fd, 8);
  1931. int i, k = num_received;
  1932. if (sd->achievement_data.achievements)
  1933. RECREATE(sd->achievement_data.achievements, struct achievement, num_received);
  1934. else
  1935. CREATE(sd->achievement_data.achievements, struct achievement, num_received);
  1936. for (i = 0; i < num_received; i++) {
  1937. std::shared_ptr<s_achievement_db> adb = achievement_db.find( received[i].achievement_id );
  1938. if (!adb) {
  1939. ShowError("intif_parse_achievements: Achievement %d not found in achievement_db.\n", received[i].achievement_id);
  1940. continue;
  1941. }
  1942. received[i].score = adb->score;
  1943. if (received[i].completed == 0) // Insert at the beginning
  1944. memcpy(&sd->achievement_data.achievements[sd->achievement_data.incompleteCount++], &received[i], sizeof(struct achievement));
  1945. else // Insert at the end
  1946. memcpy(&sd->achievement_data.achievements[--k], &received[i], sizeof(struct achievement));
  1947. sd->achievement_data.count++;
  1948. }
  1949. if (sd->achievement_data.incompleteCount < k) {
  1950. // sd->achievement_data.incompleteCount and k didn't meet in the middle: some entries were skipped
  1951. if (k < num_received) // Move the entries at the end to fill the gap
  1952. memmove(&sd->achievement_data.achievements[k], &sd->achievement_data.achievements[sd->achievement_data.incompleteCount], sizeof(struct achievement) * (num_received - k));
  1953. sd->achievement_data.achievements = (struct achievement *)aRealloc(sd->achievement_data.achievements, sizeof(struct achievement) * sd->achievement_data.count);
  1954. }
  1955. }
  1956. // Check all conditions and counters on login
  1957. for( int group = AG_NONE + 1; group < AG_MAX; group++ ){
  1958. achievement_update_objective( sd, static_cast<e_achievement_group>( group ), 0 );
  1959. }
  1960. achievement_level(sd, false); // Calculate level info but don't give any AG_GOAL_ACHIEVE achievements
  1961. achievement_get_titles(sd->status.char_id); // Populate the title list for completed achievements
  1962. clif_achievement_update(sd, nullptr, 0);
  1963. clif_achievement_list_all(sd);
  1964. }
  1965. /**
  1966. * Parses the achievement log save ack for a character from the inter server.
  1967. * Received in reply to the requests made by intif_achievement_save.
  1968. * @see intif_parse
  1969. * @param fd : char-serv link
  1970. */
  1971. void intif_parse_achievementsave(int fd)
  1972. {
  1973. int cid = RFIFOL(fd, 2);
  1974. map_session_data *sd = map_charid2sd(cid);
  1975. if (!sd) // User not online anymore
  1976. return;
  1977. if (!RFIFOB(fd, 6))
  1978. ShowError("intif_parse_achievementsave: Failed to save achievement(s) for character %s (%d)!\n", sd->status.name, cid);
  1979. }
  1980. /**
  1981. * Requests to the inter server to save a character's achievement log entries.
  1982. * @param sd: Character's data
  1983. * @return 0 in case of success, nonzero otherwise
  1984. */
  1985. int intif_achievement_save(map_session_data *sd)
  1986. {
  1987. int len = sizeof(struct achievement) * sd->achievement_data.count + 8;
  1988. if (CheckForCharServer())
  1989. return 0;
  1990. WFIFOHEAD(inter_fd, len);
  1991. WFIFOW(inter_fd, 0) = 0x3063;
  1992. WFIFOW(inter_fd, 2) = len;
  1993. WFIFOL(inter_fd, 4) = sd->status.char_id;
  1994. if (sd->achievement_data.count)
  1995. memcpy(WFIFOP(inter_fd, 8), sd->achievement_data.achievements, sizeof(struct achievement) * sd->achievement_data.count);
  1996. WFIFOSET(inter_fd, len);
  1997. sd->achievement_data.save = false;
  1998. return 1;
  1999. }
  2000. /**
  2001. * Parses the reply of the reward claiming for a achievement from the inter server.
  2002. * @see intif_parse
  2003. * @param fd : char-serv link
  2004. */
  2005. void intif_parse_achievementreward(int fd){
  2006. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2007. // User not online anymore
  2008. if( !sd ){
  2009. return;
  2010. }
  2011. achievement_get_reward(sd, RFIFOL(fd, 6), RFIFOL(fd, 10));
  2012. }
  2013. /**
  2014. * Request the achievement rewards from the inter server.
  2015. */
  2016. int intif_achievement_reward(map_session_data *sd, struct s_achievement_db *adb){
  2017. if( CheckForCharServer() ){
  2018. return 0;
  2019. }
  2020. WFIFOHEAD(inter_fd, 16+NAME_LENGTH+ACHIEVEMENT_NAME_LENGTH);
  2021. WFIFOW(inter_fd, 0) = 0x3064;
  2022. WFIFOL(inter_fd, 2) = sd->status.char_id;
  2023. WFIFOL(inter_fd, 6) = adb->achievement_id;
  2024. WFIFOL(inter_fd, 10) = adb->rewards.nameid;
  2025. WFIFOW(inter_fd, 14) = adb->rewards.amount;
  2026. safestrncpy(WFIFOCP(inter_fd, 16), sd->status.name, NAME_LENGTH);
  2027. safestrncpy(WFIFOCP(inter_fd, 16+NAME_LENGTH), adb->name.c_str(), ACHIEVEMENT_NAME_LENGTH);
  2028. WFIFOSET(inter_fd, 16+NAME_LENGTH+ACHIEVEMENT_NAME_LENGTH);
  2029. return 1;
  2030. }
  2031. /*==========================================
  2032. * MAIL SYSTEM
  2033. * By Zephyrus
  2034. *==========================================*/
  2035. /**
  2036. * Request to update inbox
  2037. * @param char_id : Player ID linked with box
  2038. * @param flag 0 Update Inbox | 1 OpenMail
  2039. * @return 0=error, 1=msg_sent
  2040. */
  2041. int intif_Mail_requestinbox(uint32 char_id, unsigned char flag, enum mail_inbox_type type)
  2042. {
  2043. if (CheckForCharServer())
  2044. return 0;
  2045. WFIFOHEAD(inter_fd,8);
  2046. WFIFOW(inter_fd,0) = 0x3048;
  2047. WFIFOL(inter_fd,2) = char_id;
  2048. WFIFOB(inter_fd,6) = flag;
  2049. WFIFOB(inter_fd,7) = type;
  2050. WFIFOSET(inter_fd,8);
  2051. return 1;
  2052. }
  2053. /**
  2054. * Map-serv received a mail from char-serv
  2055. * (inform user of new mail)
  2056. * @param fd : char-serv link
  2057. * @return 0=msg fail, 1=msg received
  2058. */
  2059. int intif_parse_Mail_inboxreceived(int fd)
  2060. {
  2061. map_session_data *sd;
  2062. unsigned char flag = RFIFOB(fd,8);
  2063. sd = map_charid2sd(RFIFOL(fd,4));
  2064. if (sd == nullptr)
  2065. {
  2066. ShowError("intif_parse_Mail_inboxreceived: char not found %d\n",RFIFOL(fd,4));
  2067. return 0;
  2068. }
  2069. if (RFIFOW(fd,2) - 10 != sizeof(struct mail_data))
  2070. {
  2071. ShowError("intif_parse_Mail_inboxreceived: data size error %d %" PRIuPTR "\n", RFIFOW(fd,2) - 10, sizeof(struct mail_data));
  2072. return 0;
  2073. }
  2074. //FIXME: this operation is not safe [ultramage]
  2075. memcpy(&sd->mail.inbox, RFIFOP(fd,10), sizeof(struct mail_data));
  2076. sd->mail.changed = false; // cache is now in sync
  2077. #if PACKETVER >= 20150513
  2078. // Refresh top right icon
  2079. clif_Mail_new(sd, 0, nullptr, nullptr);
  2080. #endif
  2081. if (flag){
  2082. clif_Mail_refreshinbox(sd,static_cast<mail_inbox_type>(RFIFOB(fd,9)),0);
  2083. }else if( battle_config.mail_show_status && ( battle_config.mail_show_status == 1 || sd->mail.inbox.unread ) )
  2084. {
  2085. char output[128];
  2086. sprintf(output, msg_txt(sd,510), sd->mail.inbox.unchecked, sd->mail.inbox.unread + sd->mail.inbox.unchecked);
  2087. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  2088. }
  2089. return 1;
  2090. }
  2091. /**
  2092. * Notify char-serv that the mail was read
  2093. * @param mail_id : mail reed
  2094. * @return 0=error, 1=msg sent
  2095. */
  2096. int intif_Mail_read(int mail_id)
  2097. {
  2098. if (CheckForCharServer())
  2099. return 0;
  2100. WFIFOHEAD(inter_fd,6);
  2101. WFIFOW(inter_fd,0) = 0x3049;
  2102. WFIFOL(inter_fd,2) = mail_id;
  2103. WFIFOSET(inter_fd,6);
  2104. return 1;
  2105. }
  2106. /**
  2107. * Request the mail attachment for mail
  2108. * @param char_id : Player requesting
  2109. * @param mail_id : Mail identification
  2110. * @return 0=error, 1=msg sent
  2111. */
  2112. bool intif_mail_getattach( map_session_data* sd, struct mail_message *msg, enum mail_attachment_type type){
  2113. if (CheckForCharServer())
  2114. return false;
  2115. WFIFOHEAD(inter_fd,11);
  2116. WFIFOW(inter_fd,0) = 0x304a;
  2117. WFIFOL(inter_fd,2) = sd->status.char_id;
  2118. WFIFOL(inter_fd,6) = msg->id;
  2119. WFIFOB(inter_fd,10) = (uint8)type;
  2120. WFIFOSET(inter_fd, 11);
  2121. return true;
  2122. }
  2123. /**
  2124. * Receive the attachment from char-serv of a mail
  2125. * @param fd : char-serv link
  2126. * @return 0=error, 1=sucess
  2127. */
  2128. int intif_parse_Mail_getattach(int fd)
  2129. {
  2130. map_session_data *sd;
  2131. struct item item[MAIL_MAX_ITEM];
  2132. int i, mail_id, zeny;
  2133. if (RFIFOW(fd, 2) - 16 != sizeof(struct item)*MAIL_MAX_ITEM)
  2134. {
  2135. ShowError("intif_parse_Mail_getattach: data size error %d %" PRIuPTR "\n", RFIFOW(fd, 2) - 16, sizeof(struct item));
  2136. return 0;
  2137. }
  2138. sd = map_charid2sd( RFIFOL(fd,4) );
  2139. if (sd == nullptr)
  2140. {
  2141. ShowError("intif_parse_Mail_getattach: char not found %d\n",RFIFOL(fd,4));
  2142. return 0;
  2143. }
  2144. mail_id = RFIFOL(fd, 8);
  2145. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  2146. if (i == MAIL_MAX_INBOX)
  2147. return 0;
  2148. zeny = RFIFOL(fd, 12);
  2149. memcpy(item, RFIFOP(fd,16), sizeof(struct item)*MAIL_MAX_ITEM);
  2150. mail_getattachment(sd, &sd->mail.inbox.msg[i], zeny, item);
  2151. return 1;
  2152. }
  2153. /**
  2154. * request to delete a mail
  2155. * @param char_id : player requesting
  2156. * @param mail_id : mail to delete
  2157. * @return 0=error, 1=msg sent
  2158. */
  2159. int intif_Mail_delete(uint32 char_id, int mail_id)
  2160. {
  2161. if (CheckForCharServer())
  2162. return 0;
  2163. WFIFOHEAD(inter_fd,10);
  2164. WFIFOW(inter_fd,0) = 0x304b;
  2165. WFIFOL(inter_fd,2) = char_id;
  2166. WFIFOL(inter_fd,6) = mail_id;
  2167. WFIFOSET(inter_fd,10);
  2168. return 1;
  2169. }
  2170. /**
  2171. * Ack of a mail deletion
  2172. * @param fd : char-serv link
  2173. * @return 0=error, 1=success
  2174. */
  2175. int intif_parse_Mail_delete(int fd)
  2176. {
  2177. uint32 char_id = RFIFOL(fd,2);
  2178. int mail_id = RFIFOL(fd,6);
  2179. bool failed = RFIFOB(fd,10) > 0;
  2180. map_session_data *sd = map_charid2sd(char_id);
  2181. if (sd == nullptr)
  2182. {
  2183. ShowError("intif_parse_Mail_delete: char not found %d\n", char_id);
  2184. return 0;
  2185. }
  2186. if (!failed)
  2187. {
  2188. int i;
  2189. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  2190. if( i < MAIL_MAX_INBOX )
  2191. {
  2192. enum mail_inbox_type type = sd->mail.inbox.msg[i].type;
  2193. clif_mail_delete(sd, &sd->mail.inbox.msg[i], !failed);
  2194. memset(&sd->mail.inbox.msg[i], 0, sizeof(struct mail_message));
  2195. sd->mail.inbox.amount--;
  2196. if( sd->mail.inbox.full || sd->mail.inbox.unchecked > 0 )
  2197. intif_Mail_requestinbox(sd->status.char_id, 1, type); // Free space is available for new mails
  2198. }
  2199. }
  2200. return 1;
  2201. }
  2202. /*------------------------------------------
  2203. * Return Message
  2204. *------------------------------------------*/
  2205. /**
  2206. * Request to return a mail to his sender
  2207. * @param char_id : player asking to return
  2208. * @param mail_id : mail to return
  2209. * @return 0=error, 1=msg sent
  2210. */
  2211. int intif_Mail_return(uint32 char_id, int mail_id)
  2212. {
  2213. if (CheckForCharServer())
  2214. return 0;
  2215. WFIFOHEAD(inter_fd,10);
  2216. WFIFOW(inter_fd,0) = 0x304c;
  2217. WFIFOL(inter_fd,2) = char_id;
  2218. WFIFOL(inter_fd,6) = mail_id;
  2219. WFIFOSET(inter_fd,10);
  2220. return 1;
  2221. }
  2222. /**
  2223. * Received a returned mail
  2224. * @param fd
  2225. * @return
  2226. */
  2227. int intif_parse_Mail_return(int fd)
  2228. {
  2229. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2230. int mail_id = RFIFOL(fd,6);
  2231. short fail = RFIFOB(fd,10);
  2232. if( sd == nullptr )
  2233. {
  2234. ShowError("intif_parse_Mail_return: char not found %d\n",RFIFOL(fd,2));
  2235. return 1;
  2236. }
  2237. if( !fail )
  2238. {
  2239. int i;
  2240. ARR_FIND(0, MAIL_MAX_INBOX, i, sd->mail.inbox.msg[i].id == mail_id);
  2241. if( i < MAIL_MAX_INBOX )
  2242. {
  2243. enum mail_inbox_type type = sd->mail.inbox.msg[i].type;
  2244. memset(&sd->mail.inbox.msg[i], 0, sizeof(struct mail_message));
  2245. sd->mail.inbox.amount--;
  2246. if( sd->mail.inbox.full )
  2247. intif_Mail_requestinbox(sd->status.char_id, 1, type); // Free space is available for new mails
  2248. }
  2249. }
  2250. clif_Mail_return(sd->fd, mail_id, fail);
  2251. return 1;
  2252. }
  2253. /*------------------------------------------
  2254. * Send Mail
  2255. *------------------------------------------*/
  2256. /**
  2257. * Request to send a mail
  2258. * @param account_id
  2259. * @param msg : mail struct
  2260. * @return 0=error, 1=msg sent
  2261. */
  2262. int intif_Mail_send(uint32 account_id, struct mail_message *msg)
  2263. {
  2264. int len = sizeof(struct mail_message) + 8;
  2265. if (CheckForCharServer())
  2266. return 0;
  2267. WFIFOHEAD(inter_fd,len);
  2268. WFIFOW(inter_fd,0) = 0x304d;
  2269. WFIFOW(inter_fd,2) = len;
  2270. WFIFOL(inter_fd,4) = account_id;
  2271. memcpy(WFIFOP(inter_fd,8), msg, sizeof(struct mail_message));
  2272. WFIFOSET(inter_fd,len);
  2273. return 1;
  2274. }
  2275. /**
  2276. * Received the ack of a mail send request
  2277. * @param fd L char-serv link
  2278. */
  2279. static void intif_parse_Mail_send(int fd)
  2280. {
  2281. struct mail_message msg;
  2282. map_session_data *sd;
  2283. bool fail;
  2284. if( RFIFOW(fd,2) - 4 != sizeof(struct mail_message) )
  2285. {
  2286. ShowError("intif_parse_Mail_send: data size error %d %" PRIuPTR "\n", RFIFOW(fd,2) - 4, sizeof(struct mail_message));
  2287. return;
  2288. }
  2289. memcpy(&msg, RFIFOP(fd,4), sizeof(struct mail_message));
  2290. fail = (msg.id == 0);
  2291. // notify sender
  2292. sd = map_charid2sd(msg.send_id);
  2293. if( sd != nullptr )
  2294. {
  2295. if( fail )
  2296. mail_deliveryfail(sd, &msg);
  2297. else
  2298. {
  2299. clif_Mail_send(sd, WRITE_MAIL_SUCCESS);
  2300. if( save_settings&CHARSAVE_MAIL )
  2301. chrif_save(sd, CSAVE_INVENTORY);
  2302. }
  2303. }
  2304. }
  2305. /**
  2306. * Received a new mail notification
  2307. * @param fd : char-link serv
  2308. */
  2309. static void intif_parse_Mail_new(int fd)
  2310. {
  2311. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2312. int mail_id = RFIFOL(fd,6);
  2313. const char* sender_name = RFIFOCP(fd,10);
  2314. const char* title = RFIFOCP(fd,34);
  2315. if( sd == nullptr )
  2316. return;
  2317. sd->mail.changed = true;
  2318. sd->mail.inbox.unread++;
  2319. clif_Mail_new(sd, mail_id, sender_name, title);
  2320. #if PACKETVER >= 20150513
  2321. // Make sure the window gets refreshed when its open
  2322. intif_Mail_requestinbox(sd->status.char_id, 1, static_cast<mail_inbox_type>(RFIFOB(fd,74)));
  2323. #endif
  2324. }
  2325. static void intif_parse_Mail_receiver( int fd ){
  2326. map_session_data *sd;
  2327. sd = map_charid2sd( RFIFOL( fd, 2 ) );
  2328. // Only f the player is online
  2329. if( sd ){
  2330. clif_Mail_Receiver_Ack( sd, RFIFOL( fd, 6 ), RFIFOW( fd, 10 ), RFIFOW( fd, 12 ), RFIFOCP( fd, 14 ) );
  2331. sd->mail.dest_id = RFIFOL(fd, 6);
  2332. }
  2333. }
  2334. bool intif_mail_checkreceiver( map_session_data* sd, char* name ){
  2335. map_session_data *tsd;
  2336. tsd = map_nick2sd( name, false );
  2337. sd->mail.dest_id = 0;
  2338. // If the target player is online on this map-server
  2339. if( tsd != nullptr ){
  2340. clif_Mail_Receiver_Ack( sd, tsd->status.char_id, tsd->status.class_, tsd->status.base_level, name );
  2341. sd->mail.dest_id = tsd->status.char_id;
  2342. return true;
  2343. }
  2344. if( CheckForCharServer() )
  2345. return false;
  2346. WFIFOHEAD(inter_fd, 6 + NAME_LENGTH);
  2347. WFIFOW(inter_fd, 0) = 0x304e;
  2348. WFIFOL(inter_fd, 2) = sd->status.char_id;
  2349. safestrncpy(WFIFOCP(inter_fd, 6), name, NAME_LENGTH);
  2350. WFIFOSET(inter_fd, 6 + NAME_LENGTH);
  2351. return true;
  2352. }
  2353. /*==========================================
  2354. * AUCTION SYSTEM
  2355. * By Zephyrus
  2356. *==========================================*/
  2357. /**
  2358. * Request a list of auction matching criteria
  2359. * @param char_id : player searching auction
  2360. * @param type : see clif_parse_Auction_search type
  2361. * @param price : min price for search
  2362. * @param searchtext : contain item name
  2363. * @param page : in case of huge result list display 5 entry per page, (kinda suck that we redo the request atm)
  2364. * @return 0=error, 1=msg sent
  2365. */
  2366. int intif_Auction_requestlist(uint32 char_id, short type, int price, const char* searchtext, short page)
  2367. {
  2368. int len = NAME_LENGTH + 16;
  2369. if( CheckForCharServer() )
  2370. return 0;
  2371. WFIFOHEAD(inter_fd,len);
  2372. WFIFOW(inter_fd,0) = 0x3050;
  2373. WFIFOW(inter_fd,2) = len;
  2374. WFIFOL(inter_fd,4) = char_id;
  2375. WFIFOW(inter_fd,8) = type;
  2376. WFIFOL(inter_fd,10) = price; //min price for search
  2377. WFIFOW(inter_fd,14) = page;
  2378. safestrncpy(WFIFOCP(inter_fd,16), searchtext, NAME_LENGTH);
  2379. WFIFOSET(inter_fd,len);
  2380. return 1;
  2381. }
  2382. /**
  2383. * Received a list of auction, display them
  2384. * @param fd : Char-serv link
  2385. */
  2386. static void intif_parse_Auction_results(int fd)
  2387. {
  2388. map_session_data *sd = map_charid2sd(RFIFOL(fd,4));
  2389. short count = RFIFOW(fd,8);
  2390. short pages = RFIFOW(fd,10);
  2391. uint8* data = RFIFOP(fd,12);
  2392. if( sd == nullptr )
  2393. return;
  2394. clif_Auction_results(sd, count, pages, data);
  2395. }
  2396. /**
  2397. * Register an auction to char-serv
  2398. * @param auction : tmp auction to register
  2399. * @return 0=error, 1=msg sent
  2400. */
  2401. int intif_Auction_register(struct auction_data *auction)
  2402. {
  2403. int len = sizeof(struct auction_data) + 4;
  2404. if( CheckForCharServer() )
  2405. return 0;
  2406. WFIFOHEAD(inter_fd,len);
  2407. WFIFOW(inter_fd,0) = 0x3051;
  2408. WFIFOW(inter_fd,2) = len;
  2409. memcpy(WFIFOP(inter_fd,4), auction, sizeof(struct auction_data));
  2410. WFIFOSET(inter_fd,len);
  2411. return 1;
  2412. }
  2413. /**
  2414. * Receive a auction available from char-serv
  2415. * @param fd : char-serv link
  2416. */
  2417. static void intif_parse_Auction_register(int fd)
  2418. {
  2419. map_session_data *sd;
  2420. struct auction_data auction;
  2421. if( RFIFOW(fd,2) - 4 != sizeof(struct auction_data) )
  2422. {
  2423. ShowError("intif_parse_Auction_register: data size error %d %" PRIuPTR "\n", RFIFOW(fd,2) - 4, sizeof(struct auction_data));
  2424. return;
  2425. }
  2426. memcpy(&auction, RFIFOP(fd,4), sizeof(struct auction_data));
  2427. if( (sd = map_charid2sd(auction.seller_id)) == nullptr )
  2428. return;
  2429. if( auction.auction_id > 0 )
  2430. {
  2431. clif_Auction_message(sd->fd, 1); // Confirmation Packet ??
  2432. if( save_settings&CHARSAVE_AUCTION )
  2433. chrif_save(sd, CSAVE_INVENTORY);
  2434. }
  2435. else
  2436. {
  2437. int zeny = auction.hours*battle_config.auction_feeperhour;
  2438. clif_Auction_message(sd->fd, 4);
  2439. pc_additem(sd, &auction.item, auction.item.amount, LOG_TYPE_AUCTION);
  2440. pc_getzeny(sd, zeny, LOG_TYPE_AUCTION);
  2441. }
  2442. }
  2443. /**
  2444. * Inform char-serv that the auction is cancelled
  2445. * @param char_id : player that has cancel the auction
  2446. * @param auction_id : auction to cancel
  2447. * @return 0=error, 1=msg sent
  2448. */
  2449. int intif_Auction_cancel(uint32 char_id, unsigned int auction_id)
  2450. {
  2451. if( CheckForCharServer() )
  2452. return 0;
  2453. WFIFOHEAD(inter_fd,10);
  2454. WFIFOW(inter_fd,0) = 0x3052;
  2455. WFIFOL(inter_fd,2) = char_id;
  2456. WFIFOL(inter_fd,6) = auction_id;
  2457. WFIFOSET(inter_fd,10);
  2458. return 1;
  2459. }
  2460. /**
  2461. * Receive a notification that the auction was cancelled
  2462. * @param fd : char-serv link
  2463. */
  2464. static void intif_parse_Auction_cancel(int fd)
  2465. {
  2466. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2467. int result = RFIFOB(fd,6);
  2468. if( sd == nullptr )
  2469. return;
  2470. switch( result )
  2471. {
  2472. case 0: clif_Auction_message(sd->fd, 2); break;
  2473. case 1: clif_Auction_close(sd->fd, 2); break;
  2474. case 2: clif_Auction_close(sd->fd, 1); break;
  2475. case 3: clif_Auction_message(sd->fd, 3); break;
  2476. }
  2477. }
  2478. /**
  2479. * Inform the char-serv that the auction has ended
  2480. * @param char_id : player that stop the auction
  2481. * @param auction_id : auction to stop
  2482. * @return 0=error, 1=msg sent
  2483. */
  2484. int intif_Auction_close(uint32 char_id, unsigned int auction_id)
  2485. {
  2486. if( CheckForCharServer() )
  2487. return 0;
  2488. WFIFOHEAD(inter_fd,10);
  2489. WFIFOW(inter_fd,0) = 0x3053;
  2490. WFIFOL(inter_fd,2) = char_id;
  2491. WFIFOL(inter_fd,6) = auction_id;
  2492. WFIFOSET(inter_fd,10);
  2493. return 1;
  2494. }
  2495. /**
  2496. * Receive a notification that the auction has ended
  2497. * @param fd : char-serv link
  2498. */
  2499. static void intif_parse_Auction_close(int fd)
  2500. {
  2501. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2502. unsigned char result = RFIFOB(fd,6);
  2503. if( sd == nullptr )
  2504. return;
  2505. clif_Auction_close(sd->fd, result);
  2506. if( result == 0 )
  2507. {
  2508. // FIXME: Leeching off a parse function
  2509. clif_parse_Auction_cancelreg(fd, sd);
  2510. intif_Auction_requestlist(sd->status.char_id, 6, 0, "", 1);
  2511. }
  2512. }
  2513. /**
  2514. * Bid for an auction
  2515. * @param char_id
  2516. * @param name
  2517. * @param auction_id
  2518. * @param bid
  2519. * @return 0=error, 1=msg sent
  2520. */
  2521. int intif_Auction_bid(uint32 char_id, const char* name, unsigned int auction_id, int bid)
  2522. {
  2523. int len = 16 + NAME_LENGTH;
  2524. if( CheckForCharServer() )
  2525. return 0;
  2526. WFIFOHEAD(inter_fd,len);
  2527. WFIFOW(inter_fd,0) = 0x3055;
  2528. WFIFOW(inter_fd,2) = len;
  2529. WFIFOL(inter_fd,4) = char_id;
  2530. WFIFOL(inter_fd,8) = auction_id;
  2531. WFIFOL(inter_fd,12) = bid;
  2532. safestrncpy(WFIFOCP(inter_fd,16), name, NAME_LENGTH);
  2533. WFIFOSET(inter_fd,len);
  2534. return 1;
  2535. }
  2536. /**
  2537. * Get back the money from biding auction,
  2538. * (someone else have bid it over)
  2539. * @param fd : char-serv link
  2540. */
  2541. static void intif_parse_Auction_bid(int fd)
  2542. {
  2543. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2544. int bid = RFIFOL(fd,6);
  2545. unsigned char result = RFIFOB(fd,10);
  2546. if( sd == nullptr )
  2547. return;
  2548. clif_Auction_message(sd->fd, result);
  2549. if( bid > 0 )
  2550. {
  2551. pc_getzeny(sd, bid, LOG_TYPE_AUCTION);
  2552. }
  2553. if( result == 1 )
  2554. { // To update the list, display your buy list
  2555. clif_parse_Auction_cancelreg(fd, sd);
  2556. intif_Auction_requestlist(sd->status.char_id, 7, 0, "", 1);
  2557. }
  2558. }
  2559. /**
  2560. * Used to send 'You have won the auction' and 'You failed to won the auction' messages
  2561. * @param fd : char-serv link
  2562. */
  2563. static void intif_parse_Auction_message(int fd)
  2564. {
  2565. map_session_data *sd = map_charid2sd(RFIFOL(fd,2));
  2566. unsigned char result = RFIFOB(fd,6);
  2567. if( sd == nullptr )
  2568. return;
  2569. clif_Auction_message(sd->fd, result);
  2570. }
  2571. /*==========================================
  2572. * Mercenary's System
  2573. *------------------------------------------*/
  2574. /**
  2575. * Request to create/register a mercenary on char-serv
  2576. * @param merc : Tmp mercenary data
  2577. * @return 0=error, 1=msg sent
  2578. */
  2579. int intif_mercenary_create(struct s_mercenary *merc)
  2580. {
  2581. int size = sizeof(struct s_mercenary) + 4;
  2582. if( CheckForCharServer() )
  2583. return 0;
  2584. WFIFOHEAD(inter_fd,size);
  2585. WFIFOW(inter_fd,0) = 0x3070;
  2586. WFIFOW(inter_fd,2) = size;
  2587. memcpy(WFIFOP(inter_fd,4), merc, sizeof(struct s_mercenary));
  2588. WFIFOSET(inter_fd,size);
  2589. return 1;
  2590. }
  2591. /**
  2592. * Ack of a load or create request
  2593. * @param fd : char-serv link
  2594. * @return 0=error, 1=success
  2595. */
  2596. int intif_parse_mercenary_received(int fd)
  2597. {
  2598. int len = RFIFOW(fd,2) - 5;
  2599. if( sizeof(struct s_mercenary) != len )
  2600. {
  2601. if( battle_config.etc_log )
  2602. ShowError("intif: create mercenary data size error %" PRIuPTR " != %d\n", sizeof(struct s_mercenary), len);
  2603. return 0;
  2604. }
  2605. mercenary_recv_data((struct s_mercenary*)RFIFOP(fd,5), RFIFOB(fd,4) > 0);
  2606. return 1;
  2607. }
  2608. /**
  2609. * Request mercenary data from char-serv
  2610. * @param merc_id : mercenary id to load
  2611. * @param char_id : player cid requesting data
  2612. * @return 0=error, 1=msg sent
  2613. */
  2614. int intif_mercenary_request(int merc_id, uint32 char_id)
  2615. {
  2616. if (CheckForCharServer())
  2617. return 0;
  2618. WFIFOHEAD(inter_fd,10);
  2619. WFIFOW(inter_fd,0) = 0x3071;
  2620. WFIFOL(inter_fd,2) = merc_id;
  2621. WFIFOL(inter_fd,6) = char_id;
  2622. WFIFOSET(inter_fd,10);
  2623. return 1;
  2624. }
  2625. /**
  2626. * Request to delete a mercenary
  2627. * @param merc_id
  2628. * @return 0=error, 1=msg sent
  2629. */
  2630. int intif_mercenary_delete(int merc_id)
  2631. {
  2632. if (CheckForCharServer())
  2633. return 0;
  2634. WFIFOHEAD(inter_fd,6);
  2635. WFIFOW(inter_fd,0) = 0x3072;
  2636. WFIFOL(inter_fd,2) = merc_id;
  2637. WFIFOSET(inter_fd,6);
  2638. return 1;
  2639. }
  2640. /**
  2641. * Ack of a mercenary deletion request
  2642. * @param fd : char-serv link
  2643. * @return 1
  2644. */
  2645. int intif_parse_mercenary_deleted(int fd)
  2646. {
  2647. if( RFIFOB(fd,2) != 1 )
  2648. ShowError("Mercenary data delete failure\n");
  2649. return 1;
  2650. }
  2651. /**
  2652. * Request to save a mercenary
  2653. * @param merc : Mercenary struct to save
  2654. * @return 0=error, 1=msg sent
  2655. */
  2656. int intif_mercenary_save(struct s_mercenary *merc)
  2657. {
  2658. int size = sizeof(struct s_mercenary) + 4;
  2659. if( CheckForCharServer() )
  2660. return 0;
  2661. WFIFOHEAD(inter_fd,size);
  2662. WFIFOW(inter_fd,0) = 0x3073;
  2663. WFIFOW(inter_fd,2) = size;
  2664. memcpy(WFIFOP(inter_fd,4), merc, sizeof(struct s_mercenary));
  2665. WFIFOSET(inter_fd,size);
  2666. return 1;
  2667. }
  2668. /**
  2669. * Ack of a mercenary save request
  2670. * @param fd : char-serv link
  2671. * @return 1
  2672. */
  2673. int intif_parse_mercenary_saved(int fd)
  2674. {
  2675. if( RFIFOB(fd,2) != 1 )
  2676. ShowError("Mercenary data save failure\n");
  2677. return 1;
  2678. }
  2679. /*==========================================
  2680. * Elemental's System
  2681. *------------------------------------------*/
  2682. /**
  2683. * Request to create elemental, (verify and save on char-serv)
  2684. * @param ele : Tmp Elemental data
  2685. * @return 0=error, 1=msg sent
  2686. */
  2687. int intif_elemental_create(struct s_elemental *ele)
  2688. {
  2689. int size = sizeof(struct s_elemental) + 4;
  2690. if( CheckForCharServer() )
  2691. return 0;
  2692. WFIFOHEAD(inter_fd,size);
  2693. WFIFOW(inter_fd,0) = 0x307c;
  2694. WFIFOW(inter_fd,2) = size;
  2695. memcpy(WFIFOP(inter_fd,4), ele, sizeof(struct s_elemental));
  2696. WFIFOSET(inter_fd,size);
  2697. return 1;
  2698. }
  2699. /**
  2700. * Receive an elemental data from char-serv
  2701. * @param fd : char-serv link
  2702. * @return 0=error, 1=success
  2703. */
  2704. int intif_parse_elemental_received(int fd)
  2705. {
  2706. int len = RFIFOW(fd,2) - 5;
  2707. if( sizeof(struct s_elemental) != len )
  2708. {
  2709. if( battle_config.etc_log )
  2710. ShowError("intif: create elemental data size error %" PRIuPTR " != %d\n", sizeof(struct s_elemental), len);
  2711. return 0;
  2712. }
  2713. elemental_data_received((struct s_elemental*)RFIFOP(fd,5), RFIFOB(fd,4) > 0);
  2714. return 1;
  2715. }
  2716. /**
  2717. * Request to load elemental from char-serv
  2718. * @param ele_id : elemental identification
  2719. * @param char_id : player identification
  2720. * @return 0=error, 1=msg sent
  2721. */
  2722. int intif_elemental_request(int ele_id, uint32 char_id)
  2723. {
  2724. if (CheckForCharServer())
  2725. return 0;
  2726. WFIFOHEAD(inter_fd,10);
  2727. WFIFOW(inter_fd,0) = 0x307d;
  2728. WFIFOL(inter_fd,2) = ele_id;
  2729. WFIFOL(inter_fd,6) = char_id;
  2730. WFIFOSET(inter_fd,10);
  2731. return 1;
  2732. }
  2733. /**
  2734. * Request to delete an elemental
  2735. * @param ele_id : Elemental to delete
  2736. * @return 0=error, 1=msg sent
  2737. */
  2738. int intif_elemental_delete(int ele_id)
  2739. {
  2740. if (CheckForCharServer())
  2741. return 0;
  2742. WFIFOHEAD(inter_fd,6);
  2743. WFIFOW(inter_fd,0) = 0x307e;
  2744. WFIFOL(inter_fd,2) = ele_id;
  2745. WFIFOSET(inter_fd,6);
  2746. return 1;
  2747. }
  2748. /**
  2749. * Ack of a delete elemental request
  2750. * @param fd : char-serv link
  2751. * @return 1
  2752. */
  2753. int intif_parse_elemental_deleted(int fd)
  2754. {
  2755. if( RFIFOB(fd,2) != 1 )
  2756. ShowError("Elemental data delete failure\n");
  2757. return 1;
  2758. }
  2759. /**
  2760. * Request to save elemental data
  2761. * @param ele : elemental struct to save
  2762. * @return 0=error, 1=msg sent
  2763. */
  2764. int intif_elemental_save(struct s_elemental *ele)
  2765. {
  2766. int size = sizeof(struct s_elemental) + 4;
  2767. if( CheckForCharServer() )
  2768. return 0;
  2769. WFIFOHEAD(inter_fd,size);
  2770. WFIFOW(inter_fd,0) = 0x307f;
  2771. WFIFOW(inter_fd,2) = size;
  2772. memcpy(WFIFOP(inter_fd,4), ele, sizeof(struct s_elemental));
  2773. WFIFOSET(inter_fd,size);
  2774. return 1;
  2775. }
  2776. /**
  2777. * Ack of a save elemental request
  2778. * @param fd : char-serv link
  2779. * @return 1
  2780. */
  2781. int intif_parse_elemental_saved(int fd)
  2782. {
  2783. if( RFIFOB(fd,2) != 1 )
  2784. ShowError("Elemental data save failure\n");
  2785. return 1;
  2786. }
  2787. /**
  2788. * Request account information to char-serv
  2789. * @param u_fd : Player fd (for message back)
  2790. * @param aid : requesting player aid
  2791. * @param group_lv : requesting player lv
  2792. * @param query : name or aid of player we want info
  2793. * @param type : 1 - Only return account id & userid, 0 - Full info
  2794. * @return : 0=error, 1=msg sent
  2795. */
  2796. int intif_request_accinfo(int u_fd, int aid, int group_lv, char* query, char type) {
  2797. if( CheckForCharServer() )
  2798. return 0;
  2799. WFIFOHEAD(inter_fd,2 + 4 + 4 + 4 + 1 + NAME_LENGTH);
  2800. WFIFOW(inter_fd,0) = 0x3007;
  2801. WFIFOL(inter_fd,2) = u_fd;
  2802. WFIFOL(inter_fd,6) = aid;
  2803. WFIFOL(inter_fd,10) = group_lv;
  2804. WFIFOB(inter_fd,14) = type;
  2805. safestrncpy(WFIFOCP(inter_fd,15), query, NAME_LENGTH);
  2806. WFIFOSET(inter_fd,2 + 4 + 4 + 4 + 1 + NAME_LENGTH);
  2807. return 1;
  2808. }
  2809. /**
  2810. * Receive the reply of a request_accinfo with type 1
  2811. * @param fd : char-serv link
  2812. */
  2813. void intif_parse_accinfo_ack( int fd ) {
  2814. char acc_name[NAME_LENGTH];
  2815. int u_fd = RFIFOL(fd,2);
  2816. int acc_id = RFIFOL(fd,6);
  2817. safestrncpy(acc_name, RFIFOCP(fd,10), NAME_LENGTH);
  2818. clif_account_name(u_fd, acc_id, acc_name);
  2819. }
  2820. /**
  2821. * Display a message from char-serv to a player
  2822. * @param fd : Char-serv link
  2823. */
  2824. void intif_parse_MessageToFD(int fd) {
  2825. int u_fd = RFIFOL(fd,4);
  2826. if( session[u_fd] && session[u_fd]->session_data ) { //check if the player still online
  2827. int aid = RFIFOL(fd,8);
  2828. map_session_data * sd = (map_session_data *)session[u_fd]->session_data;
  2829. /* matching e.g. previous fd owner didn't dc during request or is still the same */
  2830. if( sd->bl.id == aid ) {
  2831. char msg[512];
  2832. safestrncpy(msg, RFIFOCP(fd,12), RFIFOW(fd,2) - 12);
  2833. clif_displaymessage(u_fd,msg);
  2834. }
  2835. }
  2836. return;
  2837. }
  2838. /// BROADCAST OBTAIN SPECIAL ITEM
  2839. /**
  2840. * Request to send broadcast item to all servers
  2841. * ZI 3009 <cmd>.W <len>.W <nameid>.N <source>.W <type>.B <name>.?B
  2842. * @param sd Player who obtain the item
  2843. * @param nameid Obtained item
  2844. * @param sourceid Source of item, another item ID or monster ID
  2845. * @param type Obtain type @see enum BROADCASTING_SPECIAL_ITEM_OBTAIN
  2846. * @return
  2847. **/
  2848. int intif_broadcast_obtain_special_item(map_session_data *sd, t_itemid nameid, t_itemid sourceid, unsigned char type) {
  2849. nullpo_retr(0, sd);
  2850. // Should not be here!
  2851. if (type == ITEMOBTAIN_TYPE_NPC) {
  2852. intif_broadcast_obtain_special_item_npc(sd, nameid);
  2853. return 0;
  2854. }
  2855. // Send local
  2856. clif_broadcast_obtain_special_item(sd->status.name, nameid, sourceid, (enum BROADCASTING_SPECIAL_ITEM_OBTAIN)type);
  2857. if (CheckForCharServer())
  2858. return 0;
  2859. if (other_mapserver_count < 1)
  2860. return 0;
  2861. WFIFOHEAD(inter_fd, 11 + NAME_LENGTH);
  2862. WFIFOW(inter_fd, 0) = 0x3009;
  2863. WFIFOW(inter_fd, 2) = 11 + NAME_LENGTH;
  2864. WFIFOL(inter_fd, 4) = nameid;
  2865. WFIFOW(inter_fd, 8) = sourceid;
  2866. WFIFOB(inter_fd, 10) = type;
  2867. safestrncpy(WFIFOCP(inter_fd, 11), sd->status.name, NAME_LENGTH);
  2868. WFIFOSET(inter_fd, WFIFOW(inter_fd, 2));
  2869. return 1;
  2870. }
  2871. /**
  2872. * Request to send broadcast item to all servers.
  2873. * TODO: Confirm the usage. Maybe on getitem-like command?
  2874. * ZI 3009 <cmd>.W <len>.W <nameid>.N <source>.W <type>.B <name>.24B <npcname>.24B
  2875. * @param sd Player who obtain the item
  2876. * @param nameid Obtained item
  2877. * @param srcname Source name
  2878. * @return
  2879. **/
  2880. int intif_broadcast_obtain_special_item_npc(map_session_data *sd, t_itemid nameid) {
  2881. nullpo_retr(0, sd);
  2882. // Send local
  2883. clif_broadcast_obtain_special_item(sd->status.name, nameid, 0, ITEMOBTAIN_TYPE_NPC);
  2884. if (CheckForCharServer())
  2885. return 0;
  2886. if (other_mapserver_count < 1)
  2887. return 0;
  2888. WFIFOHEAD(inter_fd, 11 + NAME_LENGTH*2);
  2889. WFIFOW(inter_fd, 0) = 0x3009;
  2890. WFIFOW(inter_fd, 2) = 11 + NAME_LENGTH*2;
  2891. WFIFOL(inter_fd, 4) = nameid;
  2892. WFIFOW(inter_fd, 8) = 0;
  2893. WFIFOB(inter_fd, 10) = ITEMOBTAIN_TYPE_NPC;
  2894. safestrncpy(WFIFOCP(inter_fd, 11), sd->status.name, NAME_LENGTH);
  2895. WFIFOSET(inter_fd, WFIFOW(inter_fd, 2));
  2896. return 1;
  2897. }
  2898. /**
  2899. * Received broadcast item and broadcast on local map.
  2900. * IZ 3809 <cmd>.W <len>.W <nameid>.L <source>.W <type>.B <name>.24B <srcname>.24B
  2901. * @param fd
  2902. **/
  2903. void intif_parse_broadcast_obtain_special_item(int fd) {
  2904. int type = RFIFOB(fd, 10);
  2905. char name[NAME_LENGTH];
  2906. safestrncpy(name, RFIFOCP(fd, 11), NAME_LENGTH);
  2907. if (type == ITEMOBTAIN_TYPE_NPC)
  2908. safestrncpy(name, RFIFOCP(fd, 11 + NAME_LENGTH), NAME_LENGTH);
  2909. clif_broadcast_obtain_special_item(name, RFIFOL(fd, 4), RFIFOW(fd, 8), (enum BROADCASTING_SPECIAL_ITEM_OBTAIN)type);
  2910. }
  2911. /*==========================================
  2912. * Item Bound System
  2913. *------------------------------------------*/
  2914. #ifdef BOUND_ITEMS
  2915. /**
  2916. * ZI 0x3056 <char_id>.L <account_id>.L <guild_id>.W
  2917. * Request inter-serv to delete some bound item, for non connected cid
  2918. * @param char_id : Char to delete item ID
  2919. * @param aid : Account to delete item ID
  2920. * @param guild_id : Guild of char
  2921. */
  2922. void intif_itembound_guild_retrieve(uint32 char_id,uint32 account_id,int guild_id) {
  2923. struct s_storage *gstor = guild2storage2(guild_id);
  2924. if( CheckForCharServer() )
  2925. return;
  2926. WFIFOHEAD(inter_fd,12);
  2927. WFIFOW(inter_fd,0) = 0x3056;
  2928. WFIFOL(inter_fd,2) = char_id;
  2929. WFIFOL(inter_fd,6) = account_id;
  2930. WFIFOW(inter_fd,10) = guild_id;
  2931. WFIFOSET(inter_fd,12);
  2932. if (gstor)
  2933. gstor->lock = true; //Lock for retrieval process
  2934. ShowInfo("Request guild bound item(s) retrieval for CID = " CL_WHITE "%d" CL_RESET ", AID = %d, Guild ID = " CL_WHITE "%d" CL_RESET ".\n", char_id, account_id, guild_id);
  2935. }
  2936. /**
  2937. * Acknowledge the good deletion of the bound item
  2938. * (unlock the guild storage)
  2939. * @struct : 0x3856 <aid>.L <gid>.W
  2940. * @param fd : Char-serv link
  2941. */
  2942. void intif_parse_itembound_ack(int fd) {
  2943. int guild_id = RFIFOW(fd,6);
  2944. struct s_storage *gstor = guild2storage2(guild_id);
  2945. if (gstor)
  2946. gstor->lock = false; //Unlock now that operation is completed
  2947. }
  2948. /**
  2949. * IZ 0x3857 <size>.W <count>.W <guild_id>.W { <item>.?B }.*MAX_INVENTORY
  2950. * Received the retrieved guild bound items from inter-server, store them to guild storage.
  2951. * @param fd
  2952. * @author [Cydh]
  2953. */
  2954. void intif_parse_itembound_store2gstorage(int fd) {
  2955. unsigned short i, failed = 0;
  2956. short count = RFIFOW(fd, 4), guild_id = RFIFOW(fd, 6);
  2957. struct s_storage *gstor = guild2storage(guild_id);
  2958. if (!gstor) {
  2959. ShowError("intif_parse_itembound_store2gstorage: Guild '%d' not found.\n", guild_id);
  2960. return;
  2961. }
  2962. //@TODO: Gives some actions for item(s) that cannot be stored because storage is full or reach the limit of stack amount
  2963. for (i = 0; i < count; i++) {
  2964. struct item *item = (struct item*)RFIFOP(fd, 8 + i*sizeof(struct item));
  2965. if (!item)
  2966. continue;
  2967. if (!storage_guild_additem2(gstor, item, item->amount))
  2968. failed++;
  2969. }
  2970. ShowInfo("Retrieved '" CL_WHITE "%d" CL_RESET "' (failed: %d) guild bound item(s) for Guild ID = " CL_WHITE "%d" CL_RESET ".\n", count, failed, guild_id);
  2971. gstor->lock = false;
  2972. gstor->status = false;
  2973. }
  2974. #endif
  2975. /**
  2976. * Receive inventory/cart/storage data for player
  2977. * IZ 0x388a <size>.W <type>.B <account_id>.L <result>.B <storage>.?B
  2978. * @param fd
  2979. */
  2980. static bool intif_parse_StorageReceived(int fd)
  2981. {
  2982. char type = RFIFOB(fd,4);
  2983. uint32 account_id = RFIFOL(fd, 5);
  2984. map_session_data *sd = map_id2sd(account_id);
  2985. struct s_storage *stor, *p; //storage
  2986. size_t sz_stor = sizeof(struct s_storage);
  2987. if (!sd) {
  2988. ShowError("intif_parse_StorageReceived: No player online for receiving inventory/cart/storage data (AID: %d)\n", account_id);
  2989. return false;
  2990. }
  2991. if (!RFIFOB(fd, 9)) {
  2992. ShowError("intif_parse_StorageReceived: Failed to load! (AID: %d, type: %d)\n", account_id, type);
  2993. return false;
  2994. }
  2995. p = (struct s_storage *)RFIFOP(fd,10);
  2996. switch (type) {
  2997. case TABLE_INVENTORY:
  2998. stor = &sd->inventory;
  2999. break;
  3000. case TABLE_STORAGE:
  3001. if (p->stor_id == 0)
  3002. stor = &sd->storage;
  3003. else
  3004. stor = &sd->premiumStorage;
  3005. break;
  3006. case TABLE_CART:
  3007. stor = &sd->cart;
  3008. break;
  3009. default:
  3010. return false;
  3011. }
  3012. if (stor->stor_id == p->stor_id) {
  3013. if (stor->status) { // Already open.. lets ignore this update
  3014. ShowWarning("intif_parse_StorageReceived: storage received for a client already open (User %d:%d)\n", sd->status.account_id, sd->status.char_id);
  3015. return false;
  3016. }
  3017. if (stor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit!
  3018. ShowWarning("intif_parse_StorageReceived: received storage for an already modified non-saved storage! (User %d:%d)\n", sd->status.account_id, sd->status.char_id);
  3019. return false;
  3020. }
  3021. }
  3022. if (RFIFOW(fd,2)-10 != sz_stor) {
  3023. ShowError("intif_parse_StorageReceived: data size error %d %" PRIuPTR "\n",RFIFOW(fd,2)-10 , sz_stor);
  3024. stor->status = false;
  3025. return false;
  3026. }
  3027. memcpy(stor, p, sz_stor); //copy the items data to correct destination
  3028. switch (type) {
  3029. case TABLE_INVENTORY: {
  3030. #ifdef BOUND_ITEMS
  3031. int j, idxlist[MAX_INVENTORY];
  3032. #endif
  3033. pc_setinventorydata(sd);
  3034. pc_setequipindex(sd);
  3035. pc_check_expiration(sd);
  3036. pc_check_available_item(sd, ITMCHK_INVENTORY);
  3037. pc_itemcd_do(sd, true);
  3038. #ifdef BOUND_ITEMS
  3039. // Party bound item check
  3040. if (sd->status.party_id == 0 && (j = pc_bound_chk(sd, BOUND_PARTY, idxlist))) { // Party was deleted while character offline
  3041. int i;
  3042. for (i = 0; i < j; i++)
  3043. pc_delitem(sd, idxlist[i], sd->inventory.u.items_inventory[idxlist[i]].amount, 4, 1, LOG_TYPE_OTHER);
  3044. }
  3045. #endif
  3046. //Set here because we need the inventory data for weapon sprite parsing.
  3047. status_set_viewdata(&sd->bl, sd->status.class_);
  3048. // Set headgear data here, otherwise this is done in loadEndAck
  3049. if( sd->state.autotrade ){
  3050. pc_set_costume_view(sd);
  3051. }
  3052. pc_load_combo(sd);
  3053. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  3054. status_calc_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS)); // Refresh weight data
  3055. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  3056. break;
  3057. }
  3058. case TABLE_CART:
  3059. pc_check_available_item(sd, ITMCHK_CART);
  3060. if (sd->state.autotrade) {
  3061. clif_parse_LoadEndAck(sd->fd, sd);
  3062. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  3063. }else if( sd->state.prevend ){
  3064. clif_clearcart(sd->fd);
  3065. clif_cartlist(sd);
  3066. // Only open the vending UI, if it has not been opened already
  3067. if (sd->state.pending_vending_ui) {
  3068. clif_openvendingreq( *sd, sd->vend_skill_lv + 2 );
  3069. sd->state.pending_vending_ui = false;
  3070. }
  3071. }
  3072. break;
  3073. case TABLE_STORAGE:
  3074. if (stor->stor_id)
  3075. storage_premiumStorage_open(sd);
  3076. else {
  3077. #ifdef VIP_ENABLE
  3078. if (!pc_isvip(sd))
  3079. stor->max_amount = MIN_STORAGE;
  3080. #endif
  3081. pc_check_available_item(sd, ITMCHK_STORAGE);
  3082. }
  3083. break;
  3084. }
  3085. return true;
  3086. }
  3087. /**
  3088. * Save inventory/cart/storage data for a player
  3089. * IZ 0x388b <account_id>.L <result>.B <type>.B <storage_id>.B
  3090. * @param fd
  3091. */
  3092. static void intif_parse_StorageSaved(int fd)
  3093. {
  3094. if (RFIFOB(fd, 6)) {
  3095. switch (RFIFOB(fd, 7)) {
  3096. case TABLE_INVENTORY: //inventory
  3097. //ShowInfo("Inventory has been saved (AID: %d).\n", RFIFOL(fd, 2));
  3098. break;
  3099. case TABLE_STORAGE: //storage
  3100. {
  3101. map_session_data *sd = map_id2sd( RFIFOL( fd, 2 ) );
  3102. struct s_storage* stor = nullptr;
  3103. if( RFIFOB( fd, 8 ) ){
  3104. // ShowInfo("Storage %d has been saved (AID: %d).\n", RFIFOL(fd, 2), RFIFOB(fd, 8) );
  3105. if( sd ){
  3106. stor = &sd->premiumStorage;
  3107. }
  3108. }else{
  3109. // ShowInfo("Storage has been saved (AID: %d).\n", RFIFOL(fd, 2));
  3110. if( sd ){
  3111. stor = &sd->storage;
  3112. }
  3113. }
  3114. if( stor ){
  3115. stor->dirty = false;
  3116. }
  3117. }
  3118. break;
  3119. case TABLE_CART: // cart
  3120. //ShowInfo("Cart has been saved (AID: %d).\n", RFIFOL(fd, 2));
  3121. {
  3122. map_session_data *sd = map_id2sd(RFIFOL(fd, 2));
  3123. if( sd && sd->state.prevend ){
  3124. intif_storage_request(sd,TABLE_CART,0,STOR_MODE_ALL);
  3125. }
  3126. }
  3127. break;
  3128. default:
  3129. break;
  3130. }
  3131. } else
  3132. ShowError("Failed to save inventory/cart/storage data (AID: %d, type: %d).\n", RFIFOL(fd, 2), RFIFOB(fd, 7));
  3133. }
  3134. /**
  3135. * IZ 0x388c <len>.W { <storage_table>.? }*?
  3136. * Receive storage information
  3137. **/
  3138. void intif_parse_StorageInfo_recv(int fd) {
  3139. int size = sizeof(struct s_storage_table), count = (RFIFOW(fd, 2) - 4) / size;
  3140. storage_db.clear();
  3141. for( int i = 0; i < count; i++ ){
  3142. struct s_storage_table* ptr = (struct s_storage_table*)RFIFOP( fd, 4 + size * i );
  3143. std::shared_ptr<struct s_storage_table> storage = std::make_shared<struct s_storage_table>();
  3144. safestrncpy( storage->name, ptr->name, sizeof( storage->name ) );
  3145. safestrncpy( storage->table, ptr->table, sizeof( storage->table ) );
  3146. storage->max_num = ptr->max_num;
  3147. storage->id = ptr->id;
  3148. storage_db[storage->id] = storage;
  3149. }
  3150. if (battle_config.etc_log)
  3151. ShowInfo("Received '" CL_WHITE PRIdPTR CL_RESET "' storage info from inter-server.\n", storage_db.size());
  3152. }
  3153. /**
  3154. * Request inventory/cart/storage data for a player
  3155. * ZI 0x308a <type>.B <account_id>.L <char_id>.L <storage_id>.B
  3156. * @param sd: Player data
  3157. * @param type: Storage type
  3158. * @param stor_id: Storage ID
  3159. * @param mode: Storage mode
  3160. * @return false - error, true - message sent
  3161. */
  3162. bool intif_storage_request(map_session_data *sd, enum storage_type type, uint8 stor_id, uint8 mode)
  3163. {
  3164. if (CheckForCharServer())
  3165. return false;
  3166. WFIFOHEAD(inter_fd, 13);
  3167. WFIFOW(inter_fd, 0) = 0x308a;
  3168. WFIFOB(inter_fd, 2) = type;
  3169. WFIFOL(inter_fd, 3) = sd->status.account_id;
  3170. WFIFOL(inter_fd, 7) = sd->status.char_id;
  3171. WFIFOB(inter_fd, 11) = stor_id;
  3172. WFIFOB(inter_fd, 12) = mode;
  3173. WFIFOSET(inter_fd, 13);
  3174. return true;
  3175. }
  3176. /**
  3177. * Request to save inventory/cart/storage data from player
  3178. * ZI 0x308b <size>.W <type>.B <account_id>.L <char_id>.L <entries>.?B
  3179. * @param sd: Player data
  3180. * @param stor: Storage data
  3181. * @ return false - error, true - message sent
  3182. */
  3183. bool intif_storage_save(map_session_data *sd, struct s_storage *stor)
  3184. {
  3185. int stor_size = sizeof(struct s_storage);
  3186. nullpo_retr(false, sd);
  3187. nullpo_retr(false, stor);
  3188. if (CheckForCharServer())
  3189. return false;
  3190. WFIFOHEAD(inter_fd, stor_size+13);
  3191. WFIFOW(inter_fd, 0) = 0x308b;
  3192. WFIFOW(inter_fd, 2) = stor_size+13;
  3193. WFIFOB(inter_fd, 4) = stor->type;
  3194. WFIFOL(inter_fd, 5) = sd->status.account_id;
  3195. WFIFOL(inter_fd, 9) = sd->status.char_id;
  3196. memcpy(WFIFOP(inter_fd, 13), stor, stor_size);
  3197. WFIFOSET(inter_fd, stor_size+13);
  3198. return true;
  3199. }
  3200. int intif_clan_requestclans(){
  3201. if (CheckForCharServer())
  3202. return 0;
  3203. WFIFOHEAD(inter_fd, 2);
  3204. WFIFOW(inter_fd, 0) = 0x30A0;
  3205. WFIFOSET(inter_fd, 2);
  3206. return 1;
  3207. }
  3208. void intif_parse_clans( int fd ){
  3209. clan_load_clandata( ( RFIFOW(fd, 2) - 4 ) / sizeof( struct clan ), (struct clan*)RFIFOP(fd,4) );
  3210. }
  3211. int intif_clan_message( int clan_id, uint32 account_id, const char *mes, size_t len ){
  3212. if (CheckForCharServer())
  3213. return 0;
  3214. if (other_mapserver_count < 1)
  3215. return 0; //No need to send.
  3216. WFIFOHEAD(inter_fd, len + 12);
  3217. WFIFOW(inter_fd,0)=0x30A1;
  3218. WFIFOW( inter_fd, 2 ) = static_cast<uint16>( len + 12 );
  3219. WFIFOL(inter_fd,4)=clan_id;
  3220. WFIFOL(inter_fd,8)=account_id;
  3221. safestrncpy(WFIFOCP(inter_fd,12),mes,len);
  3222. WFIFOSET(inter_fd,len+12);
  3223. return 1;
  3224. }
  3225. int intif_parse_clan_message( int fd ){
  3226. clan_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),(char *) RFIFOP(fd,12),RFIFOW(fd,2)-12);
  3227. return 1;
  3228. }
  3229. int intif_clan_member_left( int clan_id ){
  3230. if (CheckForCharServer())
  3231. return 0;
  3232. if (other_mapserver_count < 1)
  3233. return 0; //No need to send.
  3234. WFIFOHEAD(inter_fd,6);
  3235. WFIFOW(inter_fd,0) = 0x30A2;
  3236. WFIFOL(inter_fd,2) = clan_id;
  3237. WFIFOSET(inter_fd,6);
  3238. return 1;
  3239. }
  3240. int intif_clan_member_joined( int clan_id ){
  3241. if (CheckForCharServer())
  3242. return 0;
  3243. if (other_mapserver_count < 1)
  3244. return 0; //No need to send.
  3245. WFIFOHEAD(inter_fd,6);
  3246. WFIFOW(inter_fd,0) = 0x30A3;
  3247. WFIFOL(inter_fd,2) = clan_id;
  3248. WFIFOSET(inter_fd,6);
  3249. return 1;
  3250. }
  3251. int intif_parse_clan_onlinecount( int fd ){
  3252. struct clan* clan = clan_search(RFIFOL(fd,2));
  3253. if( clan == nullptr ){
  3254. return 0;
  3255. }
  3256. clan->connect_member = RFIFOW(fd,6);
  3257. clif_clan_onlinecount( *clan );
  3258. return 1;
  3259. }
  3260. //-----------------------------------------------------------------
  3261. /**
  3262. * Communication from the inter server, Main entry point interface (inter<=>map)
  3263. * @param fd : inter-serv link
  3264. * @return
  3265. * 0 (unknow packet).
  3266. * 1 sucess (no error)
  3267. * 2 invalid length of packet (not enough data yet)
  3268. */
  3269. int intif_parse(int fd)
  3270. {
  3271. int packet_len, cmd;
  3272. cmd = RFIFOW(fd,0);
  3273. // Verify ID of the packet
  3274. if(cmd<0x3800 || cmd>=0x3800+ARRAYLENGTH(packet_len_table) ||
  3275. packet_len_table[cmd-0x3800]==0){
  3276. return 0;
  3277. }
  3278. // Check the length of the packet
  3279. packet_len = packet_len_table[cmd-0x3800];
  3280. if(packet_len==-1){
  3281. if(RFIFOREST(fd)<4)
  3282. return 2;
  3283. packet_len = RFIFOW(fd,2);
  3284. }
  3285. if((int)RFIFOREST(fd)<packet_len){
  3286. return 2;
  3287. }
  3288. // Processing branch
  3289. switch(cmd){
  3290. case 0x3800:
  3291. if (RFIFOL(fd,4) == 0xFF000000) //Normal announce.
  3292. clif_broadcast(nullptr, RFIFOCP(fd,16), packet_len-16, BC_DEFAULT, ALL_CLIENT);
  3293. else //Color announce.
  3294. clif_broadcast2(nullptr, RFIFOCP(fd,16), packet_len-16, RFIFOL(fd,4), RFIFOW(fd,8), RFIFOW(fd,10), RFIFOW(fd,12), RFIFOW(fd,14), ALL_CLIENT);
  3295. break;
  3296. case 0x3801: intif_parse_WisMessage(fd); break;
  3297. case 0x3802: intif_parse_WisEnd(fd); break;
  3298. case 0x3803: mapif_parse_WisToGM(fd); break;
  3299. case 0x3804: intif_parse_Registers(fd); break;
  3300. case 0x3806: intif_parse_ChangeNameOk(fd); break;
  3301. case 0x3807: intif_parse_MessageToFD(fd); break;
  3302. case 0x3808: intif_parse_accinfo_ack(fd); break;
  3303. case 0x3809: intif_parse_broadcast_obtain_special_item(fd); break;
  3304. case 0x3818: intif_parse_LoadGuildStorage(fd); break;
  3305. case 0x3819: intif_parse_SaveGuildStorage(fd); break;
  3306. case 0x3820: intif_parse_PartyCreated(fd); break;
  3307. case 0x3821: intif_parse_PartyInfo(fd); break;
  3308. case 0x3822: intif_parse_PartyMemberAdded(fd); break;
  3309. case 0x3823: intif_parse_PartyOptionChanged(fd); break;
  3310. case 0x3824: intif_parse_PartyMemberWithdraw(fd); break;
  3311. case 0x3825: intif_parse_PartyMove(fd); break;
  3312. case 0x3826: intif_parse_PartyBroken(fd); break;
  3313. case 0x3827: intif_parse_PartyMessage(fd); break;
  3314. case 0x3830: intif_parse_GuildCreated(fd); break;
  3315. case 0x3831: intif_parse_GuildInfo(fd); break;
  3316. case 0x3832: intif_parse_GuildMemberAdded(fd); break;
  3317. case 0x3834: intif_parse_GuildMemberWithdraw(fd); break;
  3318. case 0x3835: intif_parse_GuildMemberInfoShort(fd); break;
  3319. case 0x3836: intif_parse_GuildBroken(fd); break;
  3320. case 0x3837: intif_parse_GuildMessage(fd); break;
  3321. case 0x3839: intif_parse_GuildBasicInfoChanged(fd); break;
  3322. case 0x383a: intif_parse_GuildMemberInfoChanged(fd); break;
  3323. case 0x383b: intif_parse_GuildPosition(fd); break;
  3324. case 0x383c: intif_parse_GuildSkillUp(fd); break;
  3325. case 0x383d: intif_parse_GuildAlliance(fd); break;
  3326. case 0x383e: intif_parse_GuildNotice(fd); break;
  3327. case 0x383f: intif_parse_GuildEmblem(fd); break;
  3328. case 0x3840: intif_parse_GuildCastleDataLoad(fd); break;
  3329. case 0x3841: intif_parse_GuildEmblemVersionChanged(fd); break;
  3330. case 0x3843: intif_parse_GuildMasterChanged(fd); break;
  3331. // Mail System
  3332. case 0x3848: intif_parse_Mail_inboxreceived(fd); break;
  3333. case 0x3849: intif_parse_Mail_new(fd); break;
  3334. case 0x384a: intif_parse_Mail_getattach(fd); break;
  3335. case 0x384b: intif_parse_Mail_delete(fd); break;
  3336. case 0x384c: intif_parse_Mail_return(fd); break;
  3337. case 0x384d: intif_parse_Mail_send(fd); break;
  3338. case 0x384e: intif_parse_Mail_receiver(fd); break;
  3339. // Auction System
  3340. case 0x3850: intif_parse_Auction_results(fd); break;
  3341. case 0x3851: intif_parse_Auction_register(fd); break;
  3342. case 0x3852: intif_parse_Auction_cancel(fd); break;
  3343. case 0x3853: intif_parse_Auction_close(fd); break;
  3344. case 0x3854: intif_parse_Auction_message(fd); break;
  3345. case 0x3855: intif_parse_Auction_bid(fd); break;
  3346. //Bound items
  3347. #ifdef BOUND_ITEMS
  3348. case 0x3856: intif_parse_itembound_ack(fd); break;
  3349. case 0x3857: intif_parse_itembound_store2gstorage(fd); break;
  3350. #endif
  3351. //Quest system
  3352. case 0x3860: intif_parse_questlog(fd); break;
  3353. case 0x3861: intif_parse_questsave(fd); break;
  3354. //Achievement system
  3355. case 0x3862: intif_parse_achievements(fd); break;
  3356. case 0x3863: intif_parse_achievementsave(fd); break;
  3357. case 0x3864: intif_parse_achievementreward(fd); break;
  3358. // Mercenary System
  3359. case 0x3870: intif_parse_mercenary_received(fd); break;
  3360. case 0x3871: intif_parse_mercenary_deleted(fd); break;
  3361. case 0x3872: intif_parse_mercenary_saved(fd); break;
  3362. // Elemental System
  3363. case 0x387c: intif_parse_elemental_received(fd); break;
  3364. case 0x387d: intif_parse_elemental_deleted(fd); break;
  3365. case 0x387e: intif_parse_elemental_saved(fd); break;
  3366. // Pet System
  3367. case 0x3880: intif_parse_CreatePet(fd); break;
  3368. case 0x3881: intif_parse_RecvPetData(fd); break;
  3369. case 0x3882: intif_parse_SavePetOk(fd); break;
  3370. case 0x3883: intif_parse_DeletePetOk(fd); break;
  3371. // Storage
  3372. case 0x388a: intif_parse_StorageReceived(fd); break;
  3373. case 0x388b: intif_parse_StorageSaved(fd); break;
  3374. case 0x388c: intif_parse_StorageInfo_recv(fd); break;
  3375. // Homunculus System
  3376. case 0x3890: intif_parse_CreateHomunculus(fd); break;
  3377. case 0x3891: intif_parse_RecvHomunculusData(fd); break;
  3378. case 0x3892: intif_parse_SaveHomunculusOk(fd); break;
  3379. case 0x3893: intif_parse_DeleteHomunculusOk(fd); break;
  3380. // Clan system
  3381. case 0x38A0: intif_parse_clans(fd); break;
  3382. case 0x38A1: intif_parse_clan_message(fd); break;
  3383. case 0x38A2: intif_parse_clan_onlinecount(fd); break;
  3384. default:
  3385. ShowError("intif_parse : unknown packet %d %x\n",fd,RFIFOW(fd,0));
  3386. return 0;
  3387. }
  3388. // Skip packet
  3389. RFIFOSKIP(fd,packet_len);
  3390. return 1;
  3391. }