map.c 110 KB

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