map.c 100 KB

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