map.c 97 KB

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