map.c 97 KB

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