map.c 110 KB

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