map.c 103 KB

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