map.c 90 KB

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