map.c 104 KB

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