map.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h"
  5. #include "../common/timer.h"
  6. #include "../common/grfio.h"
  7. #include "../common/malloc.h"
  8. #include "../common/socket.h" // WFIFO*()
  9. #include "../common/showmsg.h"
  10. #include "../common/nullpo.h"
  11. #include "../common/random.h"
  12. #include "../common/strlib.h"
  13. #include "../common/utils.h"
  14. #include "../common/cli.h"
  15. #include "../common/ers.h"
  16. #include "map.h"
  17. #include "path.h"
  18. #include "chrif.h"
  19. #include "clif.h"
  20. #include "duel.h"
  21. #include "intif.h"
  22. #include "npc.h"
  23. #include "pc.h"
  24. #include "status.h"
  25. #include "mob.h"
  26. #include "npc.h" // npc_setcells(), npc_unsetcells()
  27. #include "chat.h"
  28. #include "itemdb.h"
  29. #include "storage.h"
  30. #include "skill.h"
  31. #include "trade.h"
  32. #include "party.h"
  33. #include "unit.h"
  34. #include "battle.h"
  35. #include "battleground.h"
  36. #include "quest.h"
  37. #include "script.h"
  38. #include "mapreg.h"
  39. #include "guild.h"
  40. #include "pet.h"
  41. #include "homunculus.h"
  42. #include "instance.h"
  43. #include "mercenary.h"
  44. #include "elemental.h"
  45. #include "atcommand.h"
  46. #include "log.h"
  47. #include "mail.h"
  48. #include "cashshop.h"
  49. #include "channel.h"
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <stdarg.h>
  54. #include <math.h>
  55. #ifndef _WIN32
  56. #include <unistd.h>
  57. #endif
  58. char default_codepage[32] = "";
  59. int map_server_port = 3306;
  60. char map_server_ip[32] = "127.0.0.1";
  61. char map_server_id[32] = "ragnarok";
  62. char map_server_pw[32] = "ragnarok";
  63. char map_server_db[32] = "ragnarok";
  64. Sql* mmysql_handle;
  65. int db_use_sqldbs = 0;
  66. char item_db_db[32] = "item_db";
  67. char item_db2_db[32] = "item_db2";
  68. char item_db_re_db[32] = "item_db_re";
  69. char item_cash_db_db[32] = "item_cash_db";
  70. char item_cash_db2_db[32] = "item_cash_db2";
  71. char mob_db_db[32] = "mob_db";
  72. char mob_db2_db[32] = "mob_db2";
  73. char mob_skill_db_db[32] = "mob_skill_db";
  74. char mob_skill_db2_db[32] = "mob_skill_db2";
  75. // log database
  76. char log_db_ip[32] = "127.0.0.1";
  77. int log_db_port = 3306;
  78. char log_db_id[32] = "ragnarok";
  79. char log_db_pw[32] = "ragnarok";
  80. char log_db_db[32] = "log";
  81. Sql* logmysql_handle;
  82. // DBMap declaartion
  83. static DBMap* id_db=NULL; // int id -> struct block_list*
  84. static DBMap* pc_db=NULL; // int id -> struct map_session_data*
  85. static DBMap* mobid_db=NULL; // int id -> struct mob_data*
  86. static DBMap* bossid_db=NULL; // int id -> struct mob_data* (MVP db)
  87. static DBMap* map_db=NULL; // unsigned int mapindex -> struct map_data*
  88. static DBMap* nick_db=NULL; // int char_id -> struct charid2nick* (requested names of offline characters)
  89. static DBMap* charid_db=NULL; // int char_id -> struct map_session_data*
  90. static DBMap* regen_db=NULL; // int id -> struct block_list* (status_natural_heal processing)
  91. static DBMap* map_msg_db=NULL;
  92. static int map_users=0;
  93. #define BLOCK_SIZE 8
  94. #define block_free_max 1048576
  95. struct block_list *block_free[block_free_max];
  96. static int block_free_count = 0, block_free_lock = 0;
  97. #define BL_LIST_MAX 1048576
  98. static struct block_list *bl_list[BL_LIST_MAX];
  99. static int bl_list_count = 0;
  100. #define MAP_MAX_MSG 1500
  101. struct map_data map[MAX_MAP_PER_SERVER];
  102. int map_num = 0;
  103. int map_port=0;
  104. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  105. int minsave_interval = 100;
  106. int save_settings = 0xFFFF;
  107. int agit_flag = 0;
  108. int agit2_flag = 0;
  109. int night_flag = 0; // 0=day, 1=night [Yor]
  110. struct charid_request {
  111. struct charid_request* next;
  112. int charid;// who want to be notified of the nick
  113. };
  114. struct charid2nick {
  115. char nick[NAME_LENGTH];
  116. struct charid_request* requests;// requests of notification on this nick
  117. };
  118. // This is the main header found at the very beginning of the map cache
  119. struct map_cache_main_header {
  120. uint32 file_size;
  121. uint16 map_count;
  122. };
  123. // This is the header appended before every compressed map cells info in the map cache
  124. struct map_cache_map_info {
  125. char name[MAP_NAME_LENGTH];
  126. int16 xs;
  127. int16 ys;
  128. int32 len;
  129. };
  130. char db_path[256] = "db";
  131. char motd_txt[256] = "conf/motd.txt";
  132. char help_txt[256] = "conf/help.txt";
  133. char help2_txt[256] = "conf/help2.txt";
  134. char charhelp_txt[256] = "conf/charhelp.txt";
  135. char wisp_server_name[NAME_LENGTH] = "Server"; // can be modified in char-server configuration file
  136. int console = 0;
  137. int enable_spy = 0; //To enable/disable @spy commands, which consume too much cpu time when sending packets. [Skotlex]
  138. int enable_grf = 0; //To enable/disable reading maps from GRF files, bypassing mapcache [blackhole89]
  139. /*==========================================
  140. * server player count (of all mapservers)
  141. *------------------------------------------*/
  142. void map_setusers(int users)
  143. {
  144. map_users = users;
  145. }
  146. int map_getusers(void)
  147. {
  148. return map_users;
  149. }
  150. /*==========================================
  151. * server player count (this mapserver only)
  152. *------------------------------------------*/
  153. int map_usercount(void)
  154. {
  155. return pc_db->size(pc_db);
  156. }
  157. /*==========================================
  158. * Attempt to free a map blocklist
  159. *------------------------------------------*/
  160. int map_freeblock (struct block_list *bl)
  161. {
  162. nullpo_retr(block_free_lock, bl);
  163. if (block_free_lock == 0 || block_free_count >= block_free_max)
  164. {
  165. aFree(bl);
  166. bl = NULL;
  167. if (block_free_count >= block_free_max)
  168. ShowWarning("map_freeblock: too many free block! %d %d\n", block_free_count, block_free_lock);
  169. } else
  170. block_free[block_free_count++] = bl;
  171. return block_free_lock;
  172. }
  173. /*==========================================
  174. * Lock blocklist, (prevent map_freeblock usage)
  175. *------------------------------------------*/
  176. int map_freeblock_lock (void)
  177. {
  178. return ++block_free_lock;
  179. }
  180. /*==========================================
  181. * Remove the lock on map_bl
  182. *------------------------------------------*/
  183. int map_freeblock_unlock (void)
  184. {
  185. if ((--block_free_lock) == 0) {
  186. int i;
  187. for (i = 0; i < block_free_count; i++)
  188. {
  189. aFree(block_free[i]);
  190. block_free[i] = NULL;
  191. }
  192. block_free_count = 0;
  193. } else if (block_free_lock < 0) {
  194. ShowError("map_freeblock_unlock: lock count < 0 !\n");
  195. block_free_lock = 0;
  196. }
  197. return block_free_lock;
  198. }
  199. // Timer function to check if there some remaining lock and remove them if so.
  200. // Called each 1s
  201. int map_freeblock_timer(int tid, unsigned int tick, int id, intptr_t data)
  202. {
  203. if (block_free_lock > 0) {
  204. ShowError("map_freeblock_timer: block_free_lock(%d) is invalid.\n", block_free_lock);
  205. block_free_lock = 1;
  206. map_freeblock_unlock();
  207. }
  208. return 0;
  209. }
  210. //
  211. // blocklist
  212. //
  213. /*==========================================
  214. * Handling of map_bl[]
  215. * The adresse of bl_heal is set in bl->prev
  216. *------------------------------------------*/
  217. static struct block_list bl_head;
  218. #ifdef CELL_NOSTACK
  219. /*==========================================
  220. * These pair of functions update the counter of how many objects
  221. * lie on a tile.
  222. *------------------------------------------*/
  223. static void map_addblcell(struct block_list *bl)
  224. {
  225. if( bl->m<0 || bl->x<0 || bl->x>=map[bl->m].xs || bl->y<0 || bl->y>=map[bl->m].ys || !(bl->type&BL_CHAR) )
  226. return;
  227. map[bl->m].cell[bl->x+bl->y*map[bl->m].xs].cell_bl++;
  228. return;
  229. }
  230. static void map_delblcell(struct block_list *bl)
  231. {
  232. if( bl->m <0 || bl->x<0 || bl->x>=map[bl->m].xs || bl->y<0 || bl->y>=map[bl->m].ys || !(bl->type&BL_CHAR) )
  233. return;
  234. map[bl->m].cell[bl->x+bl->y*map[bl->m].xs].cell_bl--;
  235. }
  236. #endif
  237. /*==========================================
  238. * Adds a block to the map.
  239. * Returns 0 on success, 1 on failure (illegal coordinates).
  240. *------------------------------------------*/
  241. int map_addblock(struct block_list* bl)
  242. {
  243. int16 m, x, y;
  244. int pos;
  245. nullpo_ret(bl);
  246. if (bl->prev != NULL) {
  247. ShowError("map_addblock: bl->prev != NULL\n");
  248. return 1;
  249. }
  250. m = bl->m;
  251. x = bl->x;
  252. y = bl->y;
  253. if( m < 0 || m >= map_num )
  254. {
  255. ShowError("map_addblock: invalid map id (%d), only %d are loaded.\n", m, map_num);
  256. return 1;
  257. }
  258. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  259. {
  260. ShowError("map_addblock: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d\n", map[m].name, x, y, map[m].xs, map[m].ys);
  261. return 1;
  262. }
  263. pos = x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs;
  264. if (bl->type == BL_MOB) {
  265. bl->next = map[m].block_mob[pos];
  266. bl->prev = &bl_head;
  267. if (bl->next) bl->next->prev = bl;
  268. map[m].block_mob[pos] = bl;
  269. } else {
  270. bl->next = map[m].block[pos];
  271. bl->prev = &bl_head;
  272. if (bl->next) bl->next->prev = bl;
  273. map[m].block[pos] = bl;
  274. }
  275. #ifdef CELL_NOSTACK
  276. map_addblcell(bl);
  277. #endif
  278. return 0;
  279. }
  280. /*==========================================
  281. * Removes a block from the map.
  282. *------------------------------------------*/
  283. int map_delblock(struct block_list* bl)
  284. {
  285. int pos;
  286. nullpo_ret(bl);
  287. // blocklist (2ways chainlist)
  288. if (bl->prev == NULL) {
  289. if (bl->next != NULL) {
  290. // can't delete block (already at the begining of the chain)
  291. ShowError("map_delblock error : bl->next!=NULL\n");
  292. }
  293. return 0;
  294. }
  295. #ifdef CELL_NOSTACK
  296. map_delblcell(bl);
  297. #endif
  298. pos = bl->x/BLOCK_SIZE+(bl->y/BLOCK_SIZE)*map[bl->m].bxs;
  299. if (bl->next)
  300. bl->next->prev = bl->prev;
  301. if (bl->prev == &bl_head) {
  302. //Since the head of the list, update the block_list map of []
  303. if (bl->type == BL_MOB) {
  304. map[bl->m].block_mob[pos] = bl->next;
  305. } else {
  306. map[bl->m].block[pos] = bl->next;
  307. }
  308. } else {
  309. bl->prev->next = bl->next;
  310. }
  311. bl->next = NULL;
  312. bl->prev = NULL;
  313. return 0;
  314. }
  315. /*==========================================
  316. * Moves a block a x/y target position. [Skotlex]
  317. * Pass flag as 1 to prevent doing skill_unit_move checks
  318. * (which are executed by default on BL_CHAR types)
  319. *------------------------------------------*/
  320. int map_moveblock(struct block_list *bl, int x1, int y1, unsigned int tick)
  321. {
  322. int x0 = bl->x, y0 = bl->y;
  323. struct status_change *sc = NULL;
  324. int moveblock = ( x0/BLOCK_SIZE != x1/BLOCK_SIZE || y0/BLOCK_SIZE != y1/BLOCK_SIZE);
  325. if (!bl->prev) {
  326. //Block not in map, just update coordinates, but do naught else.
  327. bl->x = x1;
  328. bl->y = y1;
  329. return 0;
  330. }
  331. //TODO: Perhaps some outs of bounds checking should be placed here?
  332. if (bl->type&BL_CHAR) {
  333. sc = status_get_sc(bl);
  334. skill_unit_move(bl,tick,2);
  335. status_change_end(bl, SC_CLOSECONFINE, INVALID_TIMER);
  336. status_change_end(bl, SC_CLOSECONFINE2, INVALID_TIMER);
  337. status_change_end(bl, SC_TINDER_BREAKER, INVALID_TIMER);
  338. status_change_end(bl, SC_TINDER_BREAKER2, INVALID_TIMER);
  339. // status_change_end(bl, SC_BLADESTOP, INVALID_TIMER); //Won't stop when you are knocked away, go figure...
  340. status_change_end(bl, SC_TATAMIGAESHI, INVALID_TIMER);
  341. status_change_end(bl, SC_MAGICROD, INVALID_TIMER);
  342. if (sc->data[SC_PROPERTYWALK] &&
  343. sc->data[SC_PROPERTYWALK]->val3 >= skill_get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2) )
  344. status_change_end(bl,SC_PROPERTYWALK,INVALID_TIMER);
  345. } else
  346. if (bl->type == BL_NPC)
  347. npc_unsetcells((TBL_NPC*)bl);
  348. if (moveblock) map_delblock(bl);
  349. #ifdef CELL_NOSTACK
  350. else map_delblcell(bl);
  351. #endif
  352. bl->x = x1;
  353. bl->y = y1;
  354. if (moveblock) map_addblock(bl);
  355. #ifdef CELL_NOSTACK
  356. else map_addblcell(bl);
  357. #endif
  358. if (bl->type&BL_CHAR) {
  359. skill_unit_move(bl,tick,3);
  360. if( bl->type == BL_PC && ((TBL_PC*)bl)->shadowform_id ) {//Shadow Form Target Moving
  361. struct block_list *d_bl;
  362. if( (d_bl = map_id2bl(((TBL_PC*)bl)->shadowform_id)) == NULL || !check_distance_bl(bl,d_bl,10) ) {
  363. if( d_bl )
  364. status_change_end(d_bl,SC__SHADOWFORM,INVALID_TIMER);
  365. ((TBL_PC*)bl)->shadowform_id = 0;
  366. }
  367. }
  368. if (sc && sc->count) {
  369. if (sc->data[SC_DANCING])
  370. skill_unit_move_unit_group(skill_id2group(sc->data[SC_DANCING]->val2), bl->m, x1-x0, y1-y0);
  371. else {
  372. if (sc->data[SC_CLOAKING] && sc->data[SC_CLOAKING]->val1 < 3 && !skill_check_cloaking(bl, NULL))
  373. status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
  374. if (sc->data[SC_WARM])
  375. skill_unit_move_unit_group(skill_id2group(sc->data[SC_WARM]->val4), bl->m, x1-x0, y1-y0);
  376. if (sc->data[SC_BANDING])
  377. skill_unit_move_unit_group(skill_id2group(sc->data[SC_BANDING]->val4), bl->m, x1-x0, y1-y0);
  378. if (sc->data[SC_NEUTRALBARRIER_MASTER])
  379. skill_unit_move_unit_group(skill_id2group(sc->data[SC_NEUTRALBARRIER_MASTER]->val2), bl->m, x1-x0, y1-y0);
  380. else if (sc->data[SC_STEALTHFIELD_MASTER])
  381. skill_unit_move_unit_group(skill_id2group(sc->data[SC_STEALTHFIELD_MASTER]->val2), bl->m, x1-x0, y1-y0);
  382. if( sc->data[SC__SHADOWFORM] ) {//Shadow Form Caster Moving
  383. struct block_list *d_bl;
  384. if( (d_bl = map_id2bl(sc->data[SC__SHADOWFORM]->val2)) == NULL || !check_distance_bl(bl,d_bl,10) )
  385. status_change_end(bl,SC__SHADOWFORM,INVALID_TIMER);
  386. }
  387. if (sc->data[SC_PROPERTYWALK]
  388. && sc->data[SC_PROPERTYWALK]->val3 < skill_get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2)
  389. && map_find_skill_unit_oncell(bl,bl->x,bl->y,SO_ELECTRICWALK,NULL,0) == NULL
  390. && map_find_skill_unit_oncell(bl,bl->x,bl->y,SO_FIREWALK,NULL,0) == NULL
  391. && skill_unitsetting(bl,sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2,x0, y0,0)) {
  392. sc->data[SC_PROPERTYWALK]->val3++;
  393. }
  394. }
  395. /* Guild Aura Moving */
  396. if( bl->type == BL_PC && ((TBL_PC*)bl)->state.gmaster_flag ) {
  397. if (sc->data[SC_LEADERSHIP])
  398. skill_unit_move_unit_group(skill_id2group(sc->data[SC_LEADERSHIP]->val4), bl->m, x1-x0, y1-y0);
  399. if (sc->data[SC_GLORYWOUNDS])
  400. skill_unit_move_unit_group(skill_id2group(sc->data[SC_GLORYWOUNDS]->val4), bl->m, x1-x0, y1-y0);
  401. if (sc->data[SC_SOULCOLD])
  402. skill_unit_move_unit_group(skill_id2group(sc->data[SC_SOULCOLD]->val4), bl->m, x1-x0, y1-y0);
  403. if (sc->data[SC_HAWKEYES])
  404. skill_unit_move_unit_group(skill_id2group(sc->data[SC_HAWKEYES]->val4), bl->m, x1-x0, y1-y0);
  405. }
  406. }
  407. } else
  408. if (bl->type == BL_NPC)
  409. npc_setcells((TBL_NPC*)bl);
  410. return 0;
  411. }
  412. /*==========================================
  413. * Counts specified number of objects on given cell.
  414. *------------------------------------------*/
  415. int map_count_oncell(int16 m, int16 x, int16 y, int type)
  416. {
  417. int bx,by;
  418. struct block_list *bl;
  419. int count = 0;
  420. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  421. return 0;
  422. bx = x/BLOCK_SIZE;
  423. by = y/BLOCK_SIZE;
  424. if (type&~BL_MOB)
  425. for( bl = map[m].block[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  426. if(bl->x == x && bl->y == y && bl->type&type)
  427. count++;
  428. if (type&BL_MOB)
  429. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  430. if(bl->x == x && bl->y == y)
  431. count++;
  432. return count;
  433. }
  434. /*
  435. * Looks for a skill unit on a given cell
  436. * flag&1: runs battle_check_target check based on unit->group->target_flag
  437. */
  438. struct skill_unit* map_find_skill_unit_oncell(struct block_list* target,int16 x,int16 y,uint16 skill_id,struct skill_unit* out_unit, int flag) {
  439. int16 m,bx,by;
  440. struct block_list *bl;
  441. struct skill_unit *unit;
  442. m = target->m;
  443. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  444. return NULL;
  445. bx = x/BLOCK_SIZE;
  446. by = y/BLOCK_SIZE;
  447. for( bl = map[m].block[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  448. {
  449. if (bl->x != x || bl->y != y || bl->type != BL_SKILL)
  450. continue;
  451. unit = (struct skill_unit *) bl;
  452. if( unit == out_unit || !unit->alive || !unit->group || unit->group->skill_id != skill_id )
  453. continue;
  454. if( !(flag&1) || battle_check_target(&unit->bl,target,unit->group->target_flag) > 0 )
  455. return unit;
  456. }
  457. return NULL;
  458. }
  459. /*==========================================
  460. * Adapted from foreachinarea for an easier invocation. [Skotlex]
  461. *------------------------------------------*/
  462. int map_foreachinrange(int (*func)(struct block_list*,va_list), struct block_list* center, int16 range, int type, ...)
  463. {
  464. int bx, by, m;
  465. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  466. struct block_list *bl;
  467. int blockcount = bl_list_count, i;
  468. int x0, x1, y0, y1;
  469. va_list ap;
  470. m = center->m;
  471. x0 = max(center->x - range, 0);
  472. y0 = max(center->y - range, 0);
  473. x1 = min(center->x + range, map[ m ].xs - 1);
  474. y1 = min(center->y + range, map[ m ].ys - 1);
  475. if ( type&~BL_MOB )
  476. for ( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  477. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  478. for( bl = map[m].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  479. if( bl->type&type
  480. && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  481. #ifdef CIRCULAR_AREA
  482. && check_distance_bl(center, bl, range)
  483. #endif
  484. && bl_list_count < BL_LIST_MAX )
  485. bl_list[ bl_list_count++ ] = bl;
  486. }
  487. }
  488. }
  489. if( type&BL_MOB )
  490. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  491. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++) {
  492. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  493. if( bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  494. #ifdef CIRCULAR_AREA
  495. && check_distance_bl(center, bl, range)
  496. #endif
  497. && bl_list_count < BL_LIST_MAX )
  498. bl_list[ bl_list_count++ ] = bl;
  499. }
  500. }
  501. }
  502. if( bl_list_count >= BL_LIST_MAX )
  503. ShowWarning("map_foreachinrange: block count too many!\n");
  504. map_freeblock_lock();
  505. for( i = blockcount; i < bl_list_count; i++ )
  506. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  507. va_start(ap, type);
  508. returnCount += func(bl_list[ i ], ap);
  509. va_end(ap);
  510. }
  511. map_freeblock_unlock();
  512. bl_list_count = blockcount;
  513. return returnCount; //[Skotlex]
  514. }
  515. /*==========================================
  516. * Same as foreachinrange, but there must be a shoot-able range between center and target to be counted in. [Skotlex]
  517. *------------------------------------------*/
  518. int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block_list* center, int16 range, int type,...)
  519. {
  520. int bx, by, m;
  521. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  522. struct block_list *bl;
  523. int blockcount = bl_list_count, i;
  524. int x0, x1, y0, y1;
  525. va_list ap;
  526. m = center->m;
  527. if ( m < 0 )
  528. return 0;
  529. x0 = max(center->x-range, 0);
  530. y0 = max(center->y-range, 0);
  531. x1 = min(center->x+range, map[m].xs-1);
  532. y1 = min(center->y+range, map[m].ys-1);
  533. if ( type&~BL_MOB )
  534. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  535. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  536. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  537. if( bl->type&type
  538. && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  539. #ifdef CIRCULAR_AREA
  540. && check_distance_bl(center, bl, range)
  541. #endif
  542. && path_search_long(NULL, center->m, center->x, center->y, bl->x, bl->y, CELL_CHKWALL)
  543. && bl_list_count < BL_LIST_MAX )
  544. bl_list[ bl_list_count++ ] = bl;
  545. }
  546. }
  547. }
  548. if( type&BL_MOB )
  549. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  550. for( bx=x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  551. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  552. if( bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  553. #ifdef CIRCULAR_AREA
  554. && check_distance_bl(center, bl, range)
  555. #endif
  556. && path_search_long(NULL, center->m, center->x, center->y, bl->x, bl->y, CELL_CHKWALL)
  557. && bl_list_count < BL_LIST_MAX )
  558. bl_list[ bl_list_count++ ] = bl;
  559. }
  560. }
  561. }
  562. if( bl_list_count >= BL_LIST_MAX )
  563. ShowWarning("map_foreachinrange: block count too many!\n");
  564. map_freeblock_lock();
  565. for( i = blockcount; i < bl_list_count; i++ )
  566. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  567. va_start(ap, type);
  568. returnCount += func(bl_list[ i ], ap);
  569. va_end(ap);
  570. }
  571. map_freeblock_unlock();
  572. bl_list_count = blockcount;
  573. return returnCount; //[Skotlex]
  574. }
  575. /*==========================================
  576. * range = map m (x0,y0)-(x1,y1)
  577. * Apply *func with ... arguments for the range.
  578. * @type = BL_PC/BL_MOB etc..
  579. *------------------------------------------*/
  580. int map_foreachinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int type, ...)
  581. {
  582. int bx, by;
  583. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  584. struct block_list *bl;
  585. int blockcount = bl_list_count, i;
  586. va_list ap;
  587. if ( m < 0 )
  588. return 0;
  589. if ( x1 < x0 )
  590. swap(x0, x1);
  591. if ( y1 < y0 )
  592. swap(y0, y1);
  593. x0 = max(x0, 0);
  594. y0 = max(y0, 0);
  595. x1 = min(x1, map[ m ].xs - 1);
  596. y1 = min(y1, map[ m ].ys - 1);
  597. if ( type&~BL_MOB )
  598. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ )
  599. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ )
  600. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next )
  601. if( bl->type&type && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1 && bl_list_count < BL_LIST_MAX )
  602. bl_list[ bl_list_count++ ] = bl;
  603. if( type&BL_MOB )
  604. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ )
  605. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ )
  606. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next )
  607. if( bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1 && bl_list_count < BL_LIST_MAX )
  608. bl_list[ bl_list_count++ ] = bl;
  609. if( bl_list_count >= BL_LIST_MAX )
  610. ShowWarning("map_foreachinarea: block count too many!\n");
  611. map_freeblock_lock();
  612. for( i = blockcount; i < bl_list_count; i++ )
  613. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  614. va_start(ap, type);
  615. returnCount += func(bl_list[ i ], ap);
  616. va_end(ap);
  617. }
  618. map_freeblock_unlock();
  619. bl_list_count = blockcount;
  620. return returnCount; //[Skotlex]
  621. }
  622. /*==========================================
  623. * Adapted from forcountinarea for an easier invocation. [pakpil]
  624. *------------------------------------------*/
  625. int map_forcountinrange(int (*func)(struct block_list*,va_list), struct block_list* center, int16 range, int count, int type, ...)
  626. {
  627. int bx, by, m;
  628. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  629. struct block_list *bl;
  630. int blockcount = bl_list_count, i;
  631. int x0, x1, y0, y1;
  632. va_list ap;
  633. m = center->m;
  634. x0 = max(center->x - range, 0);
  635. y0 = max(center->y - range, 0);
  636. x1 = min(center->x + range, map[ m ].xs - 1);
  637. y1 = min(center->y + range, map[ m ].ys - 1);
  638. if ( type&~BL_MOB )
  639. for ( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  640. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  641. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  642. if( bl->type&type
  643. && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  644. #ifdef CIRCULAR_AREA
  645. && check_distance_bl(center, bl, range)
  646. #endif
  647. && bl_list_count < BL_LIST_MAX )
  648. bl_list[ bl_list_count++ ] = bl;
  649. }
  650. }
  651. }
  652. if( type&BL_MOB )
  653. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  654. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ){
  655. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  656. if( bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1
  657. #ifdef CIRCULAR_AREA
  658. && check_distance_bl(center, bl, range)
  659. #endif
  660. && bl_list_count < BL_LIST_MAX )
  661. bl_list[ bl_list_count++ ] = bl;
  662. }
  663. }
  664. }
  665. if( bl_list_count >= BL_LIST_MAX )
  666. ShowWarning("map_forcountinrange: block count too many!\n");
  667. map_freeblock_lock();
  668. for( i = blockcount; i < bl_list_count; i++ )
  669. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  670. va_start(ap, type);
  671. returnCount += func(bl_list[ i ], ap);
  672. va_end(ap);
  673. if( count && returnCount >= count )
  674. break;
  675. }
  676. map_freeblock_unlock();
  677. bl_list_count = blockcount;
  678. return returnCount; //[Skotlex]
  679. }
  680. int map_forcountinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int count, int type, ...)
  681. {
  682. int bx, by;
  683. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  684. struct block_list *bl;
  685. int blockcount = bl_list_count, i;
  686. va_list ap;
  687. if ( m < 0 )
  688. return 0;
  689. if ( x1 < x0 )
  690. swap(x0, x1);
  691. if ( y1 < y0 )
  692. swap(y0, y1);
  693. x0 = max(x0, 0);
  694. y0 = max(y0, 0);
  695. x1 = min(x1, map[ m ].xs - 1);
  696. y1 = min(y1, map[ m ].ys - 1);
  697. if ( type&~BL_MOB )
  698. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ )
  699. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ )
  700. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next )
  701. if( bl->type&type && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1 && bl_list_count < BL_LIST_MAX )
  702. bl_list[ bl_list_count++ ] = bl;
  703. if( type&BL_MOB )
  704. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ )
  705. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ )
  706. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next )
  707. if( bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1 && bl_list_count < BL_LIST_MAX )
  708. bl_list[ bl_list_count++ ] = bl;
  709. if( bl_list_count >= BL_LIST_MAX )
  710. ShowWarning("map_foreachinarea: block count too many!\n");
  711. map_freeblock_lock();
  712. for( i = blockcount; i < bl_list_count; i++ )
  713. if(bl_list[ i ]->prev) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  714. va_start(ap, type);
  715. returnCount += func(bl_list[ i ], ap);
  716. va_end(ap);
  717. if( count && returnCount >= count )
  718. break;
  719. }
  720. map_freeblock_unlock();
  721. bl_list_count = blockcount;
  722. return returnCount; //[Skotlex]
  723. }
  724. /*==========================================
  725. * For what I get
  726. * Move bl and do func* with va_list while moving.
  727. * Mouvement is set by dx dy wich are distance in x and y
  728. *------------------------------------------*/
  729. int map_foreachinmovearea(int (*func)(struct block_list*,va_list), struct block_list* center, int16 range, int16 dx, int16 dy, int type, ...)
  730. {
  731. int bx, by, m;
  732. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  733. struct block_list *bl;
  734. int blockcount = bl_list_count, i;
  735. int x0, x1, y0, y1;
  736. va_list ap;
  737. if ( !range ) return 0;
  738. if ( !dx && !dy ) return 0; //No movement.
  739. m = center->m;
  740. x0 = center->x - range;
  741. x1 = center->x + range;
  742. y0 = center->y - range;
  743. y1 = center->y + range;
  744. if ( x1 < x0 )
  745. swap(x0, x1);
  746. if ( y1 < y0 )
  747. swap(y0, y1);
  748. if( dx == 0 || dy == 0 ) {
  749. //Movement along one axis only.
  750. if( dx == 0 ){
  751. if( dy < 0 ) //Moving south
  752. y0 = y1 + dy + 1;
  753. else //North
  754. y1 = y0 + dy - 1;
  755. } else { //dy == 0
  756. if( dx < 0 ) //West
  757. x0 = x1 + dx + 1;
  758. else //East
  759. x1 = x0 + dx - 1;
  760. }
  761. x0 = max(x0, 0);
  762. y0 = max(y0, 0);
  763. x1 = min(x1, map[ m ].xs - 1);
  764. y1 = min(y1, map[ m ].ys - 1);
  765. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  766. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  767. if ( type&~BL_MOB ) {
  768. for( bl = map[m].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  769. if( bl->type&type &&
  770. bl->x >= x0 && bl->x <= x1 &&
  771. bl->y >= y0 && bl->y <= y1 &&
  772. bl_list_count < BL_LIST_MAX )
  773. bl_list[ bl_list_count++ ] = bl;
  774. }
  775. }
  776. if ( type&BL_MOB ) {
  777. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  778. if( bl->x >= x0 && bl->x <= x1 &&
  779. bl->y >= y0 && bl->y <= y1 &&
  780. bl_list_count < BL_LIST_MAX )
  781. bl_list[ bl_list_count++ ] = bl;
  782. }
  783. }
  784. }
  785. }
  786. } else { // Diagonal movement
  787. x0 = max(x0, 0);
  788. y0 = max(y0, 0);
  789. x1 = min(x1, map[ m ].xs - 1);
  790. y1 = min(y1, map[ m ].ys - 1);
  791. for( by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++ ) {
  792. for( bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++ ) {
  793. if ( type & ~BL_MOB ) {
  794. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  795. if( bl->type&type &&
  796. bl->x >= x0 && bl->x <= x1 &&
  797. bl->y >= y0 && bl->y <= y1 &&
  798. bl_list_count < BL_LIST_MAX )
  799. if( ( dx > 0 && bl->x < x0 + dx) ||
  800. ( dx < 0 && bl->x > x1 + dx) ||
  801. ( dy > 0 && bl->y < y0 + dy) ||
  802. ( dy < 0 && bl->y > y1 + dy) )
  803. bl_list[ bl_list_count++ ] = bl;
  804. }
  805. }
  806. if ( type&BL_MOB ) {
  807. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  808. if( bl->x >= x0 && bl->x <= x1 &&
  809. bl->y >= y0 && bl->y <= y1 &&
  810. bl_list_count < BL_LIST_MAX)
  811. if( ( dx > 0 && bl->x < x0 + dx) ||
  812. ( dx < 0 && bl->x > x1 + dx) ||
  813. ( dy > 0 && bl->y < y0 + dy) ||
  814. ( dy < 0 && bl->y > y1 + dy) )
  815. bl_list[ bl_list_count++ ] = bl;
  816. }
  817. }
  818. }
  819. }
  820. }
  821. if( bl_list_count >= BL_LIST_MAX )
  822. ShowWarning("map_foreachinmovearea: block count too many!\n");
  823. map_freeblock_lock(); // Prohibit the release from memory
  824. for( i = blockcount; i < bl_list_count; i++ )
  825. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  826. va_start(ap, type);
  827. returnCount += func(bl_list[ i ], ap);
  828. va_end(ap);
  829. }
  830. map_freeblock_unlock(); // Allow Free
  831. bl_list_count = blockcount;
  832. return returnCount;
  833. }
  834. // -- moonsoul (added map_foreachincell which is a rework of map_foreachinarea but
  835. // which only checks the exact single x/y passed to it rather than an
  836. // area radius - may be more useful in some instances)
  837. //
  838. int map_foreachincell(int (*func)(struct block_list*,va_list), int16 m, int16 x, int16 y, int type, ...)
  839. {
  840. int bx, by;
  841. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  842. struct block_list *bl;
  843. int blockcount = bl_list_count, i;
  844. va_list ap;
  845. if ( x < 0 || y < 0 || x >= map[ m ].xs || y >= map[ m ].ys ) return 0;
  846. by = y / BLOCK_SIZE;
  847. bx = x / BLOCK_SIZE;
  848. if( type&~BL_MOB )
  849. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next )
  850. if( bl->type&type && bl->x == x && bl->y == y && bl_list_count < BL_LIST_MAX )
  851. bl_list[ bl_list_count++ ] = bl;
  852. if( type&BL_MOB )
  853. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs]; bl != NULL; bl = bl->next )
  854. if( bl->x == x && bl->y == y && bl_list_count < BL_LIST_MAX)
  855. bl_list[ bl_list_count++ ] = bl;
  856. if( bl_list_count >= BL_LIST_MAX )
  857. ShowWarning("map_foreachincell: block count too many!\n");
  858. map_freeblock_lock();
  859. for( i = blockcount; i < bl_list_count; i++ )
  860. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  861. va_start(ap, type);
  862. returnCount += func(bl_list[ i ], ap);
  863. va_end(ap);
  864. }
  865. map_freeblock_unlock();
  866. bl_list_count = blockcount;
  867. return returnCount;
  868. }
  869. /*============================================================
  870. * For checking a path between two points (x0, y0) and (x1, y1)
  871. *------------------------------------------------------------*/
  872. int map_foreachinpath(int (*func)(struct block_list*,va_list),int16 m,int16 x0,int16 y0,int16 x1,int16 y1,int16 range,int length, int type,...)
  873. {
  874. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  875. //////////////////////////////////////////////////////////////
  876. //
  877. // sharp shooting 3 [Skotlex]
  878. //
  879. //////////////////////////////////////////////////////////////
  880. // problem:
  881. // Same as Sharp Shooting 1. Hits all targets within range of
  882. // the line.
  883. // (t1,t2 t3 and t4 get hit)
  884. //
  885. // target 1
  886. // x t4
  887. // t2
  888. // t3 x
  889. // x
  890. // S
  891. //////////////////////////////////////////////////////////////
  892. // Methodology:
  893. // My trigonometrics and math are a little rusty... so the approach I am writing
  894. // here is basicly do a double for to check for all targets in the square that
  895. // contains the initial and final positions (area range increased to match the
  896. // radius given), then for each object to test, calculate the distance to the
  897. // path and include it if the range fits and the target is in the line (0<k<1,
  898. // as they call it).
  899. // The implementation I took as reference is found at
  900. // http://astronomy.swin.edu.au/~pbourke/geometry/pointline/
  901. // (they have a link to a C implementation, too)
  902. // This approach is a lot like #2 commented on this function, which I have no
  903. // idea why it was commented. I won't use doubles/floats, but pure int math for
  904. // speed purposes. The range considered is always the same no matter how
  905. // close/far the target is because that's how SharpShooting works currently in
  906. // kRO.
  907. //Generic map_foreach* variables.
  908. int i, blockcount = bl_list_count;
  909. struct block_list *bl;
  910. int bx, by;
  911. //method specific variables
  912. int magnitude2, len_limit; //The square of the magnitude
  913. int k, xi, yi, xu, yu;
  914. int mx0 = x0, mx1 = x1, my0 = y0, my1 = y1;
  915. va_list ap;
  916. //Avoid needless calculations by not getting the sqrt right away.
  917. #define MAGNITUDE2(x0, y0, x1, y1) ( ( ( x1 ) - ( x0 ) ) * ( ( x1 ) - ( x0 ) ) + ( ( y1 ) - ( y0 ) ) * ( ( y1 ) - ( y0 ) ) )
  918. if ( m < 0 )
  919. return 0;
  920. len_limit = magnitude2 = MAGNITUDE2(x0, y0, x1, y1);
  921. if ( magnitude2 < 1 ) //Same begin and ending point, can't trace path.
  922. return 0;
  923. if ( length ) { //Adjust final position to fit in the given area.
  924. //TODO: Find an alternate method which does not requires a square root calculation.
  925. k = (int)sqrt((float)magnitude2);
  926. mx1 = x0 + (x1 - x0) * length / k;
  927. my1 = y0 + (y1 - y0) * length / k;
  928. len_limit = MAGNITUDE2(x0, y0, mx1, my1);
  929. }
  930. //Expand target area to cover range.
  931. if ( mx0 > mx1 ) {
  932. mx0 += range;
  933. mx1 -= range;
  934. } else {
  935. mx0 -= range;
  936. mx1 += range;
  937. }
  938. if (my0 > my1) {
  939. my0 += range;
  940. my1 -= range;
  941. } else {
  942. my0 -= range;
  943. my1 += range;
  944. }
  945. //The two fors assume mx0 < mx1 && my0 < my1
  946. if ( mx0 > mx1 )
  947. swap(mx0, mx1);
  948. if ( my0 > my1 )
  949. swap(my0, my1);
  950. mx0 = max(mx0, 0);
  951. my0 = max(my0, 0);
  952. mx1 = min(mx1, map[ m ].xs - 1);
  953. my1 = min(my1, map[ m ].ys - 1);
  954. range *= range << 8; //Values are shifted later on for higher precision using int math.
  955. if ( type&~BL_MOB )
  956. for ( by = my0 / BLOCK_SIZE; by <= my1 / BLOCK_SIZE; by++ ) {
  957. for( bx = mx0 / BLOCK_SIZE; bx <= mx1 / BLOCK_SIZE; bx++ ) {
  958. for( bl = map[ m ].block[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  959. if( bl->prev && bl->type&type && bl_list_count < BL_LIST_MAX ) {
  960. xi = bl->x;
  961. yi = bl->y;
  962. k = ( xi - x0 ) * ( x1 - x0 ) + ( yi - y0 ) * ( y1 - y0 );
  963. if ( k < 0 || k > len_limit ) //Since more skills use this, check for ending point as well.
  964. continue;
  965. if ( k > magnitude2 && !path_search_long(NULL, m, x0, y0, xi, yi, CELL_CHKWALL) )
  966. continue; //Targets beyond the initial ending point need the wall check.
  967. //All these shifts are to increase the precision of the intersection point and distance considering how it's
  968. //int math.
  969. k = ( k << 4 ) / magnitude2; //k will be between 1~16 instead of 0~1
  970. xi <<= 4;
  971. yi <<= 4;
  972. xu = ( x0 << 4 ) + k * ( x1 - x0 );
  973. yu = ( y0 << 4 ) + k * ( y1 - y0 );
  974. k = MAGNITUDE2(xi, yi, xu, yu);
  975. //If all dot coordinates were <<4 the square of the magnitude is <<8
  976. if ( k > range )
  977. continue;
  978. bl_list[ bl_list_count++ ] = bl;
  979. }
  980. }
  981. }
  982. }
  983. if( type&BL_MOB )
  984. for( by = my0 / BLOCK_SIZE; by <= my1 / BLOCK_SIZE; by++ ) {
  985. for( bx = mx0 / BLOCK_SIZE; bx <= mx1 / BLOCK_SIZE; bx++ ) {
  986. for( bl = map[ m ].block_mob[ bx + by * map[ m ].bxs ]; bl != NULL; bl = bl->next ) {
  987. if( bl->prev && bl_list_count < BL_LIST_MAX ) {
  988. xi = bl->x;
  989. yi = bl->y;
  990. k = ( xi - x0 ) * ( x1 - x0 ) + ( yi - y0 ) * ( y1 - y0 );
  991. if ( k < 0 || k > len_limit )
  992. continue;
  993. if ( k > magnitude2 && !path_search_long(NULL, m, x0, y0, xi, yi, CELL_CHKWALL) )
  994. continue; //Targets beyond the initial ending point need the wall check.
  995. k = ( k << 4 ) / magnitude2; //k will be between 1~16 instead of 0~1
  996. xi <<= 4;
  997. yi <<= 4;
  998. xu = ( x0 << 4 ) + k * ( x1 - x0 );
  999. yu = ( y0 << 4 ) + k * ( y1 - y0 );
  1000. k = MAGNITUDE2(xi, yi, xu, yu);
  1001. //If all dot coordinates were <<4 the square of the magnitude is <<8
  1002. if ( k > range )
  1003. continue;
  1004. bl_list[ bl_list_count++ ] = bl;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. if( bl_list_count >= BL_LIST_MAX )
  1010. ShowWarning("map_foreachinpath: block count too many!\n");
  1011. map_freeblock_lock();
  1012. for( i = blockcount; i < bl_list_count; i++ )
  1013. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  1014. va_start(ap, type);
  1015. returnCount += func(bl_list[ i ], ap);
  1016. va_end(ap);
  1017. }
  1018. map_freeblock_unlock();
  1019. bl_list_count = blockcount;
  1020. return returnCount; //[Skotlex]
  1021. }
  1022. // Copy of map_foreachincell, but applied to the whole map. [Skotlex]
  1023. int map_foreachinmap(int (*func)(struct block_list*,va_list), int16 m, int type,...)
  1024. {
  1025. int b, bsize;
  1026. int returnCount = 0; //total sum of returned values of func() [Skotlex]
  1027. struct block_list *bl;
  1028. int blockcount = bl_list_count, i;
  1029. va_list ap;
  1030. bsize = map[ m ].bxs * map[ m ].bys;
  1031. if( type&~BL_MOB )
  1032. for( b = 0; b < bsize; b++ )
  1033. for( bl = map[ m ].block[ b ]; bl != NULL; bl = bl->next )
  1034. if( bl->type&type && bl_list_count < BL_LIST_MAX )
  1035. bl_list[ bl_list_count++ ] = bl;
  1036. if( type&BL_MOB )
  1037. for( b = 0; b < bsize; b++ )
  1038. for( bl = map[ m ].block_mob[ b ]; bl != NULL; bl = bl->next )
  1039. if( bl_list_count < BL_LIST_MAX )
  1040. bl_list[ bl_list_count++ ] = bl;
  1041. if( bl_list_count >= BL_LIST_MAX )
  1042. ShowWarning("map_foreachinmap: block count too many!\n");
  1043. map_freeblock_lock();
  1044. for( i = blockcount; i < bl_list_count ; i++ )
  1045. if( bl_list[ i ]->prev ) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
  1046. va_start(ap, type);
  1047. returnCount += func(bl_list[ i ], ap);
  1048. va_end(ap);
  1049. }
  1050. map_freeblock_unlock();
  1051. bl_list_count = blockcount;
  1052. return returnCount;
  1053. }
  1054. /// Generates a new flooritem object id from the interval [MIN_FLOORITEM, MAX_FLOORITEM).
  1055. /// Used for floor items, skill units and chatroom objects.
  1056. /// @return The new object id
  1057. int map_get_new_object_id(void)
  1058. {
  1059. static int last_object_id = MIN_FLOORITEM - 1;
  1060. int i;
  1061. // find a free id
  1062. i = last_object_id + 1;
  1063. while( i != last_object_id ) {
  1064. if( i == MAX_FLOORITEM )
  1065. i = MIN_FLOORITEM;
  1066. if( !idb_exists(id_db, i) )
  1067. break;
  1068. ++i;
  1069. }
  1070. if( i == last_object_id ) {
  1071. ShowError("map_addobject: no free object id!\n");
  1072. return 0;
  1073. }
  1074. // update cursor
  1075. last_object_id = i;
  1076. return i;
  1077. }
  1078. /*==========================================
  1079. * Timered function to clear the floor (remove remaining item)
  1080. * Called each flooritem_lifetime ms
  1081. *------------------------------------------*/
  1082. int map_clearflooritem_timer(int tid, unsigned int tick, int id, intptr_t data)
  1083. {
  1084. struct flooritem_data* fitem = (struct flooritem_data*)idb_get(id_db, id);
  1085. if (fitem == NULL || fitem->bl.type != BL_ITEM || (fitem->cleartimer != tid)) {
  1086. ShowError("map_clearflooritem_timer : error\n");
  1087. return 1;
  1088. }
  1089. if (search_petDB_index(fitem->item_data.nameid, PET_EGG) >= 0)
  1090. intif_delete_petdata(MakeDWord(fitem->item_data.card[1], fitem->item_data.card[2]));
  1091. clif_clearflooritem(fitem, 0);
  1092. map_deliddb(&fitem->bl);
  1093. map_delblock(&fitem->bl);
  1094. map_freeblock(&fitem->bl);
  1095. return 0;
  1096. }
  1097. /*
  1098. * clears a single bl item out of the map.
  1099. */
  1100. void map_clearflooritem(struct block_list *bl) {
  1101. struct flooritem_data* fitem = (struct flooritem_data*)bl;
  1102. if( fitem->cleartimer )
  1103. delete_timer(fitem->cleartimer,map_clearflooritem_timer);
  1104. clif_clearflooritem(fitem, 0);
  1105. map_deliddb(&fitem->bl);
  1106. map_delblock(&fitem->bl);
  1107. map_freeblock(&fitem->bl);
  1108. }
  1109. /*==========================================
  1110. * (m,x,y) locates a random available free cell around the given coordinates
  1111. * to place an BL_ITEM object. Scan area is 9x9, returns 1 on success.
  1112. * x and y are modified with the target cell when successful.
  1113. *------------------------------------------*/
  1114. int map_searchrandfreecell(int16 m,int16 *x,int16 *y,int stack) {
  1115. int free_cell,i,j;
  1116. int free_cells[9][2];
  1117. for(free_cell=0,i=-1;i<=1;i++){
  1118. if(i+*y<0 || i+*y>=map[m].ys)
  1119. continue;
  1120. for(j=-1;j<=1;j++){
  1121. if(j+*x<0 || j+*x>=map[m].xs)
  1122. continue;
  1123. if(map_getcell(m,j+*x,i+*y,CELL_CHKNOPASS) && !map_getcell(m,j+*x,i+*y,CELL_CHKICEWALL))
  1124. continue;
  1125. //Avoid item stacking to prevent against exploits. [Skotlex]
  1126. if(stack && map_count_oncell(m,j+*x,i+*y, BL_ITEM) > stack)
  1127. continue;
  1128. free_cells[free_cell][0] = j+*x;
  1129. free_cells[free_cell++][1] = i+*y;
  1130. }
  1131. }
  1132. if(free_cell==0)
  1133. return 0;
  1134. free_cell = rnd()%free_cell;
  1135. *x = free_cells[free_cell][0];
  1136. *y = free_cells[free_cell][1];
  1137. return 1;
  1138. }
  1139. static int map_count_sub(struct block_list *bl,va_list ap)
  1140. {
  1141. return 1;
  1142. }
  1143. /*==========================================
  1144. * Locates a random spare cell around the object given, using range as max
  1145. * distance from that spot. Used for warping functions. Use range < 0 for
  1146. * whole map range.
  1147. * Returns 1 on success. when it fails and src is available, x/y are set to src's
  1148. * src can be null as long as flag&1
  1149. * when ~flag&1, m is not needed.
  1150. * Flag values:
  1151. * &1 = random cell must be around given m,x,y, not around src
  1152. * &2 = the target should be able to walk to the target tile.
  1153. * &4 = there shouldn't be any players around the target tile (use the no_spawn_on_player setting)
  1154. *------------------------------------------*/
  1155. int map_search_freecell(struct block_list *src, int16 m, int16 *x,int16 *y, int16 rx, int16 ry, int flag)
  1156. {
  1157. int tries, spawn=0;
  1158. int bx, by;
  1159. int rx2 = 2*rx+1;
  1160. int ry2 = 2*ry+1;
  1161. if( !src && (!(flag&1) || flag&2) )
  1162. {
  1163. ShowDebug("map_search_freecell: Incorrect usage! When src is NULL, flag has to be &1 and can't have &2\n");
  1164. return 0;
  1165. }
  1166. if (flag&1) {
  1167. bx = *x;
  1168. by = *y;
  1169. } else {
  1170. bx = src->x;
  1171. by = src->y;
  1172. m = src->m;
  1173. }
  1174. if (!rx && !ry) {
  1175. //No range? Return the target cell then....
  1176. *x = bx;
  1177. *y = by;
  1178. return map_getcell(m,*x,*y,CELL_CHKREACH);
  1179. }
  1180. if (rx >= 0 && ry >= 0) {
  1181. tries = rx2*ry2;
  1182. if (tries > 100) tries = 100;
  1183. } else {
  1184. tries = map[m].xs*map[m].ys;
  1185. if (tries > 500) tries = 500;
  1186. }
  1187. while(tries--) {
  1188. *x = (rx >= 0)?(rnd()%rx2-rx+bx):(rnd()%(map[m].xs-2)+1);
  1189. *y = (ry >= 0)?(rnd()%ry2-ry+by):(rnd()%(map[m].ys-2)+1);
  1190. if (*x == bx && *y == by)
  1191. continue; //Avoid picking the same target tile.
  1192. if (map_getcell(m,*x,*y,CELL_CHKREACH))
  1193. {
  1194. if(flag&2 && !unit_can_reach_pos(src, *x, *y, 1))
  1195. continue;
  1196. if(flag&4) {
  1197. if (spawn >= 100) return 0; //Limit of retries reached.
  1198. if (spawn++ < battle_config.no_spawn_on_player &&
  1199. map_foreachinarea(map_count_sub, m,
  1200. *x-AREA_SIZE, *y-AREA_SIZE,
  1201. *x+AREA_SIZE, *y+AREA_SIZE, BL_PC)
  1202. )
  1203. continue;
  1204. }
  1205. return 1;
  1206. }
  1207. }
  1208. *x = bx;
  1209. *y = by;
  1210. return 0;
  1211. }
  1212. /*==========================================
  1213. * Add an item to location (m,x,y)
  1214. * Parameters
  1215. * @item_data item attributes
  1216. * @amount quantity
  1217. * @m, @x, @y mapid,x,y
  1218. * @first_charid, @second_charid, @third_charid, looting priority
  1219. * @flag: &1 MVP item. &2 do stacking check. &4 bypass droppable check.
  1220. *------------------------------------------*/
  1221. int map_addflooritem(struct item *item_data,int amount,int16 m,int16 x,int16 y,int first_charid,int second_charid,int third_charid,int flags)
  1222. {
  1223. int r;
  1224. struct flooritem_data *fitem=NULL;
  1225. nullpo_ret(item_data);
  1226. if(!(flags&4) && battle_config.item_onfloor && (itemdb_traderight(item_data->nameid)&1) )
  1227. return 0; //can't be dropped
  1228. if(!map_searchrandfreecell(m,&x,&y,flags&2?1:0))
  1229. return 0;
  1230. r=rnd();
  1231. CREATE(fitem, struct flooritem_data, 1);
  1232. fitem->bl.type=BL_ITEM;
  1233. fitem->bl.prev = fitem->bl.next = NULL;
  1234. fitem->bl.m=m;
  1235. fitem->bl.x=x;
  1236. fitem->bl.y=y;
  1237. fitem->bl.id = map_get_new_object_id();
  1238. if(fitem->bl.id==0){
  1239. aFree(fitem);
  1240. return 0;
  1241. }
  1242. fitem->first_get_charid = first_charid;
  1243. fitem->first_get_tick = gettick() + (flags&1 ? battle_config.mvp_item_first_get_time : battle_config.item_first_get_time);
  1244. fitem->second_get_charid = second_charid;
  1245. fitem->second_get_tick = fitem->first_get_tick + (flags&1 ? battle_config.mvp_item_second_get_time : battle_config.item_second_get_time);
  1246. fitem->third_get_charid = third_charid;
  1247. fitem->third_get_tick = fitem->second_get_tick + (flags&1 ? battle_config.mvp_item_third_get_time : battle_config.item_third_get_time);
  1248. memcpy(&fitem->item_data,item_data,sizeof(*item_data));
  1249. fitem->item_data.amount=amount;
  1250. fitem->subx=(r&3)*3+3;
  1251. fitem->suby=((r>>2)&3)*3+3;
  1252. fitem->cleartimer=add_timer(gettick()+battle_config.flooritem_lifetime,map_clearflooritem_timer,fitem->bl.id,0);
  1253. map_addiddb(&fitem->bl);
  1254. map_addblock(&fitem->bl);
  1255. clif_dropflooritem(fitem);
  1256. return fitem->bl.id;
  1257. }
  1258. /**
  1259. * @see DBCreateData
  1260. */
  1261. static DBData create_charid2nick(DBKey key, va_list args)
  1262. {
  1263. struct charid2nick *p;
  1264. CREATE(p, struct charid2nick, 1);
  1265. return db_ptr2data(p);
  1266. }
  1267. /// Adds(or replaces) the nick of charid to nick_db and fullfils pending requests.
  1268. /// Does nothing if the character is online.
  1269. void map_addnickdb(int charid, const char* nick)
  1270. {
  1271. struct charid2nick* p;
  1272. struct charid_request* req;
  1273. struct map_session_data* sd;
  1274. if( map_charid2sd(charid) )
  1275. return;// already online
  1276. p = idb_ensure(nick_db, charid, create_charid2nick);
  1277. safestrncpy(p->nick, nick, sizeof(p->nick));
  1278. while( p->requests ) {
  1279. req = p->requests;
  1280. p->requests = req->next;
  1281. sd = map_charid2sd(req->charid);
  1282. if( sd )
  1283. clif_solved_charname(sd->fd, charid, p->nick);
  1284. aFree(req);
  1285. }
  1286. }
  1287. /// Removes the nick of charid from nick_db.
  1288. /// Sends name to all pending requests on charid.
  1289. void map_delnickdb(int charid, const char* name)
  1290. {
  1291. struct charid2nick* p;
  1292. struct charid_request* req;
  1293. struct map_session_data* sd;
  1294. DBData data;
  1295. if (!nick_db->remove(nick_db, db_i2key(charid), &data) || (p = db_data2ptr(&data)) == NULL)
  1296. return;
  1297. while( p->requests ) {
  1298. req = p->requests;
  1299. p->requests = req->next;
  1300. sd = map_charid2sd(req->charid);
  1301. if( sd )
  1302. clif_solved_charname(sd->fd, charid, name);
  1303. aFree(req);
  1304. }
  1305. aFree(p);
  1306. }
  1307. /// Notifies sd of the nick of charid.
  1308. /// Uses the name in the character if online.
  1309. /// Uses the name in nick_db if offline.
  1310. void map_reqnickdb(struct map_session_data * sd, int charid)
  1311. {
  1312. struct charid2nick* p;
  1313. struct charid_request* req;
  1314. struct map_session_data* tsd;
  1315. nullpo_retv(sd);
  1316. tsd = map_charid2sd(charid);
  1317. if( tsd )
  1318. {
  1319. clif_solved_charname(sd->fd, charid, tsd->status.name);
  1320. return;
  1321. }
  1322. p = idb_ensure(nick_db, charid, create_charid2nick);
  1323. if( *p->nick )
  1324. {
  1325. clif_solved_charname(sd->fd, charid, p->nick);
  1326. return;
  1327. }
  1328. // not in cache, request it
  1329. CREATE(req, struct charid_request, 1);
  1330. req->next = p->requests;
  1331. p->requests = req;
  1332. chrif_searchcharid(charid);
  1333. }
  1334. /*==========================================
  1335. * add bl to id_db
  1336. *------------------------------------------*/
  1337. void map_addiddb(struct block_list *bl)
  1338. {
  1339. nullpo_retv(bl);
  1340. if( bl->type == BL_PC )
  1341. {
  1342. TBL_PC* sd = (TBL_PC*)bl;
  1343. idb_put(pc_db,sd->bl.id,sd);
  1344. idb_put(charid_db,sd->status.char_id,sd);
  1345. }
  1346. else if( bl->type == BL_MOB )
  1347. {
  1348. TBL_MOB* md = (TBL_MOB*)bl;
  1349. idb_put(mobid_db,bl->id,bl);
  1350. if( md->state.boss )
  1351. idb_put(bossid_db, bl->id, bl);
  1352. }
  1353. if( bl->type & BL_REGEN )
  1354. idb_put(regen_db, bl->id, bl);
  1355. idb_put(id_db,bl->id,bl);
  1356. }
  1357. /*==========================================
  1358. * remove bl from id_db
  1359. *------------------------------------------*/
  1360. void map_deliddb(struct block_list *bl)
  1361. {
  1362. nullpo_retv(bl);
  1363. if( bl->type == BL_PC )
  1364. {
  1365. TBL_PC* sd = (TBL_PC*)bl;
  1366. idb_remove(pc_db,sd->bl.id);
  1367. idb_remove(charid_db,sd->status.char_id);
  1368. }
  1369. else if( bl->type == BL_MOB )
  1370. {
  1371. idb_remove(mobid_db,bl->id);
  1372. idb_remove(bossid_db,bl->id);
  1373. }
  1374. if( bl->type & BL_REGEN )
  1375. idb_remove(regen_db,bl->id);
  1376. idb_remove(id_db,bl->id);
  1377. }
  1378. /*==========================================
  1379. * Standard call when a player connection is closed.
  1380. *------------------------------------------*/
  1381. int map_quit(struct map_session_data *sd) {
  1382. int i;
  1383. if(!sd->state.active) { //Removing a player that is not active.
  1384. struct auth_node *node = chrif_search(sd->status.account_id);
  1385. if (node && node->char_id == sd->status.char_id &&
  1386. node->state != ST_LOGOUT)
  1387. //Except when logging out, clear the auth-connect data immediately.
  1388. chrif_auth_delete(node->account_id, node->char_id, node->state);
  1389. //Non-active players should not have loaded any data yet (or it was cleared already) so no additional cleanups are needed.
  1390. return 0;
  1391. }
  1392. if (sd->npc_timer_id != INVALID_TIMER) //Cancel the event timer.
  1393. npc_timerevent_quit(sd);
  1394. if (sd->npc_id)
  1395. npc_event_dequeue(sd);
  1396. if( sd->bg_id )
  1397. bg_team_leave(sd,1);
  1398. pc_itemcd_do(sd,false);
  1399. npc_script_event(sd, NPCE_LOGOUT);
  1400. //Unit_free handles clearing the player related data,
  1401. //map_quit handles extra specific data which is related to quitting normally
  1402. //(changing map-servers invokes unit_free but bypasses map_quit)
  1403. if( sd->sc.count ) {
  1404. //Status that are not saved...
  1405. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  1406. status_change_end(&sd->bl, SC_AUTOTRADE, INVALID_TIMER);
  1407. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  1408. status_change_end(&sd->bl, SC_BERSERK, INVALID_TIMER);
  1409. status_change_end(&sd->bl, SC__BLOODYLUST, INVALID_TIMER);
  1410. status_change_end(&sd->bl, SC_TRICKDEAD, INVALID_TIMER);
  1411. status_change_end(&sd->bl, SC_LEADERSHIP, INVALID_TIMER);
  1412. status_change_end(&sd->bl, SC_GLORYWOUNDS, INVALID_TIMER);
  1413. status_change_end(&sd->bl, SC_SOULCOLD, INVALID_TIMER);
  1414. status_change_end(&sd->bl, SC_HAWKEYES, INVALID_TIMER);
  1415. if(sd->sc.data[SC_ENDURE] && sd->sc.data[SC_ENDURE]->val4)
  1416. status_change_end(&sd->bl, SC_ENDURE, INVALID_TIMER); //No need to save infinite endure.
  1417. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1418. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1419. status_change_end(&sd->bl, SC_SATURDAYNIGHTFEVER, INVALID_TIMER);
  1420. status_change_end(&sd->bl, SC_KYOUGAKU, INVALID_TIMER);
  1421. if (battle_config.debuff_on_logout&1) {
  1422. status_change_end(&sd->bl, SC_ORCISH, INVALID_TIMER);
  1423. status_change_end(&sd->bl, SC_STRIPWEAPON, INVALID_TIMER);
  1424. status_change_end(&sd->bl, SC_STRIPARMOR, INVALID_TIMER);
  1425. status_change_end(&sd->bl, SC_STRIPSHIELD, INVALID_TIMER);
  1426. status_change_end(&sd->bl, SC_STRIPHELM, INVALID_TIMER);
  1427. status_change_end(&sd->bl, SC_EXTREMITYFIST, INVALID_TIMER);
  1428. status_change_end(&sd->bl, SC_EXPLOSIONSPIRITS, INVALID_TIMER);
  1429. if(sd->sc.data[SC_REGENERATION] && sd->sc.data[SC_REGENERATION]->val4)
  1430. status_change_end(&sd->bl, SC_REGENERATION, INVALID_TIMER);
  1431. //TO-DO Probably there are way more NPC_type negative status that are removed
  1432. status_change_end(&sd->bl, SC_CHANGEUNDEAD, INVALID_TIMER);
  1433. // Both these statuses are removed on logout. [L0ne_W0lf]
  1434. status_change_end(&sd->bl, SC_SLOWCAST, INVALID_TIMER);
  1435. status_change_end(&sd->bl, SC_CRITICALWOUND, INVALID_TIMER);
  1436. }
  1437. if (battle_config.debuff_on_logout&2) {
  1438. status_change_end(&sd->bl, SC_MAXIMIZEPOWER, INVALID_TIMER);
  1439. status_change_end(&sd->bl, SC_MAXOVERTHRUST, INVALID_TIMER);
  1440. status_change_end(&sd->bl, SC_STEELBODY, INVALID_TIMER);
  1441. status_change_end(&sd->bl, SC_PRESERVE, INVALID_TIMER);
  1442. status_change_end(&sd->bl, SC_KAAHI, INVALID_TIMER);
  1443. status_change_end(&sd->bl, SC_SPIRIT, INVALID_TIMER);
  1444. }
  1445. }
  1446. for (i = 0; i < EQI_MAX; i++) {
  1447. if (sd->equip_index[i] >= 0)
  1448. if (!pc_isequip(sd,sd->equip_index[i]))
  1449. pc_unequipitem(sd,sd->equip_index[i],2);
  1450. }
  1451. // Return loot to owner
  1452. if( sd->pd ) pet_lootitem_drop(sd->pd, sd);
  1453. if( sd->state.storage_flag == 1 ) sd->state.storage_flag = 0; // No need to Double Save Storage on Quit.
  1454. if (sd->state.permanent_speed == 1) sd->state.permanent_speed = 0; // Remove lock so speed is set back to normal at login.
  1455. if( map[sd->bl.m].instance_id )
  1456. instance_delusers(map[sd->bl.m].instance_id);
  1457. if( sd->ed ) {
  1458. elemental_clean_effect(sd->ed);
  1459. unit_remove_map(&sd->ed->bl,CLR_TELEPORT);
  1460. }
  1461. unit_remove_map_pc(sd,CLR_TELEPORT);
  1462. if( map[sd->bl.m].instance_id ) { // Avoid map conflicts and warnings on next login
  1463. int16 m;
  1464. struct point *pt;
  1465. if( map[sd->bl.m].save.map )
  1466. pt = &map[sd->bl.m].save;
  1467. else
  1468. pt = &sd->status.save_point;
  1469. if( (m=map_mapindex2mapid(pt->map)) >= 0 )
  1470. {
  1471. sd->bl.m = m;
  1472. sd->bl.x = pt->x;
  1473. sd->bl.y = pt->y;
  1474. sd->mapindex = pt->map;
  1475. }
  1476. }
  1477. pc_damage_log_clear(sd,0);
  1478. party_booking_delete(sd); // Party Booking [Spiria]
  1479. pc_makesavestatus(sd);
  1480. pc_clean_skilltree(sd);
  1481. chrif_save(sd,1);
  1482. unit_free_pc(sd);
  1483. return 0;
  1484. }
  1485. /*==========================================
  1486. * Lookup, id to session (player,mob,npc,homon,merc..)
  1487. *------------------------------------------*/
  1488. struct map_session_data * map_id2sd(int id){
  1489. if (id <= 0) return NULL;
  1490. return (struct map_session_data*)idb_get(pc_db,id);
  1491. }
  1492. struct mob_data * map_id2md(int id){
  1493. if (id <= 0) return NULL;
  1494. return (struct mob_data*)idb_get(mobid_db,id);
  1495. }
  1496. struct npc_data * map_id2nd(int id){
  1497. struct block_list* bl = map_id2bl(id);
  1498. return BL_CAST(BL_NPC, bl);
  1499. }
  1500. struct homun_data* map_id2hd(int id){
  1501. struct block_list* bl = map_id2bl(id);
  1502. return BL_CAST(BL_HOM, bl);
  1503. }
  1504. struct mercenary_data* map_id2mc(int id){
  1505. struct block_list* bl = map_id2bl(id);
  1506. return BL_CAST(BL_MER, bl);
  1507. }
  1508. struct pet_data* map_id2pd(int id){
  1509. struct block_list* bl = map_id2bl(id);
  1510. return BL_CAST(BL_PET, bl);
  1511. }
  1512. struct chat_data* map_id2cd(int id){
  1513. struct block_list* bl = map_id2bl(id);
  1514. return BL_CAST(BL_CHAT, bl);
  1515. }
  1516. /// Returns the nick of the target charid or NULL if unknown (requests the nick to the char server).
  1517. const char* map_charid2nick(int charid)
  1518. {
  1519. struct charid2nick *p;
  1520. struct map_session_data* sd;
  1521. sd = map_charid2sd(charid);
  1522. if( sd )
  1523. return sd->status.name;// character is online, return it's name
  1524. p = idb_ensure(nick_db, charid, create_charid2nick);
  1525. if( *p->nick )
  1526. return p->nick;// name in nick_db
  1527. chrif_searchcharid(charid);// request the name
  1528. return NULL;
  1529. }
  1530. /// Returns the struct map_session_data of the charid or NULL if the char is not online.
  1531. struct map_session_data* map_charid2sd(int charid)
  1532. {
  1533. return (struct map_session_data*)idb_get(charid_db, charid);
  1534. }
  1535. /*==========================================
  1536. * Search session data from a nick name
  1537. * (without sensitive case if necessary)
  1538. * return map_session_data pointer or NULL
  1539. *------------------------------------------*/
  1540. struct map_session_data * map_nick2sd(const char *nick)
  1541. {
  1542. struct map_session_data* sd;
  1543. struct map_session_data* found_sd;
  1544. struct s_mapiterator* iter;
  1545. size_t nicklen;
  1546. int qty = 0;
  1547. if( nick == NULL )
  1548. return NULL;
  1549. nicklen = strlen(nick);
  1550. iter = mapit_getallusers();
  1551. found_sd = NULL;
  1552. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  1553. {
  1554. if( battle_config.partial_name_scan )
  1555. {// partial name search
  1556. if( strnicmp(sd->status.name, nick, nicklen) == 0 )
  1557. {
  1558. found_sd = sd;
  1559. if( strcmp(sd->status.name, nick) == 0 )
  1560. {// Perfect Match
  1561. qty = 1;
  1562. break;
  1563. }
  1564. qty++;
  1565. }
  1566. }
  1567. else if( strcasecmp(sd->status.name, nick) == 0 )
  1568. {// exact search only
  1569. found_sd = sd;
  1570. break;
  1571. }
  1572. }
  1573. mapit_free(iter);
  1574. if( battle_config.partial_name_scan && qty != 1 )
  1575. found_sd = NULL;
  1576. return found_sd;
  1577. }
  1578. /*==========================================
  1579. * Looksup id_db DBMap and returns BL pointer of 'id' or NULL if not found
  1580. *------------------------------------------*/
  1581. struct block_list * map_id2bl(int id) {
  1582. return (struct block_list*)idb_get(id_db,id);
  1583. }
  1584. /**
  1585. * Same as map_id2bl except it only checks for its existence
  1586. **/
  1587. bool map_blid_exists( int id ) {
  1588. return (idb_exists(id_db,id));
  1589. }
  1590. /*==========================================
  1591. * Convext Mirror
  1592. *------------------------------------------*/
  1593. struct mob_data * map_getmob_boss(int16 m)
  1594. {
  1595. DBIterator* iter;
  1596. struct mob_data *md = NULL;
  1597. bool found = false;
  1598. iter = db_iterator(bossid_db);
  1599. for( md = (struct mob_data*)dbi_first(iter); dbi_exists(iter); md = (struct mob_data*)dbi_next(iter) )
  1600. {
  1601. if( md->bl.m == m )
  1602. {
  1603. found = true;
  1604. break;
  1605. }
  1606. }
  1607. dbi_destroy(iter);
  1608. return (found)? md : NULL;
  1609. }
  1610. struct mob_data * map_id2boss(int id)
  1611. {
  1612. if (id <= 0) return NULL;
  1613. return (struct mob_data*)idb_get(bossid_db,id);
  1614. }
  1615. /// Applies func to all the players in the db.
  1616. /// Stops iterating if func returns -1.
  1617. void map_foreachpc(int (*func)(struct map_session_data* sd, va_list args), ...)
  1618. {
  1619. DBIterator* iter;
  1620. struct map_session_data* sd;
  1621. iter = db_iterator(pc_db);
  1622. for( sd = dbi_first(iter); dbi_exists(iter); sd = dbi_next(iter) )
  1623. {
  1624. va_list args;
  1625. int ret;
  1626. va_start(args, func);
  1627. ret = func(sd, args);
  1628. va_end(args);
  1629. if( ret == -1 )
  1630. break;// stop iterating
  1631. }
  1632. dbi_destroy(iter);
  1633. }
  1634. /// Applies func to all the mobs in the db.
  1635. /// Stops iterating if func returns -1.
  1636. void map_foreachmob(int (*func)(struct mob_data* md, va_list args), ...)
  1637. {
  1638. DBIterator* iter;
  1639. struct mob_data* md;
  1640. iter = db_iterator(mobid_db);
  1641. for( md = (struct mob_data*)dbi_first(iter); dbi_exists(iter); md = (struct mob_data*)dbi_next(iter) )
  1642. {
  1643. va_list args;
  1644. int ret;
  1645. va_start(args, func);
  1646. ret = func(md, args);
  1647. va_end(args);
  1648. if( ret == -1 )
  1649. break;// stop iterating
  1650. }
  1651. dbi_destroy(iter);
  1652. }
  1653. /// Applies func to all the npcs in the db.
  1654. /// Stops iterating if func returns -1.
  1655. void map_foreachnpc(int (*func)(struct npc_data* nd, va_list args), ...)
  1656. {
  1657. DBIterator* iter;
  1658. struct block_list* bl;
  1659. iter = db_iterator(id_db);
  1660. for( bl = (struct block_list*)dbi_first(iter); dbi_exists(iter); bl = (struct block_list*)dbi_next(iter) )
  1661. {
  1662. if( bl->type == BL_NPC )
  1663. {
  1664. struct npc_data* nd = (struct npc_data*)bl;
  1665. va_list args;
  1666. int ret;
  1667. va_start(args, func);
  1668. ret = func(nd, args);
  1669. va_end(args);
  1670. if( ret == -1 )
  1671. break;// stop iterating
  1672. }
  1673. }
  1674. dbi_destroy(iter);
  1675. }
  1676. /// Applies func to everything in the db.
  1677. /// Stops iteratin gif func returns -1.
  1678. void map_foreachregen(int (*func)(struct block_list* bl, va_list args), ...)
  1679. {
  1680. DBIterator* iter;
  1681. struct block_list* bl;
  1682. iter = db_iterator(regen_db);
  1683. for( bl = (struct block_list*)dbi_first(iter); dbi_exists(iter); bl = (struct block_list*)dbi_next(iter) )
  1684. {
  1685. va_list args;
  1686. int ret;
  1687. va_start(args, func);
  1688. ret = func(bl, args);
  1689. va_end(args);
  1690. if( ret == -1 )
  1691. break;// stop iterating
  1692. }
  1693. dbi_destroy(iter);
  1694. }
  1695. /// Applies func to everything in the db.
  1696. /// Stops iterating if func returns -1.
  1697. void map_foreachiddb(int (*func)(struct block_list* bl, va_list args), ...)
  1698. {
  1699. DBIterator* iter;
  1700. struct block_list* bl;
  1701. iter = db_iterator(id_db);
  1702. for( bl = (struct block_list*)dbi_first(iter); dbi_exists(iter); bl = (struct block_list*)dbi_next(iter) )
  1703. {
  1704. va_list args;
  1705. int ret;
  1706. va_start(args, func);
  1707. ret = func(bl, args);
  1708. va_end(args);
  1709. if( ret == -1 )
  1710. break;// stop iterating
  1711. }
  1712. dbi_destroy(iter);
  1713. }
  1714. /// Iterator.
  1715. /// Can filter by bl type.
  1716. struct s_mapiterator
  1717. {
  1718. enum e_mapitflags flags;// flags for special behaviour
  1719. enum bl_type types;// what bl types to return
  1720. DBIterator* dbi;// database iterator
  1721. };
  1722. /// Returns true if the block_list matches the description in the iterator.
  1723. ///
  1724. /// @param _mapit_ Iterator
  1725. /// @param _bl_ block_list
  1726. /// @return true if it matches
  1727. #define MAPIT_MATCHES(_mapit_,_bl_) \
  1728. ( \
  1729. ( (_bl_)->type & (_mapit_)->types /* type matches */ ) \
  1730. )
  1731. /// Allocates a new iterator.
  1732. /// Returns the new iterator.
  1733. /// types can represent several BL's as a bit field.
  1734. /// TODO should this be expanded to allow filtering of map/guild/party/chat/cell/area/...?
  1735. ///
  1736. /// @param flags Flags of the iterator
  1737. /// @param type Target types
  1738. /// @return Iterator
  1739. struct s_mapiterator* mapit_alloc(enum e_mapitflags flags, enum bl_type types)
  1740. {
  1741. struct s_mapiterator* mapit;
  1742. CREATE(mapit, struct s_mapiterator, 1);
  1743. mapit->flags = flags;
  1744. mapit->types = types;
  1745. if( types == BL_PC ) mapit->dbi = db_iterator(pc_db);
  1746. else if( types == BL_MOB ) mapit->dbi = db_iterator(mobid_db);
  1747. else mapit->dbi = db_iterator(id_db);
  1748. return mapit;
  1749. }
  1750. /// Frees the iterator.
  1751. ///
  1752. /// @param mapit Iterator
  1753. void mapit_free(struct s_mapiterator* mapit)
  1754. {
  1755. nullpo_retv(mapit);
  1756. dbi_destroy(mapit->dbi);
  1757. aFree(mapit);
  1758. }
  1759. /// Returns the first block_list that matches the description.
  1760. /// Returns NULL if not found.
  1761. ///
  1762. /// @param mapit Iterator
  1763. /// @return first block_list or NULL
  1764. struct block_list* mapit_first(struct s_mapiterator* mapit)
  1765. {
  1766. struct block_list* bl;
  1767. nullpo_retr(NULL,mapit);
  1768. for( bl = (struct block_list*)dbi_first(mapit->dbi); bl != NULL; bl = (struct block_list*)dbi_next(mapit->dbi) )
  1769. {
  1770. if( MAPIT_MATCHES(mapit,bl) )
  1771. break;// found match
  1772. }
  1773. return bl;
  1774. }
  1775. /// Returns the last block_list that matches the description.
  1776. /// Returns NULL if not found.
  1777. ///
  1778. /// @param mapit Iterator
  1779. /// @return last block_list or NULL
  1780. struct block_list* mapit_last(struct s_mapiterator* mapit)
  1781. {
  1782. struct block_list* bl;
  1783. nullpo_retr(NULL,mapit);
  1784. for( bl = (struct block_list*)dbi_last(mapit->dbi); bl != NULL; bl = (struct block_list*)dbi_prev(mapit->dbi) )
  1785. {
  1786. if( MAPIT_MATCHES(mapit,bl) )
  1787. break;// found match
  1788. }
  1789. return bl;
  1790. }
  1791. /// Returns the next block_list that matches the description.
  1792. /// Returns NULL if not found.
  1793. ///
  1794. /// @param mapit Iterator
  1795. /// @return next block_list or NULL
  1796. struct block_list* mapit_next(struct s_mapiterator* mapit)
  1797. {
  1798. struct block_list* bl;
  1799. nullpo_retr(NULL,mapit);
  1800. for( ; ; )
  1801. {
  1802. bl = (struct block_list*)dbi_next(mapit->dbi);
  1803. if( bl == NULL )
  1804. break;// end
  1805. if( MAPIT_MATCHES(mapit,bl) )
  1806. break;// found a match
  1807. // try next
  1808. }
  1809. return bl;
  1810. }
  1811. /// Returns the previous block_list that matches the description.
  1812. /// Returns NULL if not found.
  1813. ///
  1814. /// @param mapit Iterator
  1815. /// @return previous block_list or NULL
  1816. struct block_list* mapit_prev(struct s_mapiterator* mapit)
  1817. {
  1818. struct block_list* bl;
  1819. nullpo_retr(NULL,mapit);
  1820. for( ; ; )
  1821. {
  1822. bl = (struct block_list*)dbi_prev(mapit->dbi);
  1823. if( bl == NULL )
  1824. break;// end
  1825. if( MAPIT_MATCHES(mapit,bl) )
  1826. break;// found a match
  1827. // try prev
  1828. }
  1829. return bl;
  1830. }
  1831. /// Returns true if the current block_list exists in the database.
  1832. ///
  1833. /// @param mapit Iterator
  1834. /// @return true if it exists
  1835. bool mapit_exists(struct s_mapiterator* mapit)
  1836. {
  1837. nullpo_retr(false,mapit);
  1838. return dbi_exists(mapit->dbi);
  1839. }
  1840. /*==========================================
  1841. * Add npc-bl to id_db, basically register npc to map
  1842. *------------------------------------------*/
  1843. bool map_addnpc(int16 m,struct npc_data *nd)
  1844. {
  1845. nullpo_ret(nd);
  1846. if( m < 0 || m >= map_num )
  1847. return false;
  1848. if( map[m].npc_num == MAX_NPC_PER_MAP )
  1849. {
  1850. ShowWarning("too many NPCs in one map %s\n",map[m].name);
  1851. return false;
  1852. }
  1853. map[m].npc[map[m].npc_num]=nd;
  1854. map[m].npc_num++;
  1855. idb_put(id_db,nd->bl.id,nd);
  1856. return true;
  1857. }
  1858. /*==========================================
  1859. * Add an instance map
  1860. *------------------------------------------*/
  1861. int map_addinstancemap(const char *name, int id)
  1862. {
  1863. int src_m = map_mapname2mapid(name);
  1864. int dst_m = -1, i;
  1865. char iname[MAP_NAME_LENGTH];
  1866. size_t num_cell, size;
  1867. if(src_m < 0)
  1868. return -1;
  1869. if(strlen(name) > 20) {
  1870. // against buffer overflow
  1871. ShowError("map_addisntancemap: can't add long map name \"%s\"\n", name);
  1872. return -2;
  1873. }
  1874. for(i = instance_start; i < MAX_MAP_PER_SERVER; i++) {
  1875. if(!map[i].name[0])
  1876. break;
  1877. }
  1878. if(i < map_num) // Destination map value overwrites another
  1879. dst_m = i;
  1880. else if(i < MAX_MAP_PER_SERVER) // Destination map value increments to new map
  1881. dst_m = map_num++;
  1882. else {
  1883. // Out of bounds
  1884. ShowError("map_addinstancemap failed. map_num(%d) > map_max(%d)\n",map_num, MAX_MAP_PER_SERVER);
  1885. return -3;
  1886. }
  1887. // Copy the map
  1888. memcpy(&map[dst_m], &map[src_m], sizeof(struct map_data));
  1889. strcpy(iname,name);
  1890. // Alter the name
  1891. // Due to this being custom we only worry about preserving as many characters as necessary for accurate map distinguishing
  1892. // This also allows us to maintain complete independence with main map functions
  1893. if((strchr(iname,'@') == NULL) && strlen(iname) > 8) {
  1894. memmove(iname, iname+(strlen(iname)-9), strlen(iname));
  1895. snprintf(map[dst_m].name, sizeof(map[dst_m].name),"%d#%s", id, iname);
  1896. } else
  1897. snprintf(map[dst_m].name, sizeof(map[dst_m].name),"%.3d%s", id, iname);
  1898. map[dst_m].name[MAP_NAME_LENGTH-1] = '\0';
  1899. map[dst_m].m = dst_m;
  1900. map[dst_m].instance_id = id;
  1901. map[dst_m].users = 0;
  1902. memset(map[dst_m].npc, 0, sizeof(map[dst_m].npc));
  1903. map[dst_m].npc_num = 0;
  1904. // Reallocate cells
  1905. num_cell = map[dst_m].xs * map[dst_m].ys;
  1906. CREATE( map[dst_m].cell, struct mapcell, num_cell );
  1907. memcpy( map[dst_m].cell, map[src_m].cell, num_cell * sizeof(struct mapcell) );
  1908. size = map[dst_m].bxs * map[dst_m].bys * sizeof(struct block_list*);
  1909. map[dst_m].block = (struct block_list **)aCalloc(1,size);
  1910. map[dst_m].block_mob = (struct block_list **)aCalloc(1,size);
  1911. map[dst_m].index = mapindex_addmap(-1, map[dst_m].name);
  1912. map[dst_m].channel = NULL;
  1913. map_addmap2db(&map[dst_m]);
  1914. return dst_m;
  1915. }
  1916. /*==========================================
  1917. * Set player to save point when they leave
  1918. *------------------------------------------*/
  1919. static int map_instancemap_leave(struct block_list *bl, va_list ap)
  1920. {
  1921. struct map_session_data* sd;
  1922. nullpo_retr(0, bl);
  1923. nullpo_retr(0, sd = (struct map_session_data *)bl);
  1924. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  1925. return 1;
  1926. }
  1927. /*==========================================
  1928. * Remove all units from instance
  1929. *------------------------------------------*/
  1930. static int map_instancemap_clean(struct block_list *bl, va_list ap)
  1931. {
  1932. nullpo_retr(0, bl);
  1933. switch(bl->type) {
  1934. case BL_PC:
  1935. map_quit((struct map_session_data *) bl);
  1936. break;
  1937. case BL_NPC:
  1938. npc_unload((struct npc_data *)bl,true);
  1939. break;
  1940. case BL_MOB:
  1941. unit_free(bl,CLR_OUTSIGHT);
  1942. break;
  1943. case BL_PET:
  1944. //There is no need for this, the pet is removed together with the player. [Skotlex]
  1945. break;
  1946. case BL_ITEM:
  1947. map_clearflooritem(bl);
  1948. break;
  1949. case BL_SKILL:
  1950. skill_delunit((struct skill_unit *) bl);
  1951. break;
  1952. }
  1953. return 1;
  1954. }
  1955. /*==========================================
  1956. * Deleting an instance map
  1957. *------------------------------------------*/
  1958. int map_delinstancemap(int m)
  1959. {
  1960. if(m < 0 || !map[m].instance_id)
  1961. return 0;
  1962. // Kick everyone out
  1963. map_foreachinmap(map_instancemap_leave, m, BL_PC);
  1964. // Do the unit cleanup
  1965. map_foreachinmap(map_instancemap_clean, m, BL_ALL);
  1966. if( map[m].mob_delete_timer != INVALID_TIMER )
  1967. delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  1968. mapindex_removemap( map[m].index );
  1969. // Free memory
  1970. aFree(map[m].cell);
  1971. aFree(map[m].block);
  1972. aFree(map[m].block_mob);
  1973. map_removemapdb(&map[m]);
  1974. memset(&map[m], 0x00, sizeof(map[0]));
  1975. // Make delete timers invalid to avoid errors
  1976. map[m].mob_delete_timer = INVALID_TIMER;
  1977. return 1;
  1978. }
  1979. /*=========================================
  1980. * Dynamic Mobs [Wizputer]
  1981. *-----------------------------------------*/
  1982. // Stores the spawn data entry in the mob list.
  1983. // Returns the index of successful, or -1 if the list was full.
  1984. int map_addmobtolist(unsigned short m, struct spawn_data *spawn)
  1985. {
  1986. size_t i;
  1987. ARR_FIND( 0, MAX_MOB_LIST_PER_MAP, i, map[m].moblist[i] == NULL );
  1988. if( i < MAX_MOB_LIST_PER_MAP )
  1989. {
  1990. map[m].moblist[i] = spawn;
  1991. return i;
  1992. }
  1993. return -1;
  1994. }
  1995. void map_spawnmobs(int16 m)
  1996. {
  1997. int i, k=0;
  1998. if (map[m].mob_delete_timer != INVALID_TIMER)
  1999. { //Mobs have not been removed yet [Skotlex]
  2000. delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  2001. map[m].mob_delete_timer = INVALID_TIMER;
  2002. return;
  2003. }
  2004. for(i=0; i<MAX_MOB_LIST_PER_MAP; i++)
  2005. if(map[m].moblist[i]!=NULL)
  2006. {
  2007. k+=map[m].moblist[i]->num;
  2008. npc_parse_mob2(map[m].moblist[i]);
  2009. }
  2010. if (battle_config.etc_log && k > 0)
  2011. {
  2012. ShowStatus("Map %s: Spawned '"CL_WHITE"%d"CL_RESET"' mobs.\n",map[m].name, k);
  2013. }
  2014. }
  2015. int map_removemobs_sub(struct block_list *bl, va_list ap)
  2016. {
  2017. struct mob_data *md = (struct mob_data *)bl;
  2018. nullpo_ret(md);
  2019. //When not to remove mob:
  2020. // doesn't respawn and is not a slave
  2021. if( !md->spawn && !md->master_id )
  2022. return 0;
  2023. // respawn data is not in cache
  2024. if( md->spawn && !md->spawn->state.dynamic )
  2025. return 0;
  2026. // hasn't spawned yet
  2027. if( md->spawn_timer != INVALID_TIMER )
  2028. return 0;
  2029. // is damaged and mob_remove_damaged is off
  2030. if( !battle_config.mob_remove_damaged && md->status.hp < md->status.max_hp )
  2031. return 0;
  2032. // is a mvp
  2033. if( md->db->mexp > 0 )
  2034. return 0;
  2035. unit_free(&md->bl,CLR_OUTSIGHT);
  2036. return 1;
  2037. }
  2038. int map_removemobs_timer(int tid, unsigned int tick, int id, intptr_t data)
  2039. {
  2040. int count;
  2041. const int16 m = id;
  2042. if (m < 0 || m >= MAX_MAP_PER_SERVER)
  2043. { //Incorrect map id!
  2044. ShowError("map_removemobs_timer error: timer %d points to invalid map %d\n",tid, m);
  2045. return 0;
  2046. }
  2047. if (map[m].mob_delete_timer != tid)
  2048. { //Incorrect timer call!
  2049. ShowError("map_removemobs_timer mismatch: %d != %d (map %s)\n",map[m].mob_delete_timer, tid, map[m].name);
  2050. return 0;
  2051. }
  2052. map[m].mob_delete_timer = INVALID_TIMER;
  2053. if (map[m].users > 0) //Map not empty!
  2054. return 1;
  2055. count = map_foreachinmap(map_removemobs_sub, m, BL_MOB);
  2056. if (battle_config.etc_log && count > 0)
  2057. ShowStatus("Map %s: Removed '"CL_WHITE"%d"CL_RESET"' mobs.\n",map[m].name, count);
  2058. return 1;
  2059. }
  2060. void map_removemobs(int16 m)
  2061. {
  2062. if (map[m].mob_delete_timer != INVALID_TIMER) // should never happen
  2063. return; //Mobs are already scheduled for removal
  2064. map[m].mob_delete_timer = add_timer(gettick()+battle_config.mob_remove_delay, map_removemobs_timer, m, 0);
  2065. }
  2066. /*==========================================
  2067. * Hookup, get map_id from map_name
  2068. *------------------------------------------*/
  2069. int16 map_mapname2mapid(const char* name)
  2070. {
  2071. unsigned short map_index;
  2072. map_index = mapindex_name2id(name);
  2073. if (!map_index)
  2074. return -1;
  2075. return map_mapindex2mapid(map_index);
  2076. }
  2077. /*==========================================
  2078. * Returns the map of the given mapindex. [Skotlex]
  2079. *------------------------------------------*/
  2080. int16 map_mapindex2mapid(unsigned short mapindex)
  2081. {
  2082. struct map_data *md=NULL;
  2083. if (!mapindex)
  2084. return -1;
  2085. md = (struct map_data*)uidb_get(map_db,(unsigned int)mapindex);
  2086. if(md==NULL || md->cell==NULL)
  2087. return -1;
  2088. return md->m;
  2089. }
  2090. /*==========================================
  2091. * Switching Ip, port ? (like changing map_server) get ip/port from map_name
  2092. *------------------------------------------*/
  2093. int map_mapname2ipport(unsigned short name, uint32* ip, uint16* port)
  2094. {
  2095. struct map_data_other_server *mdos;
  2096. mdos = (struct map_data_other_server*)uidb_get(map_db,(unsigned int)name);
  2097. if(mdos==NULL || mdos->cell) //If gat isn't null, this is a local map.
  2098. return -1;
  2099. *ip=mdos->ip;
  2100. *port=mdos->port;
  2101. return 0;
  2102. }
  2103. /*==========================================
  2104. * Checks if both dirs point in the same direction.
  2105. *------------------------------------------*/
  2106. int map_check_dir(int s_dir,int t_dir)
  2107. {
  2108. if(s_dir == t_dir)
  2109. return 0;
  2110. switch(s_dir) {
  2111. case 0: if(t_dir == 7 || t_dir == 1 || t_dir == 0) return 0; break;
  2112. case 1: if(t_dir == 0 || t_dir == 2 || t_dir == 1) return 0; break;
  2113. case 2: if(t_dir == 1 || t_dir == 3 || t_dir == 2) return 0; break;
  2114. case 3: if(t_dir == 2 || t_dir == 4 || t_dir == 3) return 0; break;
  2115. case 4: if(t_dir == 3 || t_dir == 5 || t_dir == 4) return 0; break;
  2116. case 5: if(t_dir == 4 || t_dir == 6 || t_dir == 5) return 0; break;
  2117. case 6: if(t_dir == 5 || t_dir == 7 || t_dir == 6) return 0; break;
  2118. case 7: if(t_dir == 6 || t_dir == 0 || t_dir == 7) return 0; break;
  2119. }
  2120. return 1;
  2121. }
  2122. /*==========================================
  2123. * Returns the direction of the given cell, relative to 'src'
  2124. *------------------------------------------*/
  2125. uint8 map_calc_dir(struct block_list* src, int16 x, int16 y)
  2126. {
  2127. uint8 dir = 0;
  2128. int dx, dy;
  2129. nullpo_ret(src);
  2130. dx = x-src->x;
  2131. dy = y-src->y;
  2132. if( dx == 0 && dy == 0 )
  2133. { // both are standing on the same spot
  2134. //dir = 6; // aegis-style, makes knockback default to the left
  2135. dir = unit_getdir(src); // athena-style, makes knockback default to behind 'src'
  2136. }
  2137. else if( dx >= 0 && dy >=0 )
  2138. { // upper-right
  2139. if( dx*2 <= dy ) dir = 0; // up
  2140. else if( dx > dy*2 ) dir = 6; // right
  2141. else dir = 7; // up-right
  2142. }
  2143. else if( dx >= 0 && dy <= 0 )
  2144. { // lower-right
  2145. if( dx*2 <= -dy ) dir = 4; // down
  2146. else if( dx > -dy*2 ) dir = 6; // right
  2147. else dir = 5; // down-right
  2148. }
  2149. else if( dx <= 0 && dy <= 0 )
  2150. { // lower-left
  2151. if( dx*2 >= dy ) dir = 4; // down
  2152. else if( dx < dy*2 ) dir = 2; // left
  2153. else dir = 3; // down-left
  2154. }
  2155. else
  2156. { // upper-left
  2157. if( -dx*2 <= dy ) dir = 0; // up
  2158. else if( -dx > dy*2 ) dir = 2; // left
  2159. else dir = 1; // up-left
  2160. }
  2161. return dir;
  2162. }
  2163. /*==========================================
  2164. * Randomizes target cell x,y to a random walkable cell that
  2165. * has the same distance from object as given coordinates do. [Skotlex]
  2166. *------------------------------------------*/
  2167. int map_random_dir(struct block_list *bl, int16 *x, int16 *y)
  2168. {
  2169. short xi = *x-bl->x;
  2170. short yi = *y-bl->y;
  2171. short i=0, j;
  2172. int dist2 = xi*xi + yi*yi;
  2173. short dist = (short)sqrt((float)dist2);
  2174. short segment;
  2175. if (dist < 1) dist =1;
  2176. do {
  2177. j = 1 + 2*(rnd()%4); //Pick a random diagonal direction
  2178. segment = 1+(rnd()%dist); //Pick a random interval from the whole vector in that direction
  2179. xi = bl->x + segment*dirx[j];
  2180. segment = (short)sqrt((float)(dist2 - segment*segment)); //The complement of the previously picked segment
  2181. yi = bl->y + segment*diry[j];
  2182. } while (
  2183. (map_getcell(bl->m,xi,yi,CELL_CHKNOPASS) || !path_search(NULL,bl->m,bl->x,bl->y,xi,yi,1,CELL_CHKNOREACH))
  2184. && (++i)<100 );
  2185. if (i < 100) {
  2186. *x = xi;
  2187. *y = yi;
  2188. return 1;
  2189. }
  2190. return 0;
  2191. }
  2192. // gat system
  2193. inline static struct mapcell map_gat2cell(int gat) {
  2194. struct mapcell cell;
  2195. memset(&cell,0,sizeof(struct mapcell));
  2196. switch( gat ) {
  2197. case 0: cell.walkable = 1; cell.shootable = 1; cell.water = 0; break; // walkable ground
  2198. case 1: cell.walkable = 0; cell.shootable = 0; cell.water = 0; break; // non-walkable ground
  2199. case 2: cell.walkable = 1; cell.shootable = 1; cell.water = 0; break; // ???
  2200. case 3: cell.walkable = 1; cell.shootable = 1; cell.water = 1; break; // walkable water
  2201. case 4: cell.walkable = 1; cell.shootable = 1; cell.water = 0; break; // ???
  2202. case 5: cell.walkable = 0; cell.shootable = 1; cell.water = 0; break; // gap (snipable)
  2203. case 6: cell.walkable = 1; cell.shootable = 1; cell.water = 0; break; // ???
  2204. default:
  2205. ShowWarning("map_gat2cell: unrecognized gat type '%d'\n", gat);
  2206. break;
  2207. }
  2208. return cell;
  2209. }
  2210. static int map_cell2gat(struct mapcell cell)
  2211. {
  2212. if( cell.walkable == 1 && cell.shootable == 1 && cell.water == 0 ) return 0;
  2213. if( cell.walkable == 0 && cell.shootable == 0 && cell.water == 0 ) return 1;
  2214. if( cell.walkable == 1 && cell.shootable == 1 && cell.water == 1 ) return 3;
  2215. if( cell.walkable == 0 && cell.shootable == 1 && cell.water == 0 ) return 5;
  2216. ShowWarning("map_cell2gat: cell has no matching gat type\n");
  2217. return 1; // default to 'wall'
  2218. }
  2219. /*==========================================
  2220. * Confirm if celltype in (m,x,y) match the one given in cellchk
  2221. *------------------------------------------*/
  2222. int map_getcell(int16 m,int16 x,int16 y,cell_chk cellchk)
  2223. {
  2224. return (m < 0 || m >= MAX_MAP_PER_SERVER) ? 0 : map_getcellp(&map[m],x,y,cellchk);
  2225. }
  2226. int map_getcellp(struct map_data* m,int16 x,int16 y,cell_chk cellchk)
  2227. {
  2228. struct mapcell cell;
  2229. nullpo_ret(m);
  2230. //NOTE: this intentionally overrides the last row and column
  2231. if(x<0 || x>=m->xs-1 || y<0 || y>=m->ys-1)
  2232. return( cellchk == CELL_CHKNOPASS );
  2233. cell = m->cell[x + y*m->xs];
  2234. switch(cellchk)
  2235. {
  2236. // gat type retrieval
  2237. case CELL_GETTYPE:
  2238. return map_cell2gat(cell);
  2239. // base gat type checks
  2240. case CELL_CHKWALL:
  2241. return (!cell.walkable && !cell.shootable);
  2242. case CELL_CHKWATER:
  2243. return (cell.water);
  2244. case CELL_CHKCLIFF:
  2245. return (!cell.walkable && cell.shootable);
  2246. // base cell type checks
  2247. case CELL_CHKNPC:
  2248. return (cell.npc);
  2249. case CELL_CHKBASILICA:
  2250. return (cell.basilica);
  2251. case CELL_CHKLANDPROTECTOR:
  2252. return (cell.landprotector);
  2253. case CELL_CHKNOVENDING:
  2254. return (cell.novending);
  2255. case CELL_CHKNOCHAT:
  2256. return (cell.nochat);
  2257. case CELL_CHKMAELSTROM:
  2258. return (cell.maelstrom);
  2259. case CELL_CHKICEWALL:
  2260. return (cell.icewall);
  2261. // special checks
  2262. case CELL_CHKPASS:
  2263. #ifdef CELL_NOSTACK
  2264. if (cell.cell_bl >= battle_config.cell_stack_limit) return 0;
  2265. #endif
  2266. case CELL_CHKREACH:
  2267. return (cell.walkable);
  2268. case CELL_CHKNOPASS:
  2269. #ifdef CELL_NOSTACK
  2270. if (cell.cell_bl >= battle_config.cell_stack_limit) return 1;
  2271. #endif
  2272. case CELL_CHKNOREACH:
  2273. return (!cell.walkable);
  2274. case CELL_CHKSTACK:
  2275. #ifdef CELL_NOSTACK
  2276. return (cell.cell_bl >= battle_config.cell_stack_limit);
  2277. #else
  2278. return 0;
  2279. #endif
  2280. default:
  2281. return 0;
  2282. }
  2283. }
  2284. /*==========================================
  2285. * Change the type/flags of a map cell
  2286. * 'cell' - which flag to modify
  2287. * 'flag' - true = on, false = off
  2288. *------------------------------------------*/
  2289. void map_setcell(int16 m, int16 x, int16 y, cell_t cell, bool flag)
  2290. {
  2291. int j;
  2292. if( m < 0 || m >= map_num || x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  2293. return;
  2294. j = x + y*map[m].xs;
  2295. switch( cell ) {
  2296. case CELL_WALKABLE: map[m].cell[j].walkable = flag; break;
  2297. case CELL_SHOOTABLE: map[m].cell[j].shootable = flag; break;
  2298. case CELL_WATER: map[m].cell[j].water = flag; break;
  2299. case CELL_NPC: map[m].cell[j].npc = flag; break;
  2300. case CELL_BASILICA: map[m].cell[j].basilica = flag; break;
  2301. case CELL_LANDPROTECTOR: map[m].cell[j].landprotector = flag; break;
  2302. case CELL_NOVENDING: map[m].cell[j].novending = flag; break;
  2303. case CELL_NOCHAT: map[m].cell[j].nochat = flag; break;
  2304. case CELL_MAELSTROM: map[m].cell[j].maelstrom = flag; break;
  2305. case CELL_ICEWALL: map[m].cell[j].icewall = flag; break;
  2306. default:
  2307. ShowWarning("map_setcell: invalid cell type '%d'\n", (int)cell);
  2308. break;
  2309. }
  2310. }
  2311. void map_setgatcell(int16 m, int16 x, int16 y, int gat)
  2312. {
  2313. int j;
  2314. struct mapcell cell;
  2315. if( m < 0 || m >= map_num || x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  2316. return;
  2317. j = x + y*map[m].xs;
  2318. cell = map_gat2cell(gat);
  2319. map[m].cell[j].walkable = cell.walkable;
  2320. map[m].cell[j].shootable = cell.shootable;
  2321. map[m].cell[j].water = cell.water;
  2322. }
  2323. /*==========================================
  2324. * Invisible Walls
  2325. *------------------------------------------*/
  2326. static DBMap* iwall_db;
  2327. void map_iwall_nextxy(int16 x, int16 y, int8 dir, int pos, int16 *x1, int16 *y1)
  2328. {
  2329. if( dir == 0 || dir == 4 )
  2330. *x1 = x; // Keep X
  2331. else if( dir > 0 && dir < 4 )
  2332. *x1 = x - pos; // Going left
  2333. else
  2334. *x1 = x + pos; // Going right
  2335. if( dir == 2 || dir == 6 )
  2336. *y1 = y;
  2337. else if( dir > 2 && dir < 6 )
  2338. *y1 = y - pos;
  2339. else
  2340. *y1 = y + pos;
  2341. }
  2342. bool map_iwall_set(int16 m, int16 x, int16 y, int size, int8 dir, bool shootable, const char* wall_name)
  2343. {
  2344. struct iwall_data *iwall;
  2345. int i;
  2346. int16 x1 = 0, y1 = 0;
  2347. if( size < 1 || !wall_name )
  2348. return false;
  2349. if( (iwall = (struct iwall_data *)strdb_get(iwall_db, wall_name)) != NULL )
  2350. return false; // Already Exists
  2351. if( map_getcell(m, x, y, CELL_CHKNOREACH) )
  2352. return false; // Starting cell problem
  2353. CREATE(iwall, struct iwall_data, 1);
  2354. iwall->m = m;
  2355. iwall->x = x;
  2356. iwall->y = y;
  2357. iwall->size = size;
  2358. iwall->dir = dir;
  2359. iwall->shootable = shootable;
  2360. safestrncpy(iwall->wall_name, wall_name, sizeof(iwall->wall_name));
  2361. for( i = 0; i < size; i++ )
  2362. {
  2363. map_iwall_nextxy(x, y, dir, i, &x1, &y1);
  2364. if( map_getcell(m, x1, y1, CELL_CHKNOREACH) )
  2365. break; // Collision
  2366. map_setcell(m, x1, y1, CELL_WALKABLE, false);
  2367. map_setcell(m, x1, y1, CELL_SHOOTABLE, shootable);
  2368. clif_changemapcell(0, m, x1, y1, map_getcell(m, x1, y1, CELL_GETTYPE), ALL_SAMEMAP);
  2369. }
  2370. iwall->size = i;
  2371. strdb_put(iwall_db, iwall->wall_name, iwall);
  2372. map[m].iwall_num++;
  2373. return true;
  2374. }
  2375. void map_iwall_get(struct map_session_data *sd) {
  2376. struct iwall_data *iwall;
  2377. DBIterator* iter;
  2378. int16 x1, y1;
  2379. int i;
  2380. if( map[sd->bl.m].iwall_num < 1 )
  2381. return;
  2382. iter = db_iterator(iwall_db);
  2383. for( iwall = dbi_first(iter); dbi_exists(iter); iwall = dbi_next(iter) ) {
  2384. if( iwall->m != sd->bl.m )
  2385. continue;
  2386. for( i = 0; i < iwall->size; i++ ) {
  2387. map_iwall_nextxy(iwall->x, iwall->y, iwall->dir, i, &x1, &y1);
  2388. clif_changemapcell(sd->fd, iwall->m, x1, y1, map_getcell(iwall->m, x1, y1, CELL_GETTYPE), SELF);
  2389. }
  2390. }
  2391. dbi_destroy(iter);
  2392. }
  2393. void map_iwall_remove(const char *wall_name)
  2394. {
  2395. struct iwall_data *iwall;
  2396. int16 i, x1, y1;
  2397. if( (iwall = (struct iwall_data *)strdb_get(iwall_db, wall_name)) == NULL )
  2398. return; // Nothing to do
  2399. for( i = 0; i < iwall->size; i++ ) {
  2400. map_iwall_nextxy(iwall->x, iwall->y, iwall->dir, i, &x1, &y1);
  2401. map_setcell(iwall->m, x1, y1, CELL_SHOOTABLE, true);
  2402. map_setcell(iwall->m, x1, y1, CELL_WALKABLE, true);
  2403. clif_changemapcell(0, iwall->m, x1, y1, map_getcell(iwall->m, x1, y1, CELL_GETTYPE), ALL_SAMEMAP);
  2404. }
  2405. map[iwall->m].iwall_num--;
  2406. strdb_remove(iwall_db, iwall->wall_name);
  2407. }
  2408. /**
  2409. * @see DBCreateData
  2410. */
  2411. static DBData create_map_data_other_server(DBKey key, va_list args)
  2412. {
  2413. struct map_data_other_server *mdos;
  2414. unsigned short mapindex = (unsigned short)key.ui;
  2415. mdos=(struct map_data_other_server *)aCalloc(1,sizeof(struct map_data_other_server));
  2416. mdos->index = mapindex;
  2417. memcpy(mdos->name, mapindex_id2name(mapindex), MAP_NAME_LENGTH);
  2418. return db_ptr2data(mdos);
  2419. }
  2420. /*==========================================
  2421. * Add mapindex to db of another map server
  2422. *------------------------------------------*/
  2423. int map_setipport(unsigned short mapindex, uint32 ip, uint16 port)
  2424. {
  2425. struct map_data_other_server *mdos;
  2426. mdos= uidb_ensure(map_db,(unsigned int)mapindex, create_map_data_other_server);
  2427. if(mdos->cell) //Local map,Do nothing. Give priority to our own local maps over ones from another server. [Skotlex]
  2428. return 0;
  2429. if(ip == clif_getip() && port == clif_getport()) {
  2430. //That's odd, we received info that we are the ones with this map, but... we don't have it.
  2431. ShowFatalError("map_setipport : received info that this map-server SHOULD have map '%s', but it is not loaded.\n",mapindex_id2name(mapindex));
  2432. exit(EXIT_FAILURE);
  2433. }
  2434. mdos->ip = ip;
  2435. mdos->port = port;
  2436. return 1;
  2437. }
  2438. /**
  2439. * Delete all the other maps server management
  2440. * @see DBApply
  2441. */
  2442. int map_eraseallipport_sub(DBKey key, DBData *data, va_list va)
  2443. {
  2444. struct map_data_other_server *mdos = db_data2ptr(data);
  2445. if(mdos->cell == NULL) {
  2446. db_remove(map_db,key);
  2447. aFree(mdos);
  2448. }
  2449. return 0;
  2450. }
  2451. int map_eraseallipport(void)
  2452. {
  2453. map_db->foreach(map_db,map_eraseallipport_sub);
  2454. return 1;
  2455. }
  2456. /*==========================================
  2457. * Delete mapindex from db of another map server
  2458. *------------------------------------------*/
  2459. int map_eraseipport(unsigned short mapindex, uint32 ip, uint16 port)
  2460. {
  2461. struct map_data_other_server *mdos;
  2462. mdos = (struct map_data_other_server*)uidb_get(map_db,(unsigned int)mapindex);
  2463. if(!mdos || mdos->cell) //Map either does not exists or is a local map.
  2464. return 0;
  2465. if(mdos->ip==ip && mdos->port == port) {
  2466. uidb_remove(map_db,(unsigned int)mapindex);
  2467. aFree(mdos);
  2468. return 1;
  2469. }
  2470. return 0;
  2471. }
  2472. /*==========================================
  2473. * [Shinryo]: Init the mapcache
  2474. *------------------------------------------*/
  2475. static char *map_init_mapcache(FILE *fp)
  2476. {
  2477. size_t size = 0;
  2478. char *buffer;
  2479. // No file open? Return..
  2480. nullpo_ret(fp);
  2481. // Get file size
  2482. fseek(fp, 0, SEEK_END);
  2483. size = ftell(fp);
  2484. fseek(fp, 0, SEEK_SET);
  2485. // Allocate enough space
  2486. CREATE(buffer, char, size);
  2487. // No memory? Return..
  2488. nullpo_ret(buffer);
  2489. // Read file into buffer..
  2490. if(fread(buffer, sizeof(char), size, fp) != size) {
  2491. ShowError("map_init_mapcache: Could not read entire mapcache file\n");
  2492. return NULL;
  2493. }
  2494. return buffer;
  2495. }
  2496. /*==========================================
  2497. * Map cache reading
  2498. * [Shinryo]: Optimized some behaviour to speed this up
  2499. *==========================================*/
  2500. int map_readfromcache(struct map_data *m, char *buffer, char *decode_buffer)
  2501. {
  2502. int i;
  2503. struct map_cache_main_header *header = (struct map_cache_main_header *)buffer;
  2504. struct map_cache_map_info *info = NULL;
  2505. char *p = buffer + sizeof(struct map_cache_main_header);
  2506. for(i = 0; i < header->map_count; i++) {
  2507. info = (struct map_cache_map_info *)p;
  2508. if( strcmp(m->name, info->name) == 0 )
  2509. break; // Map found
  2510. // Jump to next entry..
  2511. p += sizeof(struct map_cache_map_info) + info->len;
  2512. }
  2513. if( info && i < header->map_count ) {
  2514. unsigned long size, xy;
  2515. if( info->xs <= 0 || info->ys <= 0 )
  2516. return 0;// Invalid
  2517. m->xs = info->xs;
  2518. m->ys = info->ys;
  2519. size = (unsigned long)info->xs*(unsigned long)info->ys;
  2520. if(size > MAX_MAP_SIZE) {
  2521. ShowWarning("map_readfromcache: %s exceeded MAX_MAP_SIZE of %d\n", info->name, MAX_MAP_SIZE);
  2522. return 0; // Say not found to remove it from list.. [Shinryo]
  2523. }
  2524. // TO-DO: Maybe handle the scenario, if the decoded buffer isn't the same size as expected? [Shinryo]
  2525. decode_zip(decode_buffer, &size, p+sizeof(struct map_cache_map_info), info->len);
  2526. CREATE(m->cell, struct mapcell, size);
  2527. for( xy = 0; xy < size; ++xy )
  2528. m->cell[xy] = map_gat2cell(decode_buffer[xy]);
  2529. return 1;
  2530. }
  2531. return 0; // Not found
  2532. }
  2533. int map_addmap(char* mapname)
  2534. {
  2535. if( strcmpi(mapname,"clear")==0 )
  2536. {
  2537. map_num = 0;
  2538. instance_start = 0;
  2539. return 0;
  2540. }
  2541. if( map_num >= MAX_MAP_PER_SERVER - 1 )
  2542. {
  2543. ShowError("Could not add map '"CL_WHITE"%s"CL_RESET"', the limit of maps has been reached.\n",mapname);
  2544. return 1;
  2545. }
  2546. mapindex_getmapname(mapname, map[map_num].name);
  2547. map_num++;
  2548. return 0;
  2549. }
  2550. static void map_delmapid(int id)
  2551. {
  2552. ShowNotice("Removing map [ %s ] from maplist"CL_CLL"\n",map[id].name);
  2553. memmove(map+id, map+id+1, sizeof(map[0])*(map_num-id-1));
  2554. map_num--;
  2555. }
  2556. int map_delmap(char* mapname)
  2557. {
  2558. int i;
  2559. char map_name[MAP_NAME_LENGTH];
  2560. if (strcmpi(mapname, "all") == 0) {
  2561. map_num = 0;
  2562. return 0;
  2563. }
  2564. mapindex_getmapname(mapname, map_name);
  2565. for(i = 0; i < map_num; i++) {
  2566. if (strcmp(map[i].name, map_name) == 0) {
  2567. map_delmapid(i);
  2568. return 1;
  2569. }
  2570. }
  2571. return 0;
  2572. }
  2573. /// Initializes map flags and adjusts them depending on configuration.
  2574. void map_flags_init(void)
  2575. {
  2576. int i;
  2577. for( i = 0; i < map_num; i++ )
  2578. {
  2579. // mapflags
  2580. memset(&map[i].flag, 0, sizeof(map[i].flag));
  2581. // additional mapflag data
  2582. map[i].zone = 0; // restricted mapflag zone
  2583. map[i].nocommand = 0; // nocommand mapflag level
  2584. map[i].adjust.bexp = 100; // per map base exp multiplicator
  2585. map[i].adjust.jexp = 100; // per map job exp multiplicator
  2586. memset(map[i].drop_list, 0, sizeof(map[i].drop_list)); // pvp nightmare drop list
  2587. // skill damage
  2588. #ifdef ADJUST_SKILL_DAMAGE
  2589. memset(map[i].skill_damage, 0, sizeof(map[i].skill_damage));
  2590. memset(&map[i].adjust.damage, 0, sizeof(map[i].adjust.damage));
  2591. #endif
  2592. // adjustments
  2593. if( battle_config.pk_mode )
  2594. map[i].flag.pvp = 1; // make all maps pvp for pk_mode [Valaris]
  2595. }
  2596. }
  2597. #define NO_WATER 1000000
  2598. /*
  2599. * Reads from the .rsw for each map
  2600. * Returns water height (or NO_WATER if file doesn't exist) or other error is encountered.
  2601. * Assumed path for file is data/mapname.rsw
  2602. * Credits to LittleWolf
  2603. */
  2604. int map_waterheight(char* mapname)
  2605. {
  2606. char fn[256];
  2607. char *rsw, *found;
  2608. //Look up for the rsw
  2609. sprintf(fn, "data\\%s.rsw", mapname);
  2610. found = grfio_find_file(fn);
  2611. if (found) strcpy(fn, found); // replace with real name
  2612. // read & convert fn
  2613. rsw = (char *) grfio_read (fn);
  2614. if (rsw)
  2615. { //Load water height from file
  2616. int wh = (int) *(float*)(rsw+166);
  2617. aFree(rsw);
  2618. return wh;
  2619. }
  2620. ShowWarning("Failed to find water level for (%s)\n", mapname, fn);
  2621. return NO_WATER;
  2622. }
  2623. /*==================================
  2624. * .GAT format
  2625. *----------------------------------*/
  2626. int map_readgat (struct map_data* m)
  2627. {
  2628. char filename[256];
  2629. uint8* gat;
  2630. int water_height;
  2631. size_t xy, off, num_cells;
  2632. sprintf(filename, "data\\%s.gat", m->name);
  2633. gat = (uint8 *) grfio_read(filename);
  2634. if (gat == NULL)
  2635. return 0;
  2636. m->xs = *(int32*)(gat+6);
  2637. m->ys = *(int32*)(gat+10);
  2638. num_cells = m->xs * m->ys;
  2639. CREATE(m->cell, struct mapcell, num_cells);
  2640. water_height = map_waterheight(m->name);
  2641. // Set cell properties
  2642. off = 14;
  2643. for( xy = 0; xy < num_cells; ++xy )
  2644. {
  2645. // read cell data
  2646. float height = *(float*)( gat + off );
  2647. uint32 type = *(uint32*)( gat + off + 16 );
  2648. off += 20;
  2649. if( type == 0 && water_height != NO_WATER && height > water_height )
  2650. type = 3; // Cell is 0 (walkable) but under water level, set to 3 (walkable water)
  2651. m->cell[xy] = map_gat2cell(type);
  2652. }
  2653. aFree(gat);
  2654. return 1;
  2655. }
  2656. /*======================================
  2657. * Add/Remove map to the map_db
  2658. *--------------------------------------*/
  2659. void map_addmap2db(struct map_data *m)
  2660. {
  2661. uidb_put(map_db, (unsigned int)m->index, m);
  2662. }
  2663. void map_removemapdb(struct map_data *m)
  2664. {
  2665. uidb_remove(map_db, (unsigned int)m->index);
  2666. }
  2667. /*======================================
  2668. * Initiate maps loading stage
  2669. *--------------------------------------*/
  2670. int map_readallmaps (void)
  2671. {
  2672. int i;
  2673. FILE* fp=NULL;
  2674. int maps_removed = 0;
  2675. char *map_cache_buffer = NULL; // Has the uncompressed gat data of all maps, so just one allocation has to be made
  2676. char map_cache_decode_buffer[MAX_MAP_SIZE];
  2677. if( enable_grf )
  2678. ShowStatus("Loading maps (using GRF files)...\n");
  2679. else {
  2680. char mapcachefilepath[254];
  2681. sprintf(mapcachefilepath,"%s/%s%s",db_path,DBPATH,"map_cache.dat");
  2682. ShowStatus("Loading maps (using %s as map cache)...\n", mapcachefilepath);
  2683. if( (fp = fopen(mapcachefilepath, "rb")) == NULL ) {
  2684. ShowFatalError("Unable to open map cache file "CL_WHITE"%s"CL_RESET"\n", mapcachefilepath);
  2685. exit(EXIT_FAILURE); //No use launching server if maps can't be read.
  2686. }
  2687. // Init mapcache data.. [Shinryo]
  2688. map_cache_buffer = map_init_mapcache(fp);
  2689. if(!map_cache_buffer) {
  2690. ShowFatalError("Failed to initialize mapcache data (%s)..\n", mapcachefilepath);
  2691. exit(EXIT_FAILURE);
  2692. }
  2693. }
  2694. for(i = 0; i < map_num; i++) {
  2695. size_t size;
  2696. // show progress
  2697. if(enable_grf)
  2698. ShowStatus("Loading maps [%i/%i]: %s"CL_CLL"\r", i, map_num, map[i].name);
  2699. // try to load the map
  2700. if( !
  2701. (enable_grf?
  2702. map_readgat(&map[i])
  2703. :map_readfromcache(&map[i], map_cache_buffer, map_cache_decode_buffer))
  2704. ) {
  2705. map_delmapid(i);
  2706. maps_removed++;
  2707. i--;
  2708. continue;
  2709. }
  2710. map[i].index = mapindex_name2id(map[i].name);
  2711. if (uidb_get(map_db,(unsigned int)map[i].index) != NULL)
  2712. {
  2713. ShowWarning("Map %s already loaded!"CL_CLL"\n", map[i].name);
  2714. if (map[i].cell) {
  2715. aFree(map[i].cell);
  2716. map[i].cell = NULL;
  2717. }
  2718. map_delmapid(i);
  2719. maps_removed++;
  2720. i--;
  2721. continue;
  2722. }
  2723. map_addmap2db(&map[i]);
  2724. map[i].m = i;
  2725. memset(map[i].moblist, 0, sizeof(map[i].moblist)); //Initialize moblist [Skotlex]
  2726. map[i].mob_delete_timer = INVALID_TIMER; //Initialize timer [Skotlex]
  2727. map[i].bxs = (map[i].xs + BLOCK_SIZE - 1) / BLOCK_SIZE;
  2728. map[i].bys = (map[i].ys + BLOCK_SIZE - 1) / BLOCK_SIZE;
  2729. size = map[i].bxs * map[i].bys * sizeof(struct block_list*);
  2730. map[i].block = (struct block_list**)aCalloc(size, 1);
  2731. map[i].block_mob = (struct block_list**)aCalloc(size, 1);
  2732. }
  2733. // intialization and configuration-dependent adjustments of mapflags
  2734. map_flags_init();
  2735. if( !enable_grf ) {
  2736. fclose(fp);
  2737. // The cache isn't needed anymore, so free it.. [Shinryo]
  2738. aFree(map_cache_buffer);
  2739. }
  2740. // finished map loading
  2741. ShowInfo("Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps."CL_CLL"\n",map_num);
  2742. instance_start = map_num + 1; // Next Map Index will be instances
  2743. if (maps_removed)
  2744. ShowNotice("Maps removed: '"CL_WHITE"%d"CL_RESET"'\n",maps_removed);
  2745. return 0;
  2746. }
  2747. ////////////////////////////////////////////////////////////////////////
  2748. static int map_ip_set = 0;
  2749. static int char_ip_set = 0;
  2750. /*==========================================
  2751. * Console Command Parser [Wizputer]
  2752. *------------------------------------------*/
  2753. int parse_console(const char* buf){
  2754. char type[64];
  2755. char command[64];
  2756. char map[64];
  2757. int16 x = 0;
  2758. int16 y = 0;
  2759. int16 m;
  2760. int n;
  2761. struct map_session_data sd;
  2762. memset(&sd, 0, sizeof(struct map_session_data));
  2763. strcpy(sd.status.name, "console");
  2764. if( ( n = sscanf(buf, "%63[^:]:%63[^:]:%63s %hd %hd[^\n]", type, command, map, &x, &y) ) < 5 ){
  2765. if( ( n = sscanf(buf, "%63[^:]:%63[^\n]", type, command) ) < 2 ) {
  2766. if((n = sscanf(buf, "%63[^\n]", type))<1) return -1; //nothing to do no arg
  2767. }
  2768. }
  2769. if( n != 5 ){ //end string and display
  2770. if( n < 2 ) {
  2771. ShowNotice("Type of command: '%s'\n", type);
  2772. command[0] = '\0';
  2773. map[0] = '\0';
  2774. }
  2775. else {
  2776. ShowNotice("Type of command: '%s' || Command: '%s'\n", type, command);
  2777. map[0] = '\0';
  2778. }
  2779. }
  2780. else
  2781. ShowNotice("Type of command: '%s' || Command: '%s' || Map: '%s' Coords: %d %d\n", type, command, map, x, y);
  2782. if(strcmpi("admin",type) == 0 ) {
  2783. if(strcmpi("map",command) == 0){
  2784. m = map_mapname2mapid(map);
  2785. if( m < 0 ){
  2786. ShowWarning("Console: Unknown map.\n");
  2787. return 0;
  2788. }
  2789. sd.bl.m = m;
  2790. map_search_freecell(&sd.bl, m, &sd.bl.x, &sd.bl.y, -1, -1, 0);
  2791. if( x > 0 )
  2792. sd.bl.x = x;
  2793. if( y > 0 )
  2794. sd.bl.y = y;
  2795. ShowNotice("Now at: '%s' Coords: %d %d\n", map, x, y);
  2796. }
  2797. else if( !is_atcommand(sd.fd, &sd, command, 2) )
  2798. ShowInfo("Console: Invalid atcommand.\n");
  2799. }
  2800. else if( n == 2 && strcmpi("server", type) == 0 ){
  2801. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 ){
  2802. runflag = 0;
  2803. }
  2804. }
  2805. else if( strcmpi("ers_report", type) == 0 ){
  2806. ers_report();
  2807. }
  2808. else if( strcmpi("help", type) == 0 ) {
  2809. ShowInfo("Available commands:\n");
  2810. ShowInfo("\t admin:@<atcommand> => Uses an atcommand. Do NOT use commands requiring an attached player.\n");
  2811. ShowInfo("\t admin:map:<map> <x> <y> => Changes the map from which console commands are executed.\n");
  2812. ShowInfo("\t server:shutdown => Stops the server.\n");
  2813. ShowInfo("\t ers_report => Displays database usage.\n");
  2814. }
  2815. return 0;
  2816. }
  2817. /*==========================================
  2818. * Read map server configuration files (conf/map_athena.conf...)
  2819. *------------------------------------------*/
  2820. int map_config_read(char *cfgName)
  2821. {
  2822. char line[1024], w1[1024], w2[1024];
  2823. FILE *fp;
  2824. fp = fopen(cfgName,"r");
  2825. if( fp == NULL )
  2826. {
  2827. ShowError("Map configuration file not found at: %s\n", cfgName);
  2828. return 1;
  2829. }
  2830. while( fgets(line, sizeof(line), fp) )
  2831. {
  2832. char* ptr;
  2833. if( line[0] == '/' && line[1] == '/' )
  2834. continue;
  2835. if( (ptr = strstr(line, "//")) != NULL )
  2836. *ptr = '\n'; //Strip comments
  2837. if( sscanf(line, "%[^:]: %[^\t\r\n]", w1, w2) < 2 )
  2838. continue;
  2839. //Strip trailing spaces
  2840. ptr = w2 + strlen(w2);
  2841. while (--ptr >= w2 && *ptr == ' ');
  2842. ptr++;
  2843. *ptr = '\0';
  2844. if(strcmpi(w1,"timestamp_format")==0)
  2845. safestrncpy(timestamp_format, w2, 20);
  2846. else if(strcmpi(w1,"stdout_with_ansisequence")==0)
  2847. stdout_with_ansisequence = config_switch(w2);
  2848. else if(strcmpi(w1,"console_silent")==0) {
  2849. msg_silent = atoi(w2);
  2850. if( msg_silent ) // only bother if its actually enabled
  2851. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2852. } else if (strcmpi(w1, "userid")==0)
  2853. chrif_setuserid(w2);
  2854. else if (strcmpi(w1, "passwd") == 0)
  2855. chrif_setpasswd(w2);
  2856. else if (strcmpi(w1, "char_ip") == 0)
  2857. char_ip_set = chrif_setip(w2);
  2858. else if (strcmpi(w1, "char_port") == 0)
  2859. chrif_setport(atoi(w2));
  2860. else if (strcmpi(w1, "map_ip") == 0)
  2861. map_ip_set = clif_setip(w2);
  2862. else if (strcmpi(w1, "bind_ip") == 0)
  2863. clif_setbindip(w2);
  2864. else if (strcmpi(w1, "map_port") == 0) {
  2865. clif_setport(atoi(w2));
  2866. map_port = (atoi(w2));
  2867. } else if (strcmpi(w1, "map") == 0)
  2868. map_addmap(w2);
  2869. else if (strcmpi(w1, "delmap") == 0)
  2870. map_delmap(w2);
  2871. else if (strcmpi(w1, "npc") == 0)
  2872. npc_addsrcfile(w2);
  2873. else if (strcmpi(w1, "delnpc") == 0)
  2874. npc_delsrcfile(w2);
  2875. else if (strcmpi(w1, "autosave_time") == 0) {
  2876. autosave_interval = atoi(w2);
  2877. if (autosave_interval < 1) //Revert to default saving.
  2878. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2879. else
  2880. autosave_interval *= 1000; //Pass from sec to ms
  2881. } else if (strcmpi(w1, "minsave_time") == 0) {
  2882. minsave_interval= atoi(w2);
  2883. if (minsave_interval < 1)
  2884. minsave_interval = 1;
  2885. } else if (strcmpi(w1, "save_settings") == 0)
  2886. save_settings = atoi(w2);
  2887. else if (strcmpi(w1, "motd_txt") == 0)
  2888. strcpy(motd_txt, w2);
  2889. else if (strcmpi(w1, "help_txt") == 0)
  2890. strcpy(help_txt, w2);
  2891. else if (strcmpi(w1, "help2_txt") == 0)
  2892. strcpy(help2_txt, w2);
  2893. else if (strcmpi(w1, "charhelp_txt") == 0)
  2894. strcpy(charhelp_txt, w2);
  2895. else if(strcmpi(w1,"db_path") == 0)
  2896. safestrncpy(db_path,w2,255);
  2897. else if (strcmpi(w1, "console") == 0) {
  2898. console = config_switch(w2);
  2899. if (console)
  2900. ShowNotice("Console Commands are enabled.\n");
  2901. } else if (strcmpi(w1, "enable_spy") == 0)
  2902. enable_spy = config_switch(w2);
  2903. else if (strcmpi(w1, "use_grf") == 0)
  2904. enable_grf = config_switch(w2);
  2905. else if (strcmpi(w1, "console_msg_log") == 0)
  2906. console_msg_log = atoi(w2);//[Ind]
  2907. else if (strcmpi(w1, "import") == 0)
  2908. map_config_read(w2);
  2909. else
  2910. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  2911. }
  2912. fclose(fp);
  2913. return 0;
  2914. }
  2915. void map_reloadnpc_sub(char *cfgName)
  2916. {
  2917. char line[1024], w1[1024], w2[1024];
  2918. FILE *fp;
  2919. fp = fopen(cfgName,"r");
  2920. if( fp == NULL )
  2921. {
  2922. ShowError("Map configuration file not found at: %s\n", cfgName);
  2923. return;
  2924. }
  2925. while( fgets(line, sizeof(line), fp) )
  2926. {
  2927. char* ptr;
  2928. if( line[0] == '/' && line[1] == '/' )
  2929. continue;
  2930. if( (ptr = strstr(line, "//")) != NULL )
  2931. *ptr = '\n'; //Strip comments
  2932. if( sscanf(line, "%[^:]: %[^\t\r\n]", w1, w2) < 2 )
  2933. continue;
  2934. //Strip trailing spaces
  2935. ptr = w2 + strlen(w2);
  2936. while (--ptr >= w2 && *ptr == ' ');
  2937. ptr++;
  2938. *ptr = '\0';
  2939. if (strcmpi(w1, "npc") == 0)
  2940. npc_addsrcfile(w2);
  2941. else if (strcmpi(w1, "delnpc") == 0)
  2942. npc_delsrcfile(w2);
  2943. else if (strcmpi(w1, "import") == 0)
  2944. map_reloadnpc_sub(w2);
  2945. else
  2946. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  2947. }
  2948. fclose(fp);
  2949. }
  2950. void map_reloadnpc(bool clear)
  2951. {
  2952. if (clear)
  2953. npc_addsrcfile("clear"); // this will clear the current script list
  2954. #ifdef RENEWAL
  2955. map_reloadnpc_sub("npc/re/scripts_main.conf");
  2956. #else
  2957. map_reloadnpc_sub("npc/pre-re/scripts_main.conf");
  2958. #endif
  2959. }
  2960. int inter_config_read(char *cfgName)
  2961. {
  2962. char line[1024],w1[1024],w2[1024];
  2963. FILE *fp;
  2964. fp=fopen(cfgName,"r");
  2965. if(fp==NULL){
  2966. ShowError("File not found: %s\n",cfgName);
  2967. return 1;
  2968. }
  2969. while(fgets(line, sizeof(line), fp))
  2970. {
  2971. if(line[0] == '/' && line[1] == '/')
  2972. continue;
  2973. if( sscanf(line,"%[^:]: %[^\r\n]",w1,w2) < 2 )
  2974. continue;
  2975. if(strcmpi(w1,"item_db_db")==0)
  2976. strcpy(item_db_db,w2);
  2977. else
  2978. if(strcmpi(w1,"mob_db_db")==0)
  2979. strcpy(mob_db_db,w2);
  2980. else
  2981. if(strcmpi(w1,"item_db2_db")==0)
  2982. strcpy(item_db2_db,w2);
  2983. else
  2984. if(strcmpi(w1,"item_db_re_db")==0)
  2985. strcpy(item_db_re_db,w2);
  2986. else
  2987. if(strcmpi(w1,"mob_db2_db")==0)
  2988. strcpy(mob_db2_db,w2);
  2989. else if( strcmpi( w1, "item_cash_db_db" ) == 0 )
  2990. strcpy( item_cash_db_db, w2 );
  2991. else if( strcmpi( w1, "item_cash_db2_db" ) == 0 )
  2992. strcpy( item_cash_db2_db, w2 );
  2993. else
  2994. //Map Server SQL DB
  2995. if(strcmpi(w1,"map_server_ip")==0)
  2996. strcpy(map_server_ip, w2);
  2997. else
  2998. if(strcmpi(w1,"map_server_port")==0)
  2999. map_server_port=atoi(w2);
  3000. else
  3001. if(strcmpi(w1,"map_server_id")==0)
  3002. strcpy(map_server_id, w2);
  3003. else
  3004. if(strcmpi(w1,"map_server_pw")==0)
  3005. strcpy(map_server_pw, w2);
  3006. else
  3007. if(strcmpi(w1,"map_server_db")==0)
  3008. strcpy(map_server_db, w2);
  3009. else
  3010. if(strcmpi(w1,"default_codepage")==0)
  3011. strcpy(default_codepage, w2);
  3012. else
  3013. if(strcmpi(w1,"use_sql_db")==0) {
  3014. db_use_sqldbs = config_switch(w2);
  3015. ShowStatus ("Using SQL dbs: %s\n",w2);
  3016. } else
  3017. if(strcmpi(w1,"log_db_ip")==0)
  3018. strcpy(log_db_ip, w2);
  3019. else
  3020. if(strcmpi(w1,"log_db_id")==0)
  3021. strcpy(log_db_id, w2);
  3022. else
  3023. if(strcmpi(w1,"log_db_pw")==0)
  3024. strcpy(log_db_pw, w2);
  3025. else
  3026. if(strcmpi(w1,"log_db_port")==0)
  3027. log_db_port = atoi(w2);
  3028. else
  3029. if(strcmpi(w1,"log_db_db")==0)
  3030. strcpy(log_db_db, w2);
  3031. else
  3032. if( mapreg_config_read(w1,w2) )
  3033. continue;
  3034. //support the import command, just like any other config
  3035. else
  3036. if(strcmpi(w1,"import")==0)
  3037. inter_config_read(w2);
  3038. }
  3039. fclose(fp);
  3040. return 0;
  3041. }
  3042. /*=======================================
  3043. * MySQL Init
  3044. *---------------------------------------*/
  3045. int map_sql_init(void)
  3046. {
  3047. // main db connection
  3048. mmysql_handle = Sql_Malloc();
  3049. ShowInfo("Connecting to the Map DB Server....\n");
  3050. if( SQL_ERROR == Sql_Connect(mmysql_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3051. exit(EXIT_FAILURE);
  3052. ShowStatus("connect success! (Map Server Connection)\n");
  3053. if( strlen(default_codepage) > 0 )
  3054. if ( SQL_ERROR == Sql_SetEncoding(mmysql_handle, default_codepage) )
  3055. Sql_ShowDebug(mmysql_handle);
  3056. return 0;
  3057. }
  3058. int map_sql_close(void)
  3059. {
  3060. ShowStatus("Close Map DB Connection....\n");
  3061. Sql_Free(mmysql_handle);
  3062. mmysql_handle = NULL;
  3063. #ifndef BETA_THREAD_TEST
  3064. if (log_config.sql_logs)
  3065. {
  3066. ShowStatus("Close Log DB Connection....\n");
  3067. Sql_Free(logmysql_handle);
  3068. logmysql_handle = NULL;
  3069. }
  3070. #endif
  3071. return 0;
  3072. }
  3073. int log_sql_init(void)
  3074. {
  3075. #ifndef BETA_THREAD_TEST
  3076. // log db connection
  3077. logmysql_handle = Sql_Malloc();
  3078. ShowInfo(""CL_WHITE"[SQL]"CL_RESET": Connecting to the Log Database "CL_WHITE"%s"CL_RESET" At "CL_WHITE"%s"CL_RESET"...\n",log_db_db,log_db_ip);
  3079. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3080. exit(EXIT_FAILURE);
  3081. ShowStatus(""CL_WHITE"[SQL]"CL_RESET": Successfully '"CL_GREEN"connected"CL_RESET"' to Database '"CL_WHITE"%s"CL_RESET"'.\n", log_db_db);
  3082. if( strlen(default_codepage) > 0 )
  3083. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3084. Sql_ShowDebug(logmysql_handle);
  3085. #endif
  3086. return 0;
  3087. }
  3088. /**
  3089. * @see DBApply
  3090. */
  3091. int map_db_final(DBKey key, DBData *data, va_list ap)
  3092. {
  3093. struct map_data_other_server *mdos = db_data2ptr(data);
  3094. if(mdos && mdos->cell == NULL)
  3095. aFree(mdos);
  3096. return 0;
  3097. }
  3098. /**
  3099. * @see DBApply
  3100. */
  3101. int nick_db_final(DBKey key, DBData *data, va_list args)
  3102. {
  3103. struct charid2nick* p = db_data2ptr(data);
  3104. struct charid_request* req;
  3105. if( p == NULL )
  3106. return 0;
  3107. while( p->requests )
  3108. {
  3109. req = p->requests;
  3110. p->requests = req->next;
  3111. aFree(req);
  3112. }
  3113. aFree(p);
  3114. return 0;
  3115. }
  3116. int cleanup_sub(struct block_list *bl, va_list ap)
  3117. {
  3118. nullpo_ret(bl);
  3119. switch(bl->type) {
  3120. case BL_PC:
  3121. map_quit((struct map_session_data *) bl);
  3122. break;
  3123. case BL_NPC:
  3124. npc_unload((struct npc_data *)bl,false);
  3125. break;
  3126. case BL_MOB:
  3127. unit_free(bl,CLR_OUTSIGHT);
  3128. break;
  3129. case BL_PET:
  3130. //There is no need for this, the pet is removed together with the player. [Skotlex]
  3131. break;
  3132. case BL_ITEM:
  3133. map_clearflooritem(bl);
  3134. break;
  3135. case BL_SKILL:
  3136. skill_delunit((struct skill_unit *) bl);
  3137. break;
  3138. }
  3139. return 1;
  3140. }
  3141. /**
  3142. * @see DBApply
  3143. */
  3144. static int cleanup_db_sub(DBKey key, DBData *data, va_list va)
  3145. {
  3146. return cleanup_sub(db_data2ptr(data), va);
  3147. }
  3148. /*==========================================
  3149. * map destructor
  3150. *------------------------------------------*/
  3151. void do_final(void)
  3152. {
  3153. int i, j;
  3154. struct map_session_data* sd;
  3155. struct s_mapiterator* iter;
  3156. ShowStatus("Terminating...\n");
  3157. Channel_Config.closing = true;
  3158. //Ladies and babies first.
  3159. iter = mapit_getallusers();
  3160. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  3161. map_quit(sd);
  3162. mapit_free(iter);
  3163. /* prepares npcs for a faster shutdown process */
  3164. do_clear_npc();
  3165. // remove all objects on maps
  3166. for (i = 0; i < map_num; i++) {
  3167. ShowStatus("Cleaning up maps [%d/%d]: %s..."CL_CLL"\r", i+1, map_num, map[i].name);
  3168. if (map[i].m >= 0) {
  3169. map_foreachinmap(cleanup_sub, i, BL_ALL);
  3170. channel_delete(map[i].channel);
  3171. }
  3172. }
  3173. ShowStatus("Cleaned up %d maps."CL_CLL"\n", map_num);
  3174. id_db->foreach(id_db,cleanup_db_sub);
  3175. chrif_char_reset_offline();
  3176. chrif_flush_fifo();
  3177. do_final_atcommand();
  3178. do_final_battle();
  3179. do_final_chrif();
  3180. do_final_clif();
  3181. do_final_npc();
  3182. do_final_script();
  3183. do_final_instance();
  3184. do_final_itemdb();
  3185. do_final_storage();
  3186. do_final_guild();
  3187. do_final_party();
  3188. do_final_pc();
  3189. do_final_pet();
  3190. do_final_mob();
  3191. do_final_msg();
  3192. do_final_skill();
  3193. do_final_status();
  3194. do_final_unit();
  3195. do_final_battleground();
  3196. do_final_duel();
  3197. do_final_elemental();
  3198. do_final_cashshop();
  3199. do_final_channel(); //should be called after final guild
  3200. do_final_vending();
  3201. map_db->destroy(map_db, map_db_final);
  3202. for (i=0; i<map_num; i++) {
  3203. if(map[i].cell) aFree(map[i].cell);
  3204. if(map[i].block) aFree(map[i].block);
  3205. if(map[i].block_mob) aFree(map[i].block_mob);
  3206. if(battle_config.dynamic_mobs) { //Dynamic mobs flag by [random]
  3207. if(map[i].mob_delete_timer != INVALID_TIMER)
  3208. delete_timer(map[i].mob_delete_timer, map_removemobs_timer);
  3209. for (j=0; j<MAX_MOB_LIST_PER_MAP; j++)
  3210. if (map[i].moblist[j]) aFree(map[i].moblist[j]);
  3211. }
  3212. }
  3213. mapindex_final();
  3214. if(enable_grf)
  3215. grfio_final();
  3216. id_db->destroy(id_db, NULL);
  3217. pc_db->destroy(pc_db, NULL);
  3218. mobid_db->destroy(mobid_db, NULL);
  3219. bossid_db->destroy(bossid_db, NULL);
  3220. nick_db->destroy(nick_db, nick_db_final);
  3221. charid_db->destroy(charid_db, NULL);
  3222. iwall_db->destroy(iwall_db, NULL);
  3223. regen_db->destroy(regen_db, NULL);
  3224. map_sql_close();
  3225. ShowStatus("Finished.\n");
  3226. }
  3227. static int map_abort_sub(struct map_session_data* sd, va_list ap)
  3228. {
  3229. chrif_save(sd,1);
  3230. return 1;
  3231. }
  3232. //------------------------------
  3233. // Function called when the server
  3234. // has received a crash signal.
  3235. //------------------------------
  3236. void do_abort(void)
  3237. {
  3238. static int run = 0;
  3239. //Save all characters and then flush the inter-connection.
  3240. if (run) {
  3241. ShowFatalError("Server has crashed while trying to save characters. Character data can't be saved!\n");
  3242. return;
  3243. }
  3244. run = 1;
  3245. if (!chrif_isconnected())
  3246. {
  3247. if (pc_db->size(pc_db))
  3248. ShowFatalError("Server has crashed without a connection to the char-server, %u characters can't be saved!\n", pc_db->size(pc_db));
  3249. return;
  3250. }
  3251. ShowError("Server received crash signal! Attempting to save all online characters!\n");
  3252. map_foreachpc(map_abort_sub);
  3253. chrif_flush_fifo();
  3254. }
  3255. /*======================================================
  3256. * Map-Server help options screen
  3257. *------------------------------------------------------*/
  3258. void display_helpscreen(bool do_exit)
  3259. {
  3260. ShowInfo("Usage: %s [options]\n", SERVER_NAME);
  3261. ShowInfo("\n");
  3262. ShowInfo("Options:\n");
  3263. ShowInfo(" -?, -h [--help]\t\tDisplays this help screen.\n");
  3264. ShowInfo(" -v [--version]\t\tDisplays the server's version.\n");
  3265. ShowInfo(" --run-once\t\t\tCloses server after loading (testing).\n");
  3266. ShowInfo(" --map-config <file>\t\tAlternative map-server configuration.\n");
  3267. ShowInfo(" --battle-config <file>\tAlternative battle configuration.\n");
  3268. ShowInfo(" --atcommand-config <file>\tAlternative atcommand configuration.\n");
  3269. ShowInfo(" --script-config <file>\tAlternative script configuration.\n");
  3270. ShowInfo(" --msg-config <file>\t\tAlternative message configuration.\n");
  3271. ShowInfo(" --grf-path <file>\t\tAlternative GRF path configuration.\n");
  3272. ShowInfo(" --inter-config <file>\t\tAlternative inter-server configuration.\n");
  3273. ShowInfo(" --log-config <file>\t\tAlternative logging configuration.\n");
  3274. if( do_exit )
  3275. exit(EXIT_SUCCESS);
  3276. }
  3277. /*======================================================
  3278. * Map-Server Init and Command-line Arguments [Valaris]
  3279. *------------------------------------------------------*/
  3280. void set_server_type(void)
  3281. {
  3282. SERVER_TYPE = ATHENA_SERVER_MAP;
  3283. }
  3284. /*======================================================
  3285. * Message System
  3286. *------------------------------------------------------*/
  3287. struct msg_data {
  3288. char* msg[MAP_MAX_MSG];
  3289. };
  3290. struct msg_data *map_lang2msgdb(uint8 lang){
  3291. return (struct msg_data*)idb_get(map_msg_db, lang);
  3292. }
  3293. void map_do_init_msg(void){
  3294. int test=0, i=0, size;
  3295. char * listelang[] = {
  3296. MSG_CONF_NAME_EN, //default
  3297. MSG_CONF_NAME_RUS,
  3298. MSG_CONF_NAME_SPN,
  3299. MSG_CONF_NAME_GRM,
  3300. MSG_CONF_NAME_CHN,
  3301. MSG_CONF_NAME_MAL,
  3302. MSG_CONF_NAME_IDN,
  3303. MSG_CONF_NAME_FRN,
  3304. MSG_CONF_NAME_POR,
  3305. MSG_CONF_NAME_THA
  3306. };
  3307. map_msg_db = idb_alloc(DB_OPT_BASE);
  3308. size = ARRAYLENGTH(listelang); //avoid recalc
  3309. while(test!=-1 && size>i){ //for all enable lang +(English default)
  3310. test = msg_checklangtype(i,false);
  3311. if(test == 1) msg_config_read(listelang[i],i); //if enabled read it and assign i to langtype
  3312. i++;
  3313. }
  3314. }
  3315. void map_do_final_msg(void){
  3316. DBIterator *iter = db_iterator(map_msg_db);
  3317. struct msg_data *mdb;
  3318. for (mdb = dbi_first(iter); dbi_exists(iter); mdb = dbi_next(iter)) {
  3319. _do_final_msg(MAP_MAX_MSG,mdb->msg);
  3320. aFree(mdb);
  3321. }
  3322. dbi_destroy(iter);
  3323. map_msg_db->destroy(map_msg_db, NULL);
  3324. }
  3325. void map_msg_reload(void){
  3326. map_do_final_msg(); //clear data
  3327. map_do_init_msg();
  3328. }
  3329. int map_msg_config_read(char *cfgName, int lang){
  3330. struct msg_data *mdb;
  3331. if( (mdb = map_lang2msgdb(lang)) == NULL )
  3332. CREATE(mdb, struct msg_data, 1);
  3333. else
  3334. idb_remove(map_msg_db, lang);
  3335. idb_put(map_msg_db, lang, mdb);
  3336. if(_msg_config_read(cfgName,MAP_MAX_MSG,mdb->msg)!=0){ //an error occur
  3337. idb_remove(map_msg_db, lang); //@TRYME
  3338. aFree(mdb);
  3339. }
  3340. return 0;
  3341. }
  3342. const char* map_msg_txt(struct map_session_data *sd, int msg_number){
  3343. struct msg_data *mdb;
  3344. uint8 lang = 0; //default
  3345. const char *tmp; //to verify result
  3346. if(sd && sd->langtype) lang = sd->langtype;
  3347. if( (mdb = map_lang2msgdb(lang)) != NULL){
  3348. tmp = _msg_txt(msg_number,MAP_MAX_MSG,mdb->msg);
  3349. if(strcmp(tmp,"??"))
  3350. return tmp;
  3351. ShowDebug("Message #%d not found for langtype %d.\n",msg_number,lang);
  3352. }
  3353. ShowDebug("Selected langtype %d not loaded, trying fallback...\n",lang);
  3354. if(lang != 0 && (mdb = map_lang2msgdb(0)) != NULL) //fallback
  3355. return _msg_txt(msg_number,MAP_MAX_MSG,mdb->msg);
  3356. return "??";
  3357. }
  3358. /// Called when a terminate signal is received.
  3359. void do_shutdown(void)
  3360. {
  3361. if( runflag != MAPSERVER_ST_SHUTDOWN )
  3362. {
  3363. runflag = MAPSERVER_ST_SHUTDOWN;
  3364. ShowStatus("Shutting down...\n");
  3365. {
  3366. struct map_session_data* sd;
  3367. struct s_mapiterator* iter = mapit_getallusers();
  3368. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  3369. clif_GM_kick(NULL, sd);
  3370. mapit_free(iter);
  3371. flush_fifos();
  3372. }
  3373. chrif_check_shutdown();
  3374. }
  3375. }
  3376. int do_init(int argc, char *argv[])
  3377. {
  3378. #ifdef GCOLLECT
  3379. GC_enable_incremental();
  3380. #endif
  3381. INTER_CONF_NAME="conf/inter_athena.conf";
  3382. LOG_CONF_NAME="conf/log_athena.conf";
  3383. MAP_CONF_NAME = "conf/map_athena.conf";
  3384. BATTLE_CONF_FILENAME = "conf/battle_athena.conf";
  3385. ATCOMMAND_CONF_FILENAME = "conf/atcommand_athena.conf";
  3386. SCRIPT_CONF_NAME = "conf/script_athena.conf";
  3387. GRF_PATH_FILENAME = "conf/grf-files.txt";
  3388. /* Multilanguage */
  3389. MSG_CONF_NAME_EN = "conf/msg_conf/map_msg.conf"; // English (default)
  3390. MSG_CONF_NAME_RUS = "conf/msg_conf/map_msg_rus.conf"; // Russian
  3391. MSG_CONF_NAME_SPN = "conf/msg_conf/map_msg_spn.conf"; // Spanish
  3392. MSG_CONF_NAME_GRM = "conf/msg_conf/map_msg_grm.conf"; // German
  3393. MSG_CONF_NAME_CHN = "conf/msg_conf/map_msg_chn.conf"; // Chinese
  3394. MSG_CONF_NAME_MAL = "conf/msg_conf/map_msg_mal.conf"; // Malaysian
  3395. MSG_CONF_NAME_IDN = "conf/msg_conf/map_msg_idn.conf"; // Indonesian
  3396. MSG_CONF_NAME_FRN = "conf/msg_conf/map_msg_frn.conf"; // French
  3397. MSG_CONF_NAME_POR = "conf/msg_conf/map_msg_por.conf"; // Brazilian Portuguese
  3398. MSG_CONF_NAME_THA = "conf/msg_conf/map_msg_tha.conf"; // Thai
  3399. /* Multilanguage */
  3400. cli_get_options(argc,argv);
  3401. rnd_init();
  3402. map_config_read(MAP_CONF_NAME);
  3403. // loads npcs
  3404. map_reloadnpc(false);
  3405. chrif_checkdefaultlogin();
  3406. if (!map_ip_set || !char_ip_set) {
  3407. char ip_str[16];
  3408. ip2str(addr_[0], ip_str);
  3409. ShowWarning("Not all IP addresses in map_athena.conf configured, autodetecting...\n");
  3410. if (naddr_ == 0)
  3411. ShowError("Unable to determine your IP address...\n");
  3412. else if (naddr_ > 1)
  3413. ShowNotice("Multiple interfaces detected...\n");
  3414. ShowInfo("Defaulting to %s as our IP address\n", ip_str);
  3415. if (!map_ip_set)
  3416. clif_setip(ip_str);
  3417. if (!char_ip_set)
  3418. chrif_setip(ip_str);
  3419. }
  3420. battle_config_read(BATTLE_CONF_FILENAME);
  3421. script_config_read(SCRIPT_CONF_NAME);
  3422. inter_config_read(INTER_CONF_NAME);
  3423. log_config_read(LOG_CONF_NAME);
  3424. id_db = idb_alloc(DB_OPT_BASE);
  3425. pc_db = idb_alloc(DB_OPT_BASE); //Added for reliable map_id2sd() use. [Skotlex]
  3426. mobid_db = idb_alloc(DB_OPT_BASE); //Added to lower the load of the lazy mob ai. [Skotlex]
  3427. bossid_db = idb_alloc(DB_OPT_BASE); // Used for Convex Mirror quick MVP search
  3428. map_db = uidb_alloc(DB_OPT_BASE);
  3429. nick_db = idb_alloc(DB_OPT_BASE);
  3430. charid_db = idb_alloc(DB_OPT_BASE);
  3431. regen_db = idb_alloc(DB_OPT_BASE); // efficient status_natural_heal processing
  3432. iwall_db = strdb_alloc(DB_OPT_RELEASE_DATA,2*NAME_LENGTH+2+1); // [Zephyrus] Invisible Walls
  3433. map_sql_init();
  3434. if (log_config.sql_logs)
  3435. log_sql_init();
  3436. mapindex_init();
  3437. if(enable_grf)
  3438. grfio_init(GRF_PATH_FILENAME);
  3439. map_readallmaps();
  3440. add_timer_func_list(map_freeblock_timer, "map_freeblock_timer");
  3441. add_timer_func_list(map_clearflooritem_timer, "map_clearflooritem_timer");
  3442. add_timer_func_list(map_removemobs_timer, "map_removemobs_timer");
  3443. add_timer_interval(gettick()+1000, map_freeblock_timer, 0, 0, 60*1000);
  3444. map_do_init_msg();
  3445. do_init_atcommand();
  3446. do_init_battle();
  3447. do_init_instance();
  3448. do_init_channel();
  3449. do_init_chrif();
  3450. do_init_clif();
  3451. do_init_script();
  3452. do_init_itemdb();
  3453. do_init_cashshop();
  3454. do_init_skill();
  3455. do_init_mob();
  3456. do_init_pc();
  3457. do_init_status();
  3458. do_init_party();
  3459. do_init_guild();
  3460. do_init_storage();
  3461. do_init_pet();
  3462. do_init_merc();
  3463. do_init_mercenary();
  3464. do_init_elemental();
  3465. do_init_quest();
  3466. do_init_npc();
  3467. do_init_unit();
  3468. do_init_battleground();
  3469. do_init_duel();
  3470. do_init_vending();
  3471. npc_event_do_oninit(); // Init npcs (OnInit)
  3472. if (battle_config.pk_mode)
  3473. ShowNotice("Server is running on '"CL_WHITE"PK Mode"CL_RESET"'.\n");
  3474. ShowStatus("Server is '"CL_GREEN"ready"CL_RESET"' and listening on port '"CL_WHITE"%d"CL_RESET"'.\n\n", map_port);
  3475. if( runflag != CORE_ST_STOP )
  3476. {
  3477. shutdown_callback = do_shutdown;
  3478. runflag = MAPSERVER_ST_RUNNING;
  3479. }
  3480. #if defined(BUILDBOT)
  3481. if( buildbotflag )
  3482. exit(EXIT_FAILURE);
  3483. #endif
  3484. if( console ){ //start listening
  3485. add_timer_func_list(parse_console_timer, "parse_console_timer");
  3486. add_timer_interval(gettick()+1000, parse_console_timer, 0, 0, 1000); //start in 1s each 1sec
  3487. }
  3488. return 0;
  3489. }