map.c 98 KB

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