map.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <math.h>
  8. #ifndef _WIN32
  9. #include <unistd.h>
  10. #endif
  11. #include "../common/core.h"
  12. #include "../common/timer.h"
  13. #include "../common/grfio.h"
  14. #include "../common/malloc.h"
  15. #include "../common/socket.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/version.h"
  18. #include "../common/nullpo.h"
  19. #include "map.h"
  20. #include "chrif.h"
  21. #include "clif.h"
  22. #include "intif.h"
  23. #include "npc.h"
  24. #include "pc.h"
  25. #include "status.h"
  26. #include "mob.h"
  27. #include "chat.h"
  28. #include "itemdb.h"
  29. #include "storage.h"
  30. #include "skill.h"
  31. #include "trade.h"
  32. #include "party.h"
  33. #include "unit.h"
  34. #include "battle.h"
  35. #include "script.h"
  36. #include "guild.h"
  37. #include "pet.h"
  38. #include "mercenary.h" //[orn]
  39. #include "atcommand.h"
  40. #include "charcommand.h"
  41. #include "log.h"
  42. #include "charsave.h"
  43. #include "irc.h"
  44. // maybe put basic macros to somewhere else
  45. #define swap(a,b) ((a == b) || ((a ^= b), (b ^= a), (a ^= b)))
  46. #ifndef TXT_ONLY
  47. #include "mail.h" // mail system [Valaris]
  48. MYSQL mmysql_handle;
  49. MYSQL_RES* sql_res ;
  50. MYSQL_ROW sql_row ;
  51. char tmp_sql[65535]="";
  52. MYSQL logmysql_handle; //For the log database - fix by [Maeki]
  53. MYSQL_RES* logsql_res ;
  54. MYSQL_ROW logsql_row ;
  55. MYSQL mail_handle; // mail system [Valaris]
  56. MYSQL_RES* mail_res ;
  57. MYSQL_ROW mail_row ;
  58. int map_server_port = 3306;
  59. char map_server_ip[16] = "127.0.0.1";
  60. char map_server_id[32] = "ragnarok";
  61. char map_server_pw[32] = "ragnarok";
  62. char map_server_db[32] = "ragnarok";
  63. char default_codepage[32] = ""; //Feature by irmin.
  64. int db_use_sqldbs = 0;
  65. int connection_ping_interval = 0;
  66. char item_db_db[32] = "item_db";
  67. char item_db2_db[32] = "item_db2";
  68. char mob_db_db[32] = "mob_db";
  69. char mob_db2_db[32] = "mob_db2";
  70. int log_db_port = 3306;
  71. char log_db_ip[16] = "127.0.0.1";
  72. char log_db_id[32] = "ragnarok";
  73. char log_db_pw[32] = "ragnarok";
  74. char log_db[32] = "log";
  75. int mail_server_port = 3306;
  76. char mail_server_ip[16] = "127.0.0.1";
  77. char mail_server_id[32] = "ragnarok";
  78. char mail_server_pw[32] = "ragnarok";
  79. char mail_server_db[32] = "ragnarok";
  80. int mail_server_enable = 0;
  81. char char_db[32] = "char";
  82. char mail_db[32] = "mail";
  83. char charsql_host[40] = "localhost";
  84. int charsql_port = 3306;
  85. char charsql_user[32] = "ragnarok";
  86. char charsql_pass[32] = "eAthena";
  87. char charsql_db[40] = "ragnarok";
  88. MYSQL charsql_handle;
  89. MYSQL_RES* charsql_res;
  90. MYSQL_ROW charsql_row;
  91. #endif /* not TXT_ONLY */
  92. int lowest_gm_level = 1;
  93. // This param using for sending mainchat
  94. // messages like whispers to this nick. [LuzZza]
  95. char main_chat_nick[16] = "Main";
  96. char *INTER_CONF_NAME;
  97. char *LOG_CONF_NAME;
  98. char *MAP_CONF_NAME;
  99. char *BATTLE_CONF_FILENAME;
  100. char *ATCOMMAND_CONF_FILENAME;
  101. char *CHARCOMMAND_CONF_FILENAME;
  102. char *SCRIPT_CONF_NAME;
  103. char *MSG_CONF_NAME;
  104. char *GRF_PATH_FILENAME;
  105. // 極力 staticでロ?カルに?める
  106. static struct dbt * id_db=NULL;
  107. static struct dbt * pc_db=NULL;
  108. static struct dbt * map_db=NULL;
  109. static struct dbt * charid_db=NULL;
  110. static int map_users=0;
  111. static struct block_list *objects[MAX_FLOORITEM];
  112. static int first_free_object_id=0,last_object_id=0;
  113. #define block_free_max 1048576
  114. struct block_list *block_free[block_free_max];
  115. static int block_free_count = 0, block_free_lock = 0;
  116. #define BL_LIST_MAX 1048576
  117. static struct block_list *bl_list[BL_LIST_MAX];
  118. static int bl_list_count = 0;
  119. static char afm_dir[1024] = ""; // [Valaris]
  120. struct map_data map[MAX_MAP_PER_SERVER];
  121. int map_num = 0;
  122. int map_port=0;
  123. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  124. int minsave_interval = 100;
  125. int save_settings = 0xFFFF;
  126. int charsave_method = 0; //Default 'OLD' Save method (SQL ONLY!) [Sirius]
  127. int agit_flag = 0;
  128. int night_flag = 0; // 0=day, 1=night [Yor]
  129. struct charid2nick {
  130. char nick[NAME_LENGTH];
  131. int req_id;
  132. };
  133. // ォ゙ォテォラォュォ罩テォキォ袮ラ鯑ォユォ鬮ー(map_athana.conf?ェホread_map_from_cacheェヌ�・)
  134. // 0:ララ鯑ェキェハェ、 1:゙ェ?�ワチ� 2:?�ワチ�
  135. int map_read_flag = READ_FROM_GAT;
  136. char map_cache_file[256]="db/map.info"; // ォ゙ォテォラォュォ罩テォキォ雖ユォ。ォ、ォ・」
  137. char db_path[256] = "db";
  138. char motd_txt[256] = "conf/motd.txt";
  139. char help_txt[256] = "conf/help.txt";
  140. char help2_txt[256] = "conf/help2.txt";
  141. char charhelp_txt[256] = "conf/charhelp.txt";
  142. char wisp_server_name[NAME_LENGTH] = "Server"; // can be modified in char-server configuration file
  143. int console = 0;
  144. int enable_spy = 0; //To enable/disable @spy commands, which consume too much cpu time when sending packets. [Skotlex]
  145. /*==========================================
  146. * 全map鯖?計での接??設定
  147. * (char鯖から送られてくる)
  148. *------------------------------------------
  149. */
  150. void map_setusers(int fd)
  151. {
  152. RFIFOHEAD(fd);
  153. WFIFOHEAD(fd, 2);
  154. map_users = RFIFOL(fd,2);
  155. // send some anser
  156. WFIFOW(fd,0) = 0x2718;
  157. WFIFOSET(fd,2);
  158. }
  159. /*==========================================
  160. * 全map鯖?計での接??取得 (/wへの?答用)
  161. *------------------------------------------
  162. */
  163. int map_getusers(void) {
  164. return map_users;
  165. }
  166. //Distance functions, taken from http://www.flipcode.com/articles/article_fastdistance.shtml
  167. int check_distance(int dx, int dy, int distance) {
  168. #ifdef CIRCULAR_AREA
  169. //In this case, we just do a square comparison. Add 1 tile grace for diagonal range checks.
  170. return (dx*dx + dy*dy <= distance*distance + (dx&&dy?1:0));
  171. #else
  172. if (dx < 0) dx = -dx;
  173. if (dy < 0) dy = -dy;
  174. return ((dx<dy?dy:dx) <= distance);
  175. #endif
  176. }
  177. unsigned int distance(int dx, int dy) {
  178. #ifdef CIRCULAR_AREA
  179. unsigned int min, max;
  180. if ( dx < 0 ) dx = -dx;
  181. if ( dy < 0 ) dy = -dy;
  182. //There appears to be something wrong with the aproximation below when either dx/dy is 0! [Skotlex]
  183. if ( dx == 0 ) return dy;
  184. if ( dy == 0 ) return dx;
  185. if ( dx < dy )
  186. {
  187. min = dx;
  188. max = dy;
  189. } else {
  190. min = dy;
  191. max = dx;
  192. }
  193. // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
  194. return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) +
  195. ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 );
  196. #else
  197. if (dx < 0) dx = -dx;
  198. if (dy < 0) dy = -dy;
  199. return (dx<dy?dy:dx);
  200. #endif
  201. }
  202. //
  203. // block削除の安全性確保?理
  204. //
  205. /*==========================================
  206. * blockをfreeするときfreeの?わりに呼ぶ
  207. * ロックされているときはバッファにためる
  208. *------------------------------------------
  209. */
  210. int map_freeblock (struct block_list *bl)
  211. {
  212. nullpo_retr(block_free_lock, bl);
  213. if (block_free_lock == 0 || block_free_count >= block_free_max)
  214. {
  215. aFree(bl);
  216. bl = NULL;
  217. if (block_free_count >= block_free_max)
  218. if (battle_config.error_log)
  219. ShowWarning("map_freeblock: too many free block! %d %d\n",
  220. block_free_count, block_free_lock);
  221. } else
  222. block_free[block_free_count++] = bl;
  223. return block_free_lock;
  224. }
  225. /*==========================================
  226. * blockのfreeを一市Iに禁止する
  227. *------------------------------------------
  228. */
  229. int map_freeblock_lock (void)
  230. {
  231. return ++block_free_lock;
  232. }
  233. /*==========================================
  234. * blockのfreeのロックを解除する
  235. * このとき、ロックが完全になくなると
  236. * バッファにたまっていたblockを全部削除
  237. *------------------------------------------
  238. */
  239. //int map_freeblock_unlock (void)
  240. int map_freeblock_unlock_sub(char *file, int lineno)
  241. {
  242. if ((--block_free_lock) == 0) {
  243. int i;
  244. for (i = 0; i < block_free_count; i++)
  245. { //Directly calling aFree shouldn't be a leak, as Free remembers the size the original pointed to memory was allocated with? [Skotlex]
  246. // aFree(block_free[i]);
  247. // _mfree(block_free[i], file, lineno, __func__);
  248. _mfree(block_free[i], file, ((block_free[i]?block_free[i]->type:0)*100000)+lineno, __func__);
  249. block_free[i] = NULL;
  250. }
  251. block_free_count = 0;
  252. } else if (block_free_lock < 0) {
  253. if (battle_config.error_log)
  254. ShowError("map_freeblock_unlock: lock count < 0 !\n");
  255. block_free_lock = 0; // 次回以降のロックに支障が出てくるのでリセット
  256. }
  257. return block_free_lock;
  258. }
  259. // map_freeblock_lock() を呼んで map_freeblock_unlock() を呼ばない
  260. // 関数があったので、定期的にblock_free_lockをリセットするようにする。
  261. // この関数は、do_timer() のトップレベルから呼ばれるので、
  262. // block_free_lock を直接いじっても支障無いはず。
  263. int map_freeblock_timer (int tid, unsigned int tick, int id, int data)
  264. {
  265. if (block_free_lock > 0) {
  266. ShowError("map_freeblock_timer: block_free_lock(%d) is invalid.\n", block_free_lock);
  267. block_free_lock = 1;
  268. map_freeblock_unlock();
  269. }
  270. return 0;
  271. }
  272. //
  273. // block化?理
  274. //
  275. /*==========================================
  276. * map[]のblock_listから?がっている場合に
  277. * bl->prevにbl_headのアドレスを入れておく
  278. *------------------------------------------
  279. */
  280. static struct block_list bl_head;
  281. #ifdef CELL_NOSTACK
  282. /*==========================================
  283. * These pair of functions update the counter of how many objects
  284. * lie on a tile.
  285. *------------------------------------------
  286. */
  287. void map_addblcell(struct block_list *bl)
  288. {
  289. if(bl->m<0 || bl->x<0 || bl->x>=map[bl->m].xs
  290. || bl->y<0 || bl->y>=map[bl->m].ys || !(bl->type&BL_CHAR))
  291. return;
  292. map[bl->m].cell_bl[bl->x+bl->y*map[bl->m].xs]++;
  293. return;
  294. }
  295. void map_delblcell(struct block_list *bl)
  296. {
  297. if(bl->m <0 || bl->x<0 || bl->x>=map[bl->m].xs
  298. || bl->y<0 || bl->y>=map[bl->m].ys || !(bl->type&BL_CHAR))
  299. return;
  300. map[bl->m].cell_bl[bl->x+bl->y*map[bl->m].xs]--;
  301. }
  302. #endif
  303. /*==========================================
  304. * Adds a block to the map.
  305. * If flag is 1, then the block was just added
  306. * otherwise it is part of a transition.
  307. *------------------------------------------
  308. */
  309. int map_addblock_sub (struct block_list *bl, int flag)
  310. {
  311. int m, x, y, pos;
  312. nullpo_retr(0, bl);
  313. if (bl->prev != NULL) {
  314. if(battle_config.error_log)
  315. ShowError("map_addblock error : bl->prev != NULL\n");
  316. return 0;
  317. }
  318. m = bl->m;
  319. x = bl->x;
  320. y = bl->y;
  321. if (m < 0 || m >= map_num ||
  322. x < 0 || x >= map[m].xs ||
  323. y < 0 || y >= map[m].ys)
  324. return 1;
  325. #ifdef CELL_NOSTACK
  326. map_addblcell(bl);
  327. #endif
  328. pos = x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs;
  329. if (bl->type == BL_MOB) {
  330. bl->next = map[m].block_mob[pos];
  331. bl->prev = &bl_head;
  332. if (bl->next) bl->next->prev = bl;
  333. map[m].block_mob[pos] = bl;
  334. map[m].block_mob_count[pos]++;
  335. } else {
  336. bl->next = map[m].block[pos];
  337. bl->prev = &bl_head;
  338. if (bl->next) bl->next->prev = bl;
  339. map[m].block[pos] = bl;
  340. map[m].block_count[pos]++;
  341. if (bl->type == BL_PC && flag)
  342. {
  343. struct map_session_data* sd;
  344. if (map[m].users++ == 0 && battle_config.dynamic_mobs) //Skotlex
  345. map_spawnmobs(m);
  346. sd = (struct map_session_data*)bl;
  347. if (battle_config.pet_no_gvg && map_flag_gvg(m) && sd->pd)
  348. { //Return the pet to egg. [Skotlex]
  349. clif_displaymessage(sd->fd, "Pets are not allowed in Guild Wars.");
  350. pet_menu(sd, 3); //Option 3 is return to egg.
  351. }
  352. }
  353. }
  354. return 0;
  355. }
  356. /*==========================================
  357. * Removes a block from the map.
  358. * If flag is 1, then the block is removed for good
  359. * otherwise it is part of a transition.
  360. *------------------------------------------
  361. */
  362. int map_delblock_sub (struct block_list *bl, int flag)
  363. {
  364. int b;
  365. nullpo_retr(0, bl);
  366. // ?にblocklistから?けている
  367. if (bl->prev == NULL) {
  368. if (bl->next != NULL) {
  369. // prevがNULLでnextがNULLでないのは有ってはならない
  370. if(battle_config.error_log)
  371. ShowError("map_delblock error : bl->next!=NULL\n");
  372. }
  373. return 0;
  374. }
  375. #ifdef CELL_NOSTACK
  376. map_delblcell(bl);
  377. #endif
  378. b = bl->x/BLOCK_SIZE+(bl->y/BLOCK_SIZE)*map[bl->m].bxs;
  379. if (bl->type == BL_PC && flag)
  380. if (--map[bl->m].users == 0 && battle_config.dynamic_mobs) //[Skotlex]
  381. map_removemobs(bl->m);
  382. if (bl->next)
  383. bl->next->prev = bl->prev;
  384. if (bl->prev == &bl_head) {
  385. // リストの頭なので、map[]のblock_listを更新する
  386. if (bl->type == BL_MOB) {
  387. map[bl->m].block_mob[b] = bl->next;
  388. if ((map[bl->m].block_mob_count[b]--) < 0)
  389. map[bl->m].block_mob_count[b] = 0;
  390. } else {
  391. map[bl->m].block[b] = bl->next;
  392. if((map[bl->m].block_count[b]--) < 0)
  393. map[bl->m].block_count[b] = 0;
  394. }
  395. } else {
  396. bl->prev->next = bl->next;
  397. }
  398. bl->next = NULL;
  399. bl->prev = NULL;
  400. return 0;
  401. }
  402. /*==========================================
  403. * Moves a block a x/y target position. [Skotlex]
  404. * Pass flag as 1 to prevent doing skill_unit_move checks
  405. * (which are executed by default on BL_CHAR types)
  406. *------------------------------------------
  407. */
  408. int map_moveblock(struct block_list *bl, int x1, int y1, unsigned int tick) {
  409. int x0 = bl->x, y0 = bl->y;
  410. struct status_change *sc = NULL;
  411. int moveblock = ( x0/BLOCK_SIZE != x1/BLOCK_SIZE || y0/BLOCK_SIZE != y1/BLOCK_SIZE);
  412. if (!bl->prev) {
  413. //Block not in map, just update coordinates, but do naught else.
  414. bl->x = x1;
  415. bl->y = y1;
  416. return 0;
  417. }
  418. //TODO: Perhaps some outs of bounds checking should be placed here?
  419. if (bl->type&BL_CHAR) {
  420. skill_unit_move(bl,tick,2);
  421. sc = status_get_sc(bl);
  422. if (sc && sc->count) {
  423. if (sc->data[SC_CLOSECONFINE].timer != -1)
  424. status_change_end(bl, SC_CLOSECONFINE, -1);
  425. if (sc->data[SC_CLOSECONFINE2].timer != -1)
  426. status_change_end(bl, SC_CLOSECONFINE2, -1);
  427. // if (sc->data[SC_BLADESTOP].timer != -1) //Won't stop when you are knocked away, go figure...
  428. // status_change_end(bl, SC_BLADESTOP, -1);
  429. if (sc->data[SC_BASILICA].timer != -1)
  430. status_change_end(bl, SC_BASILICA, -1);
  431. }
  432. }
  433. if (moveblock) map_delblock_sub(bl,0);
  434. #ifdef CELL_NOSTACK
  435. else map_delblcell(bl);
  436. #endif
  437. bl->x = x1;
  438. bl->y = y1;
  439. if (moveblock) map_addblock_sub(bl,0);
  440. #ifdef CELL_NOSTACK
  441. else map_addblcell(bl);
  442. #endif
  443. if (bl->type&BL_CHAR) {
  444. skill_unit_move(bl,tick,3);
  445. if (sc) {
  446. if (sc->count) {
  447. if (sc->data[SC_CLOAKING].timer != -1)
  448. skill_check_cloaking(bl, sc);
  449. if (sc->data[SC_DANCING].timer != -1)
  450. skill_unit_move_unit_group((struct skill_unit_group *)sc->data[SC_DANCING].val2, bl->m, x1-x0, y1-y0);
  451. if (sc->data[SC_WARM].timer != -1)
  452. skill_unit_move_unit_group((struct skill_unit_group *)sc->data[SC_WARM].val4, bl->m, x1-x0, y1-y0);
  453. }
  454. }
  455. }
  456. return 0;
  457. }
  458. /*==========================================
  459. * 周?のPC人?を?える (unused)
  460. *------------------------------------------
  461. */
  462. int map_countnearpc (int m, int x, int y)
  463. {
  464. int bx, by, c = 0;
  465. struct block_list *bl=NULL;
  466. if (map[m].users == 0)
  467. return 0;
  468. for (by = y/BLOCK_SIZE-AREA_SIZE/BLOCK_SIZE-1; by<=y/BLOCK_SIZE+AREA_SIZE/BLOCK_SIZE+1; by++) {
  469. if (by < 0 || by >= map[m].bys)
  470. continue;
  471. for (bx = x/BLOCK_SIZE-AREA_SIZE/BLOCK_SIZE-1; bx <= x/BLOCK_SIZE+AREA_SIZE/BLOCK_SIZE+1; bx++) {
  472. if (bx < 0 || bx >= map[m].bxs)
  473. continue;
  474. bl = map[m].block[bx+by*map[m].bxs];
  475. while(bl) {
  476. if (bl->type == BL_PC)
  477. c++;
  478. bl = bl->next;
  479. }
  480. }
  481. }
  482. return c;
  483. }
  484. /*==========================================
  485. * Counts specified number of objects on given cell.
  486. *------------------------------------------
  487. */
  488. int map_count_oncell(int m, int x, int y, int type) {
  489. int bx,by;
  490. struct block_list *bl=NULL;
  491. int i,c;
  492. int count = 0;
  493. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  494. return 0;
  495. bx = x/BLOCK_SIZE;
  496. by = y/BLOCK_SIZE;
  497. if (type&~BL_MOB)
  498. {
  499. bl = map[m].block[bx+by*map[m].bxs];
  500. c = map[m].block_count[bx+by*map[m].bxs];
  501. for(i=0;i<c && bl;i++,bl=bl->next)
  502. if(bl->x == x && bl->y == y && bl->type&type) count++;
  503. }
  504. if (type&BL_MOB)
  505. {
  506. bl = map[m].block_mob[bx+by*map[m].bxs];
  507. c = map[m].block_mob_count[bx+by*map[m].bxs];
  508. for(i=0;i<c && bl;i++,bl=bl->next)
  509. if(bl->x == x && bl->y == y) count++;
  510. }
  511. return count;
  512. }
  513. /*
  514. * ォサォ・セェホ��ェヒフクェトェアェソォケォュォ・讚ヒォテォネェ��ケ
  515. */
  516. struct skill_unit *map_find_skill_unit_oncell(struct block_list *target,int x,int y,int skill_id,struct skill_unit *out_unit)
  517. {
  518. int m,bx,by;
  519. struct block_list *bl;
  520. int i,c;
  521. struct skill_unit *unit;
  522. m = target->m;
  523. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  524. return NULL;
  525. bx = x/BLOCK_SIZE;
  526. by = y/BLOCK_SIZE;
  527. bl = map[m].block[bx+by*map[m].bxs];
  528. c = map[m].block_count[bx+by*map[m].bxs];
  529. for(i=0;i<c && bl;i++,bl=bl->next){
  530. if (bl->x != x || bl->y != y || bl->type != BL_SKILL)
  531. continue;
  532. unit = (struct skill_unit *) bl;
  533. if (unit==out_unit || !unit->alive ||
  534. !unit->group || unit->group->skill_id!=skill_id)
  535. continue;
  536. if (battle_check_target(&unit->bl,target,unit->group->target_flag)>0)
  537. return unit;
  538. }
  539. return NULL;
  540. }
  541. /*==========================================
  542. * Adapted from foreachinarea for an easier invocation. [Skotlex]
  543. *------------------------------------------
  544. */
  545. int map_foreachinrange(int (*func)(struct block_list*,va_list),struct block_list *center, int range,int type,...) {
  546. va_list ap;
  547. int bx,by,m;
  548. int returnCount =0; //total sum of returned values of func() [Skotlex]
  549. struct block_list *bl=NULL;
  550. int blockcount=bl_list_count,i,c;
  551. int x0,x1,y0,y1;
  552. m = center->m;
  553. if (m < 0)
  554. return 0;
  555. va_start(ap,type);
  556. x0 = center->x-range;
  557. x1 = center->x+range;
  558. y0 = center->y-range;
  559. y1 = center->y+range;
  560. if (x0 < 0) x0 = 0;
  561. if (y0 < 0) y0 = 0;
  562. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  563. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  564. if (type&~BL_MOB)
  565. for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
  566. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  567. bl = map[m].block[bx+by*map[m].bxs];
  568. c = map[m].block_count[bx+by*map[m].bxs];
  569. for(i=0;i<c && bl;i++,bl=bl->next){
  570. if(bl && bl->type&type
  571. && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1
  572. #ifdef CIRCULAR_AREA
  573. && check_distance_bl(center, bl, range)
  574. #endif
  575. && bl_list_count<BL_LIST_MAX)
  576. bl_list[bl_list_count++]=bl;
  577. }
  578. }
  579. }
  580. if(type&BL_MOB)
  581. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  582. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  583. bl = map[m].block_mob[bx+by*map[m].bxs];
  584. c = map[m].block_mob_count[bx+by*map[m].bxs];
  585. for(i=0;i<c && bl;i++,bl=bl->next){
  586. if(bl
  587. && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1
  588. #ifdef CIRCULAR_AREA
  589. && check_distance_bl(center, bl, range)
  590. #endif
  591. && bl_list_count<BL_LIST_MAX)
  592. bl_list[bl_list_count++]=bl;
  593. }
  594. }
  595. }
  596. if(bl_list_count>=BL_LIST_MAX) {
  597. if(battle_config.error_log)
  598. ShowWarning("map_foreachinrange: block count too many!\n");
  599. }
  600. map_freeblock_lock(); // メモリからの解放を禁止する
  601. for(i=blockcount;i<bl_list_count;i++)
  602. if(bl_list[i]->prev) // 有?かどうかチェック
  603. returnCount += func(bl_list[i],ap);
  604. map_freeblock_unlock(); // 解放を許可する
  605. va_end(ap);
  606. bl_list_count = blockcount;
  607. return returnCount; //[Skotlex]
  608. }
  609. /*==========================================
  610. * Same as foreachinrange, but there must be a shoot-able range between center and target to be counted in. [Skotlex]
  611. *------------------------------------------
  612. */
  613. int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block_list *center, int range,int type,...) {
  614. va_list ap;
  615. int bx,by,m;
  616. int returnCount =0; //total sum of returned values of func() [Skotlex]
  617. struct block_list *bl=NULL;
  618. int blockcount=bl_list_count,i,c;
  619. int x0,x1,y0,y1;
  620. m = center->m;
  621. if (m < 0)
  622. return 0;
  623. va_start(ap,type);
  624. x0 = center->x-range;
  625. x1 = center->x+range;
  626. y0 = center->y-range;
  627. y1 = center->y+range;
  628. if (x0 < 0) x0 = 0;
  629. if (y0 < 0) y0 = 0;
  630. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  631. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  632. if (type&~BL_MOB)
  633. for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
  634. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  635. bl = map[m].block[bx+by*map[m].bxs];
  636. c = map[m].block_count[bx+by*map[m].bxs];
  637. for(i=0;i<c && bl;i++,bl=bl->next){
  638. if(bl && bl->type&type
  639. && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1
  640. #ifdef CIRCULAR_AREA
  641. && check_distance_bl(center, bl, range)
  642. #endif
  643. && path_search_long(NULL,center->m,center->x,center->y,bl->x,bl->y)
  644. && bl_list_count<BL_LIST_MAX)
  645. bl_list[bl_list_count++]=bl;
  646. }
  647. }
  648. }
  649. if(type&BL_MOB)
  650. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  651. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  652. bl = map[m].block_mob[bx+by*map[m].bxs];
  653. c = map[m].block_mob_count[bx+by*map[m].bxs];
  654. for(i=0;i<c && bl;i++,bl=bl->next){
  655. if(bl
  656. && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1
  657. #ifdef CIRCULAR_AREA
  658. && check_distance_bl(center, bl, range)
  659. #endif
  660. && path_search_long(NULL,center->m,center->x,center->y,bl->x,bl->y)
  661. && bl_list_count<BL_LIST_MAX)
  662. bl_list[bl_list_count++]=bl;
  663. }
  664. }
  665. }
  666. if(bl_list_count>=BL_LIST_MAX) {
  667. if(battle_config.error_log)
  668. ShowWarning("map_foreachinrange: block count too many!\n");
  669. }
  670. map_freeblock_lock(); // メモリからの解放を禁止する
  671. for(i=blockcount;i<bl_list_count;i++)
  672. if(bl_list[i]->prev) // 有?かどうかチェック
  673. returnCount += func(bl_list[i],ap);
  674. map_freeblock_unlock(); // 解放を許可する
  675. va_end(ap);
  676. bl_list_count = blockcount;
  677. return returnCount; //[Skotlex]
  678. }
  679. /*==========================================
  680. * map m (x0,y0)-(x1,y1)?の全objに?して
  681. * funcを呼ぶ
  682. * type!=0 ならその種類のみ
  683. *------------------------------------------
  684. */
  685. int map_foreachinarea(int (*func)(struct block_list*,va_list),int m,int x0,int y0,int x1,int y1,int type,...) {
  686. va_list ap;
  687. int bx,by;
  688. int returnCount =0; //total sum of returned values of func() [Skotlex]
  689. struct block_list *bl=NULL;
  690. int blockcount=bl_list_count,i,c;
  691. if (m < 0)
  692. return 0;
  693. va_start(ap,type);
  694. if (x1 < x0)
  695. { //Swap range
  696. bx = x0;
  697. x0 = x1;
  698. x1 = bx;
  699. }
  700. if (y1 < y0)
  701. {
  702. bx = y0;
  703. y0 = y1;
  704. y1 = bx;
  705. }
  706. if (x0 < 0) x0 = 0;
  707. if (y0 < 0) y0 = 0;
  708. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  709. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  710. if (type&~BL_MOB)
  711. for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
  712. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  713. bl = map[m].block[bx+by*map[m].bxs];
  714. c = map[m].block_count[bx+by*map[m].bxs];
  715. for(i=0;i<c && bl;i++,bl=bl->next){
  716. if(bl && bl->type&type && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  717. bl_list[bl_list_count++]=bl;
  718. }
  719. }
  720. }
  721. if(type&BL_MOB)
  722. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  723. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  724. bl = map[m].block_mob[bx+by*map[m].bxs];
  725. c = map[m].block_mob_count[bx+by*map[m].bxs];
  726. for(i=0;i<c && bl;i++,bl=bl->next){
  727. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  728. bl_list[bl_list_count++]=bl;
  729. }
  730. }
  731. }
  732. if(bl_list_count>=BL_LIST_MAX) {
  733. if(battle_config.error_log)
  734. ShowWarning("map_foreachinarea: block count too many!\n");
  735. }
  736. map_freeblock_lock(); // メモリからの解放を禁止する
  737. for(i=blockcount;i<bl_list_count;i++)
  738. if(bl_list[i]->prev) // 有?かどうかチェック
  739. returnCount += func(bl_list[i],ap);
  740. map_freeblock_unlock(); // 解放を許可する
  741. va_end(ap);
  742. bl_list_count = blockcount;
  743. return returnCount; //[Skotlex]
  744. }
  745. /*==========================================
  746. * 矩形(x0,y0)-(x1,y1)が(dx,dy)移動した暫フ
  747. * 領域外になる領域(矩形かL字形)?のobjに
  748. * ?してfuncを呼ぶ
  749. *
  750. * dx,dyは-1,0,1のみとする(どんな値でもいいっぽい?)
  751. *------------------------------------------
  752. */
  753. int map_foreachinmovearea(int (*func)(struct block_list*,va_list),int m,int x0,int y0,int x1,int y1,int dx,int dy,int type,...) {
  754. int bx,by;
  755. int returnCount =0; //total sum of returned values of func() [Skotlex]
  756. struct block_list *bl=NULL;
  757. va_list ap;
  758. int blockcount=bl_list_count,i,c;
  759. va_start(ap,type);
  760. if (x1 < x0)
  761. { //Swap range
  762. bx = x0;
  763. x0 = x1;
  764. x1 = bx;
  765. }
  766. if (y1 < y0)
  767. {
  768. bx = y0;
  769. y0 = y1;
  770. y1 = bx;
  771. }
  772. if(dx==0 || dy==0){
  773. // 矩形領域の場合
  774. if(dx==0){
  775. if(dy<0){
  776. y0=y1+dy+1;
  777. } else {
  778. y1=y0+dy-1;
  779. }
  780. } else if(dy==0){
  781. if(dx<0){
  782. x0=x1+dx+1;
  783. } else {
  784. x1=x0+dx-1;
  785. }
  786. }
  787. if(x0<0) x0=0;
  788. if(y0<0) y0=0;
  789. if(x1>=map[m].xs) x1=map[m].xs-1;
  790. if(y1>=map[m].ys) y1=map[m].ys-1;
  791. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  792. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  793. if (type&~BL_MOB) {
  794. bl = map[m].block[bx+by*map[m].bxs];
  795. c = map[m].block_count[bx+by*map[m].bxs];
  796. for(i=0;i<c && bl;i++,bl=bl->next){
  797. if(bl && bl->type&type && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  798. bl_list[bl_list_count++]=bl;
  799. }
  800. }
  801. if (type&BL_MOB) {
  802. bl = map[m].block_mob[bx+by*map[m].bxs];
  803. c = map[m].block_mob_count[bx+by*map[m].bxs];
  804. for(i=0;i<c && bl;i++,bl=bl->next){
  805. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  806. bl_list[bl_list_count++]=bl;
  807. }
  808. }
  809. }
  810. }
  811. }else{
  812. // L字領域の場合
  813. if(x0<0) x0=0;
  814. if(y0<0) y0=0;
  815. if(x1>=map[m].xs) x1=map[m].xs-1;
  816. if(y1>=map[m].ys) y1=map[m].ys-1;
  817. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  818. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  819. if (type & ~BL_MOB) {
  820. bl = map[m].block[bx+by*map[m].bxs];
  821. c = map[m].block_count[bx+by*map[m].bxs];
  822. for(i=0;i<c && bl;i++,bl=bl->next){
  823. if(!bl || !(bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1))
  824. continue;
  825. if(bl && bl->type&type && ((dx>0 && bl->x<x0+dx) || (dx<0 && bl->x>x1+dx) ||
  826. (dy>0 && bl->y<y0+dy) || (dy<0 && bl->y>y1+dy)) &&
  827. bl_list_count<BL_LIST_MAX)
  828. bl_list[bl_list_count++]=bl;
  829. }
  830. }
  831. if (type & BL_MOB) {
  832. bl = map[m].block_mob[bx+by*map[m].bxs];
  833. c = map[m].block_mob_count[bx+by*map[m].bxs];
  834. for(i=0;i<c && bl;i++,bl=bl->next){
  835. if(!bl || !(bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1))
  836. continue;
  837. if(bl && ((dx>0 && bl->x<x0+dx) || (dx<0 && bl->x>x1+dx) ||
  838. (dy>0 && bl->y<y0+dy) || (dy<0 && bl->y>y1+dy)) &&
  839. bl_list_count<BL_LIST_MAX)
  840. bl_list[bl_list_count++]=bl;
  841. }
  842. }
  843. }
  844. }
  845. }
  846. if(bl_list_count>=BL_LIST_MAX) {
  847. if(battle_config.error_log)
  848. ShowWarning("map_foreachinmovearea: block count too many!\n");
  849. }
  850. map_freeblock_lock(); // メモリからの解放を禁止する
  851. for(i=blockcount;i<bl_list_count;i++)
  852. if(bl_list[i]->prev) { // 有?かどうかチェック
  853. if (bl_list[i]->type == BL_PC
  854. && session[((struct map_session_data *) bl_list[i])->fd] == NULL)
  855. continue;
  856. returnCount += func(bl_list[i],ap);
  857. }
  858. map_freeblock_unlock(); // 解放を許可する
  859. va_end(ap);
  860. bl_list_count = blockcount;
  861. return returnCount;
  862. }
  863. // -- moonsoul (added map_foreachincell which is a rework of map_foreachinarea but
  864. // which only checks the exact single x/y passed to it rather than an
  865. // area radius - may be more useful in some instances)
  866. //
  867. int map_foreachincell(int (*func)(struct block_list*,va_list),int m,int x,int y,int type,...) {
  868. int bx,by;
  869. int returnCount =0; //total sum of returned values of func() [Skotlex]
  870. struct block_list *bl=NULL;
  871. va_list ap;
  872. int blockcount=bl_list_count,i,c;
  873. va_start(ap,type);
  874. by=y/BLOCK_SIZE;
  875. bx=x/BLOCK_SIZE;
  876. if(type&~BL_MOB)
  877. {
  878. bl = map[m].block[bx+by*map[m].bxs];
  879. c = map[m].block_count[bx+by*map[m].bxs];
  880. for(i=0;i<c && bl;i++,bl=bl->next)
  881. {
  882. if(bl && bl->type&type && bl->x==x && bl->y==y && bl_list_count<BL_LIST_MAX)
  883. bl_list[bl_list_count++]=bl;
  884. }
  885. }
  886. if(type&BL_MOB)
  887. {
  888. bl = map[m].block_mob[bx+by*map[m].bxs];
  889. c = map[m].block_mob_count[bx+by*map[m].bxs];
  890. for(i=0;i<c && bl;i++,bl=bl->next)
  891. {
  892. if(bl && bl->x==x && bl->y==y && bl_list_count<BL_LIST_MAX)
  893. bl_list[bl_list_count++]=bl;
  894. }
  895. }
  896. if(bl_list_count>=BL_LIST_MAX) {
  897. if(battle_config.error_log)
  898. ShowWarning("map_foreachincell: block count too many!\n");
  899. }
  900. map_freeblock_lock(); // メモリからの解放を禁止する
  901. for(i=blockcount;i<bl_list_count;i++)
  902. if(bl_list[i]->prev) // 有?かどうかチェック
  903. returnCount += func(bl_list[i],ap);
  904. map_freeblock_unlock(); // 解放を許可する
  905. va_end(ap);
  906. bl_list_count = blockcount;
  907. return returnCount;
  908. }
  909. /*============================================================
  910. * For checking a path between two points (x0, y0) and (x1, y1)
  911. *------------------------------------------------------------
  912. */
  913. int map_foreachinpath(int (*func)(struct block_list*,va_list),int m,int x0,int y0,int x1,int y1,int range,int type,...)
  914. {
  915. int returnCount =0; //total sum of returned values of func() [Skotlex]
  916. //////////////////////////////////////////////////////////////
  917. //
  918. // sharp shooting 3 [Skotlex]
  919. //
  920. //////////////////////////////////////////////////////////////
  921. // problem:
  922. // Same as Sharp Shooting 1. Hits all targets within range of
  923. // the line.
  924. // (t1,t2 t3 and t4 get hit)
  925. //
  926. // target 1
  927. // x t4
  928. // t2
  929. // t3 x
  930. // x
  931. // S
  932. //////////////////////////////////////////////////////////////
  933. // Methodology:
  934. // My trigonometrics and math are a little rusty... so the approach I am writing
  935. // here is basicly do a double for to check for all targets in the square that
  936. // contains the initial and final positions (area range increased to match the
  937. // radius given), then for each object to test, calculate the distance to the
  938. // path and include it if the range fits and the target is in the line (0<k<1,
  939. // as they call it).
  940. // The implementation I took as reference is found at
  941. // http://astronomy.swin.edu.au/~pbourke/geometry/pointline/
  942. // (they have a link to a C implementation, too)
  943. // This approach is a lot like #2 commented on this function, which I have no
  944. // idea why it was commented. I won't use doubles/floats, but pure int math for
  945. // speed purposes. The range considered is always the same no matter how
  946. // close/far the target is because that's how SharpShooting works currently in
  947. // kRO.
  948. //Generic map_foreach* variables.
  949. va_list ap;
  950. int i, blockcount = bl_list_count;
  951. struct block_list *bl;
  952. int c, bx, by;
  953. //method specific variables
  954. int magnitude2; //The square of the magnitude
  955. int k, xi, yi, xu, yu;
  956. int mx0 = x0, mx1 = x1, my0 = y0, my1 = y1;
  957. //Avoid needless calculations by not getting the sqrt right away.
  958. #define MAGNITUDE2(x0, y0, x1, y1) (((x1)-(x0))*((x1)-(x0)) + ((y1)-(y0))*((y1)-(y0)))
  959. if (m < 0)
  960. return 0;
  961. va_start(ap,type);
  962. //Expand target area to cover range.
  963. if (mx0 > mx1)
  964. {
  965. mx0+=range;
  966. mx1-=range;
  967. } else {
  968. mx0-=range;
  969. mx1+=range;
  970. }
  971. if (my0 > my1)
  972. {
  973. my0+=range;
  974. my1-=range;
  975. } else {
  976. my0-=range;
  977. my1+=range;
  978. }
  979. //The two fors assume mx0 < mx1 && my0 < my1
  980. if (mx0 > mx1)
  981. {
  982. k = mx1;
  983. mx1 = mx0;
  984. mx0 = k;
  985. }
  986. if (my0 > my1)
  987. {
  988. k = my1;
  989. my1 = my0;
  990. my0 = k;
  991. }
  992. if (mx0 < 0) mx0 = 0;
  993. if (my0 < 0) my0 = 0;
  994. if (mx1 >= map[m].xs) mx1 = map[m].xs-1;
  995. if (my1 >= map[m].ys) my1 = map[m].ys-1;
  996. range*=range<<8; //Values are shifted later on for higher precision using int math.
  997. magnitude2 = MAGNITUDE2(x0,y0, x1,y1);
  998. if (magnitude2 < 1) //Same begin and ending point, can't trace path.
  999. return 0;
  1000. if (type & ~BL_MOB)
  1001. for (by = my0 / BLOCK_SIZE; by <= my1 / BLOCK_SIZE; by++) {
  1002. for(bx=mx0/BLOCK_SIZE;bx<=mx1/BLOCK_SIZE;bx++){
  1003. bl = map[m].block[bx+by*map[m].bxs];
  1004. c = map[m].block_count[bx+by*map[m].bxs];
  1005. for(i=0;i<c && bl;i++,bl=bl->next){
  1006. if(bl && bl->prev && bl->type&type && bl_list_count<BL_LIST_MAX)
  1007. {
  1008. xi = bl->x;
  1009. yi = bl->y;
  1010. k = (xi-x0)*(x1-x0) + (yi-y0)*(y1-y0);
  1011. if (k < 0 || k > magnitude2) //Since more skills use this, check for ending point as well.
  1012. continue;
  1013. //All these shifts are to increase the precision of the intersection point and distance considering how it's
  1014. //int math.
  1015. k = (k<<4)/magnitude2; //k will be between 1~16 instead of 0~1
  1016. xi<<=4;
  1017. yi<<=4;
  1018. xu= (x0<<4) +k*(x1-x0);
  1019. yu= (y0<<4) +k*(y1-y0);
  1020. k = MAGNITUDE2(xi, yi, xu, yu);
  1021. //If all dot coordinates were <<4 the square of the magnitude is <<8
  1022. if (k > range)
  1023. continue;
  1024. bl_list[bl_list_count++]=bl;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. if(type&BL_MOB)
  1030. for(by=my0/BLOCK_SIZE;by<=my1/BLOCK_SIZE;by++){
  1031. for(bx=mx0/BLOCK_SIZE;bx<=mx1/BLOCK_SIZE;bx++){
  1032. bl = map[m].block_mob[bx+by*map[m].bxs];
  1033. c = map[m].block_mob_count[bx+by*map[m].bxs];
  1034. for(i=0;i<c && bl;i++,bl=bl->next){
  1035. if(bl && bl->prev && bl_list_count<BL_LIST_MAX)
  1036. {
  1037. xi = bl->x;
  1038. yi = bl->y;
  1039. k = (xi-x0)*(x1-x0) + (yi-y0)*(y1-y0);
  1040. if (k < 0 || k > magnitude2)
  1041. continue;
  1042. k = (k<<4)/magnitude2; //k will be between 1~16 instead of 0~1
  1043. xi<<=4;
  1044. yi<<=4;
  1045. xu= (x0<<4) +k*(x1-x0);
  1046. yu= (y0<<4) +k*(y1-y0);
  1047. k = MAGNITUDE2(xi, yi, xu, yu);
  1048. //If all dot coordinates were <<4 the square of the magnitude is <<8
  1049. if (k > range)
  1050. continue;
  1051. bl_list[bl_list_count++]=bl;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if(bl_list_count>=BL_LIST_MAX) {
  1057. if(battle_config.error_log)
  1058. ShowWarning("map_foreachinpath: block count too many!\n");
  1059. }
  1060. map_freeblock_lock();
  1061. for(i=blockcount;i<bl_list_count;i++)
  1062. if(bl_list[i]->prev) //This check is done in case some object gets killed due to further skill processing.
  1063. returnCount += func(bl_list[i],ap);
  1064. map_freeblock_unlock();
  1065. va_end(ap);
  1066. bl_list_count = blockcount;
  1067. return returnCount; //[Skotlex]
  1068. }
  1069. // Copy of map_foreachincell, but applied to the whole map. [Skotlex]
  1070. int map_foreachinmap(int (*func)(struct block_list*,va_list),int m,int type,...) {
  1071. int b, bsize;
  1072. int returnCount =0; //total sum of returned values of func() [Skotlex]
  1073. struct block_list *bl=NULL;
  1074. va_list ap;
  1075. int blockcount=bl_list_count,i,c;
  1076. va_start(ap,type);
  1077. bsize = map[m].bxs * map[m].bys;
  1078. if(type&~BL_MOB)
  1079. {
  1080. for(b=0;b<bsize;b++){
  1081. bl = map[m].block[b];
  1082. c = map[m].block_count[b];
  1083. for(i=0;i<c && bl;i++,bl=bl->next)
  1084. {
  1085. if(bl && bl->type&type && bl_list_count<BL_LIST_MAX)
  1086. bl_list[bl_list_count++]=bl;
  1087. }
  1088. }
  1089. }
  1090. if(type&BL_MOB)
  1091. {
  1092. for(b=0;b<bsize;b++){
  1093. bl = map[m].block_mob[b];
  1094. c = map[m].block_mob_count[b];
  1095. for(i=0;i<c && bl;i++,bl=bl->next)
  1096. {
  1097. if(bl && bl_list_count<BL_LIST_MAX)
  1098. bl_list[bl_list_count++]=bl;
  1099. }
  1100. }
  1101. }
  1102. if(bl_list_count>=BL_LIST_MAX) {
  1103. if(battle_config.error_log)
  1104. ShowWarning("map_foreachinmap: block count too many!\n");
  1105. }
  1106. map_freeblock_lock(); // メモリからの解放を禁止する
  1107. for(i=blockcount;i<bl_list_count;i++)
  1108. if(bl_list[i]->prev) // 有?かどうかチェック
  1109. returnCount += func(bl_list[i],ap);
  1110. map_freeblock_unlock(); // 解放を許可する
  1111. va_end(ap);
  1112. bl_list_count = blockcount;
  1113. return returnCount;
  1114. }
  1115. /*==========================================
  1116. * 床アイテムやエフェクト用の一三bj割り?て
  1117. * object[]への保存とid_db登?まで
  1118. *
  1119. * bl->idもこの中で設定して問題無い?
  1120. *------------------------------------------
  1121. */
  1122. int map_addobject(struct block_list *bl) {
  1123. int i;
  1124. if( bl == NULL ){
  1125. ShowWarning("map_addobject nullpo?\n");
  1126. return 0;
  1127. }
  1128. if(first_free_object_id<2 || first_free_object_id>=MAX_FLOORITEM)
  1129. first_free_object_id=2;
  1130. for(i=first_free_object_id;i<MAX_FLOORITEM && objects[i];i++);
  1131. if(i>=MAX_FLOORITEM){
  1132. if(battle_config.error_log)
  1133. ShowWarning("no free object id\n");
  1134. return 0;
  1135. }
  1136. first_free_object_id=i;
  1137. if(last_object_id<i)
  1138. last_object_id=i;
  1139. objects[i]=bl;
  1140. idb_put(id_db,i,bl);
  1141. return i;
  1142. }
  1143. /*==========================================
  1144. * 一三bjectの解放
  1145. * map_delobjectのfreeしないバ?ジョン
  1146. *------------------------------------------
  1147. */
  1148. int map_delobjectnofree(int id) {
  1149. if(objects[id]==NULL)
  1150. return 0;
  1151. map_delblock(objects[id]);
  1152. idb_remove(id_db,id);
  1153. objects[id]=NULL;
  1154. if(first_free_object_id>id)
  1155. first_free_object_id=id;
  1156. while(last_object_id>2 && objects[last_object_id]==NULL)
  1157. last_object_id--;
  1158. return 0;
  1159. }
  1160. /*==========================================
  1161. * 一三bjectの解放
  1162. * block_listからの削除、id_dbからの削除
  1163. * object dataのfree、object[]へのNULL代入
  1164. *
  1165. * addとの??性が無いのが?になる
  1166. *------------------------------------------
  1167. */
  1168. int map_delobject(int id) {
  1169. struct block_list *obj = objects[id];
  1170. if(obj==NULL)
  1171. return 0;
  1172. map_delobjectnofree(id);
  1173. map_freeblock(obj);
  1174. return 0;
  1175. }
  1176. /*==========================================
  1177. * 全一三bj相手にfuncを呼ぶ
  1178. *
  1179. *------------------------------------------
  1180. */
  1181. void map_foreachobject(int (*func)(struct block_list*,va_list),int type,...) {
  1182. int i;
  1183. int blockcount=bl_list_count;
  1184. va_list ap;
  1185. va_start(ap,type);
  1186. for(i=2;i<=last_object_id;i++){
  1187. if(objects[i]){
  1188. if(!(objects[i]->type==type)) // Fixed [Lance]
  1189. continue;
  1190. if(bl_list_count>=BL_LIST_MAX) {
  1191. if(battle_config.error_log)
  1192. ShowWarning("map_foreachobject: too many blocks !\n");
  1193. break;
  1194. }
  1195. bl_list[bl_list_count++]=objects[i];
  1196. }
  1197. }
  1198. map_freeblock_lock();
  1199. for(i=blockcount;i<bl_list_count;i++)
  1200. if( bl_list[i]->prev || bl_list[i]->next )
  1201. func(bl_list[i],ap);
  1202. map_freeblock_unlock();
  1203. va_end(ap);
  1204. bl_list_count = blockcount;
  1205. }
  1206. /*==========================================
  1207. * 床アイテムを消す
  1208. *
  1209. * data==0の暫ヘtimerで消えた殊 * data!=0の暫ヘ拾う等で消えた暫ニして動作
  1210. *
  1211. * 後者は、map_clearflooritem(id)へ
  1212. * map.h?で#defineしてある
  1213. *------------------------------------------
  1214. */
  1215. int map_clearflooritem_timer(int tid,unsigned int tick,int id,int data) {
  1216. struct flooritem_data *fitem=NULL;
  1217. fitem = (struct flooritem_data *)objects[id];
  1218. if(fitem==NULL || fitem->bl.type!=BL_ITEM || (!data && fitem->cleartimer != tid)){
  1219. if(battle_config.error_log)
  1220. ShowError("map_clearflooritem_timer : error\n");
  1221. return 1;
  1222. }
  1223. if(data)
  1224. delete_timer(fitem->cleartimer,map_clearflooritem_timer);
  1225. else if(fitem->item_data.card[0] == CARD0_PET)
  1226. intif_delete_petdata( MakeDWord(fitem->item_data.card[1],fitem->item_data.card[2]) );
  1227. clif_clearflooritem(fitem,0);
  1228. map_delobject(fitem->bl.id);
  1229. return 0;
  1230. }
  1231. /*==========================================
  1232. * (m,x,y) locates a random available free cell around the given coordinates
  1233. * to place an BL_ITEM object. Scan area is 9x9, returns 1 on success.
  1234. * x and y are modified with the target cell when successful.
  1235. *------------------------------------------
  1236. */
  1237. int map_searchrandfreecell(int m,int *x,int *y,int stack) {
  1238. int free_cell,i,j;
  1239. int free_cells[9][2];
  1240. for(free_cell=0,i=-1;i<=1;i++){
  1241. if(i+*y<0 || i+*y>=map[m].ys)
  1242. continue;
  1243. for(j=-1;j<=1;j++){
  1244. if(j+*x<0 || j+*x>=map[m].xs)
  1245. continue;
  1246. if(map_getcell(m,j+*x,i+*y,CELL_CHKNOPASS))
  1247. continue;
  1248. //Avoid item stacking to prevent against exploits. [Skotlex]
  1249. if(stack && map_count_oncell(m,j+*x,i+*y, BL_ITEM) > stack)
  1250. continue;
  1251. free_cells[free_cell][0] = j+*x;
  1252. free_cells[free_cell++][1] = i+*y;
  1253. }
  1254. }
  1255. if(free_cell==0)
  1256. return 0;
  1257. free_cell = rand()%free_cell;
  1258. *x = free_cells[free_cell][0];
  1259. *y = free_cells[free_cell][1];
  1260. return 1;
  1261. }
  1262. static int map_count_sub(struct block_list *bl,va_list ap)
  1263. {
  1264. return 1;
  1265. }
  1266. /*==========================================
  1267. * Locates a random spare cell around the object given, using range as max
  1268. * distance from that spot. Used for warping functions. Use range < 0 for
  1269. * whole map range.
  1270. * Returns 1 on success. when it fails and src is available, x/y are set to src's
  1271. * src can be null as long as flag&1
  1272. * when ~flag&1, m is not needed.
  1273. * Flag values:
  1274. * &1 = random cell must be around given m,x,y, not around src
  1275. * &2 = the target should be able to walk to the target tile.
  1276. * &4 = there shouldn't be any players around the target tile (use the no_spawn_on_player setting)
  1277. *------------------------------------------
  1278. */
  1279. int map_search_freecell(struct block_list *src, int m, short *x,short *y, int rx, int ry, int flag) {
  1280. int tries, spawn=0;
  1281. int bx, by;
  1282. int rx2 = 2*rx+1;
  1283. int ry2 = 2*ry+1;
  1284. if (!src && (!(flag&1) || flag&2))
  1285. {
  1286. ShowDebug("map_search_freecell: Incorrect usage! When src is NULL, flag has to be &1 and can't have &2\n");
  1287. return 0;
  1288. }
  1289. if (flag&1) {
  1290. bx = *x;
  1291. by = *y;
  1292. } else {
  1293. bx = src->x;
  1294. by = src->y;
  1295. m = src->m;
  1296. }
  1297. if (!rx && !ry) {
  1298. //No range? Return the target cell then....
  1299. *x = bx;
  1300. *y = by;
  1301. return map_getcell(m,*x,*y,CELL_CHKREACH);
  1302. }
  1303. if (rx >= 0 && ry >= 0) {
  1304. tries = rx2*ry2;
  1305. if (tries > 100) tries = 100;
  1306. } else {
  1307. tries = map[m].xs*map[m].ys;
  1308. if (tries > 500) tries = 500;
  1309. }
  1310. while(tries--) {
  1311. *x = (rx >= 0)?(rand()%rx2-rx+bx):(rand()%(map[m].xs-2)+1);
  1312. *y = (ry >= 0)?(rand()%ry2-ry+by):(rand()%(map[m].ys-2)+1);
  1313. if (*x == bx && *y == by)
  1314. continue; //Avoid picking the same target tile.
  1315. if (map_getcell(m,*x,*y,CELL_CHKREACH))
  1316. {
  1317. if(flag&2 && !unit_can_reach_pos(src, *x, *y, 1))
  1318. continue;
  1319. if(flag&4) {
  1320. if (spawn >= 100) return 0; //Limit of retries reached.
  1321. if (spawn++ < battle_config.no_spawn_on_player &&
  1322. map_foreachinarea(map_count_sub, m,
  1323. *x-AREA_SIZE, *y-AREA_SIZE,
  1324. *x+AREA_SIZE, *y+AREA_SIZE, BL_PC)
  1325. )
  1326. continue;
  1327. }
  1328. return 1;
  1329. }
  1330. }
  1331. *x = bx;
  1332. *y = by;
  1333. return 0;
  1334. }
  1335. /*==========================================
  1336. * (m,x,y)を中心に3x3以?に床アイテム設置
  1337. *
  1338. * item_dataはamount以外をcopyする
  1339. * type flag: &1 MVP item. &2 do stacking check.
  1340. *------------------------------------------
  1341. */
  1342. int map_addflooritem(struct item *item_data,int amount,int m,int x,int y,struct map_session_data *first_sd,
  1343. struct map_session_data *second_sd,struct map_session_data *third_sd,int type) {
  1344. int r;
  1345. unsigned int tick;
  1346. struct flooritem_data *fitem=NULL;
  1347. nullpo_retr(0, item_data);
  1348. if(!map_searchrandfreecell(m,&x,&y,type&2?1:0))
  1349. return 0;
  1350. r=rand();
  1351. fitem = (struct flooritem_data *)aCalloc(1,sizeof(*fitem));
  1352. fitem->bl.type=BL_ITEM;
  1353. fitem->bl.prev = fitem->bl.next = NULL;
  1354. fitem->bl.m=m;
  1355. fitem->bl.x=x;
  1356. fitem->bl.y=y;
  1357. fitem->bl.id = map_addobject(&fitem->bl);
  1358. if(fitem->bl.id==0){
  1359. aFree(fitem);
  1360. return 0;
  1361. }
  1362. tick = gettick();
  1363. if(first_sd) {
  1364. fitem->first_get_id = first_sd->bl.id;
  1365. if(type&1)
  1366. fitem->first_get_tick = tick + battle_config.mvp_item_first_get_time;
  1367. else
  1368. fitem->first_get_tick = tick + battle_config.item_first_get_time;
  1369. }
  1370. if(second_sd) {
  1371. fitem->second_get_id = second_sd->bl.id;
  1372. if(type&1)
  1373. fitem->second_get_tick = tick + battle_config.mvp_item_first_get_time + battle_config.mvp_item_second_get_time;
  1374. else
  1375. fitem->second_get_tick = tick + battle_config.item_first_get_time + battle_config.item_second_get_time;
  1376. }
  1377. if(third_sd) {
  1378. fitem->third_get_id = third_sd->bl.id;
  1379. if(type&1)
  1380. fitem->third_get_tick = tick + battle_config.mvp_item_first_get_time + battle_config.mvp_item_second_get_time + battle_config.mvp_item_third_get_time;
  1381. else
  1382. fitem->third_get_tick = tick + battle_config.item_first_get_time + battle_config.item_second_get_time + battle_config.item_third_get_time;
  1383. }
  1384. memcpy(&fitem->item_data,item_data,sizeof(*item_data));
  1385. fitem->item_data.amount=amount;
  1386. fitem->subx=(r&3)*3+3;
  1387. fitem->suby=((r>>2)&3)*3+3;
  1388. fitem->cleartimer=add_timer(gettick()+battle_config.flooritem_lifetime,map_clearflooritem_timer,fitem->bl.id,0);
  1389. map_addblock(&fitem->bl);
  1390. clif_dropflooritem(fitem);
  1391. return fitem->bl.id;
  1392. }
  1393. static void* create_charid2nick(DBKey key, va_list args) {
  1394. struct charid2nick *p;
  1395. p = (struct charid2nick *)aCallocA(1, sizeof (struct charid2nick));
  1396. return p;
  1397. }
  1398. /*==========================================
  1399. * charid_dbへ追加(返信待ちがあれば返信)
  1400. *------------------------------------------
  1401. */
  1402. void map_addchariddb(int charid, char *name) {
  1403. struct charid2nick *p;
  1404. int req = 0;
  1405. p = idb_ensure(charid_db,charid,create_charid2nick);
  1406. req = p->req_id;
  1407. p->req_id = 0;
  1408. //We overwrite the nick anyway in case a different one arrived.
  1409. memcpy(p->nick, name, NAME_LENGTH);
  1410. if (req) {
  1411. struct map_session_data *sd = map_id2sd(req);
  1412. if (sd) clif_solved_charname(sd,charid);
  1413. }
  1414. }
  1415. /*==========================================
  1416. * charid_dbへ追加(返信要求のみ)
  1417. *------------------------------------------
  1418. */
  1419. int map_reqchariddb(struct map_session_data * sd,int charid) {
  1420. struct charid2nick *p=NULL;
  1421. nullpo_retr(0, sd);
  1422. p = (struct charid2nick*)idb_get(charid_db,charid);
  1423. if(p) return 0; //Nothing to request, we already have the name!
  1424. p = (struct charid2nick *)aCalloc(1,sizeof(struct charid2nick));
  1425. p->req_id=sd->bl.id;
  1426. idb_put(charid_db,charid,p);
  1427. return 0;
  1428. }
  1429. /*==========================================
  1430. * id_dbへblを追加
  1431. *------------------------------------------
  1432. */
  1433. void map_addiddb(struct block_list *bl) {
  1434. nullpo_retv(bl);
  1435. if (bl->type == BL_PC)
  1436. idb_put(pc_db,bl->id,bl);
  1437. idb_put(id_db,bl->id,bl);
  1438. }
  1439. /*==========================================
  1440. * id_dbからblを削除
  1441. *------------------------------------------
  1442. */
  1443. void map_deliddb(struct block_list *bl) {
  1444. nullpo_retv(bl);
  1445. if (bl->type == BL_PC)
  1446. idb_remove(pc_db,bl->id);
  1447. idb_remove(id_db,bl->id);
  1448. }
  1449. /*==========================================
  1450. * PCのquit?理 map.c?分
  1451. *
  1452. * quit?理の主?が違うような?もしてきた
  1453. *------------------------------------------
  1454. */
  1455. int map_quit(struct map_session_data *sd) {
  1456. //nullpo_retr(0, sd); //Utterly innecessary, all invokations to this function already have an SD non-null check.
  1457. //Learn to use proper coding and stop relying on nullpo_'s for safety :P [Skotlex]
  1458. if(!sd->state.waitingdisconnect) {
  1459. if (sd->npc_timer_id != -1) //Cancel the event timer.
  1460. npc_timerevent_quit(sd);
  1461. if (sd->state.event_disconnect)
  1462. npc_script_event(sd, NPCE_LOGOUT);
  1463. sd->state.waitingdisconnect = 1;
  1464. if (sd->pd) unit_free(&sd->pd->bl,0);
  1465. if (sd->hd) unit_free(&sd->hd->bl,0);
  1466. unit_free(&sd->bl,3);
  1467. chrif_save(sd,1);
  1468. } else { //Try to free some data, without saving anything (this could be invoked on map server change. [Skotlex]
  1469. if (sd->bl.prev != NULL)
  1470. { //Remove from map...
  1471. unit_remove_map(&sd->bl, 0);
  1472. if (sd->pd && sd->pd->bl.prev != NULL)
  1473. unit_remove_map(&sd->pd->bl, 0);
  1474. }
  1475. }
  1476. //Do we really need to remove the name?
  1477. idb_remove(charid_db,sd->status.char_id);
  1478. idb_remove(id_db,sd->bl.id);
  1479. if(sd->reg)
  1480. { //Double logout already freed pointer fix... [Skotlex]
  1481. aFree(sd->reg);
  1482. sd->reg = NULL;
  1483. sd->reg_num = 0;
  1484. }
  1485. if(sd->regstr)
  1486. {
  1487. aFree(sd->regstr);
  1488. sd->regstr = NULL;
  1489. sd->regstr_num = 0;
  1490. }
  1491. if (sd->st) {
  1492. if (sd->st->stack)
  1493. script_free_stack (sd->st->stack);
  1494. aFree(sd->st);
  1495. sd->st = NULL;
  1496. sd->npc_id = 0;
  1497. }
  1498. #ifndef TXT_ONLY
  1499. if(charsave_method)
  1500. { //Let player be free'd on closing the connection.
  1501. idb_remove(pc_db,sd->status.account_id);
  1502. if (!(sd->fd && session[sd->fd]->session_data))
  1503. aFree(sd); //In case player was not attached to session.
  1504. return 0;
  1505. }
  1506. #endif
  1507. if(sd->fd)
  1508. { //Player will be free'd on save-ack. [Skotlex]
  1509. if (session[sd->fd])
  1510. session[sd->fd]->session_data = NULL;
  1511. sd->fd = 0;
  1512. }
  1513. return 0;
  1514. }
  1515. void map_quit_ack(struct map_session_data *sd) {
  1516. if (sd && sd->state.finalsave) {
  1517. idb_remove(pc_db,sd->status.account_id);
  1518. aFree(sd);
  1519. }
  1520. }
  1521. static int do_reconnect_map_sub(DBKey key,void *data,va_list va) {
  1522. struct map_session_data *sd = (TBL_PC*)data;
  1523. if (sd->state.finalsave) {
  1524. sd->state.finalsave = 0;
  1525. chrif_save(sd, 1); //Resend to save!
  1526. return 1;
  1527. }
  1528. return 0;
  1529. }
  1530. void do_reconnect_map(void) {
  1531. pc_db->foreach(pc_db,do_reconnect_map_sub);
  1532. }
  1533. /*==========================================
  1534. * id番?のPCを探す。居なければNULL
  1535. *------------------------------------------
  1536. */
  1537. struct map_session_data * map_id2sd(int id) {
  1538. // Now using pc_db to handle all players, should be quicker than both previous methods at a small expense of more memory. [Skotlex]
  1539. if (id <= 0) return NULL;
  1540. return (struct map_session_data*)idb_get(pc_db,id);
  1541. }
  1542. /*==========================================
  1543. * char_id番?の名前を探す
  1544. *------------------------------------------
  1545. */
  1546. char * map_charid2nick(int id) {
  1547. struct charid2nick *p = (struct charid2nick*)idb_get(charid_db,id);
  1548. if(p==NULL)
  1549. return NULL;
  1550. return p->nick;
  1551. }
  1552. struct map_session_data * map_charid2sd(int id) {
  1553. int i, users;
  1554. struct map_session_data **all_sd;
  1555. if (id <= 0) return 0;
  1556. all_sd = map_getallusers(&users);
  1557. for(i = 0; i < users; i++)
  1558. if (all_sd[i] && all_sd[i]->status.char_id == id)
  1559. return all_sd[i];
  1560. return NULL;
  1561. }
  1562. /*==========================================
  1563. * Search session data from a nick name
  1564. * (without sensitive case if necessary)
  1565. * return map_session_data pointer or NULL
  1566. *------------------------------------------
  1567. */
  1568. struct map_session_data * map_nick2sd(char *nick) {
  1569. int i, users;
  1570. struct map_session_data *pl_sd = NULL, **pl_allsd;
  1571. if (nick == NULL)
  1572. return NULL;
  1573. pl_allsd = map_getallusers(&users);
  1574. if (battle_config.partial_name_scan)
  1575. {
  1576. int qty = 0, nicklen = strlen(nick);
  1577. struct map_session_data *sd = NULL;
  1578. for (i = 0; i < users; i++) {
  1579. pl_sd = pl_allsd[i];
  1580. // Without case sensitive check (increase the number of similar character names found)
  1581. if (strnicmp(pl_sd->status.name, nick, nicklen) == 0) {
  1582. // Strict comparison (if found, we finish the function immediatly with correct value)
  1583. if (strcmp(pl_sd->status.name, nick) == 0)
  1584. return pl_sd;
  1585. qty++;
  1586. sd = pl_sd;
  1587. }
  1588. }
  1589. // We return the found index of a similar account ONLY if there is 1 similar character
  1590. if (qty == 1)
  1591. return sd;
  1592. } else { //Exact Search
  1593. for (i = 0; i < users; i++) {
  1594. if (strcasecmp(pl_allsd[i]->status.name, nick) == 0)
  1595. return pl_allsd[i];
  1596. }
  1597. }
  1598. //Not found.
  1599. return NULL;
  1600. }
  1601. /*==========================================
  1602. * id番?の物を探す
  1603. * 一三bjectの場合は配列を引くのみ
  1604. *------------------------------------------
  1605. */
  1606. struct block_list * map_id2bl(int id)
  1607. {
  1608. struct block_list *bl=NULL;
  1609. if(id >= 0 && id < sizeof(objects)/sizeof(objects[0]))
  1610. bl = objects[id];
  1611. else
  1612. bl = idb_get(id_db,id);
  1613. return bl;
  1614. }
  1615. static int map_getallpc_sub(DBKey key,void * data,va_list ap)
  1616. {
  1617. struct map_session_data *sd = (struct map_session_data*) data;
  1618. if (!sd->state.auth || sd->state.waitingdisconnect || sd->state.finalsave)
  1619. return 1; //Do not count in not-yet authenticated characters or ready to disconnect ones.
  1620. return 0;
  1621. }
  1622. /*==========================================
  1623. * Returns an array of all players in the server (includes non connected ones) [Skotlex]
  1624. * The int pointer given returns the count of elements in the array.
  1625. * If null is passed, it is requested that the memory be freed (for shutdown), and null is returned.
  1626. *------------------------------------------
  1627. */
  1628. struct map_session_data** map_getallusers(int *users) {
  1629. static struct map_session_data **all_sd=NULL;
  1630. static unsigned int all_count = 0;
  1631. if (users == NULL)
  1632. { //Free up data
  1633. if (all_sd) aFree(all_sd);
  1634. all_sd = NULL;
  1635. return NULL;
  1636. }
  1637. if (all_sd == NULL)
  1638. { //Init data
  1639. all_count = pc_db->size(pc_db); //This is the real number of chars in the db, better use this than the actual "online" count.
  1640. if (all_count < 1)
  1641. all_count = 10; //Allow room for at least 10 chars.
  1642. all_sd = aCalloc(all_count, sizeof(struct map_session_data*)); //it's actually just the size of a pointer.
  1643. }
  1644. if (all_count < pc_db->size(pc_db))
  1645. {
  1646. all_count = pc_db->size(pc_db)+10; //Give some room to prevent doing reallocs often.
  1647. all_sd = aRealloc(all_sd, all_count*sizeof(struct map_session_data*));
  1648. }
  1649. *users = pc_db->getall(pc_db,(void**)all_sd,all_count,map_getallpc_sub);
  1650. if (*users > (signed int)all_count) //Which should be impossible...
  1651. *users = all_count;
  1652. return all_sd;
  1653. }
  1654. void map_foreachpc(int (*func)(DBKey,void*,va_list),...) {
  1655. va_list ap;
  1656. va_start(ap,func);
  1657. pc_db->vforeach(pc_db,func,ap);
  1658. va_end(ap);
  1659. }
  1660. /*==========================================
  1661. * id_db?の全てにfuncを?行
  1662. *------------------------------------------
  1663. */
  1664. int map_foreachiddb(int (*func)(DBKey,void*,va_list),...) {
  1665. va_list ap;
  1666. va_start(ap,func);
  1667. id_db->vforeach(id_db,func,ap);
  1668. va_end(ap);
  1669. return 0;
  1670. }
  1671. /*==========================================
  1672. * map.npcへ追加 (warp等の領域持ちのみ)
  1673. *------------------------------------------
  1674. */
  1675. int map_addnpc(int m,struct npc_data *nd) {
  1676. int i;
  1677. if(m<0 || m>=map_num)
  1678. return -1;
  1679. for(i=0;i<map[m].npc_num && i<MAX_NPC_PER_MAP;i++)
  1680. if(map[m].npc[i]==NULL)
  1681. break;
  1682. if(i==MAX_NPC_PER_MAP){
  1683. if(battle_config.error_log)
  1684. ShowWarning("too many NPCs in one map %s\n",map[m].name);
  1685. return -1;
  1686. }
  1687. if(i==map[m].npc_num){
  1688. map[m].npc_num++;
  1689. }
  1690. nullpo_retr(0, nd);
  1691. map[m].npc[i]=nd;
  1692. nd->n = i;
  1693. idb_put(id_db,nd->bl.id,nd);
  1694. return i;
  1695. }
  1696. void map_removenpc(void) {
  1697. int i,m,n=0;
  1698. for(m=0;m<map_num;m++) {
  1699. for(i=0;i<map[m].npc_num && i<MAX_NPC_PER_MAP;i++) {
  1700. if(map[m].npc[i]!=NULL) {
  1701. clif_clearchar_area(&map[m].npc[i]->bl,2);
  1702. map_delblock(&map[m].npc[i]->bl);
  1703. idb_remove(id_db,map[m].npc[i]->bl.id);
  1704. if(map[m].npc[i]->bl.subtype==SCRIPT) {
  1705. aFree(map[m].npc[i]->u.scr.script);
  1706. aFree(map[m].npc[i]->u.scr.label_list);
  1707. }
  1708. aFree(map[m].npc[i]);
  1709. map[m].npc[i] = NULL;
  1710. n++;
  1711. }
  1712. }
  1713. }
  1714. ShowStatus("Successfully removed and freed from memory '"CL_WHITE"%d"CL_RESET"' NPCs.\n",n);
  1715. }
  1716. /*=========================================
  1717. * Dynamic Mobs [Wizputer]
  1718. *-----------------------------------------
  1719. */
  1720. // allocates a struct when it there is place free in the cache,
  1721. // and returns NULL otherwise
  1722. // -- i'll just leave the old code in case it's needed ^^;
  1723. int map_addmobtolist(unsigned short m, struct spawn_data *spawn)
  1724. {
  1725. size_t i;
  1726. for (i = 0; i < MAX_MOB_LIST_PER_MAP; i++) {
  1727. if (map[m].moblist[i] == NULL) {
  1728. map[m].moblist[i] = spawn;
  1729. return i;
  1730. }
  1731. }
  1732. return -1;
  1733. }
  1734. void map_spawnmobs(int m)
  1735. {
  1736. int i, k=0;
  1737. if (map[m].mob_delete_timer != -1)
  1738. { //Mobs have not been removed yet [Skotlex]
  1739. delete_timer(map[m].mob_delete_timer, map_removemobs_timer);
  1740. map[m].mob_delete_timer = -1;
  1741. return;
  1742. }
  1743. for(i=0; i<MAX_MOB_LIST_PER_MAP; i++)
  1744. if(map[m].moblist[i]!=NULL)
  1745. {
  1746. k+=map[m].moblist[i]->num;
  1747. npc_parse_mob2(map[m].moblist[i],i);
  1748. }
  1749. if (battle_config.etc_log && k > 0)
  1750. {
  1751. ShowStatus("Map %s: Spawned '"CL_WHITE"%d"CL_RESET"' mobs.\n",map[m].name, k);
  1752. }
  1753. }
  1754. int mob_cache_cleanup_sub(struct block_list *bl, va_list ap) {
  1755. struct mob_data *md = (struct mob_data *)bl;
  1756. nullpo_retr(0, md);
  1757. //When not to remove:
  1758. //Mob has the cached flag on 0
  1759. if (!md->special_state.cached)
  1760. return 0;
  1761. if (!battle_config.mob_remove_damaged &&
  1762. md->status.hp < md->status.max_hp)
  1763. return 0; //Do not remove damaged mobs.
  1764. unit_free(&md->bl,0);
  1765. return 1;
  1766. }
  1767. int map_removemobs_timer(int tid, unsigned int tick, int id, int data)
  1768. {
  1769. int k;
  1770. if (id < 0 || id >= MAX_MAP_PER_SERVER)
  1771. { //Incorrect map id!
  1772. if (battle_config.error_log)
  1773. ShowError("map_removemobs_timer error: timer %d points to invalid map %d\n",tid, id);
  1774. return 0;
  1775. }
  1776. if (map[id].mob_delete_timer != tid)
  1777. { //Incorrect timer call!
  1778. if (battle_config.error_log)
  1779. ShowError("map_removemobs_timer mismatch: %d != %d (map %s)\n",map[id].mob_delete_timer, tid, map[id].name);
  1780. return 0;
  1781. }
  1782. map[id].mob_delete_timer = -1;
  1783. if (map[id].users > 0) //Map not empty!
  1784. return 1;
  1785. k = map_foreachinmap(mob_cache_cleanup_sub, id, BL_MOB);
  1786. if (battle_config.etc_log && k > 0)
  1787. ShowStatus("Map %s: Removed '"CL_WHITE"%d"CL_RESET"' mobs.\n",map[id].name, k);
  1788. return 1;
  1789. }
  1790. void map_removemobs(int m)
  1791. {
  1792. if (map[m].mob_delete_timer != -1)
  1793. return; //Mobs are already scheduled for removal
  1794. map[m].mob_delete_timer = add_timer(gettick()+battle_config.mob_remove_delay, map_removemobs_timer, m, 0);
  1795. }
  1796. /*==========================================
  1797. * map名からmap番?へ?換
  1798. *------------------------------------------
  1799. */
  1800. int map_mapname2mapid(char *name) {
  1801. unsigned short map_index;
  1802. map_index = mapindex_name2id(name);
  1803. if (!map_index)
  1804. return -1;
  1805. return map_mapindex2mapid(map_index);
  1806. }
  1807. /*==========================================
  1808. * Returns the map of the given mapindex. [Skotlex]
  1809. *------------------------------------------
  1810. */
  1811. int map_mapindex2mapid(unsigned short mapindex) {
  1812. struct map_data *md=NULL;
  1813. if (!mapindex)
  1814. return -1;
  1815. md = (struct map_data*)uidb_get(map_db,(unsigned int)mapindex);
  1816. if(md==NULL || md->gat==NULL)
  1817. return -1;
  1818. return md->m;
  1819. }
  1820. /*==========================================
  1821. * 他鯖map名からip,port?換
  1822. *------------------------------------------
  1823. */
  1824. int map_mapname2ipport(unsigned short name,int *ip,int *port) {
  1825. struct map_data_other_server *mdos=NULL;
  1826. mdos = (struct map_data_other_server*)uidb_get(map_db,(unsigned int)name);
  1827. if(mdos==NULL || mdos->gat) //If gat isn't null, this is a local map.
  1828. return -1;
  1829. *ip=mdos->ip;
  1830. *port=mdos->port;
  1831. return 0;
  1832. }
  1833. /*==========================================
  1834. * Checks if both dirs point in the same direction.
  1835. *------------------------------------------
  1836. */
  1837. int map_check_dir(int s_dir,int t_dir) {
  1838. if(s_dir == t_dir)
  1839. return 0;
  1840. switch(s_dir) {
  1841. case 0:
  1842. if(t_dir == 7 || t_dir == 1 || t_dir == 0)
  1843. return 0;
  1844. break;
  1845. case 1:
  1846. if(t_dir == 0 || t_dir == 2 || t_dir == 1)
  1847. return 0;
  1848. break;
  1849. case 2:
  1850. if(t_dir == 1 || t_dir == 3 || t_dir == 2)
  1851. return 0;
  1852. break;
  1853. case 3:
  1854. if(t_dir == 2 || t_dir == 4 || t_dir == 3)
  1855. return 0;
  1856. break;
  1857. case 4:
  1858. if(t_dir == 3 || t_dir == 5 || t_dir == 4)
  1859. return 0;
  1860. break;
  1861. case 5:
  1862. if(t_dir == 4 || t_dir == 6 || t_dir == 5)
  1863. return 0;
  1864. break;
  1865. case 6:
  1866. if(t_dir == 5 || t_dir == 7 || t_dir == 6)
  1867. return 0;
  1868. break;
  1869. case 7:
  1870. if(t_dir == 6 || t_dir == 0 || t_dir == 7)
  1871. return 0;
  1872. break;
  1873. }
  1874. return 1;
  1875. }
  1876. /*==========================================
  1877. * Returns the direction of the given cell in absolute relation to the char
  1878. * (regardless of where the char is facing)
  1879. *------------------------------------------
  1880. */
  1881. int map_calc_dir( struct block_list *src,int x,int y) {
  1882. int dir=0;
  1883. int dx,dy;
  1884. nullpo_retr(0, src);
  1885. dx=x-src->x;
  1886. dy=y-src->y;
  1887. if( dx==0 && dy==0 ){ // 彼我の場所一致
  1888. dir=0; // 上
  1889. }else if( dx>=0 && dy>=0 ){ // 方向的に右上
  1890. dir=7; // 右上
  1891. if( dx*2-1<dy ) dir=0; // 上
  1892. if( dx>dy*2 ) dir=6; // 右
  1893. }else if( dx>=0 && dy<=0 ){ // 方向的に右下
  1894. dir=5; // 右下
  1895. if( dx*2-1<-dy ) dir=4; // 下
  1896. if( dx>-dy*2 ) dir=6; // 右
  1897. }else if( dx<=0 && dy<=0 ){ // 方向的に左下
  1898. dir=3; // 左下
  1899. if( dx*2+1>dy ) dir=4; // 下
  1900. if( dx<dy*2 ) dir=2; // 左
  1901. }else{ // 方向的に左上
  1902. dir=1; // 左上
  1903. if( -dx*2-1<dy ) dir=0; // 上
  1904. if( -dx>dy*2 ) dir=2; // 左
  1905. }
  1906. return dir;
  1907. }
  1908. /*==========================================
  1909. * Randomizes target cell x,y to a random walkable cell that
  1910. * has the same distance from object as given coordinates do. [Skotlex]
  1911. *------------------------------------------
  1912. */
  1913. int map_random_dir(struct block_list *bl, short *x, short *y) {
  1914. struct walkpath_data wpd;
  1915. short xi = *x-bl->x;
  1916. short yi = *y-bl->y;
  1917. short i=0, j;
  1918. int dist2 = xi*xi + yi*yi;
  1919. short dist = (short)sqrt(dist2);
  1920. short segment;
  1921. if (dist < 1) dist =1;
  1922. do {
  1923. j = rand()%8; //Pick a random direction
  1924. segment = 1+(rand()%dist); //Pick a random interval from the whole vector in that direction
  1925. xi = bl->x + segment*dirx[j];
  1926. segment = (short)sqrt(dist2 - segment*segment); //The complement of the previously picked segment
  1927. yi = bl->y + segment*diry[j];
  1928. } while ((
  1929. map_getcell(bl->m,xi,yi,CELL_CHKNOPASS) ||
  1930. path_search_real(&wpd,bl->m,bl->x,bl->y,xi,yi,1,CELL_CHKNOREACH) == -1)
  1931. && (++i)<100);
  1932. if (i < 100) {
  1933. *x = xi;
  1934. *y = yi;
  1935. return 1;
  1936. }
  1937. return 0;
  1938. }
  1939. // gat系
  1940. /*==========================================
  1941. * (m,x,y)の状態を調べる
  1942. *------------------------------------------
  1943. */
  1944. int map_getcell(int m,int x,int y,cell_t cellchk)
  1945. {
  1946. return (m < 0 || m >= MAX_MAP_PER_SERVER) ? 0 : map_getcellp(&map[m],x,y,cellchk);
  1947. }
  1948. int map_getcellp(struct map_data* m,int x,int y,cell_t cellchk)
  1949. {
  1950. int type, type2;
  1951. #ifdef CELL_NOSTACK
  1952. int type3;
  1953. #endif
  1954. nullpo_ret(m);
  1955. if(x<0 || x>=m->xs-1 || y<0 || y>=m->ys-1)
  1956. {
  1957. if(cellchk==CELL_CHKNOPASS) return 1;
  1958. return 0;
  1959. }
  1960. type = m->gat[x+y*m->xs];
  1961. type2 = m->cell[x+y*m->xs];
  1962. #ifdef CELL_NOSTACK
  1963. type3 = m->cell_bl[x+y*m->xs];
  1964. #endif
  1965. switch(cellchk)
  1966. {
  1967. case CELL_CHKPASS:
  1968. #ifdef CELL_NOSTACK
  1969. if (type3 >= battle_config.cell_stack_limit) return 0;
  1970. #endif
  1971. case CELL_CHKREACH:
  1972. return (type!=1 && type!=5 && !(type2&CELL_ICEWALL));
  1973. case CELL_CHKNOPASS:
  1974. #ifdef CELL_NOSTACK
  1975. if (type3 >= battle_config.cell_stack_limit) return 1;
  1976. #endif
  1977. case CELL_CHKNOREACH:
  1978. return (type==1 || type==5 || type2&CELL_ICEWALL);
  1979. case CELL_CHKSTACK:
  1980. #ifdef CELL_NOSTACK
  1981. return (type3 >= battle_config.cell_stack_limit);
  1982. #else
  1983. return 0;
  1984. #endif
  1985. case CELL_CHKWALL:
  1986. return (type==1/* || type2&CELL_ICEWALL*/); //Uncomment to prevent sniping/casting through the icewall. [Skotlex]
  1987. case CELL_CHKWATER:
  1988. return (type==3);
  1989. case CELL_CHKGROUND:
  1990. return (type==5 || type2&CELL_ICEWALL);
  1991. case CELL_GETTYPE:
  1992. return type;
  1993. case CELL_GETCELLTYPE:
  1994. return type2;
  1995. case CELL_CHKNPC:
  1996. return (type2&CELL_NPC);
  1997. case CELL_CHKPNEUMA:
  1998. return (type2&CELL_PNEUMA);
  1999. case CELL_CHKSAFETYWALL:
  2000. return (type2&CELL_SAFETYWALL);
  2001. case CELL_CHKBASILICA:
  2002. return (type2&CELL_BASILICA);
  2003. case CELL_CHKLANDPROTECTOR:
  2004. return (type2&CELL_LANDPROTECTOR);
  2005. case CELL_CHKREGEN:
  2006. return (type2&CELL_REGEN);
  2007. case CELL_CHKICEWALL:
  2008. return (type2&CELL_ICEWALL);
  2009. default:
  2010. return 0;
  2011. }
  2012. }
  2013. /*==========================================
  2014. * (m,x,y)の状態を設定する
  2015. *------------------------------------------
  2016. */
  2017. void map_setcell(int m,int x,int y,int cell)
  2018. {
  2019. int j;
  2020. if(x<0 || x>=map[m].xs || y<0 || y>=map[m].ys)
  2021. return;
  2022. j=x+y*map[m].xs;
  2023. switch (cell) {
  2024. case CELL_SETNPC:
  2025. map[m].cell[j] |= CELL_NPC;
  2026. break;
  2027. case CELL_CLRNPC:
  2028. map[m].cell[j] &= ~CELL_NPC;
  2029. break;
  2030. case CELL_SETICEWALL:
  2031. map[m].cell[j] |= CELL_ICEWALL;
  2032. break;
  2033. case CELL_CLRICEWALL:
  2034. map[m].cell[j] &= ~CELL_ICEWALL;
  2035. break;
  2036. case CELL_SETBASILICA:
  2037. map[m].cell[j] |= CELL_BASILICA;
  2038. break;
  2039. case CELL_CLRBASILICA:
  2040. map[m].cell[j] &= ~CELL_BASILICA;
  2041. break;
  2042. case CELL_SETPNEUMA:
  2043. map[m].cell[j] |= CELL_PNEUMA;
  2044. break;
  2045. case CELL_CLRPNEUMA:
  2046. map[m].cell[j] &= ~CELL_PNEUMA;
  2047. break;
  2048. case CELL_SETSAFETYWALL:
  2049. map[m].cell[j] |= CELL_SAFETYWALL;
  2050. break;
  2051. case CELL_CLRSAFETYWALL:
  2052. map[m].cell[j] &= ~CELL_SAFETYWALL;
  2053. break;
  2054. case CELL_SETLANDPROTECTOR:
  2055. map[m].cell[j] |= CELL_LANDPROTECTOR;
  2056. break;
  2057. case CELL_CLRLANDPROTECTOR:
  2058. map[m].cell[j] &= ~CELL_LANDPROTECTOR;
  2059. break;
  2060. case CELL_SETREGEN:
  2061. map[m].cell[j] |= CELL_REGEN;
  2062. break;
  2063. default:
  2064. map[m].gat[j] = cell;
  2065. break;
  2066. }
  2067. }
  2068. static void* create_map_data_other_server(DBKey key, va_list args) {
  2069. struct map_data_other_server *mdos;
  2070. unsigned short mapindex = (unsigned short)key.ui;
  2071. mdos=(struct map_data_other_server *)aCalloc(1,sizeof(struct map_data_other_server));
  2072. mdos->index = mapindex;
  2073. memcpy(mdos->name, mapindex_id2name(mapindex), MAP_NAME_LENGTH);
  2074. return mdos;
  2075. }
  2076. /*==========================================
  2077. * 他鯖管理のマップをdbに追加
  2078. *------------------------------------------
  2079. */
  2080. int map_setipport(unsigned short mapindex,unsigned long ip,int port) {
  2081. struct map_data_other_server *mdos=NULL;
  2082. mdos=(struct map_data_other_server *)uidb_ensure(map_db,(unsigned int)mapindex, create_map_data_other_server);
  2083. if(mdos->gat) //Local map,Do nothing. Give priority to our own local maps over ones from another server. [Skotlex]
  2084. return 0;
  2085. if(ip == clif_getip_long() && port == clif_getport()) {
  2086. //That's odd, we received info that we are the ones with this map, but... we don't have it.
  2087. ShowFatalError("map_setipport : received info that this map-server SHOULD have map '%s', but it is not loaded.\n",mapindex_id2name(mapindex));
  2088. exit(1);
  2089. }
  2090. mdos->ip = ip;
  2091. mdos->port = port;
  2092. return 1;
  2093. }
  2094. /*==========================================
  2095. * 他鯖管理のマップを全て削除
  2096. *------------------------------------------
  2097. */
  2098. int map_eraseallipport_sub(DBKey key,void *data,va_list va) {
  2099. struct map_data_other_server *mdos = (struct map_data_other_server*)data;
  2100. if(mdos->gat == NULL) {
  2101. db_remove(map_db,key);
  2102. aFree(mdos);
  2103. }
  2104. return 0;
  2105. }
  2106. int map_eraseallipport(void) {
  2107. map_db->foreach(map_db,map_eraseallipport_sub);
  2108. return 1;
  2109. }
  2110. /*==========================================
  2111. * 他鯖管理のマップをdbから削除
  2112. *------------------------------------------
  2113. */
  2114. int map_eraseipport(unsigned short mapindex,unsigned long ip,int port)
  2115. {
  2116. struct map_data_other_server *mdos;
  2117. // unsigned char *p=(unsigned char *)&ip;
  2118. mdos = uidb_get(map_db,(unsigned int)mapindex);
  2119. if(!mdos || mdos->gat) //Map either does not exists or is a local map.
  2120. return 0;
  2121. if(mdos->ip==ip && mdos->port == port) {
  2122. uidb_remove(map_db,(unsigned int)mapindex);
  2123. aFree(mdos);
  2124. return 1;
  2125. }
  2126. return 0;
  2127. }
  2128. #define NO_WATER 1000000
  2129. static int map_setwaterheight_sub(int m) {
  2130. char fn[256];
  2131. char *gat;
  2132. int x,y;
  2133. int wh;
  2134. struct gat_1cell {float high[4]; int type;} *p = NULL;
  2135. if (m < 0)
  2136. return 0;
  2137. wh = map[m].water_height;
  2138. sprintf(fn,"data\\%s",mapindex_id2name(map[m].index));
  2139. // read & convert fn
  2140. // again, might not need to be unsigned char
  2141. gat = (char *) grfio_read (fn);
  2142. if (gat == NULL)
  2143. return 0;
  2144. for (y = 0; y < map[m].ys; y++) {
  2145. p = (struct gat_1cell*)(gat+y*map[m].xs*20+14);
  2146. for (x = 0; x < map[m].xs; x++) {
  2147. if (wh != NO_WATER && p->type == 0) //Set water cell
  2148. map[m].gat[x+y*map[m].xs] = (p->high[0]>wh || p->high[1]>wh || p->high[2]>wh || p->high[3]>wh) ? 3 : 0;
  2149. else //Remove water cell
  2150. map[m].gat[x+y*map[m].xs] = p->type==3?0:p->type;
  2151. p++;
  2152. }
  2153. }
  2154. aFree(gat);
  2155. return 1;
  2156. }
  2157. int map_setwaterheight(int m, char *mapname, int height) {
  2158. if (height < 0)
  2159. height = NO_WATER;
  2160. map[m].water_height = height;
  2161. return map_setwaterheight_sub(m);
  2162. }
  2163. /* map_readwaterheight
  2164. * Reads from the .rsw for each map
  2165. * Returns water height (or NO_WATER if file doesn't exist)
  2166. * or other error is encountered.
  2167. * This receives a map-name, and changes the extension to rsw if it isn't set already.
  2168. * Assumed path for file is data/mapname.rsw
  2169. * Credits to LittleWolf
  2170. */
  2171. int map_waterheight(char *mapname) {
  2172. char fn[256];
  2173. char *rsw, *found;
  2174. float whtemp;
  2175. int wh;
  2176. //Look up for the rsw
  2177. if(!strstr(mapname,"data\\"))
  2178. sprintf(fn,"data\\%s", mapname);
  2179. else
  2180. strcpy(fn, mapname);
  2181. found = grfio_find_file(fn);
  2182. if (!found)
  2183. ; //Stick to the current fn
  2184. else if(!strstr(found,"data\\"))
  2185. sprintf(fn,"data\\%s", found);
  2186. else
  2187. strcpy(fn, found);
  2188. rsw = strstr(fn, ".");
  2189. if (rsw && strstr(fn, ".rsw") == NULL)
  2190. sprintf(rsw,".rsw");
  2191. // read & convert fn
  2192. // again, might not need to be unsigned char
  2193. rsw = (char *) grfio_read (fn);
  2194. if (rsw)
  2195. { //Load water height from file
  2196. whtemp = *(float*)(rsw+166);
  2197. wh = (int) whtemp;
  2198. aFree(rsw);
  2199. return wh;
  2200. }
  2201. ShowWarning("Failed to find water level for (%s)\n", mapname, fn);
  2202. return NO_WATER;
  2203. }
  2204. /*==========================================
  2205. * マップキャッシュに追加する
  2206. *===========================================*/
  2207. // マップキャッシュの最大値
  2208. #define MAX_MAP_CACHE 768
  2209. //各マップごとの最小限情報を入れるもの、READ_FROM_BITMAP用
  2210. struct map_cache_info {
  2211. char fn[32];//ファイル名
  2212. int xs,ys; //幅と高さ
  2213. int water_height;
  2214. int pos; // データが入れてある場所
  2215. int compressed; // zilb通せるようにする為の予約
  2216. int compressed_len; // zilb通せるようにする為の予約
  2217. }; // 56 byte
  2218. struct map_cache_head {
  2219. int sizeof_header;
  2220. int sizeof_map;
  2221. // 上の2つ改変不可
  2222. int nmaps; // マップの個数
  2223. int filesize;
  2224. };
  2225. struct {
  2226. struct map_cache_head head;
  2227. struct map_cache_info *map;
  2228. FILE *fp;
  2229. int dirty;
  2230. } map_cache;
  2231. static int map_cache_open(char *fn);
  2232. static void map_cache_close(void);
  2233. static int map_cache_read(struct map_data *m);
  2234. static int map_cache_write(struct map_data *m);
  2235. static int map_cache_open(char *fn)
  2236. {
  2237. if (map_cache.fp)
  2238. map_cache_close();
  2239. map_cache.fp = fopen(fn, "r+b");
  2240. if (map_cache.fp) {
  2241. fread(&map_cache.head,1,sizeof(struct map_cache_head),map_cache.fp);
  2242. fseek(map_cache.fp,0,SEEK_END);
  2243. if(
  2244. map_cache.head.sizeof_header == sizeof(struct map_cache_head) &&
  2245. map_cache.head.sizeof_map == sizeof(struct map_cache_info) &&
  2246. map_cache.head.filesize == ftell(map_cache.fp)
  2247. ) {
  2248. // キャッシュ読み甲ン成功
  2249. map_cache.map = (struct map_cache_info *) aMalloc(sizeof(struct map_cache_info) * map_cache.head.nmaps);
  2250. fseek(map_cache.fp,sizeof(struct map_cache_head),SEEK_SET);
  2251. fread(map_cache.map,sizeof(struct map_cache_info),map_cache.head.nmaps,map_cache.fp);
  2252. return 1;
  2253. }
  2254. fclose(map_cache.fp);
  2255. }
  2256. // 読み甲ンに失敗したので新規に作成する
  2257. map_cache.fp = fopen(fn,"wb");
  2258. if(map_cache.fp) {
  2259. malloc_set(&map_cache.head,0,sizeof(struct map_cache_head));
  2260. map_cache.map = (struct map_cache_info *) aCalloc(sizeof(struct map_cache_info),MAX_MAP_CACHE);
  2261. map_cache.head.nmaps = MAX_MAP_CACHE;
  2262. map_cache.head.sizeof_header = sizeof(struct map_cache_head);
  2263. map_cache.head.sizeof_map = sizeof(struct map_cache_info);
  2264. map_cache.head.filesize = sizeof(struct map_cache_head);
  2265. map_cache.head.filesize += sizeof(struct map_cache_info) * map_cache.head.nmaps;
  2266. map_cache.dirty = 1;
  2267. return 1;
  2268. }
  2269. return 0;
  2270. }
  2271. static void map_cache_close(void)
  2272. {
  2273. if(!map_cache.fp) { return; }
  2274. if(map_cache.dirty) {
  2275. fseek(map_cache.fp,0,SEEK_SET);
  2276. fwrite(&map_cache.head,1,sizeof(struct map_cache_head),map_cache.fp);
  2277. fwrite(map_cache.map,map_cache.head.nmaps,sizeof(struct map_cache_info),map_cache.fp);
  2278. }
  2279. fclose(map_cache.fp);
  2280. aFree(map_cache.map);
  2281. map_cache.fp = NULL;
  2282. return;
  2283. }
  2284. int map_cache_read(struct map_data *m)
  2285. {
  2286. int i;
  2287. if(!map_cache.fp) { return 0; }
  2288. for(i = 0;i < map_cache.head.nmaps ; i++) {
  2289. if(!strcmp(m->name,map_cache.map[i].fn)) {
  2290. if(map_cache.map[i].compressed == 0) {
  2291. // 非圧縮ファイル
  2292. int size = map_cache.map[i].xs * map_cache.map[i].ys;
  2293. m->xs = map_cache.map[i].xs;
  2294. m->ys = map_cache.map[i].ys;
  2295. m->water_height = map_cache.map[i].water_height;
  2296. m->gat = (unsigned char *)aCalloc(m->xs * m->ys,sizeof(unsigned char));
  2297. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  2298. if(fread(m->gat,1,size,map_cache.fp) == size) {
  2299. // 成功
  2300. return 1;
  2301. } else {
  2302. // なぜかファイル後半が欠けてるので読み直し
  2303. m->xs = 0; m->ys = 0; aFree(m->gat); m->gat = NULL;
  2304. return 0;
  2305. }
  2306. } else if(map_cache.map[i].compressed == 1) {
  2307. // 圧縮フラグ=1 : zlib
  2308. unsigned char *buf;
  2309. unsigned long dest_len;
  2310. int size_compress = map_cache.map[i].compressed_len;
  2311. m->xs = map_cache.map[i].xs;
  2312. m->ys = map_cache.map[i].ys;
  2313. m->water_height = map_cache.map[i].water_height;
  2314. m->gat = (unsigned char *)aMalloc(m->xs * m->ys * sizeof(unsigned char));
  2315. buf = (unsigned char*)aMalloc(size_compress);
  2316. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  2317. if(fread(buf,1,size_compress,map_cache.fp) != size_compress) {
  2318. // なぜかファイル後半が欠けてるので読み直し
  2319. ShowError("fread error\n");
  2320. aFree(m->gat); m->xs = 0; m->ys = 0; m->gat = NULL;
  2321. aFree(buf);
  2322. return 0;
  2323. }
  2324. dest_len = m->xs * m->ys;
  2325. decode_zip(m->gat,&dest_len,buf,size_compress);
  2326. if(dest_len != map_cache.map[i].xs * map_cache.map[i].ys) {
  2327. // 正常に解凍が出来てない
  2328. aFree(m->gat); m->xs = 0; m->ys = 0; m->gat = NULL;
  2329. aFree(buf);
  2330. return 0;
  2331. }
  2332. aFree(buf);
  2333. return 1;
  2334. }
  2335. }
  2336. }
  2337. return 0;
  2338. }
  2339. static int map_cache_write(struct map_data *m)
  2340. {
  2341. int i;
  2342. unsigned long len_new , len_old;
  2343. char *write_buf;
  2344. if(!map_cache.fp) { return 0; }
  2345. for(i = 0;i < map_cache.head.nmaps ; i++) {
  2346. if(!strcmp(m->name,map_cache.map[i].fn)) {
  2347. // 同じエントリーがあれば上書き
  2348. if(map_cache.map[i].compressed == 0) {
  2349. len_old = map_cache.map[i].xs * map_cache.map[i].ys;
  2350. } else if(map_cache.map[i].compressed == 1) {
  2351. len_old = map_cache.map[i].compressed_len;
  2352. } else {
  2353. // サポートされてない形式なので長さ0
  2354. len_old = 0;
  2355. }
  2356. if(map_read_flag == 2) {
  2357. // 圧縮保存
  2358. // さすがに2倍に膨れる事はないという事で
  2359. write_buf = (char *) aMalloc(m->xs * m->ys * 2);
  2360. len_new = m->xs * m->ys * 2;
  2361. encode_zip((unsigned char *) write_buf,&len_new,m->gat,m->xs * m->ys);
  2362. map_cache.map[i].compressed = 1;
  2363. map_cache.map[i].compressed_len = len_new;
  2364. } else {
  2365. len_new = m->xs * m->ys;
  2366. write_buf = (char *) m->gat;
  2367. map_cache.map[i].compressed = 0;
  2368. map_cache.map[i].compressed_len = 0;
  2369. }
  2370. if(len_new <= len_old) {
  2371. // サイズが同じか小さくなったので場所は変わらない
  2372. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  2373. fwrite(write_buf,1,len_new,map_cache.fp);
  2374. } else {
  2375. // 新しい場所に登録
  2376. fseek(map_cache.fp,map_cache.head.filesize,SEEK_SET);
  2377. fwrite(write_buf,1,len_new,map_cache.fp);
  2378. map_cache.map[i].pos = map_cache.head.filesize;
  2379. map_cache.head.filesize += len_new;
  2380. }
  2381. map_cache.map[i].xs = m->xs;
  2382. map_cache.map[i].ys = m->ys;
  2383. map_cache.map[i].water_height = m->water_height;
  2384. map_cache.dirty = 1;
  2385. if(map_read_flag == 2) {
  2386. aFree(write_buf);
  2387. }
  2388. return 0;
  2389. }
  2390. }
  2391. // 同じエントリが無ければ書き甲゚る場所を探す
  2392. for(i = 0;i < map_cache.head.nmaps ; i++) {
  2393. if(map_cache.map[i].fn[0] == 0) {
  2394. // 新しい場所に登録
  2395. if(map_read_flag == 2) {
  2396. write_buf = (char *) aMalloc(m->xs * m->ys * 2);
  2397. len_new = m->xs * m->ys * 2;
  2398. encode_zip((unsigned char *) write_buf,&len_new,m->gat,m->xs * m->ys);
  2399. map_cache.map[i].compressed = 1;
  2400. map_cache.map[i].compressed_len = len_new;
  2401. } else {
  2402. len_new = m->xs * m->ys;
  2403. write_buf = (char *) m->gat;
  2404. map_cache.map[i].compressed = 0;
  2405. map_cache.map[i].compressed_len = 0;
  2406. }
  2407. strncpy(map_cache.map[i].fn,m->name,sizeof(map_cache.map[0].fn));
  2408. fseek(map_cache.fp,map_cache.head.filesize,SEEK_SET);
  2409. fwrite(write_buf,1,len_new,map_cache.fp);
  2410. map_cache.map[i].pos = map_cache.head.filesize;
  2411. map_cache.map[i].xs = m->xs;
  2412. map_cache.map[i].ys = m->ys;
  2413. map_cache.map[i].water_height = m->water_height;
  2414. map_cache.head.filesize += len_new;
  2415. map_cache.dirty = 1;
  2416. if(map_read_flag == 2)
  2417. aFree(write_buf);
  2418. return 0;
  2419. }
  2420. }
  2421. // 書き甲゚なかった
  2422. return 1;
  2423. }
  2424. /*==========================================
  2425. * ?み?むmapを追加する
  2426. *------------------------------------------
  2427. */
  2428. int map_addmap(char *mapname) {
  2429. if (strcmpi(mapname,"clear")==0) {
  2430. map_num=0;
  2431. return 0;
  2432. }
  2433. if (map_num >= MAX_MAP_PER_SERVER - 1) {
  2434. ShowError("Could not add map '"
  2435. CL_WHITE"%s"CL_RESET"', the limit of maps has been reached.\n",mapname);
  2436. return 1;
  2437. }
  2438. memcpy(map[map_num].name, mapname, MAP_NAME_LENGTH-1);
  2439. map_num++;
  2440. return 0;
  2441. }
  2442. /*==========================================
  2443. * Removes the map in the index passed.
  2444. *------------------------------------------
  2445. */
  2446. static void map_delmapid(int id)
  2447. {
  2448. ShowNotice("Removing map [ %s ] from maplist\n",map[id].name);
  2449. memmove(map+id, map+id+1, sizeof(map[0])*(map_num-id-1));
  2450. map_num--;
  2451. }
  2452. /*==========================================
  2453. * ?み?むmapを削除する
  2454. *------------------------------------------
  2455. */
  2456. int map_delmap(char *mapname) {
  2457. int i;
  2458. if (strcmpi(mapname, "all") == 0) {
  2459. map_num = 0;
  2460. return 0;
  2461. }
  2462. for(i = 0; i < map_num; i++) {
  2463. if (strcmp(map[i].name, mapname) == 0) {
  2464. map_delmapid(i);
  2465. return 1;
  2466. }
  2467. }
  2468. return 0;
  2469. }
  2470. ////////////////////////////////////////////////
  2471. /*
  2472. Advanced Fusion Maps Support
  2473. (c) 2003-2004, The Fusion Project
  2474. - AlexKreuz
  2475. The following code has been provided by me for eAthena
  2476. under the GNU GPL. It provides Advanced Fusion
  2477. Map, the map format desgined by me for Fusion, support
  2478. for the eAthena emulator.
  2479. I understand that because it is under the GPL
  2480. that other emulators may very well use this code in their
  2481. GNU project as well.
  2482. The AFM map format was not originally a part of the GNU
  2483. GPL. It originated from scratch by my own hand. I understand
  2484. that distributing this code to read the AFM maps with eAthena
  2485. causes the GPL to apply to this code. But the actual AFM
  2486. maps are STILL copyrighted to the Fusion Project. By choosing
  2487. In exchange for that 'act of faith' I ask for the following.
  2488. A) Give credit where it is due. If you use this code, do not
  2489. place your name on the changelog. Credit should be given
  2490. to AlexKreuz.
  2491. B) As an act of courtesy, ask me and let me know that you are putting
  2492. AFM support in your project. You will have my blessings if you do.
  2493. C) Use the code in its entirety INCLUDING the copyright message.
  2494. Although the code provided may now be GPL, the AFM maps are not
  2495. and so I ask you to display the copyright message on the STARTUP
  2496. SCREEN as I have done here. (refer to core.c)
  2497. "Advanced Fusion Maps (c) 2003-2004 The Fusion Project"
  2498. Without this copyright, you are NOT entitled to bundle or distribute
  2499. the AFM maps at all. On top of that, your "support" for AFM maps
  2500. becomes just as shady as your "support" for Gravity GRF files.
  2501. The bottom line is this. I know that there are those of you who
  2502. would like to use this code but aren't going to want to provide the
  2503. proper credit. I know this because I speak frome experience. If
  2504. you are one of those people who is going to try to get around my
  2505. requests, then save your breath because I don't want to hear it.
  2506. I have zero faith in GPL and I know and accept that if you choose to
  2507. not display the copyright for the AFMs then there is absolutely nothing
  2508. I can do about it. I am not about to start a legal battle over something
  2509. this silly.
  2510. Provide the proper credit because you believe in the GPL. If you choose
  2511. not to and would rather argue about it, consider the GPL failed.
  2512. October 18th, 2004
  2513. - AlexKreuz
  2514. - The Fusion Project
  2515. */
  2516. static int map_loadafm (struct map_data *m, char *fn)
  2517. {
  2518. // check if .afm file exists
  2519. FILE *afm_file = fopen(fn, "r");
  2520. if (afm_file != NULL) {
  2521. int x,y,xs,ys;
  2522. char afm_line[65535];
  2523. int afm_size[2];
  2524. char *str;
  2525. //Gotta skip the first two lines which are just a header of sorts.
  2526. str = fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2527. str = fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2528. str = fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2529. if (!str) return 0;
  2530. sscanf(str , "%d%d", &afm_size[0], &afm_size[1]);
  2531. xs = m->xs = afm_size[0];
  2532. ys = m->ys = afm_size[1];
  2533. m->water_height = map_waterheight(m->name);
  2534. // check this, unsigned where it might not need to be
  2535. m->gat = (unsigned char*)aMallocA(xs * ys);
  2536. for (y = 0; y < ys; y++) {
  2537. str = fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2538. for (x = 0; x < xs; x++)
  2539. m->gat[x+y*xs] = str[x]-48;
  2540. }
  2541. fclose(afm_file);
  2542. return 1;
  2543. }
  2544. return 0;
  2545. }
  2546. /*==================================
  2547. * .AFM format
  2548. *----------------------------------
  2549. */
  2550. int map_readafm (struct map_data *m)
  2551. {
  2552. char afm_name[256] = "";
  2553. char fn[256], *p;
  2554. // convert map name to .afm
  2555. if(!strstr(m->name, ".afm")) {
  2556. // check if it's necessary to replace the extension - speeds up loading a bit
  2557. strncpy(afm_name, m->name, strlen(m->name) - 4);
  2558. strcat(afm_name, ".afm");
  2559. } else {
  2560. strcpy(afm_name, m->name);
  2561. }
  2562. sprintf(fn, "%s\\%s", afm_dir, afm_name);
  2563. for (p = &fn[0]; *p != 0; p++)
  2564. if (*p == '\\') *p = '/'; // * At the time of Unix
  2565. return map_loadafm(m, fn);
  2566. }
  2567. /*==================================
  2568. * .AF2 format
  2569. *----------------------------------
  2570. */
  2571. int map_readaf2 (struct map_data *m)
  2572. {
  2573. FILE *af2_file;
  2574. char af2_name[256] = "";
  2575. char fn[256], *p, *out;
  2576. // convert map name to .af2
  2577. p = out = m->name;
  2578. while ((p = strchr(p, '/')) != NULL)
  2579. out = ++p;
  2580. strncpy (af2_name, out, strlen(out));
  2581. // grr, this is so troublesome >.< [celest]
  2582. p = strrchr (af2_name, '.');
  2583. if (p) *p++ = 0;
  2584. strcat(af2_name, ".af2");
  2585. sprintf(fn, "%s\\%s", afm_dir, af2_name);
  2586. for (p = &fn[0]; *p != 0; p++)
  2587. if (*p == '\\') *p = '/'; // * At the time of Unix
  2588. // check if .af2 file exists
  2589. af2_file = fopen(fn, "r");
  2590. if (af2_file != NULL) {
  2591. char out_file[256];
  2592. fclose(af2_file);
  2593. // convert map name to .out
  2594. strncpy (out_file, out, strlen(out));
  2595. p = strrchr (out_file, '.');
  2596. if (p) *p++ = 0;
  2597. strcat(out_file, ".out");
  2598. // unzip .out file and use loadafm()
  2599. if (deflate_file(fn, out_file) &&
  2600. map_loadafm(m, out_file))
  2601. {
  2602. unlink (out_file);
  2603. return 1;
  2604. }
  2605. }
  2606. return 0;
  2607. }
  2608. /*==========================================
  2609. * マップ1枚読み甲ン
  2610. * ===================================================*/
  2611. //static int map_readmap(int m,char *fn, char *alias, int *map_cache, int maxmap) {
  2612. /*==================================
  2613. * .GAT format
  2614. *----------------------------------
  2615. */
  2616. int map_readgat (struct map_data *m)
  2617. {
  2618. char fn[256];
  2619. char *gat;
  2620. int wh,x,y,xs,ys;
  2621. struct gat_1cell {float high[4]; int type;} *p = NULL;
  2622. if (strstr(m->name,".gat") == NULL)
  2623. return 0;
  2624. sprintf(fn,"data\\%s",m->name);
  2625. // read & convert fn
  2626. // again, might not need to be unsigned char
  2627. gat = (char *) grfio_read (fn);
  2628. if (gat == NULL)
  2629. return 0;
  2630. xs = m->xs = *(int*)(gat+6);
  2631. ys = m->ys = *(int*)(gat+10);
  2632. m->gat = (unsigned char *)aMallocA((m->xs * m->ys)*sizeof(unsigned char));
  2633. m->water_height = wh = map_waterheight(m->name);
  2634. for (y = 0; y < ys; y++) {
  2635. p = (struct gat_1cell*)(gat+y*xs*20+14);
  2636. for (x = 0; x < xs; x++) {
  2637. if (wh != NO_WATER && p->type == 0)
  2638. // 水場判定
  2639. m->gat[x+y*xs] = (p->high[0]>wh || p->high[1]>wh || p->high[2]>wh || p->high[3]>wh) ? 3 : 0;
  2640. else
  2641. m->gat[x+y*xs] = p->type;
  2642. p++;
  2643. }
  2644. }
  2645. aFree(gat);
  2646. return 1;
  2647. }
  2648. //////////////////////////////////////////////////////
  2649. static int map_cache_init (void);
  2650. static int map_readafm_init (void);
  2651. static int map_readaf2_init (void);
  2652. static int map_readgat_init (void);
  2653. // Todo: Properly implement this system as plugins/safer code [Celest]
  2654. enum {
  2655. MAP_CACHE = 0, // jAthena map cache
  2656. MAP_AFM, // Advanced Fusion Map
  2657. MAP_AF2, // Advanced Fusion Map
  2658. MAP_GAT, // GRF map
  2659. MAP_MAXSOURCE
  2660. };
  2661. // in descending order
  2662. int (*mapsource_init[MAP_MAXSOURCE])(void) = {
  2663. map_cache_init,
  2664. map_readafm_init,
  2665. map_readaf2_init,
  2666. map_readgat_init
  2667. };
  2668. int (*mapsource_read[MAP_MAXSOURCE])(struct map_data *) = {
  2669. map_cache_read,
  2670. map_readafm,
  2671. map_readaf2,
  2672. map_readgat
  2673. };
  2674. void (*mapsource_final[MAP_MAXSOURCE])(void) = {
  2675. map_cache_close,
  2676. NULL,
  2677. NULL,
  2678. NULL
  2679. };
  2680. static int map_cache_init (void)
  2681. {
  2682. if (map_read_flag >= READ_FROM_BITMAP && map_cache_open(map_cache_file)) {
  2683. ShowMessage("[cache] ");
  2684. return 1;
  2685. }
  2686. return 0;
  2687. }
  2688. static int map_readafm_init (void)
  2689. {
  2690. ShowMessage("[afm] ");
  2691. return 1;
  2692. }
  2693. static int map_readaf2_init (void)
  2694. {
  2695. // check if AFM loading is available,
  2696. // otherwise disable AF2 loading
  2697. if (mapsource_read[1] != NULL) {
  2698. ShowMessage("[af2] ");
  2699. return 1;
  2700. }
  2701. return 0;
  2702. }
  2703. static int map_readgat_init (void)
  2704. {
  2705. ShowMessage("[gat] ");
  2706. return 1;
  2707. }
  2708. /*======================================
  2709. * Initiate maps loading stage
  2710. *--------------------------------------
  2711. */
  2712. int map_readallmaps (void)
  2713. {
  2714. // pre-loading stage
  2715. int i;
  2716. int maps_removed = 0;
  2717. int maps_cached = 0;
  2718. ShowMessage(CL_GREEN"[Status]"CL_RESET": Loading Maps with... "CL_WHITE);
  2719. for (i = 0; i < MAP_MAXSOURCE; i++) {
  2720. if (mapsource_init[i] && // check if source requires initialisation
  2721. mapsource_init[i]() == 0) // if init failed
  2722. {
  2723. // remove all loading methods associated with this source
  2724. mapsource_init[i] = NULL;
  2725. mapsource_read[i] = NULL;
  2726. mapsource_final[i] = NULL;
  2727. }
  2728. }
  2729. ShowMessage(CL_RESET"\n");
  2730. // initiate map loading
  2731. for (i = 0; i < map_num; i++)
  2732. {
  2733. int success = 0;
  2734. static int lasti = -1;
  2735. static int last_time = -1;
  2736. int j = i*20/map_num;
  2737. // show progress
  2738. if (map_num && //avoid map-server crashing if there are 0 maps
  2739. (j != lasti || last_time != time(0)))
  2740. {
  2741. char progress[21] = " ";
  2742. char c = '-';
  2743. int k;
  2744. lasti = j;
  2745. printf("\r");
  2746. ShowStatus("Progress: [");
  2747. for (k=0; k < j; k++) progress[k] = '#';
  2748. printf(progress);
  2749. last_time = (int)time(0);
  2750. switch(last_time % 4) {
  2751. case 0: c='\\'; break;
  2752. case 1: c='|'; break;
  2753. case 2: c='/'; break;
  2754. case 3: c='-'; break;
  2755. }
  2756. printf("] Working: [%c]",c);
  2757. fflush(stdout);
  2758. }
  2759. // pre-init some data
  2760. map[i].alias = NULL;
  2761. map[i].m = i;
  2762. malloc_set (map[i].moblist, 0, sizeof(map[i].moblist)); //Initialize moblist [Skotlex]
  2763. map[i].mob_delete_timer = -1; //Initialize timer [Skotlex]
  2764. if (battle_config.pk_mode)
  2765. map[i].flag.pvp = 1; // make all maps pvp for pk_mode [Valaris]
  2766. for (j = 0; j < MAP_MAXSOURCE; j++)
  2767. {
  2768. if (mapsource_read[j] && // check if map source is valid
  2769. mapsource_read[j](&map[i])) // check if map source is available
  2770. {
  2771. // successful, now initialise map
  2772. size_t size;
  2773. char *alias;
  2774. if (map[i].alias && (alias = strstr(map[i].name, "<")) != NULL) { // alias has been set by one of the sources
  2775. *alias++ = '\0';
  2776. }
  2777. if (map[i].alias)
  2778. map[i].index = mapindex_name2id(map[i].alias);
  2779. else
  2780. map[i].index = mapindex_name2id(map[i].name);
  2781. if (!map[i].index) {
  2782. if (map[i].alias)
  2783. ShowWarning("Map %s (alias %s) is not in the map-index cache!\n", map[i].name, map[i].alias);
  2784. else
  2785. ShowWarning("Map %s is not in the map-index cache!\n", map[i].name);
  2786. success = 0; //Can't load a map that isn't in our cache.
  2787. if (map[i].gat) {
  2788. aFree(map[i].gat);
  2789. map[i].gat = NULL;
  2790. }
  2791. break;
  2792. }
  2793. if (uidb_get(map_db,(unsigned int)map[i].index) != NULL) {
  2794. ShowWarning("Map %s already loaded!\n", map[i].name);
  2795. success = 0; //Can't load a map already in the db
  2796. if (map[i].gat) {
  2797. aFree(map[i].gat);
  2798. map[i].gat = NULL;
  2799. }
  2800. break;
  2801. }
  2802. map[i].cell = (unsigned char *)aCalloc(map[i].xs * map[i].ys, sizeof(unsigned char));
  2803. #ifdef CELL_NOSTACK
  2804. map[i].cell_bl = (unsigned char *)aCalloc(map[i].xs * map[i].ys, sizeof(unsigned char));
  2805. #endif
  2806. map[i].bxs = (map[i].xs + BLOCK_SIZE - 1) / BLOCK_SIZE;
  2807. map[i].bys = (map[i].ys + BLOCK_SIZE - 1) / BLOCK_SIZE;
  2808. // default experience multiplicator
  2809. map[i].jexp = 100;
  2810. map[i].bexp = 100;
  2811. size = map[i].bxs * map[i].bys * sizeof(struct block_list*);
  2812. map[i].block = (struct block_list**)aCalloc(size, 1);
  2813. map[i].block_mob = (struct block_list**)aCalloc(size, 1);
  2814. size = map[i].bxs * map[i].bys * sizeof(int);
  2815. map[i].block_count = (int*)aCallocA(size, 1);
  2816. map[i].block_mob_count = (int*)aCallocA(size, 1);
  2817. uidb_put(map_db, (unsigned int)map[i].index, &map[i]);
  2818. // cache our map if necessary
  2819. if (j != MAP_CACHE && mapsource_read[MAP_CACHE] != NULL) { // map data is not cached yet
  2820. map_cache_write(&map[i]);
  2821. maps_cached++;
  2822. }
  2823. // next map
  2824. success = 1;
  2825. break;
  2826. }
  2827. }
  2828. // no sources have been found, so remove map from list
  2829. if (!success) {
  2830. map_delmapid(i);
  2831. maps_removed++;
  2832. i--;
  2833. }
  2834. }
  2835. // unload map sources
  2836. for (i = 0; i < MAP_MAXSOURCE; i++) {
  2837. if (mapsource_final[i])
  2838. mapsource_final[i]();
  2839. }
  2840. // finished map loading
  2841. printf("\r");
  2842. ShowInfo("Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps.%30s\n",map_num,"");
  2843. if (maps_removed)
  2844. ShowNotice("Maps Removed: '"CL_WHITE"%d"CL_RESET"'\n",maps_removed);
  2845. if (maps_cached)
  2846. ShowNotice("Maps Added to Cache: '"CL_WHITE"%d"CL_RESET"'\n",maps_cached);
  2847. return 0;
  2848. }
  2849. ////////////////////////////////////////////////////////////////////////
  2850. static int map_ip_set = 0;
  2851. static int char_ip_set = 0;
  2852. /*==========================================
  2853. * Console Command Parser [Wizputer]
  2854. *------------------------------------------
  2855. */
  2856. int parse_console(char *buf) {
  2857. char type[64],command[64],map[64], buf2[72];
  2858. int x = 0, y = 0;
  2859. int m, n;
  2860. struct map_session_data sd;
  2861. malloc_set(&sd, 0, sizeof(struct map_session_data));
  2862. strcpy( sd.status.name , "console");
  2863. if ( ( n = sscanf(buf, "%[^:]:%[^:]:%99s %d %d[^\n]", type , command , map , &x , &y )) < 5 )
  2864. if ( ( n = sscanf(buf, "%[^:]:%[^\n]", type , command )) < 2 )
  2865. n = sscanf(buf,"%[^\n]",type);
  2866. if ( n == 5 ) {
  2867. m = map_mapname2mapid(map);
  2868. if ( m < 0 ) {
  2869. ShowWarning("Console: Unknown map\n");
  2870. return 0;
  2871. }
  2872. sd.bl.m = m;
  2873. map_search_freecell(&sd.bl, m, &sd.bl.x, &sd.bl.y, -1, -1, 0);
  2874. if (x > 0)
  2875. sd.bl.x = x;
  2876. if (y > 0)
  2877. sd.bl.y = y;
  2878. }
  2879. ShowInfo("Type of command: %s || Command: %s || Map: %s Coords: %d %d\n",type,command,map,x,y);
  2880. if ( strcmpi("admin",type) == 0 && n == 5 ) {
  2881. sprintf(buf2,"console: %s",command);
  2882. if( is_atcommand(sd.fd,&sd,buf2,99) == AtCommand_None )
  2883. printf("Console: not atcommand\n");
  2884. } else if ( strcmpi("server",type) == 0 && n == 2 ) {
  2885. if ( strcmpi("shutdown", command) == 0 || strcmpi("exit",command) == 0 || strcmpi("quit",command) == 0 ) {
  2886. runflag = 0;
  2887. }
  2888. } else if ( strcmpi("help",type) == 0 ) {
  2889. ShowNotice("To use GM commands:\n");
  2890. printf("admin:<gm command>:<map of \"gm\"> <x> <y>\n");
  2891. printf("You can use any GM command that doesn't require the GM.\n");
  2892. printf("No using @item or @warp however you can use @charwarp\n");
  2893. printf("The <map of \"gm\"> <x> <y> is for commands that need coords of the GM\n");
  2894. printf("IE: @spawn\n");
  2895. printf("To shutdown the server:\n");
  2896. printf("server:shutdown\n");
  2897. }
  2898. return 0;
  2899. }
  2900. //-------------------------------------------------
  2901. // Return numerical value of a switch configuration
  2902. // on/off, english, fran軋is, deutsch, espa�l
  2903. //-------------------------------------------------
  2904. int config_switch(const char *str) {
  2905. if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0)
  2906. return 1;
  2907. if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0)
  2908. return 0;
  2909. return atoi(str);
  2910. }
  2911. /*==========================================
  2912. * 設定ファイルを?み?む
  2913. *------------------------------------------
  2914. */
  2915. int map_config_read(char *cfgName) {
  2916. char line[1024], w1[1024], w2[1024], *ptr;
  2917. FILE *fp;
  2918. fp = fopen(cfgName,"r");
  2919. if (fp == NULL) {
  2920. ShowFatalError("Map configuration file not found at: %s\n", cfgName);
  2921. exit(1);
  2922. }
  2923. while(fgets(line, sizeof(line) -1, fp)) {
  2924. if (line[0] == '/' && line[1] == '/')
  2925. continue;
  2926. if ((ptr = strstr(line, "//")) != NULL)
  2927. *ptr = '\n'; //Strip comments
  2928. if (sscanf(line, "%[^:]: %[^\t\r\n]", w1, w2) == 2) {
  2929. //Strip trailing spaces
  2930. ptr = w2 + strlen(w2);
  2931. while (--ptr >= w2 && *ptr == ' ');
  2932. ptr++;
  2933. *ptr = '\0';
  2934. if(strcmpi(w1,"timestamp_format")==0){
  2935. strncpy(timestamp_format, w2, 20);
  2936. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  2937. stdout_with_ansisequence = config_switch(w2);
  2938. } else if(strcmpi(w1,"console_silent")==0){
  2939. msg_silent = 0; //To always allow the next line to show up.
  2940. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2941. msg_silent = atoi(w2);
  2942. } else if (strcmpi(w1, "userid")==0){
  2943. chrif_setuserid(w2);
  2944. } else if (strcmpi(w1, "passwd") == 0) {
  2945. chrif_setpasswd(w2);
  2946. } else if (strcmpi(w1, "char_ip") == 0) {
  2947. char_ip_set = chrif_setip(w2);
  2948. } else if (strcmpi(w1, "char_port") == 0) {
  2949. chrif_setport(atoi(w2));
  2950. } else if (strcmpi(w1, "map_ip") == 0) {
  2951. map_ip_set = clif_setip(w2);
  2952. } else if (strcmpi(w1, "bind_ip") == 0) {
  2953. clif_setbindip(w2);
  2954. } else if (strcmpi(w1, "map_port") == 0) {
  2955. clif_setport(atoi(w2));
  2956. map_port = (atoi(w2));
  2957. } else if (strcmpi(w1, "map") == 0) {
  2958. map_addmap(w2);
  2959. } else if (strcmpi(w1, "delmap") == 0) {
  2960. map_delmap(w2);
  2961. } else if (strcmpi(w1, "npc") == 0) {
  2962. npc_addsrcfile(w2);
  2963. } else if (strcmpi(w1, "delnpc") == 0) {
  2964. npc_delsrcfile(w2);
  2965. } else if (strcmpi(w1, "autosave_time") == 0) {
  2966. autosave_interval = atoi(w2);
  2967. if (autosave_interval < 1) //Revert to default saving.
  2968. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2969. else
  2970. autosave_interval *= 1000; //Pass from sec to ms
  2971. } else if (strcmpi(w1, "minsave_time") == 0) {
  2972. minsave_interval= atoi(w2);
  2973. if (minsave_interval < 1)
  2974. minsave_interval = 1;
  2975. } else if (strcmpi(w1, "save_settings") == 0) {
  2976. save_settings = atoi(w2);
  2977. } else if (strcmpi(w1, "motd_txt") == 0) {
  2978. strcpy(motd_txt, w2);
  2979. } else if (strcmpi(w1, "help_txt") == 0) {
  2980. strcpy(help_txt, w2);
  2981. } else if (strcmpi(w1, "help2_txt") == 0) {
  2982. strcpy(help2_txt, w2);
  2983. } else if (strcmpi(w1, "charhelp_txt") == 0) {
  2984. strcpy(charhelp_txt, w2);
  2985. } else if (strcmpi(w1, "mapreg_txt") == 0) {
  2986. strcpy(mapreg_txt, w2);
  2987. } else if(strcmpi(w1,"read_map_from_cache") == 0){
  2988. if (atoi(w2) == 2)
  2989. map_read_flag = READ_FROM_BITMAP_COMPRESSED;
  2990. else if (atoi(w2) == 1)
  2991. map_read_flag = READ_FROM_BITMAP;
  2992. else
  2993. map_read_flag = READ_FROM_GAT;
  2994. } else if(strcmpi(w1,"map_cache_file") == 0) {
  2995. strncpy(map_cache_file,w2,255);
  2996. } else if(strcmpi(w1,"db_path") == 0) {
  2997. strncpy(db_path,w2,255);
  2998. } else if(strcmpi(w1,"afm_dir") == 0) {
  2999. strcpy(afm_dir, w2);
  3000. } else if (strcmpi(w1, "console") == 0) {
  3001. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 ) {
  3002. console = 1;
  3003. ShowNotice("Console Commands are enabled.\n");
  3004. }
  3005. } else if (strcmpi(w1, "enable_spy") == 0) {
  3006. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 )
  3007. enable_spy = 1;
  3008. else
  3009. enable_spy = 0;
  3010. } else if (strcmpi(w1, "import") == 0) {
  3011. map_config_read(w2);
  3012. } else
  3013. ShowWarning("Unknown setting [%s] in file %s\n", w1, cfgName);
  3014. }
  3015. }
  3016. fclose(fp);
  3017. return 0;
  3018. }
  3019. int inter_config_read(char *cfgName)
  3020. {
  3021. int i;
  3022. char line[1024],w1[1024],w2[1024];
  3023. FILE *fp;
  3024. fp=fopen(cfgName,"r");
  3025. if(fp==NULL){
  3026. ShowError("File not found: '%s'.\n",cfgName);
  3027. return 1;
  3028. }
  3029. while(fgets(line,1020,fp)){
  3030. if(line[0] == '/' && line[1] == '/')
  3031. continue;
  3032. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3033. if(i!=2)
  3034. continue;
  3035. if(strcmpi(w1,"party_share_level")==0){
  3036. party_share_level = battle_config_switch(w2);
  3037. } else if(strcmpi(w1,"lowest_gm_level")==0){
  3038. lowest_gm_level = atoi(w2);
  3039. /* Main chat nick [LuzZza] */
  3040. } else if(strcmpi(w1, "main_chat_nick")==0){
  3041. strcpy(main_chat_nick, w2);
  3042. #ifndef TXT_ONLY
  3043. } else if(strcmpi(w1,"charsave_method")==0){
  3044. charsave_method = atoi(w2); //New char saving method.
  3045. } else if(strcmpi(w1,"item_db_db")==0){
  3046. strcpy(item_db_db,w2);
  3047. } else if(strcmpi(w1,"mob_db_db")==0){
  3048. strcpy(mob_db_db,w2);
  3049. } else if(strcmpi(w1,"item_db2_db")==0){
  3050. strcpy(item_db2_db,w2);
  3051. } else if(strcmpi(w1,"mob_db2_db")==0){
  3052. strcpy(mob_db2_db,w2);
  3053. } else if (strcmpi(w1, "char_db") == 0) {
  3054. strcpy(char_db, w2);
  3055. //Map Server SQL DB
  3056. } else if(strcmpi(w1,"map_server_ip")==0){
  3057. strcpy(map_server_ip, w2);
  3058. } else if(strcmpi(w1,"map_server_port")==0){
  3059. map_server_port=atoi(w2);
  3060. } else if(strcmpi(w1,"map_server_id")==0){
  3061. strcpy(map_server_id, w2);
  3062. } else if(strcmpi(w1,"map_server_pw")==0){
  3063. strcpy(map_server_pw, w2);
  3064. } else if(strcmpi(w1,"map_server_db")==0){
  3065. strcpy(map_server_db, w2);
  3066. } else if(strcmpi(w1,"default_codepage")==0){
  3067. strcpy(default_codepage, w2);
  3068. } else if(strcmpi(w1,"use_sql_db")==0){
  3069. db_use_sqldbs = battle_config_switch(w2);
  3070. ShowStatus ("Using SQL dbs: %s\n",w2);
  3071. } else if(strcmpi(w1,"connection_ping_interval")==0) {
  3072. connection_ping_interval = battle_config_switch(w2);
  3073. }else if(strcmpi(w1, "char_server_ip") == 0){
  3074. strcpy(charsql_host, w2);
  3075. }else if(strcmpi(w1, "char_server_port") == 0){
  3076. charsql_port = atoi(w2);
  3077. }else if(strcmpi(w1, "char_server_id") == 0){
  3078. strcpy(charsql_user, w2);
  3079. }else if(strcmpi(w1, "char_server_pw") == 0){
  3080. strcpy(charsql_pass, w2);
  3081. }else if(strcmpi(w1, "char_server_db") == 0){
  3082. strcpy(charsql_db, w2);
  3083. } else if(strcmpi(w1,"log_db")==0) {
  3084. strcpy(log_db, w2);
  3085. } else if(strcmpi(w1,"log_db_ip")==0) {
  3086. strcpy(log_db_ip, w2);
  3087. } else if(strcmpi(w1,"log_db")==0) {
  3088. strcpy(log_db, w2);
  3089. } else if(strcmpi(w1,"log_db_id")==0) {
  3090. strcpy(log_db_id, w2);
  3091. } else if(strcmpi(w1,"log_db_pw")==0) {
  3092. strcpy(log_db_pw, w2);
  3093. } else if(strcmpi(w1,"log_db_port")==0) {
  3094. log_db_port = atoi(w2);
  3095. // Mail Server SQL
  3096. } else if(strcmpi(w1,"mail_server_enable")==0){
  3097. mail_server_enable = battle_config_switch(w2);
  3098. ShowStatus ("Using Mail Server: %s\n",w2);
  3099. } else if(strcmpi(w1,"mail_server_ip")==0){
  3100. strcpy(mail_server_ip, w2);
  3101. } else if(strcmpi(w1,"mail_server_port")==0){
  3102. mail_server_port=atoi(w2);
  3103. } else if(strcmpi(w1,"mail_server_id")==0){
  3104. strcpy(mail_server_id, w2);
  3105. } else if(strcmpi(w1,"mail_server_pw")==0){
  3106. strcpy(mail_server_pw, w2);
  3107. } else if(strcmpi(w1,"mail_server_db")==0){
  3108. strcpy(mail_server_db, w2);
  3109. } else if(strcmpi(w1,"mail_db")==0) {
  3110. strcpy(mail_db, w2);
  3111. #endif
  3112. //support the import command, just like any other config
  3113. } else if(strcmpi(w1,"import")==0){
  3114. inter_config_read(w2);
  3115. }
  3116. }
  3117. fclose(fp);
  3118. return 0;
  3119. }
  3120. #ifndef TXT_ONLY
  3121. /*=======================================
  3122. * MySQL Init
  3123. *---------------------------------------
  3124. */
  3125. int map_sql_init(void){
  3126. mysql_init(&mmysql_handle);
  3127. //DB connection start
  3128. ShowInfo("Connecting to the Map DB Server....\n");
  3129. if(!mysql_real_connect(&mmysql_handle, map_server_ip, map_server_id, map_server_pw,
  3130. map_server_db ,map_server_port, (char *)NULL, 0)) {
  3131. //pointer check
  3132. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3133. exit(1);
  3134. }
  3135. else {
  3136. ShowStatus("connect success! (Map Server Connection)\n");
  3137. }
  3138. if(mail_server_enable) { // mail system [Valaris]
  3139. mysql_init(&mail_handle);
  3140. ShowInfo("Connecting to the Mail DB Server....\n");
  3141. if(!mysql_real_connect(&mail_handle, mail_server_ip, mail_server_id, mail_server_pw,
  3142. mail_server_db ,mail_server_port, (char *)NULL, 0)) {
  3143. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  3144. exit(1);
  3145. }
  3146. if( strlen(default_codepage) > 0 ) {
  3147. sprintf( tmp_sql, "SET NAMES %s", default_codepage );
  3148. if (mysql_query(&mail_handle, tmp_sql)) {
  3149. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  3150. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3151. }
  3152. }
  3153. }
  3154. if( strlen(default_codepage) > 0 ) {
  3155. sprintf( tmp_sql, "SET NAMES %s", default_codepage );
  3156. if (mysql_query(&mmysql_handle, tmp_sql)) {
  3157. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3158. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3159. }
  3160. }
  3161. return 0;
  3162. }
  3163. int map_sql_close(void){
  3164. mysql_close(&mmysql_handle);
  3165. ShowStatus("Close Map DB Connection....\n");
  3166. if (log_config.sql_logs)
  3167. {
  3168. mysql_close(&logmysql_handle);
  3169. ShowStatus("Close Log DB Connection....\n");
  3170. }
  3171. return 0;
  3172. }
  3173. int log_sql_init(void){
  3174. mysql_init(&logmysql_handle);
  3175. //DB connection start
  3176. ShowInfo(""CL_WHITE"[SQL]"CL_RESET": Connecting to the Log Database "CL_WHITE"%s"CL_RESET" At "CL_WHITE"%s"CL_RESET"...\n",log_db,log_db_ip);
  3177. if(!mysql_real_connect(&logmysql_handle, log_db_ip, log_db_id, log_db_pw,
  3178. log_db ,log_db_port, (char *)NULL, 0)) {
  3179. //pointer check
  3180. ShowSQL("DB error - %s\n",mysql_error(&logmysql_handle));
  3181. exit(1);
  3182. }
  3183. ShowStatus(""CL_WHITE"[SQL]"CL_RESET": Successfully '"CL_GREEN"connected"CL_RESET"' to Database '"CL_WHITE"%s"CL_RESET"'.\n", log_db);
  3184. if( strlen(default_codepage) > 0 ) {
  3185. sprintf( tmp_sql, "SET NAMES %s", default_codepage );
  3186. if (mysql_query(&logmysql_handle, tmp_sql)) {
  3187. ShowSQL("DB error - %s\n",mysql_error(&logmysql_handle));
  3188. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3189. }
  3190. }
  3191. return 0;
  3192. }
  3193. #endif /* not TXT_ONLY */
  3194. int map_db_final(DBKey k,void *d,va_list ap)
  3195. {
  3196. struct map_data_other_server *mdos = (struct map_data_other_server*)d;
  3197. if(mdos && mdos->gat == NULL)
  3198. aFree(mdos);
  3199. return 0;
  3200. }
  3201. int nick_db_final(void *k,void *d,va_list ap)
  3202. {
  3203. char *p = (char *) d;
  3204. if (p) aFree(p);
  3205. return 0;
  3206. }
  3207. int cleanup_sub(struct block_list *bl, va_list ap) {
  3208. nullpo_retr(0, bl);
  3209. switch(bl->type) {
  3210. case BL_PC:
  3211. map_quit((struct map_session_data *) bl);
  3212. break;
  3213. case BL_NPC:
  3214. npc_unload((struct npc_data *)bl);
  3215. break;
  3216. case BL_MOB:
  3217. unit_free(bl,0);
  3218. break;
  3219. case BL_PET:
  3220. //There is no need for this, the pet is removed together with the player. [Skotlex]
  3221. break;
  3222. case BL_ITEM:
  3223. map_clearflooritem(bl->id);
  3224. break;
  3225. case BL_SKILL:
  3226. skill_delunit((struct skill_unit *) bl, 1);
  3227. break;
  3228. }
  3229. return 1;
  3230. }
  3231. static int cleanup_db_sub(DBKey key,void *data,va_list va) {
  3232. return cleanup_sub((struct block_list*)data, NULL);
  3233. }
  3234. static int cleanup_db_subpc(DBKey key,void *data,va_list va) {
  3235. struct map_session_data *sd = (TBL_PC*)data;
  3236. if (!sd->state.finalsave)
  3237. { //Error?
  3238. ShowError("do_final: Player character in DB which was not sent to save! %d:%d\n", sd->status.account_id, sd->status.char_id);
  3239. map_quit(sd); //Attempt force-save
  3240. }
  3241. //Force remove from memory...
  3242. map_quit_ack(sd);
  3243. return 1;
  3244. }
  3245. /*==========================================
  3246. * map鯖終了・理
  3247. *------------------------------------------
  3248. */
  3249. void do_final(void) {
  3250. int i, j;
  3251. struct map_session_data **pl_allsd;
  3252. ShowStatus("Terminating...\n");
  3253. //we probably don't need the cache open at all times 'yet', so this is closed by mapsource_final [celest]
  3254. //map_cache_close();
  3255. // We probably don't need the grfio after server bootup 'yet' too. So this is closed near the end of do_init [Lance]
  3256. if((battle_config.cardillust_read_grffile || battle_config.item_equip_override_grffile ||
  3257. battle_config.item_slots_override_grffile || battle_config.item_name_override_grffile ||
  3258. battle_config.skill_sp_override_grffile))
  3259. grfio_final();
  3260. for (i = 0; i < map_num; i++)
  3261. if (map[i].m >= 0)
  3262. map_foreachinmap(cleanup_sub, i, BL_ALL);
  3263. //Scan any remaining players (between maps?) to kick them out. [Skotlex]
  3264. pl_allsd = map_getallusers(&j);
  3265. for (i = 0; i < j; i++)
  3266. map_quit(pl_allsd[i]);
  3267. id_db->foreach(id_db,cleanup_db_sub);
  3268. chrif_char_reset_offline();
  3269. chrif_flush_fifo();
  3270. //Online players were sent to save, but the ack will not arrive on time!
  3271. //They have to be removed from memory, and assume the char-server saved them.
  3272. pc_db->foreach(pc_db,cleanup_db_subpc);
  3273. do_final_atcommand();
  3274. do_final_battle();
  3275. do_final_chrif(); // この内部でキャラを全て切断する
  3276. do_final_npc();
  3277. // map_removenpc();
  3278. do_final_script();
  3279. do_final_itemdb();
  3280. do_final_storage();
  3281. do_final_guild();
  3282. do_final_party();
  3283. do_final_pc();
  3284. do_final_pet();
  3285. do_final_mob();
  3286. do_final_msg();
  3287. do_final_skill();
  3288. do_final_unit();
  3289. if(use_irc)
  3290. do_final_irc();
  3291. map_getallusers(NULL); //Clear the memory allocated for this array.
  3292. map_db->destroy(map_db, map_db_final);
  3293. for (i=0; i<map_num; i++) {
  3294. if(map[i].gat) aFree(map[i].gat);
  3295. if(map[i].cell) aFree(map[i].cell);
  3296. #ifdef CELL_NOSTACK
  3297. if(map[i].cell_bl) aFree(map[i].cell_bl);
  3298. #endif
  3299. if(map[i].block) aFree(map[i].block);
  3300. if(map[i].block_mob) aFree(map[i].block_mob);
  3301. if(map[i].block_count) aFree(map[i].block_count);
  3302. if(map[i].block_mob_count) aFree(map[i].block_mob_count);
  3303. if(battle_config.dynamic_mobs) { //Dynamic mobs flag by [random]
  3304. for (j=0; j<MAX_MOB_LIST_PER_MAP; j++)
  3305. if (map[i].moblist[j]) aFree(map[i].moblist[j]);
  3306. }
  3307. }
  3308. mapindex_final();
  3309. id_db->destroy(id_db, NULL);
  3310. pc_db->destroy(pc_db, NULL);
  3311. charid_db->destroy(charid_db, NULL);
  3312. //#endif
  3313. #ifndef TXT_ONLY
  3314. map_sql_close();
  3315. if(charsave_method)
  3316. charsql_db_init(0); //Connecting to chardb
  3317. #endif /* not TXT_ONLY */
  3318. ShowStatus("Successfully terminated.\n");
  3319. }
  3320. /*======================================================
  3321. * Map-Server Version Screen [MC Cameri]
  3322. *------------------------------------------------------
  3323. */
  3324. void map_helpscreen(int flag) { // by MC Cameri
  3325. puts("Usage: map-server [options]");
  3326. puts("Options:");
  3327. puts(CL_WHITE" Commands\t\t\tDescription"CL_RESET);
  3328. puts("-----------------------------------------------------------------------------");
  3329. puts(" --help, --h, --?, /? Displays this help screen");
  3330. puts(" --map-config <file> Load map-server configuration from <file>");
  3331. puts(" --battle-config <file> Load battle configuration from <file>");
  3332. puts(" --atcommand-config <file> Load atcommand configuration from <file>");
  3333. puts(" --charcommand-config <file> Load charcommand configuration from <file>");
  3334. puts(" --script-config <file> Load script configuration from <file>");
  3335. puts(" --msg-config <file> Load message configuration from <file>");
  3336. puts(" --grf-path-file <file> Load grf path file configuration from <file>");
  3337. puts(" --sql-config <file> Load inter-server configuration from <file>");
  3338. puts(" (SQL Only)");
  3339. puts(" --log-config <file> Load logging configuration from <file>");
  3340. puts(" (SQL Only)");
  3341. puts(" --version, --v, -v, /v Displays the server's version");
  3342. puts("\n");
  3343. if (flag) exit(1);
  3344. }
  3345. /*======================================================
  3346. * Map-Server Version Screen [MC Cameri]
  3347. *------------------------------------------------------
  3348. */
  3349. void map_versionscreen(int flag) {
  3350. printf("CL_WHITE" "eAthena version %d.%02d.%02d, Athena Mod version %d" CL_RESET"\n",
  3351. ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION,
  3352. ATHENA_MOD_VERSION);
  3353. puts(CL_GREEN "Website/Forum:" CL_RESET "\thttp://eathena.deltaanime.net/");
  3354. puts(CL_GREEN "Download URL:" CL_RESET "\thttp://eathena.systeminplace.net/");
  3355. puts(CL_GREEN "IRC Channel:" CL_RESET "\tirc://irc.deltaanime.net/#athena");
  3356. puts("\nOpen " CL_WHITE "readme.html" CL_RESET " for more information.");
  3357. if (ATHENA_RELEASE_FLAG) ShowNotice("This version is not for release.\n");
  3358. if (flag) exit(1);
  3359. }
  3360. #ifndef TXT_ONLY
  3361. /*======================================================
  3362. * Does a mysql_ping to all connection handles. [Skotlex]
  3363. *------------------------------------------------------
  3364. */
  3365. int map_sql_ping(int tid, unsigned int tick, int id, int data)
  3366. {
  3367. ShowInfo("Pinging SQL server to keep connection alive...\n");
  3368. mysql_ping(&mmysql_handle);
  3369. if (log_config.sql_logs)
  3370. mysql_ping(&logmysql_handle);
  3371. if(mail_server_enable)
  3372. mysql_ping(&mail_handle);
  3373. return 0;
  3374. }
  3375. #endif
  3376. /*======================================================
  3377. * Map-Server Init and Command-line Arguments [Valaris]
  3378. *------------------------------------------------------
  3379. */
  3380. void set_server_type(void)
  3381. {
  3382. SERVER_TYPE = ATHENA_SERVER_MAP;
  3383. }
  3384. int do_init(int argc, char *argv[]) {
  3385. int i;
  3386. #ifdef GCOLLECT
  3387. GC_enable_incremental();
  3388. #endif
  3389. INTER_CONF_NAME="conf/inter_athena.conf";
  3390. LOG_CONF_NAME="conf/log_athena.conf";
  3391. MAP_CONF_NAME = "conf/map_athena.conf";
  3392. BATTLE_CONF_FILENAME = "conf/battle_athena.conf";
  3393. ATCOMMAND_CONF_FILENAME = "conf/atcommand_athena.conf";
  3394. CHARCOMMAND_CONF_FILENAME = "conf/charcommand_athena.conf";
  3395. SCRIPT_CONF_NAME = "conf/script_athena.conf";
  3396. MSG_CONF_NAME = "conf/msg_athena.conf";
  3397. GRF_PATH_FILENAME = "conf/grf-files.txt";
  3398. srand(gettick());
  3399. for (i = 1; i < argc ; i++) {
  3400. if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--h") == 0 || strcmp(argv[i], "--?") == 0 || strcmp(argv[i], "/?") == 0)
  3401. map_helpscreen(1);
  3402. else if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "--v") == 0 || strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "/v") == 0)
  3403. map_versionscreen(1);
  3404. else if (strcmp(argv[i], "--map_config") == 0 || strcmp(argv[i], "--map-config") == 0)
  3405. MAP_CONF_NAME=argv[i+1];
  3406. else if (strcmp(argv[i],"--battle_config") == 0 || strcmp(argv[i],"--battle-config") == 0)
  3407. BATTLE_CONF_FILENAME = argv[i+1];
  3408. else if (strcmp(argv[i],"--atcommand_config") == 0 || strcmp(argv[i],"--atcommand-config") == 0)
  3409. ATCOMMAND_CONF_FILENAME = argv[i+1];
  3410. else if (strcmp(argv[i],"--charcommand_config") == 0 || strcmp(argv[i],"--charcommand-config") == 0)
  3411. CHARCOMMAND_CONF_FILENAME = argv[i+1];
  3412. else if (strcmp(argv[i],"--script_config") == 0 || strcmp(argv[i],"--script-config") == 0)
  3413. SCRIPT_CONF_NAME = argv[i+1];
  3414. else if (strcmp(argv[i],"--msg_config") == 0 || strcmp(argv[i],"--msg-config") == 0)
  3415. MSG_CONF_NAME = argv[i+1];
  3416. else if (strcmp(argv[i],"--grf_path_file") == 0 || strcmp(argv[i],"--grf-path-file") == 0)
  3417. GRF_PATH_FILENAME = argv[i+1];
  3418. #ifndef TXT_ONLY
  3419. else if (strcmp(argv[i],"--inter_config") == 0 || strcmp(argv[i],"--inter-config") == 0)
  3420. INTER_CONF_NAME = argv[i+1];
  3421. #endif
  3422. else if (strcmp(argv[i],"--log_config") == 0 || strcmp(argv[i],"--log-config") == 0)
  3423. LOG_CONF_NAME = argv[i+1];
  3424. else if (strcmp(argv[i],"--run_once") == 0) // close the map-server as soon as its done.. for testing [Celest]
  3425. runflag = 0;
  3426. }
  3427. map_config_read(MAP_CONF_NAME);
  3428. irc_read_conf(IRC_CONF); // [Zido]
  3429. chrif_checkdefaultlogin();
  3430. if (!map_ip_set || !char_ip_set) {
  3431. // The map server should know what IP address it is running on
  3432. // - MouseJstr
  3433. int localaddr = ntohl(addr_[0]);
  3434. unsigned char *ptr = (unsigned char *) &localaddr;
  3435. char buf[16];
  3436. if (naddr_ == 0) {
  3437. ShowError("\nUnable to determine your IP address... please edit the map_athena.conf file and set it.\n");
  3438. ShowError("(127.0.0.1 is valid if you have no network interface)\n");
  3439. }
  3440. sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);;
  3441. if (naddr_ != 1)
  3442. ShowNotice("Multiple interfaces detected.. using %s as our IP address\n", buf);
  3443. else
  3444. ShowInfo("Defaulting to %s as our IP address\n", buf);
  3445. if (!map_ip_set)
  3446. clif_setip(buf);
  3447. if (!char_ip_set)
  3448. chrif_setip(buf);
  3449. if (ptr[0] == 192 && ptr[1] == 168)
  3450. ShowNotice("\nFirewall detected.. \n edit subnet_athena.conf and map_athena.conf\n\n");
  3451. }
  3452. if (SHOW_DEBUG_MSG)
  3453. ShowNotice("Server running in '"CL_WHITE"Debug Mode"CL_RESET"'.\n");
  3454. battle_config_read(BATTLE_CONF_FILENAME);
  3455. msg_config_read(MSG_CONF_NAME);
  3456. atcommand_config_read(ATCOMMAND_CONF_FILENAME);
  3457. charcommand_config_read(CHARCOMMAND_CONF_FILENAME);
  3458. script_config_read(SCRIPT_CONF_NAME);
  3459. inter_config_read(INTER_CONF_NAME);
  3460. log_config_read(LOG_CONF_NAME);
  3461. id_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3462. pc_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int)); //Added for reliable map_id2sd() use. [Skotlex]
  3463. map_db = db_alloc(__FILE__,__LINE__,DB_UINT,DB_OPT_BASE,sizeof(int));
  3464. charid_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3465. #ifndef TXT_ONLY
  3466. map_sql_init();
  3467. if(charsave_method)
  3468. charsql_db_init(1); //Connecting to chardb
  3469. #endif /* not TXT_ONLY */
  3470. mapindex_init();
  3471. grfio_init(GRF_PATH_FILENAME);
  3472. map_readallmaps();
  3473. add_timer_func_list(map_freeblock_timer, "map_freeblock_timer");
  3474. add_timer_func_list(map_clearflooritem_timer, "map_clearflooritem_timer");
  3475. add_timer_func_list(map_removemobs_timer, "map_removemobs_timer");
  3476. add_timer_interval(gettick()+1000, map_freeblock_timer, 0, 0, 60*1000);
  3477. if(use_irc)
  3478. do_init_irc();
  3479. do_init_atcommand();
  3480. do_init_battle();
  3481. do_init_chrif();
  3482. do_init_clif();
  3483. do_init_script();
  3484. do_init_itemdb();
  3485. do_init_mob(); // npcの初期化・でmob_spawnして、mob_dbを?照するのでinit_npcより先
  3486. do_init_pc();
  3487. do_init_status();
  3488. do_init_party();
  3489. do_init_guild();
  3490. do_init_storage();
  3491. do_init_skill();
  3492. do_init_pet();
  3493. do_init_merc(); //[orn]
  3494. do_init_npc();
  3495. do_init_unit();
  3496. #ifndef TXT_ONLY /* mail system [Valaris] */
  3497. if(mail_server_enable)
  3498. do_init_mail();
  3499. if (log_config.sql_logs)
  3500. log_sql_init();
  3501. if (connection_ping_interval) {
  3502. add_timer_func_list(map_sql_ping, "map_sql_ping");
  3503. add_timer_interval(gettick()+connection_ping_interval*60*60*1000,
  3504. map_sql_ping, 0, 0, connection_ping_interval*60*60*1000);
  3505. }
  3506. #endif /* not TXT_ONLY */
  3507. npc_event_do_oninit(); // npcのOnInitイベント?行
  3508. if ( console ) {
  3509. set_defaultconsoleparse(parse_console);
  3510. start_console();
  3511. }
  3512. if (battle_config.pk_mode == 1)
  3513. ShowNotice("Server is running on '"CL_WHITE"PK Mode"CL_RESET"'.\n");
  3514. if(!(battle_config.cardillust_read_grffile || battle_config.item_equip_override_grffile ||
  3515. battle_config.item_slots_override_grffile || battle_config.item_name_override_grffile ||
  3516. battle_config.skill_sp_override_grffile))
  3517. grfio_final(); // Unused after reading all maps.
  3518. //However, some reload functions still use it,disable them.
  3519. //battle_config.cardillust_read_grffile =
  3520. //battle_config.item_equip_override_grffile =
  3521. //battle_config.item_slots_override_grffile =
  3522. //battle_config.item_name_override_grffile = 0;
  3523. ShowStatus("Server is '"CL_GREEN"ready"CL_RESET"' and listening on port '"CL_WHITE"%d"CL_RESET"'.\n\n", map_port);
  3524. return 0;
  3525. }
  3526. int compare_item(struct item *a, struct item *b) {
  3527. if (a->nameid == b->nameid &&
  3528. a->identify == b->identify &&
  3529. a->refine == b->refine &&
  3530. a->attribute == b->attribute)
  3531. {
  3532. int i;
  3533. for (i = 0; i < MAX_SLOTS && (a->card[i] == b->card[i]); i++);
  3534. return (i == MAX_SLOTS);
  3535. }
  3536. return 0;
  3537. }
  3538. #ifndef TXT_ONLY
  3539. int charsql_db_init(int method){
  3540. if(method == 1){ //'INIT / START'
  3541. ShowInfo("Connecting to 'character' Database... ");
  3542. mysql_init(&charsql_handle);
  3543. if(!mysql_real_connect(&charsql_handle, charsql_host, charsql_user, charsql_pass, charsql_db, charsql_port, (char *)NULL, 0)){
  3544. ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
  3545. exit(1);
  3546. }else{
  3547. printf("success.\n");
  3548. if( strlen(default_codepage) > 0 ) {
  3549. sprintf( tmp_sql, "SET NAMES %s", default_codepage );
  3550. if (mysql_query(&charsql_handle, tmp_sql)) {
  3551. ShowSQL("DB error - %s\n",mysql_error(&charsql_handle));
  3552. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3553. }
  3554. }
  3555. }
  3556. }else if(method == 0){ //'FINAL' / Shutdown
  3557. ShowInfo("Closing 'character' Database connection ... ");
  3558. mysql_close(&charsql_handle);
  3559. printf("done.\n");
  3560. }
  3561. return 0;
  3562. }
  3563. #endif