map.c 90 KB

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