map.c 109 KB

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