map.c 90 KB

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