map.c 108 KB

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