map.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  1. // $Id: map.c,v 1.6 2004/09/25 17:37:01 MouseJstr Exp $
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #ifdef _WIN32
  7. #include <winsock.h>
  8. #else
  9. #include <netdb.h>
  10. #endif
  11. #include <math.h>
  12. #include "core.h"
  13. #include "timer.h"
  14. #include "db.h"
  15. #include "grfio.h"
  16. #include "malloc.h"
  17. #include "version.h"
  18. #include "map.h"
  19. #include "chrif.h"
  20. #include "clif.h"
  21. #include "intif.h"
  22. #include "npc.h"
  23. #include "pc.h"
  24. #include "status.h"
  25. #include "mob.h"
  26. #include "chat.h"
  27. #include "itemdb.h"
  28. #include "storage.h"
  29. #include "skill.h"
  30. #include "trade.h"
  31. #include "party.h"
  32. #include "battle.h"
  33. #include "script.h"
  34. #include "guild.h"
  35. #include "pet.h"
  36. #include "atcommand.h"
  37. #include "charcommand.h"
  38. #include "nullpo.h"
  39. #include "socket.h"
  40. #include "log.h"
  41. #include "showmsg.h"
  42. #ifdef MEMWATCH
  43. #include "memwatch.h"
  44. #endif
  45. // maybe put basic macros to somewhere else
  46. #define swap(a,b) ((a == b) || ((a ^= b), (b ^= a), (a ^= b)))
  47. #ifndef TXT_ONLY
  48. #include "mail.h" // mail system [Valaris]
  49. MYSQL mmysql_handle;
  50. MYSQL_RES* sql_res ;
  51. MYSQL_ROW sql_row ;
  52. char tmp_sql[65535]="";
  53. MYSQL lmysql_handle;
  54. MYSQL_RES* lsql_res ;
  55. MYSQL_ROW lsql_row ;
  56. char tmp_lsql[65535]="";
  57. MYSQL mail_handle; // mail system [Valaris]
  58. MYSQL_RES* mail_res ;
  59. MYSQL_ROW mail_row ;
  60. char tmp_msql[65535]="";
  61. int map_server_port = 3306;
  62. char map_server_ip[16] = "127.0.0.1";
  63. char map_server_id[32] = "ragnarok";
  64. char map_server_pw[32] = "ragnarok";
  65. char map_server_db[32] = "ragnarok";
  66. int db_use_sqldbs = 0;
  67. int login_server_port = 3306;
  68. char login_server_ip[16] = "127.0.0.1";
  69. char login_server_id[32] = "ragnarok";
  70. char login_server_pw[32] = "ragnarok";
  71. char login_server_db[32] = "ragnarok";
  72. char item_db_db[32] = "item_db";
  73. char mob_db_db[32] = "mob_db";
  74. char login_db[32] = "login";
  75. char login_db_level[32] = "level";
  76. char login_db_account_id[32] = "account_id";
  77. char log_db[32] = "log";
  78. char log_db_ip[16] = "127.0.0.1";
  79. char log_db_id[32] = "ragnarok";
  80. char log_db_pw[32] = "ragnarok";
  81. int log_db_port = 3306;
  82. char gm_db[32] = "login";
  83. char gm_db_level[32] = "level";
  84. char gm_db_account_id[32] = "account_id";
  85. int lowest_gm_level = 1;
  86. int read_gm_interval = 600000;
  87. char char_db[32] = "char";
  88. static int online_timer(int,unsigned int,int,int);
  89. int CHECK_INTERVAL = 3600000; // [Valaris]
  90. int check_online_timer=0; // [Valaris]
  91. #endif /* not TXT_ONLY */
  92. #define USE_AFM
  93. #define USE_AF2
  94. // 極力 staticでロ?カルに?める
  95. static struct dbt * id_db=NULL;
  96. static struct dbt * map_db=NULL;
  97. static struct dbt * nick_db=NULL;
  98. static struct dbt * charid_db=NULL;
  99. static int users=0;
  100. static struct block_list *objects[MAX_FLOORITEM];
  101. static int first_free_object_id=0,last_object_id=0;
  102. #define block_free_max 1048576
  103. static void *block_free[block_free_max];
  104. static int block_free_count = 0, block_free_lock = 0;
  105. #define BL_LIST_MAX 1048576
  106. static struct block_list *bl_list[BL_LIST_MAX];
  107. static int bl_list_count = 0;
  108. static char afm_dir[1024] = ""; // [Valaris]
  109. struct map_data map[MAX_MAP_PER_SERVER];
  110. int map_num = 0;
  111. int map_port=0;
  112. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  113. int agit_flag = 0;
  114. int night_flag = 0; // 0=day, 1=night [Yor]
  115. //Added for Mugendai's I'm Alive mod
  116. int imalive_on=0;
  117. int imalive_time=60;
  118. //Added by Mugendai for GUI
  119. int flush_on=1;
  120. int flush_time=100;
  121. struct charid2nick {
  122. char nick[24];
  123. int req_id;
  124. };
  125. // ォ゙ォテォラォュォ罩テォキォ袮ラ鯑ォユォ鬮ー(map_athana.conf?ェホread_map_from_cacheェヌ�・)
  126. // 0:ララ鯑ェキェハェ、 1:゙ェ?�ワチ� 2:?�ワチ�
  127. int map_read_flag = READ_FROM_GAT;
  128. char map_cache_file[256]="db/map.info"; // ォ゙ォテォラォュォ罩テォキォ雖ユォ。ォ、ォ・」
  129. char motd_txt[256] = "conf/motd.txt";
  130. char help_txt[256] = "conf/help.txt";
  131. char wisp_server_name[24] = "Server"; // can be modified in char-server configuration file
  132. int console = 0;
  133. /*==========================================
  134. * 全map鯖?計での接??設定
  135. * (char鯖から送られてくる)
  136. *------------------------------------------
  137. */
  138. void map_setusers(int n) {
  139. users = n;
  140. }
  141. /*==========================================
  142. * 全map鯖?計での接??取得 (/wへの?答用)
  143. *------------------------------------------
  144. */
  145. int map_getusers(void) {
  146. return users;
  147. }
  148. //
  149. // block削除の安全性確保?理
  150. //
  151. /*==========================================
  152. * blockをfreeするときfreeの?わりに呼ぶ
  153. * ロックされているときはバッファにためる
  154. *------------------------------------------
  155. */
  156. int map_freeblock( void *bl )
  157. {
  158. if(block_free_lock==0){
  159. aFree(bl);
  160. bl = NULL;
  161. }
  162. else{
  163. if( block_free_count>=block_free_max ) {
  164. if(battle_config.error_log)
  165. printf("map_freeblock: *WARNING* too many free block! %d %d\n",
  166. block_free_count,block_free_lock);
  167. }
  168. else
  169. block_free[block_free_count++]=bl;
  170. }
  171. return block_free_lock;
  172. }
  173. /*==========================================
  174. * blockのfreeを一時的に禁止する
  175. *------------------------------------------
  176. */
  177. int map_freeblock_lock(void) {
  178. return ++block_free_lock;
  179. }
  180. /*==========================================
  181. * blockのfreeのロックを解除する
  182. * このとき、ロックが完全になくなると
  183. * バッファにたまっていたblockを全部削除
  184. *------------------------------------------
  185. */
  186. int map_freeblock_unlock(void) {
  187. if ((--block_free_lock) == 0) {
  188. int i;
  189. // if(block_free_count>0) {
  190. // if(battle_config.error_log)
  191. // printf("map_freeblock_unlock: free %d object\n",block_free_count);
  192. // }
  193. for(i=0;i<block_free_count;i++){
  194. aFree(block_free[i]);
  195. block_free[i] = NULL;
  196. }
  197. block_free_count=0;
  198. }else if(block_free_lock<0){
  199. if(battle_config.error_log)
  200. printf("map_freeblock_unlock: lock count < 0 !\n");
  201. block_free_lock = 0; // 次回以降のロックに支障が出てくるのでリセット
  202. }
  203. return block_free_lock;
  204. }
  205. // map_freeblock_lock() を呼んで map_freeblock_unlock() を呼ばない
  206. // 関数があったので、定期的にblock_free_lockをリセットするようにする。
  207. // この関数は、do_timer() のトップレベルから呼ばれるので、
  208. // block_free_lock を直接いじっても支障無いはず。
  209. int map_freeblock_timer(int tid,unsigned int tick,int id,int data) {
  210. if(block_free_lock > 0) {
  211. printf("map_freeblock_timer: block_free_lock(%d) is invalid.\n",block_free_lock);
  212. block_free_lock = 1;
  213. map_freeblock_unlock();
  214. }
  215. // else {
  216. // printf("map_freeblock_timer: check ok\n");
  217. // }
  218. return 0;
  219. }
  220. //
  221. // block化?理
  222. //
  223. /*==========================================
  224. * map[]のblock_listから?がっている場合に
  225. * bl->prevにbl_headのアドレスを入れておく
  226. *------------------------------------------
  227. */
  228. static struct block_list bl_head;
  229. /*==========================================
  230. * map[]のblock_listに追加
  231. * mobは?が多いので別リスト
  232. *
  233. * ?にlink?みかの確認が無い。危?かも
  234. *------------------------------------------
  235. */
  236. int map_addblock(struct block_list *bl)
  237. {
  238. int m,x,y;
  239. nullpo_retr(0, bl);
  240. if(bl->prev != NULL){
  241. if(battle_config.error_log)
  242. printf("map_addblock error : bl->prev!=NULL\n");
  243. return 0;
  244. }
  245. m=bl->m;
  246. x=bl->x;
  247. y=bl->y;
  248. if(m<0 || m>=map_num ||
  249. x<0 || x>=map[m].xs ||
  250. y<0 || y>=map[m].ys)
  251. return 1;
  252. if(bl->type==BL_MOB){
  253. bl->next = map[m].block_mob[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs];
  254. bl->prev = &bl_head;
  255. if(bl->next) bl->next->prev = bl;
  256. map[m].block_mob[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs] = bl;
  257. map[m].block_mob_count[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs]++;
  258. } else {
  259. bl->next = map[m].block[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs];
  260. bl->prev = &bl_head;
  261. if(bl->next) bl->next->prev = bl;
  262. map[m].block[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs] = bl;
  263. map[m].block_count[x/BLOCK_SIZE+(y/BLOCK_SIZE)*map[m].bxs]++;
  264. if(bl->type==BL_PC)
  265. map[m].users++;
  266. }
  267. return 0;
  268. }
  269. /*==========================================
  270. * map[]のblock_listから外す
  271. * prevがNULLの場合listに?がってない
  272. *------------------------------------------
  273. */
  274. int map_delblock(struct block_list *bl)
  275. {
  276. int b;
  277. nullpo_retr(0, bl);
  278. // ?にblocklistから?けている
  279. if(bl->prev==NULL){
  280. if(bl->next!=NULL){
  281. // prevがNULLでnextがNULLでないのは有ってはならない
  282. if(battle_config.error_log)
  283. printf("map_delblock error : bl->next!=NULL\n");
  284. }
  285. return 0;
  286. }
  287. b = bl->x/BLOCK_SIZE+(bl->y/BLOCK_SIZE)*map[bl->m].bxs;
  288. if(bl->type==BL_PC)
  289. map[bl->m].users--;
  290. if(bl->next)
  291. bl->next->prev = bl->prev;
  292. if(bl->prev==&bl_head){
  293. // リストの頭なので、map[]のblock_listを更新する
  294. if(bl->type==BL_MOB){
  295. map[bl->m].block_mob[b] = bl->next;
  296. if((map[bl->m].block_mob_count[b]--) < 0)
  297. map[bl->m].block_mob_count[b] = 0;
  298. } else {
  299. map[bl->m].block[b] = bl->next;
  300. if((map[bl->m].block_count[b]--) < 0)
  301. map[bl->m].block_count[b] = 0;
  302. }
  303. } else {
  304. bl->prev->next = bl->next;
  305. }
  306. bl->next = NULL;
  307. bl->prev = NULL;
  308. return 0;
  309. }
  310. /*==========================================
  311. * 周?のPC人?を?える (現在未使用)
  312. *------------------------------------------
  313. */
  314. int map_countnearpc(int m, int x, int y) {
  315. int bx,by,c=0;
  316. struct block_list *bl=NULL;
  317. if(map[m].users==0)
  318. return 0;
  319. for(by=y/BLOCK_SIZE-AREA_SIZE/BLOCK_SIZE-1;by<=y/BLOCK_SIZE+AREA_SIZE/BLOCK_SIZE+1;by++){
  320. if(by<0 || by>=map[m].bys)
  321. continue;
  322. for(bx=x/BLOCK_SIZE-AREA_SIZE/BLOCK_SIZE-1;bx<=x/BLOCK_SIZE+AREA_SIZE/BLOCK_SIZE+1;bx++){
  323. if(bx<0 || bx>=map[m].bxs)
  324. continue;
  325. bl = map[m].block[bx+by*map[m].bxs];
  326. for(;bl;bl=bl->next){
  327. if(bl->type==BL_PC)
  328. c++;
  329. }
  330. }
  331. }
  332. return c;
  333. }
  334. /*==========================================
  335. * セル上のPCとMOBの?を?える (グランドクロス用)
  336. *------------------------------------------
  337. */
  338. int map_count_oncell(int m, int x, int y) {
  339. int bx,by;
  340. struct block_list *bl=NULL;
  341. int i,c;
  342. int count = 0;
  343. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  344. return 1;
  345. bx = x/BLOCK_SIZE;
  346. by = y/BLOCK_SIZE;
  347. bl = map[m].block[bx+by*map[m].bxs];
  348. c = map[m].block_count[bx+by*map[m].bxs];
  349. for(i=0;i<c && bl;i++,bl=bl->next){
  350. if(bl->x == x && bl->y == y && bl->type == BL_PC) count++;
  351. }
  352. bl = map[m].block_mob[bx+by*map[m].bxs];
  353. c = map[m].block_mob_count[bx+by*map[m].bxs];
  354. for(i=0;i<c && bl;i++,bl=bl->next){
  355. if(bl->x == x && bl->y == y) count++;
  356. }
  357. if(!count) count = 1;
  358. return count;
  359. }
  360. /*
  361. * ォサォ・セェホ��ェヒフクェトェアェソォケォュォ・讚ヒォテォネェ��ケ
  362. */
  363. struct skill_unit *map_find_skill_unit_oncell(struct block_list *target,int x,int y,int skill_id,struct skill_unit *out_unit)
  364. {
  365. int m,bx,by;
  366. struct block_list *bl;
  367. int i,c;
  368. struct skill_unit *unit;
  369. m = target->m;
  370. if (x < 0 || y < 0 || (x >= map[m].xs) || (y >= map[m].ys))
  371. return NULL;
  372. bx = x/BLOCK_SIZE;
  373. by = y/BLOCK_SIZE;
  374. bl = map[m].block[bx+by*map[m].bxs];
  375. c = map[m].block_count[bx+by*map[m].bxs];
  376. for(i=0;i<c && bl;i++,bl=bl->next){
  377. if (bl->x != x || bl->y != y || bl->type != BL_SKILL)
  378. continue;
  379. unit = (struct skill_unit *) bl;
  380. if (unit==out_unit || !unit->alive ||
  381. !unit->group || unit->group->skill_id!=skill_id)
  382. continue;
  383. if (battle_check_target(&unit->bl,target,unit->group->target_flag)>0)
  384. return unit;
  385. }
  386. return NULL;
  387. }
  388. /*==========================================
  389. * map m (x0,y0)-(x1,y1)?の全objに?して
  390. * funcを呼ぶ
  391. * type!=0 ならその種類のみ
  392. *------------------------------------------
  393. */
  394. void map_foreachinarea(int (*func)(struct block_list*,va_list),int m,int x0,int y0,int x1,int y1,int type,...) {
  395. va_list ap;
  396. int bx,by;
  397. struct block_list *bl=NULL;
  398. int blockcount=bl_list_count,i,c;
  399. if(m < 0)
  400. return;
  401. va_start(ap,type);
  402. if (x0 < 0) x0 = 0;
  403. if (y0 < 0) y0 = 0;
  404. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  405. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  406. if (type == 0 || type != BL_MOB)
  407. for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
  408. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  409. bl = map[m].block[bx+by*map[m].bxs];
  410. c = map[m].block_count[bx+by*map[m].bxs];
  411. for(i=0;i<c && bl;i++,bl=bl->next){
  412. if(bl && type && bl->type!=type)
  413. continue;
  414. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  415. bl_list[bl_list_count++]=bl;
  416. }
  417. }
  418. }
  419. if(type==0 || type==BL_MOB)
  420. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  421. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  422. bl = map[m].block_mob[bx+by*map[m].bxs];
  423. c = map[m].block_mob_count[bx+by*map[m].bxs];
  424. for(i=0;i<c && bl;i++,bl=bl->next){
  425. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  426. bl_list[bl_list_count++]=bl;
  427. }
  428. }
  429. }
  430. if(bl_list_count>=BL_LIST_MAX) {
  431. if(battle_config.error_log)
  432. printf("map_foreachinarea: *WARNING* block count too many!\n");
  433. }
  434. map_freeblock_lock(); // メモリからの解放を禁止する
  435. for(i=blockcount;i<bl_list_count;i++)
  436. if(bl_list[i]->prev) // 有?かどうかチェック
  437. func(bl_list[i],ap);
  438. map_freeblock_unlock(); // 解放を許可する
  439. va_end(ap);
  440. bl_list_count = blockcount;
  441. }
  442. /*==========================================
  443. * 矩形(x0,y0)-(x1,y1)が(dx,dy)移動した時の
  444. * 領域外になる領域(矩形かL字形)?のobjに
  445. * ?してfuncを呼ぶ
  446. *
  447. * dx,dyは-1,0,1のみとする(どんな値でもいいっぽい?)
  448. *------------------------------------------
  449. */
  450. void 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,...) {
  451. int bx,by;
  452. struct block_list *bl=NULL;
  453. va_list ap;
  454. int blockcount=bl_list_count,i,c;
  455. va_start(ap,type);
  456. if(dx==0 || dy==0){
  457. // 矩形領域の場合
  458. if(dx==0){
  459. if(dy<0){
  460. y0=y1+dy+1;
  461. } else {
  462. y1=y0+dy-1;
  463. }
  464. } else if(dy==0){
  465. if(dx<0){
  466. x0=x1+dx+1;
  467. } else {
  468. x1=x0+dx-1;
  469. }
  470. }
  471. if(x0<0) x0=0;
  472. if(y0<0) y0=0;
  473. if(x1>=map[m].xs) x1=map[m].xs-1;
  474. if(y1>=map[m].ys) y1=map[m].ys-1;
  475. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  476. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  477. bl = map[m].block[bx+by*map[m].bxs];
  478. c = map[m].block_count[bx+by*map[m].bxs];
  479. for(i=0;i<c && bl;i++,bl=bl->next){
  480. if(bl && type && bl->type!=type)
  481. continue;
  482. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  483. bl_list[bl_list_count++]=bl;
  484. }
  485. bl = map[m].block_mob[bx+by*map[m].bxs];
  486. c = map[m].block_mob_count[bx+by*map[m].bxs];
  487. for(i=0;i<c && bl;i++,bl=bl->next){
  488. if(bl && type && bl->type!=type)
  489. continue;
  490. if(bl && bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1 && bl_list_count<BL_LIST_MAX)
  491. bl_list[bl_list_count++]=bl;
  492. }
  493. }
  494. }
  495. }else{
  496. // L字領域の場合
  497. if(x0<0) x0=0;
  498. if(y0<0) y0=0;
  499. if(x1>=map[m].xs) x1=map[m].xs-1;
  500. if(y1>=map[m].ys) y1=map[m].ys-1;
  501. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  502. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  503. bl = map[m].block[bx+by*map[m].bxs];
  504. c = map[m].block_count[bx+by*map[m].bxs];
  505. for(i=0;i<c && bl;i++,bl=bl->next){
  506. if(bl && type && bl->type!=type)
  507. continue;
  508. if((bl) && !(bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1))
  509. continue;
  510. if((bl) && ((dx>0 && bl->x<x0+dx) || (dx<0 && bl->x>x1+dx) ||
  511. (dy>0 && bl->y<y0+dy) || (dy<0 && bl->y>y1+dy)) &&
  512. bl_list_count<BL_LIST_MAX)
  513. bl_list[bl_list_count++]=bl;
  514. }
  515. bl = map[m].block_mob[bx+by*map[m].bxs];
  516. c = map[m].block_mob_count[bx+by*map[m].bxs];
  517. for(i=0;i<c && bl;i++,bl=bl->next){
  518. if(bl && type && bl->type!=type)
  519. continue;
  520. if((bl) && !(bl->x>=x0 && bl->x<=x1 && bl->y>=y0 && bl->y<=y1))
  521. continue;
  522. if((bl) && ((dx>0 && bl->x<x0+dx) || (dx<0 && bl->x>x1+dx) ||
  523. (dy>0 && bl->y<y0+dy) || (dy<0 && bl->y>y1+dy)) &&
  524. bl_list_count<BL_LIST_MAX)
  525. bl_list[bl_list_count++]=bl;
  526. }
  527. }
  528. }
  529. }
  530. if(bl_list_count>=BL_LIST_MAX) {
  531. if(battle_config.error_log)
  532. printf("map_foreachinarea: *WARNING* block count too many!\n");
  533. }
  534. map_freeblock_lock(); // メモリからの解放を禁止する
  535. for(i=blockcount;i<bl_list_count;i++)
  536. if(bl_list[i]->prev) { // 有?かどうかチェック
  537. if (bl_list[i]->type == BL_PC
  538. && session[((struct map_session_data *) bl_list[i])->fd] == NULL)
  539. continue;
  540. func(bl_list[i],ap);
  541. }
  542. map_freeblock_unlock(); // 解放を許可する
  543. va_end(ap);
  544. bl_list_count = blockcount;
  545. }
  546. // -- moonsoul (added map_foreachincell which is a rework of map_foreachinarea but
  547. // which only checks the exact single x/y passed to it rather than an
  548. // area radius - may be more useful in some instances)
  549. //
  550. void map_foreachincell(int (*func)(struct block_list*,va_list),int m,int x,int y,int type,...) {
  551. int bx,by;
  552. struct block_list *bl=NULL;
  553. va_list ap;
  554. int blockcount=bl_list_count,i,c;
  555. va_start(ap,type);
  556. by=y/BLOCK_SIZE;
  557. bx=x/BLOCK_SIZE;
  558. if(type==0 || type!=BL_MOB)
  559. {
  560. bl = map[m].block[bx+by*map[m].bxs];
  561. c = map[m].block_count[bx+by*map[m].bxs];
  562. for(i=0;i<c && bl;i++,bl=bl->next)
  563. {
  564. if(type && bl && bl->type!=type)
  565. continue;
  566. if(bl && bl->x==x && bl->y==y && bl_list_count<BL_LIST_MAX)
  567. bl_list[bl_list_count++]=bl;
  568. }
  569. }
  570. if(type==0 || type==BL_MOB)
  571. {
  572. bl = map[m].block_mob[bx+by*map[m].bxs];
  573. c = map[m].block_mob_count[bx+by*map[m].bxs];
  574. for(i=0;i<c && bl;i++,bl=bl->next)
  575. {
  576. if(bl && bl->x==x && bl->y==y && bl_list_count<BL_LIST_MAX)
  577. bl_list[bl_list_count++]=bl;
  578. }
  579. }
  580. if(bl_list_count>=BL_LIST_MAX) {
  581. if(battle_config.error_log)
  582. printf("map_foreachincell: *WARNING* block count too many!\n");
  583. }
  584. map_freeblock_lock(); // メモリからの解放を禁止する
  585. for(i=blockcount;i<bl_list_count;i++)
  586. if(bl_list[i]->prev) // 有?かどうかチェック
  587. func(bl_list[i],ap);
  588. map_freeblock_unlock(); // 解放を許可する
  589. va_end(ap);
  590. bl_list_count = blockcount;
  591. }
  592. /*============================================================
  593. * For checking a path between two points (x0, y0) and (x1, y1)
  594. *------------------------------------------------------------
  595. */
  596. void map_foreachinpath(int (*func)(struct block_list*,va_list),int m,int x0,int y0,int x1,int y1,int range,int type,...)
  597. {
  598. /* va_list ap;
  599. double deltax = 0.0;
  600. double deltay = 0.0;
  601. int t, bx, by;
  602. int *xs, *ys;
  603. int blockcount = bl_list_count, i, c;
  604. struct block_list *bl = NULL;
  605. if(m < 0)
  606. return;
  607. va_start(ap,type);
  608. if (x0 < 0) x0 = 0;
  609. if (y0 < 0) y0 = 0;
  610. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  611. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  612. // I'm not finished thinking on it
  613. // but first it might better use a parameter equation
  614. // x=(x1-x0)*t+x0; y=(y1-y0)*t+y0; t=[0,1]
  615. // would not need special case aproximating for infinity/zero slope
  616. // so maybe this way:
  617. // find maximum runindex
  618. int tmax = abs(y1-y0);
  619. if(tmax < abs(x1-x0))
  620. tmax = abs(x1-x0);
  621. xs = (int *)aCallocA(tmax + 1, sizeof(int));
  622. ys = (int *)aCallocA(tmax + 1, sizeof(int));
  623. // pre-calculate delta values for x and y destination
  624. // should speed up cause you don't need to divide in the loop
  625. if(tmax>0)
  626. {
  627. deltax = ((double)(x1-x0)) / ((double)tmax);
  628. deltay = ((double)(y1-y0)) / ((double)tmax);
  629. }
  630. // go along the index
  631. for(t=0; t<=tmax; t++)
  632. {
  633. int x = (int)floor(deltax * (double)t +0.5)+x0;
  634. int y = (int)floor(deltay * (double)t +0.5)+y0;
  635. // the xy pairs of points in line between x0y0 and x1y1
  636. // including start and end point
  637. xs[t] = x;
  638. ys[t] = y;
  639. }
  640. if (type == 0 || type != BL_MOB)
  641. this here is wrong,
  642. there is no check if x0<x1 and y0<y1
  643. but this is not valid in 3 of 4 cases,
  644. so in this case here you check only blocks when shooting to a positive direction
  645. shooting in other directions just do nothing like the skill has failed
  646. if you want to keep this that way then check and swap x0,y0 with x1,y1
  647. for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
  648. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  649. bl = map[m].block[bx+by*map[m].bxs];
  650. c = map[m].block_count[bx+by*map[m].bxs];
  651. for(i=0;i<c && bl;i++,bl=bl->next){
  652. if(bl) {
  653. if (type && bl->type!=type)
  654. continue;
  655. for(t=0; t<=tmax; t++)
  656. if(bl->x==xs[t] && bl->y==ys[t] && bl_list_count<BL_LIST_MAX)
  657. bl_list[bl_list_count++]=bl;
  658. }
  659. }
  660. }
  661. }
  662. if(type==0 || type==BL_MOB)
  663. for(by=y0/BLOCK_SIZE;by<=y1/BLOCK_SIZE;by++){
  664. for(bx=x0/BLOCK_SIZE;bx<=x1/BLOCK_SIZE;bx++){
  665. bl = map[m].block_mob[bx+by*map[m].bxs];
  666. c = map[m].block_mob_count[bx+by*map[m].bxs];
  667. for(i=0;i<c && bl;i++,bl=bl->next){
  668. if(bl) {
  669. for(t=0; t<=tmax; t++)
  670. if(bl->x==xs[t] && bl->y==ys[t] && bl_list_count<BL_LIST_MAX)
  671. bl_list[bl_list_count++]=bl;
  672. }
  673. }
  674. }
  675. }
  676. if(bl_list_count>=BL_LIST_MAX) {
  677. if(battle_config.error_log)
  678. printf("map_foreachinarea: *WARNING* block count too many!\n");
  679. }
  680. map_freeblock_lock(); // メモリからの解放を禁止する
  681. for(i=blockcount;i<bl_list_count;i++)
  682. if(bl_list[i]->prev) // 有?かどうかチェック
  683. func(bl_list[i],ap);
  684. map_freeblock_unlock(); // 解放を許可する
  685. bl_list_count = blockcount;
  686. aFree (xs);
  687. aFree (ys);
  688. va_end(ap);
  689. */
  690. /*
  691. //////////////////////////////////////////////////////////////
  692. //
  693. // sharp shooting 1
  694. //
  695. //////////////////////////////////////////////////////////////
  696. // problem:
  697. // finding targets standing on and within some range of a line
  698. // (t1,t2 t3 and t4 get hit)
  699. //
  700. // target 1
  701. // x t4
  702. // t2
  703. // t3 x
  704. // x
  705. // S
  706. //////////////////////////////////////////////////////////////
  707. // solution 1 (straight forward, but a bit calculation expensive)
  708. // calculating perpendiculars from quesionable mobs to the straight line
  709. // if the mob is hit then depends on the distance to the line
  710. //
  711. // solution 2 (complex, need to handle many cases, but maybe faster)
  712. // make a formula to deside if a given (x,y) is within a shooting area
  713. // the shape can be ie. rectangular or triangular
  714. // if the mob is hit then depends on if the mob is inside or outside the area
  715. // I'm not going to implement this, but if somebody is interested
  716. // in vector algebra, it might be some fun
  717. //////////////////////////////////////////////////////////////
  718. // possible shooting ranges (I prefer the second one)
  719. //////////////////////////////////////////////////////////////
  720. //
  721. // ---------------- ------
  722. // ---------------- ------------
  723. // Sxxxxxxxxxxxxxxxxtarget Sxxxxxxxxxxxxxxxxtarget
  724. // ---------------- ------------
  725. // ---------------- -----
  726. //
  727. // the original code implemented the left structure
  728. // might be not that realistic, so I changed to the other one
  729. // I take "range" as max distance from the line
  730. //////////////////////////////////////////////////////////////
  731. va_list ap;
  732. int i, blockcount = bl_list_count;
  733. struct block_list *bl;
  734. int c1,c2;
  735. ///////////
  736. double deltax,deltay;
  737. double k,kfact,knorm;
  738. double v1,v2,distance;
  739. double xm,ym,rd;
  740. int bx,by,bx0,bx1,by0,by1;
  741. //////////////
  742. // no map
  743. if(m < 0) return;
  744. // xy out of range
  745. if (x0 < 0) x0 = 0;
  746. if (y0 < 0) y0 = 0;
  747. if (x1 >= map[m].xs) x1 = map[m].xs-1;
  748. if (y1 >= map[m].ys) y1 = map[m].ys-1;
  749. ///////////////////////////////
  750. // stuff for a linear equation in xy coord to calculate
  751. // the perpendicular from a block xy to the straight line
  752. deltax = (x1-x0);
  753. deltay = (y1-y0);
  754. kfact = (deltax*deltax+deltay*deltay); // the sqare length of the line
  755. knorm = -deltax*x0-deltay*y0; // the offset vector param
  756. //printf("(%i,%i)(%i,%i) range: %i\n",x0,y0,x1,y1,range);
  757. if(kfact==0) return; // shooting at the standing position should not happen
  758. kfact = 1/kfact; // divide here and multiply in the loop
  759. range *= range; // compare with range^2 so we can skip a sqrt and signs
  760. ///////////////////////////////
  761. // prepare shooting area check
  762. xm = (x1+x0)/2.0;
  763. ym = (y1+y0)/2.0;// middle point on the shooting line
  764. // the sqared radius of a circle around the shooting range
  765. // plus the sqared radius of a block
  766. rd = (x0-xm)*(x0-xm) + (y0-ym)*(y0-ym) + (range*range)
  767. +BLOCK_SIZE*BLOCK_SIZE/2;
  768. // so whenever a block midpoint is within this circle
  769. // some of the block area is possibly within the shooting range
  770. ///////////////////////////////
  771. // what blocks we need to test
  772. // blocks covered by the xy position of begin and end of the line
  773. bx0 = x0/BLOCK_SIZE;
  774. bx1 = x1/BLOCK_SIZE;
  775. by0 = y0/BLOCK_SIZE;
  776. by1 = y1/BLOCK_SIZE;
  777. // swap'em for a smallest-to-biggest run
  778. if(bx0>bx1) swap(bx0,bx1);
  779. if(by0>by1) swap(by0,by1);
  780. // enlarge the block area by a range value and 1
  781. // so we can be sure to process all blocks that might touch the shooting area
  782. // in this case here with BLOCK_SIZE=8 and range=2 it will be only enlarged by 1
  783. // but I implement it anyway just in case that ranges will be larger
  784. // or BLOCK_SIZE smaller in future
  785. i = (range/BLOCK_SIZE+1);//temp value
  786. if(bx0>i) bx0 -=i; else bx0=0;
  787. if(by0>i) by0 -=i; else by0=0;
  788. if(bx1+i<map[m].bxs) bx1 +=i; else bx1=map[m].bxs-1;
  789. if(by1+i<map[m].bys) by1 +=i; else by1=map[m].bys-1;
  790. //printf("run for (%i,%i)(%i,%i)\n",bx0,by0,bx1,by1);
  791. for(bx=bx0; bx<=bx1; bx++)
  792. for(by=by0; by<=by1; by++)
  793. { // block xy
  794. c1 = map[m].block_count[bx+by*map[m].bxs]; // number of elements in the block
  795. c2 = map[m].block_mob_count[bx+by*map[m].bxs]; // number of mobs in the mob block
  796. if( (c1==0) && (c2==0) ) continue; // skip if nothing in the blocks
  797. //printf("block(%i,%i) %i %i\n",bx,by,c1,c2);fflush(stdout);
  798. // test if the mid-point of the block is too far away
  799. // so we could skip the whole block in this case
  800. v1 = (bx*BLOCK_SIZE+BLOCK_SIZE/2-xm)*(bx*BLOCK_SIZE+BLOCK_SIZE/2-xm)
  801. +(by*BLOCK_SIZE+BLOCK_SIZE/2-ym)*(by*BLOCK_SIZE+BLOCK_SIZE/2-ym);
  802. //printf("block(%i,%i) v1=%f rd=%f\n",bx,by,v1,rd);fflush(stdout);
  803. // check for the worst case scenario
  804. if(v1 > rd) continue;
  805. // it seems that the block is at least partially covered by the shooting range
  806. // so we go into it
  807. if(type==0 || type!=BL_MOB) {
  808. bl = map[m].block[bx+by*map[m].bxs]; // a block with the elements
  809. for(i=0;i<c1 && bl;i++,bl=bl->next){ // go through all elements
  810. if( bl && ( !type || bl->type==type ) && bl_list_count<BL_LIST_MAX )
  811. {
  812. // calculate the perpendicular from block xy to the straight line
  813. k = kfact*(deltax*bl->x + deltay*bl->y + knorm);
  814. // check if the perpendicular is within start and end of our line
  815. if(k>=0 && k<=1)
  816. { // calculate the distance
  817. v1 = deltax*k+x0 - bl->x;
  818. v2 = deltay*k+y0 - bl->y;
  819. distance = v1*v1+v2*v2;
  820. // triangular shooting range
  821. if( distance <= range*k )
  822. bl_list[bl_list_count++]=bl;
  823. }
  824. }
  825. }//end for elements
  826. }
  827. if(type==0 || type==BL_MOB) {
  828. bl = map[m].block_mob[bx+by*map[m].bxs]; // and the mob block
  829. for(i=0;i<c2 && bl;i++,bl=bl->next){
  830. if(bl && bl_list_count<BL_LIST_MAX) {
  831. // calculate the perpendicular from block xy to the straight line
  832. k = kfact*(deltax*bl->x + deltay*bl->y + knorm);
  833. //printf("mob: (%i,%i) k=%f ",bl->x,bl->y, k);
  834. // check if the perpendicular is within start and end of our line
  835. if(k>=0 && k<=1)
  836. {
  837. v1 = deltax*k+x0 - bl->x;
  838. v2 = deltay*k+y0 - bl->y;
  839. distance = v1*v1+v2*v2;
  840. //printf("dist: %f",distance);
  841. // triangular shooting range
  842. if( distance <= range*k )
  843. {
  844. //printf(" hit");
  845. bl_list[bl_list_count++]=bl;
  846. }
  847. }
  848. //printf("\n");
  849. }
  850. }//end for mobs
  851. }
  852. }//end for(bx,by)
  853. if(bl_list_count>=BL_LIST_MAX) {
  854. if(battle_config.error_log)
  855. printf("map_foreachinarea: *WARNING* block count too many!\n");
  856. }
  857. va_start(ap,type);
  858. map_freeblock_lock(); // メモリからの解放を禁止する
  859. for(i=blockcount;i<bl_list_count;i++)
  860. if(bl_list[i]->prev) // 有?かどうかチェック
  861. func(bl_list[i],ap);
  862. map_freeblock_unlock(); // 解放を許可する
  863. va_end(ap);
  864. bl_list_count = blockcount;
  865. */
  866. //////////////////////////////////////////////////////////////
  867. //
  868. // sharp shooting 2
  869. //
  870. //////////////////////////////////////////////////////////////
  871. // problem:
  872. // finding targets standing exactly on a line
  873. // (only t1 and t2 get hit)
  874. //
  875. // target 1
  876. // x t4
  877. // t2
  878. // t3 x
  879. // x
  880. // S
  881. //////////////////////////////////////////////////////////////
  882. va_list ap;
  883. int i, blockcount = bl_list_count;
  884. struct block_list *bl;
  885. int c1,c2;
  886. //////////////////////////////////////////////////////////////
  887. // linear parametric equation
  888. // x=(x1-x0)*t+x0; y=(y1-y0)*t+y0; t=[0,1]
  889. //////////////////////////////////////////////////////////////
  890. // linear equation for finding a single line between (x0,y0)->(x1,y1)
  891. // independent of the given xy-values
  892. double dx = 0.0;
  893. double dy = 0.0;
  894. int bx=-1; // initialize block coords to some impossible value
  895. int by=-1;
  896. int t;
  897. ///////////////////////////////
  898. // find maximum runindex
  899. int tmax = abs(y1-y0);
  900. if(tmax < abs(x1-x0))
  901. tmax = abs(x1-x0);
  902. // pre-calculate delta values for x and y destination
  903. // should speed up cause you don't need to divide in the loop
  904. if(tmax>0)
  905. {
  906. dx = ((double)(x1-x0)) / ((double)tmax);
  907. dy = ((double)(y1-y0)) / ((double)tmax);
  908. }
  909. // go along the index
  910. for(t=0; t<=tmax; t++)
  911. { // xy-values of the line including start and end point
  912. int x = (int)floor(dx * (double)t +0.5)+x0;
  913. int y = (int)floor(dy * (double)t +0.5)+y0;
  914. // check the block index of the calculated xy
  915. if( (bx!=x/BLOCK_SIZE) || (by!=y/BLOCK_SIZE) )
  916. { // we have reached a new block
  917. // so we store the current block coordinates
  918. bx = x/BLOCK_SIZE;
  919. by = y/BLOCK_SIZE;
  920. // and process the data
  921. c1 = map[m].block_count[bx+by*map[m].bxs]; // number of elements in the block
  922. c2 = map[m].block_mob_count[bx+by*map[m].bxs]; // number of mobs in the mob block
  923. if( (c1==0) && (c2==0) ) continue; // skip if nothing in the block
  924. if(type==0 || type!=BL_MOB) {
  925. bl = map[m].block[bx+by*map[m].bxs]; // a block with the elements
  926. for(i=0;i<c1 && bl;i++,bl=bl->next){ // go through all elements
  927. if( bl && ( !type || bl->type==type ) && bl_list_count<BL_LIST_MAX )
  928. {
  929. // check if block xy is on the line
  930. if( (bl->x-x0)*(y1-y0) == (bl->y-y0)*(x1-x0) )
  931. // and if it is within start and end point
  932. if( (((x0<=x1)&&(x0<=bl->x)&&(bl->x<=x1)) || ((x0>=x1)&&(x0>=bl->x)&&(bl->x>=x1))) &&
  933. (((y0<=y1)&&(y0<=bl->y)&&(bl->y<=y1)) || ((y0>=y1)&&(y0>=bl->y)&&(bl->y>=y1))) )
  934. bl_list[bl_list_count++]=bl;
  935. }
  936. }//end for elements
  937. }
  938. if(type==0 || type==BL_MOB) {
  939. bl = map[m].block_mob[bx+by*map[m].bxs]; // and the mob block
  940. for(i=0;i<c2 && bl;i++,bl=bl->next){
  941. if(bl && bl_list_count<BL_LIST_MAX) {
  942. // check if mob xy is on the line
  943. if( (bl->x-x0)*(y1-y0) == (bl->y-y0)*(x1-x0) )
  944. // and if it is within start and end point
  945. if( (((x0<=x1)&&(x0<=bl->x)&&(bl->x<=x1)) || ((x0>=x1)&&(x0>=bl->x)&&(bl->x>=x1))) &&
  946. (((y0<=y1)&&(y0<=bl->y)&&(bl->y<=y1)) || ((y0>=y1)&&(y0>=bl->y)&&(bl->y>=y1))) )
  947. bl_list[bl_list_count++]=bl;
  948. }
  949. }//end for mobs
  950. }
  951. }
  952. }//end for index
  953. if(bl_list_count>=BL_LIST_MAX) {
  954. if(battle_config.error_log)
  955. printf("map_foreachinarea: *WARNING* block count too many!\n");
  956. }
  957. va_start(ap,type);
  958. map_freeblock_lock(); // メモリからの解放を禁止する
  959. for(i=blockcount;i<bl_list_count;i++)
  960. if(bl_list[i]->prev) // 有?かどうかチェック
  961. func(bl_list[i],ap);
  962. map_freeblock_unlock(); // 解放を許可する
  963. va_end(ap);
  964. bl_list_count = blockcount;
  965. }
  966. /*==========================================
  967. * 床アイテムやエフェクト用の一時obj割り?て
  968. * object[]への保存とid_db登?まで
  969. *
  970. * bl->idもこの中で設定して問題無い?
  971. *------------------------------------------
  972. */
  973. int map_addobject(struct block_list *bl) {
  974. int i;
  975. if( bl == NULL ){
  976. printf("map_addobject nullpo?\n");
  977. return 0;
  978. }
  979. if(first_free_object_id<2 || first_free_object_id>=MAX_FLOORITEM)
  980. first_free_object_id=2;
  981. for(i=first_free_object_id;i<MAX_FLOORITEM;i++)
  982. if(objects[i]==NULL)
  983. break;
  984. if(i>=MAX_FLOORITEM){
  985. if(battle_config.error_log)
  986. printf("no free object id\n");
  987. return 0;
  988. }
  989. first_free_object_id=i;
  990. if(last_object_id<i)
  991. last_object_id=i;
  992. objects[i]=bl;
  993. numdb_insert(id_db,i,bl);
  994. return i;
  995. }
  996. /*==========================================
  997. * 一時objectの解放
  998. * map_delobjectのfreeしないバ?ジョン
  999. *------------------------------------------
  1000. */
  1001. int map_delobjectnofree(int id) {
  1002. if(objects[id]==NULL)
  1003. return 0;
  1004. map_delblock(objects[id]);
  1005. numdb_erase(id_db,id);
  1006. // map_freeblock(objects[id]);
  1007. objects[id]=NULL;
  1008. if(first_free_object_id>id)
  1009. first_free_object_id=id;
  1010. while(last_object_id>2 && objects[last_object_id]==NULL)
  1011. last_object_id--;
  1012. return 0;
  1013. }
  1014. /*==========================================
  1015. * 一時objectの解放
  1016. * block_listからの削除、id_dbからの削除
  1017. * object dataのfree、object[]へのNULL代入
  1018. *
  1019. * addとの??性が無いのが?になる
  1020. *------------------------------------------
  1021. */
  1022. int map_delobject(int id) {
  1023. struct block_list *obj = objects[id];
  1024. if(obj==NULL)
  1025. return 0;
  1026. map_delobjectnofree(id);
  1027. map_freeblock(obj);
  1028. return 0;
  1029. }
  1030. /*==========================================
  1031. * 全一時obj相手にfuncを呼ぶ
  1032. *
  1033. *------------------------------------------
  1034. */
  1035. void map_foreachobject(int (*func)(struct block_list*,va_list),int type,...) {
  1036. int i;
  1037. int blockcount=bl_list_count;
  1038. va_list ap;
  1039. va_start(ap,type);
  1040. for(i=2;i<=last_object_id;i++){
  1041. if(objects[i]){
  1042. if(type && objects[i]->type!=type)
  1043. continue;
  1044. if(bl_list_count>=BL_LIST_MAX) {
  1045. if(battle_config.error_log)
  1046. printf("map_foreachobject: too many block !\n");
  1047. }
  1048. else
  1049. bl_list[bl_list_count++]=objects[i];
  1050. }
  1051. }
  1052. map_freeblock_lock();
  1053. for(i=blockcount;i<bl_list_count;i++)
  1054. if( bl_list[i]->prev || bl_list[i]->next )
  1055. func(bl_list[i],ap);
  1056. map_freeblock_unlock();
  1057. va_end(ap);
  1058. bl_list_count = blockcount;
  1059. }
  1060. /*==========================================
  1061. * 床アイテムを消す
  1062. *
  1063. * data==0の時はtimerで消えた時
  1064. * data!=0の時は拾う等で消えた時として動作
  1065. *
  1066. * 後者は、map_clearflooritem(id)へ
  1067. * map.h?で#defineしてある
  1068. *------------------------------------------
  1069. */
  1070. int map_clearflooritem_timer(int tid,unsigned int tick,int id,int data) {
  1071. struct flooritem_data *fitem=NULL;
  1072. fitem = (struct flooritem_data *)objects[id];
  1073. if(fitem==NULL || fitem->bl.type!=BL_ITEM || (!data && fitem->cleartimer != tid)){
  1074. if(battle_config.error_log)
  1075. printf("map_clearflooritem_timer : error\n");
  1076. return 1;
  1077. }
  1078. if(data)
  1079. delete_timer(fitem->cleartimer,map_clearflooritem_timer);
  1080. else if(fitem->item_data.card[0] == (short)0xff00)
  1081. intif_delete_petdata(*((long *)(&fitem->item_data.card[1])));
  1082. clif_clearflooritem(fitem,0);
  1083. map_delobject(fitem->bl.id);
  1084. return 0;
  1085. }
  1086. /*==========================================
  1087. * (m,x,y)の周?rangeマス?の空き(=侵入可能)cellの
  1088. * ?から適?なマス目の座標をx+(y<<16)で返す
  1089. *
  1090. * 現?range=1でアイテムドロップ用途のみ
  1091. *------------------------------------------
  1092. */
  1093. int map_searchrandfreecell(int m,int x,int y,int range) {
  1094. int free_cell,i,j;
  1095. for(free_cell=0,i=-range;i<=range;i++){
  1096. if(i+y<0 || i+y>=map[m].ys)
  1097. continue;
  1098. for(j=-range;j<=range;j++){
  1099. if(j+x<0 || j+x>=map[m].xs)
  1100. continue;
  1101. if(map_getcell(m,j+x,i+y,CELL_CHKNOPASS))
  1102. continue;
  1103. free_cell++;
  1104. }
  1105. }
  1106. if(free_cell==0)
  1107. return -1;
  1108. free_cell=rand()%free_cell;
  1109. for(i=-range;i<=range;i++){
  1110. if(i+y<0 || i+y>=map[m].ys)
  1111. continue;
  1112. for(j=-range;j<=range;j++){
  1113. if(j+x<0 || j+x>=map[m].xs)
  1114. continue;
  1115. if(map_getcell(m,j+x,i+y,CELL_CHKNOPASS))
  1116. continue;
  1117. if(free_cell==0){
  1118. x+=j;
  1119. y+=i;
  1120. i=range+1;
  1121. break;
  1122. }
  1123. free_cell--;
  1124. }
  1125. }
  1126. return x+(y<<16);
  1127. }
  1128. /*==========================================
  1129. * (m,x,y)を中心に3x3以?に床アイテム設置
  1130. *
  1131. * item_dataはamount以外をcopyする
  1132. *------------------------------------------
  1133. */
  1134. int map_addflooritem(struct item *item_data,int amount,int m,int x,int y,struct map_session_data *first_sd,
  1135. struct map_session_data *second_sd,struct map_session_data *third_sd,int type) {
  1136. int xy,r;
  1137. unsigned int tick;
  1138. struct flooritem_data *fitem=NULL;
  1139. nullpo_retr(0, item_data);
  1140. if((xy=map_searchrandfreecell(m,x,y,1))<0)
  1141. return 0;
  1142. r=rand();
  1143. fitem = (struct flooritem_data *)aCalloc(1,sizeof(*fitem));
  1144. fitem->bl.type=BL_ITEM;
  1145. fitem->bl.prev = fitem->bl.next = NULL;
  1146. fitem->bl.m=m;
  1147. fitem->bl.x=xy&0xffff;
  1148. fitem->bl.y=(xy>>16)&0xffff;
  1149. fitem->first_get_id = 0;
  1150. fitem->first_get_tick = 0;
  1151. fitem->second_get_id = 0;
  1152. fitem->second_get_tick = 0;
  1153. fitem->third_get_id = 0;
  1154. fitem->third_get_tick = 0;
  1155. fitem->bl.id = map_addobject(&fitem->bl);
  1156. if(fitem->bl.id==0){
  1157. aFree(fitem);
  1158. return 0;
  1159. }
  1160. tick = gettick();
  1161. if(first_sd) {
  1162. fitem->first_get_id = first_sd->bl.id;
  1163. if(type)
  1164. fitem->first_get_tick = tick + battle_config.mvp_item_first_get_time;
  1165. else
  1166. fitem->first_get_tick = tick + battle_config.item_first_get_time;
  1167. }
  1168. if(second_sd) {
  1169. fitem->second_get_id = second_sd->bl.id;
  1170. if(type)
  1171. fitem->second_get_tick = tick + battle_config.mvp_item_first_get_time + battle_config.mvp_item_second_get_time;
  1172. else
  1173. fitem->second_get_tick = tick + battle_config.item_first_get_time + battle_config.item_second_get_time;
  1174. }
  1175. if(third_sd) {
  1176. fitem->third_get_id = third_sd->bl.id;
  1177. if(type)
  1178. 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;
  1179. else
  1180. fitem->third_get_tick = tick + battle_config.item_first_get_time + battle_config.item_second_get_time + battle_config.item_third_get_time;
  1181. }
  1182. memcpy(&fitem->item_data,item_data,sizeof(*item_data));
  1183. fitem->item_data.amount=amount;
  1184. fitem->subx=(r&3)*3+3;
  1185. fitem->suby=((r>>2)&3)*3+3;
  1186. fitem->cleartimer=add_timer(gettick()+battle_config.flooritem_lifetime,map_clearflooritem_timer,fitem->bl.id,0);
  1187. map_addblock(&fitem->bl);
  1188. clif_dropflooritem(fitem);
  1189. return fitem->bl.id;
  1190. }
  1191. /*==========================================
  1192. * charid_dbへ追加(返信待ちがあれば返信)
  1193. *------------------------------------------
  1194. */
  1195. void map_addchariddb(int charid, char *name) {
  1196. struct charid2nick *p=NULL;
  1197. int req=0;
  1198. p = (struct charid2nick*)numdb_search(charid_db,charid);
  1199. if(p==NULL){ // デ?タベ?スにない
  1200. p = (struct charid2nick *)aCallocA(1,sizeof(struct charid2nick));
  1201. p->req_id=0;
  1202. }else
  1203. numdb_erase(charid_db,charid);
  1204. req=p->req_id;
  1205. memcpy(p->nick,name,24);
  1206. p->req_id=0;
  1207. numdb_insert(charid_db,charid,p);
  1208. if(req){ // 返信待ちがあれば返信
  1209. struct map_session_data *sd = map_id2sd(req);
  1210. if(sd!=NULL)
  1211. clif_solved_charname(sd,charid);
  1212. }
  1213. }
  1214. /*==========================================
  1215. * charid_dbへ追加(返信要求のみ)
  1216. *------------------------------------------
  1217. */
  1218. int map_reqchariddb(struct map_session_data * sd,int charid) {
  1219. struct charid2nick *p=NULL;
  1220. nullpo_retr(0, sd);
  1221. p = (struct charid2nick*)numdb_search(charid_db,charid);
  1222. if(p!=NULL) // デ?タベ?スにすでにある
  1223. return 0;
  1224. p = (struct charid2nick *)aCalloc(1,sizeof(struct charid2nick));
  1225. p->req_id=sd->bl.id;
  1226. numdb_insert(charid_db,charid,p);
  1227. return 0;
  1228. }
  1229. /*==========================================
  1230. * id_dbへblを追加
  1231. *------------------------------------------
  1232. */
  1233. void map_addiddb(struct block_list *bl) {
  1234. nullpo_retv(bl);
  1235. numdb_insert(id_db,bl->id,bl);
  1236. }
  1237. /*==========================================
  1238. * id_dbからblを削除
  1239. *------------------------------------------
  1240. */
  1241. void map_deliddb(struct block_list *bl) {
  1242. nullpo_retv(bl);
  1243. numdb_erase(id_db,bl->id);
  1244. }
  1245. /*==========================================
  1246. * nick_dbへsdを追加
  1247. *------------------------------------------
  1248. */
  1249. void map_addnickdb(struct map_session_data *sd) {
  1250. nullpo_retv(sd);
  1251. strdb_insert(nick_db,sd->status.name,sd);
  1252. }
  1253. /*==========================================
  1254. * PCのquit?理 map.c?分
  1255. *
  1256. * quit?理の主?が違うような?もしてきた
  1257. *------------------------------------------
  1258. */
  1259. int map_quit(struct map_session_data *sd) {
  1260. nullpo_retr(0, sd);
  1261. if(!sd->state.waitingdisconnect) {
  1262. if (sd->state.event_disconnect) {
  1263. struct npc_data *npc;
  1264. if ((npc = npc_name2id(script_config.logout_event_name))) {
  1265. run_script(npc->u.scr.script,0,sd->bl.id,npc->bl.id); // PCLogoutNPC
  1266. sprintf (tmp_output, "Event '"CL_WHITE"%s"CL_RESET"' executed.\n", script_config.logout_event_name);
  1267. ShowStatus(tmp_output);
  1268. }
  1269. }
  1270. if(sd->chatID) // チャットから出る
  1271. chat_leavechat(sd);
  1272. if(sd->trade_partner) // 取引を中?する
  1273. trade_tradecancel(sd);
  1274. if(sd->party_invite>0) // パ?ティ?誘を拒否する
  1275. party_reply_invite(sd,sd->party_invite_account,0);
  1276. if(sd->guild_invite>0) // ギルド?誘を拒否する
  1277. guild_reply_invite(sd,sd->guild_invite,0);
  1278. if(sd->guild_alliance>0) // ギルド同盟?誘を拒否する
  1279. guild_reply_reqalliance(sd,sd->guild_alliance_account,0);
  1280. party_send_logout(sd); // パ?ティのログアウトメッセ?ジ送信
  1281. guild_send_memberinfoshort(sd,0); // ギルドのログアウトメッセ?ジ送信
  1282. pc_cleareventtimer(sd); // イベントタイマを破棄する
  1283. if(sd->state.storage_flag)
  1284. storage_guild_storage_quit(sd,0);
  1285. else
  1286. storage_storage_quit(sd); // 倉庫を開いてるなら保存する
  1287. // check if we've been authenticated [celest]
  1288. if (sd->state.auth)
  1289. skill_castcancel(&sd->bl,0); // 詠唱を中?する
  1290. skill_stop_dancing(&sd->bl,1);// ダンス/演奏中?
  1291. if(sd->sc_data && sd->sc_data[SC_BERSERK].timer!=-1) //バ?サ?ク中の終了はHPを100に
  1292. sd->status.hp = 100;
  1293. status_change_clear(&sd->bl,1); // ステ?タス異常を解除する
  1294. skill_clear_unitgroup(&sd->bl); // スキルユニットグル?プの削除
  1295. skill_cleartimerskill(&sd->bl);
  1296. // check if we've been authenticated [celest]
  1297. if (sd->state.auth) {
  1298. pc_stop_walking(sd,0);
  1299. pc_stopattack(sd);
  1300. pc_delinvincibletimer(sd);
  1301. }
  1302. pc_delspiritball(sd,sd->spiritball,1);
  1303. skill_gangsterparadise(sd,0);
  1304. skill_unit_move(&sd->bl,gettick(),0);
  1305. if (sd->state.auth)
  1306. status_calc_pc(sd,4);
  1307. // skill_clear_unitgroup(&sd->bl); // [Sara-chan]
  1308. clif_clearchar_area(&sd->bl,2);
  1309. if(sd->status.pet_id && sd->pd) {
  1310. pet_lootitem_drop(sd->pd,sd);
  1311. pet_remove_map(sd);
  1312. if(sd->pet.intimate <= 0) {
  1313. intif_delete_petdata(sd->status.pet_id);
  1314. sd->status.pet_id = 0;
  1315. sd->pd = NULL;
  1316. sd->petDB = NULL;
  1317. }
  1318. else
  1319. intif_save_petdata(sd->status.account_id,&sd->pet);
  1320. }
  1321. if(pc_isdead(sd))
  1322. pc_setrestartvalue(sd,2);
  1323. pc_makesavestatus(sd);
  1324. chrif_save(sd);
  1325. storage_storage_dirty(sd);
  1326. storage_storage_save(sd);
  1327. map_delblock(&sd->bl);
  1328. }
  1329. if( sd->npc_stackbuf && sd->npc_stackbuf != NULL) {
  1330. aFree( sd->npc_stackbuf );
  1331. sd->npc_stackbuf = NULL;
  1332. }
  1333. #ifndef TXT_ONLY
  1334. chrif_char_offline(sd);
  1335. #endif
  1336. {
  1337. void *p = numdb_search(charid_db,sd->status.char_id);
  1338. if(p) {
  1339. numdb_erase(charid_db,sd->status.char_id);
  1340. aFree(p);
  1341. }
  1342. }
  1343. strdb_erase(nick_db,sd->status.name);
  1344. numdb_erase(charid_db,sd->status.char_id);
  1345. numdb_erase(id_db,sd->bl.id);
  1346. aFree(sd->reg);
  1347. aFree(sd->regstr);
  1348. return 0;
  1349. }
  1350. /*==========================================
  1351. * id番?のPCを探す。居なければNULL
  1352. *------------------------------------------
  1353. */
  1354. struct map_session_data * map_id2sd(int id) {
  1355. // remove search from db, because:
  1356. // 1 - all players, npc, items and mob are in this db (to search, it's not speed, and search in session is more sure)
  1357. // 2 - DB seems not always correct. Sometimes, when a player disconnects, its id (account value) is not removed and structure
  1358. // point to a memory area that is not more a session_data and value are incorrect (or out of available memory) -> crash
  1359. // replaced by searching in all session.
  1360. // by searching in session, we are sure that fd, session, and account exist.
  1361. /*
  1362. struct block_list *bl;
  1363. bl=numdb_search(id_db,id);
  1364. if(bl && bl->type==BL_PC)
  1365. return (struct map_session_data*)bl;
  1366. return NULL;
  1367. */
  1368. int i;
  1369. struct map_session_data *sd;
  1370. if (id <= 0) return 0;
  1371. for(i = 0; i < fd_max; i++)
  1372. if (session[i] && (sd = (struct map_session_data*)session[i]->session_data) && sd->bl.id == id)
  1373. return sd;
  1374. return NULL;
  1375. }
  1376. /*==========================================
  1377. * char_id番?の名前を探す
  1378. *------------------------------------------
  1379. */
  1380. char * map_charid2nick(int id) {
  1381. struct charid2nick *p = (struct charid2nick*)numdb_search(charid_db,id);
  1382. if(p==NULL)
  1383. return NULL;
  1384. if(p->req_id!=0)
  1385. return NULL;
  1386. return p->nick;
  1387. }
  1388. /*==========================================
  1389. * Search session data from a nick name
  1390. * (without sensitive case if necessary)
  1391. * return map_session_data pointer or NULL
  1392. *------------------------------------------
  1393. */
  1394. struct map_session_data * map_nick2sd(char *nick) {
  1395. int i, quantity=0, nicklen;
  1396. struct map_session_data *sd = NULL;
  1397. struct map_session_data *pl_sd = NULL;
  1398. if (nick == NULL)
  1399. return NULL;
  1400. nicklen = strlen(nick);
  1401. for (i = 0; i < fd_max; i++) {
  1402. if (session[i] && (pl_sd = (struct map_session_data*)session[i]->session_data) && pl_sd->state.auth)
  1403. // Without case sensitive check (increase the number of similar character names found)
  1404. if (strnicmp(pl_sd->status.name, nick, nicklen) == 0) {
  1405. // Strict comparison (if found, we finish the function immediatly with correct value)
  1406. if (strcmp(pl_sd->status.name, nick) == 0)
  1407. return pl_sd;
  1408. quantity++;
  1409. sd = pl_sd;
  1410. }
  1411. }
  1412. // Here, the exact character name is not found
  1413. // We return the found index of a similar account ONLY if there is 1 similar character
  1414. if (quantity == 1)
  1415. return sd;
  1416. // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found
  1417. return NULL;
  1418. }
  1419. /*==========================================
  1420. * id番?の物を探す
  1421. * 一時objectの場合は配列を引くのみ
  1422. *------------------------------------------
  1423. */
  1424. struct block_list * map_id2bl(int id)
  1425. {
  1426. struct block_list *bl=NULL;
  1427. if(id >= 0 && id < sizeof(objects)/sizeof(objects[0]))
  1428. bl = objects[id];
  1429. else
  1430. bl = (struct block_list*)numdb_search(id_db,id);
  1431. return bl;
  1432. }
  1433. /*==========================================
  1434. * id_db?の全てにfuncを?行
  1435. *------------------------------------------
  1436. */
  1437. int map_foreachiddb(int (*func)(void*,void*,va_list),...) {
  1438. va_list ap;
  1439. va_start(ap,func);
  1440. numdb_foreach(id_db,func,ap);
  1441. va_end(ap);
  1442. return 0;
  1443. }
  1444. /*==========================================
  1445. * map.npcへ追加 (warp等の領域持ちのみ)
  1446. *------------------------------------------
  1447. */
  1448. int map_addnpc(int m,struct npc_data *nd) {
  1449. int i;
  1450. if(m<0 || m>=map_num)
  1451. return -1;
  1452. for(i=0;i<map[m].npc_num && i<MAX_NPC_PER_MAP;i++)
  1453. if(map[m].npc[i]==NULL)
  1454. break;
  1455. if(i==MAX_NPC_PER_MAP){
  1456. if(battle_config.error_log)
  1457. printf("too many NPCs in one map %s\n",map[m].name);
  1458. return -1;
  1459. }
  1460. if(i==map[m].npc_num){
  1461. map[m].npc_num++;
  1462. }
  1463. nullpo_retr(0, nd);
  1464. map[m].npc[i]=nd;
  1465. nd->n = i;
  1466. numdb_insert(id_db,nd->bl.id,nd);
  1467. return i;
  1468. }
  1469. void map_removenpc(void) {
  1470. int i,m,n=0;
  1471. for(m=0;m<map_num;m++) {
  1472. for(i=0;i<map[m].npc_num && i<MAX_NPC_PER_MAP;i++) {
  1473. if(map[m].npc[i]!=NULL) {
  1474. clif_clearchar_area(&map[m].npc[i]->bl,2);
  1475. map_delblock(&map[m].npc[i]->bl);
  1476. numdb_erase(id_db,map[m].npc[i]->bl.id);
  1477. if(map[m].npc[i]->bl.subtype==SCRIPT) {
  1478. // aFree(map[m].npc[i]->u.scr.script);
  1479. // aFree(map[m].npc[i]->u.scr.label_list);
  1480. }
  1481. aFree(map[m].npc[i]);
  1482. map[m].npc[i] = NULL;
  1483. n++;
  1484. }
  1485. }
  1486. }
  1487. sprintf(tmp_output,"Successfully removed and freed from memory '"CL_WHITE"%d"CL_RESET"' NPCs.\n",n);
  1488. ShowStatus(tmp_output);
  1489. }
  1490. /*==========================================
  1491. * map名からmap番?へ?換
  1492. *------------------------------------------
  1493. */
  1494. int map_mapname2mapid(char *name) {
  1495. struct map_data *md=NULL;
  1496. md = (struct map_data*)strdb_search(map_db,name);
  1497. #ifdef USE_AFM
  1498. // If we can't find the .gat map try .afm instead [celest]
  1499. if(md==NULL && strstr(name,".gat")) {
  1500. char *afm_name = strdup(name);
  1501. strcpy(&afm_name[strlen(name) - 3], "afm");
  1502. md = (struct map_data*)strdb_search(map_db,afm_name);
  1503. }
  1504. #endif
  1505. if(md==NULL || md->gat==NULL)
  1506. return -1;
  1507. return md->m;
  1508. }
  1509. /*==========================================
  1510. * 他鯖map名からip,port?換
  1511. *------------------------------------------
  1512. */
  1513. int map_mapname2ipport(char *name,int *ip,int *port) {
  1514. struct map_data_other_server *mdos=NULL;
  1515. mdos = (struct map_data_other_server*)strdb_search(map_db,name);
  1516. if(mdos==NULL || mdos->gat)
  1517. return -1;
  1518. *ip=mdos->ip;
  1519. *port=mdos->port;
  1520. return 0;
  1521. }
  1522. /*==========================================
  1523. *
  1524. *------------------------------------------
  1525. */
  1526. int map_check_dir(int s_dir,int t_dir) {
  1527. if(s_dir == t_dir)
  1528. return 0;
  1529. switch(s_dir) {
  1530. case 0:
  1531. if(t_dir == 7 || t_dir == 1 || t_dir == 0)
  1532. return 0;
  1533. break;
  1534. case 1:
  1535. if(t_dir == 0 || t_dir == 2 || t_dir == 1)
  1536. return 0;
  1537. break;
  1538. case 2:
  1539. if(t_dir == 1 || t_dir == 3 || t_dir == 2)
  1540. return 0;
  1541. break;
  1542. case 3:
  1543. if(t_dir == 2 || t_dir == 4 || t_dir == 3)
  1544. return 0;
  1545. break;
  1546. case 4:
  1547. if(t_dir == 3 || t_dir == 5 || t_dir == 4)
  1548. return 0;
  1549. break;
  1550. case 5:
  1551. if(t_dir == 4 || t_dir == 6 || t_dir == 5)
  1552. return 0;
  1553. break;
  1554. case 6:
  1555. if(t_dir == 5 || t_dir == 7 || t_dir == 6)
  1556. return 0;
  1557. break;
  1558. case 7:
  1559. if(t_dir == 6 || t_dir == 0 || t_dir == 7)
  1560. return 0;
  1561. break;
  1562. }
  1563. return 1;
  1564. }
  1565. /*==========================================
  1566. * 彼我の方向を計算
  1567. *------------------------------------------
  1568. */
  1569. int map_calc_dir( struct block_list *src,int x,int y) {
  1570. int dir=0;
  1571. int dx,dy;
  1572. nullpo_retr(0, src);
  1573. dx=x-src->x;
  1574. dy=y-src->y;
  1575. if( dx==0 && dy==0 ){ // 彼我の場所一致
  1576. dir=0; // 上
  1577. }else if( dx>=0 && dy>=0 ){ // 方向的に右上
  1578. dir=7; // 右上
  1579. if( dx*3-1<dy ) dir=0; // 上
  1580. if( dx>dy*3 ) dir=6; // 右
  1581. }else if( dx>=0 && dy<=0 ){ // 方向的に右下
  1582. dir=5; // 右下
  1583. if( dx*3-1<-dy ) dir=4; // 下
  1584. if( dx>-dy*3 ) dir=6; // 右
  1585. }else if( dx<=0 && dy<=0 ){ // 方向的に左下
  1586. dir=3; // 左下
  1587. if( dx*3+1>dy ) dir=4; // 下
  1588. if( dx<dy*3 ) dir=2; // 左
  1589. }else{ // 方向的に左上
  1590. dir=1; // 左上
  1591. if( -dx*3-1<dy ) dir=0; // 上
  1592. if( -dx>dy*3 ) dir=2; // 左
  1593. }
  1594. return dir;
  1595. }
  1596. // gat系
  1597. /*==========================================
  1598. * (m,x,y)の状態を調べる
  1599. *------------------------------------------
  1600. */
  1601. int map_getcell(int m,int x,int y,cell_t cellchk)
  1602. {
  1603. return (m < 0 || m > MAX_MAP_PER_SERVER) ? 0 : map_getcellp(&map[m],x,y,cellchk);
  1604. }
  1605. int map_getcellp(struct map_data* m,int x,int y,cell_t cellchk)
  1606. {
  1607. int type;
  1608. nullpo_ret(m);
  1609. if(x<0 || x>=m->xs-1 || y<0 || y>=m->ys-1)
  1610. {
  1611. if(cellchk==CELL_CHKNOPASS) return 1;
  1612. return 0;
  1613. }
  1614. type = m->gat[x+y*m->xs];
  1615. if (cellchk<0x10)
  1616. type &= CELL_MASK;
  1617. switch(cellchk)
  1618. {
  1619. case CELL_CHKPASS:
  1620. return (type!=1 && type!=5);
  1621. case CELL_CHKNOPASS:
  1622. return (type==1 || type==5);
  1623. case CELL_CHKWALL:
  1624. return (type==1);
  1625. case CELL_CHKWATER:
  1626. return (type==3);
  1627. case CELL_CHKGROUND:
  1628. return (type==5);
  1629. case CELL_GETTYPE:
  1630. return type;
  1631. case CELL_CHKNPC:
  1632. return (type&CELL_NPC);
  1633. case CELL_CHKBASILICA:
  1634. return (type&CELL_BASILICA);
  1635. default:
  1636. return 0;
  1637. }
  1638. }
  1639. /*==========================================
  1640. * (m,x,y)の状態を設定する
  1641. *------------------------------------------
  1642. */
  1643. void map_setcell(int m,int x,int y,int cell)
  1644. {
  1645. int j;
  1646. if(x<0 || x>=map[m].xs || y<0 || y>=map[m].ys)
  1647. return;
  1648. j=x+y*map[m].xs;
  1649. switch (cell) {
  1650. case CELL_SETNPC:
  1651. map[m].gat[j] |= CELL_NPC;
  1652. break;
  1653. case CELL_SETBASILICA:
  1654. map[m].gat[j] |= CELL_BASILICA;
  1655. break;
  1656. case CELL_CLRBASILICA:
  1657. map[m].gat[j] &= ~CELL_BASILICA;
  1658. break;
  1659. default:
  1660. map[m].gat[j] = (map[m].gat[j]&~CELL_MASK) + cell;
  1661. break;
  1662. }
  1663. }
  1664. /*==========================================
  1665. * 他鯖管理のマップをdbに追加
  1666. *------------------------------------------
  1667. */
  1668. int map_setipport(char *name,unsigned long ip,int port) {
  1669. struct map_data *md=NULL;
  1670. struct map_data_other_server *mdos=NULL;
  1671. md = (struct map_data*)strdb_search(map_db,name);
  1672. if(md==NULL){ // not exist -> add new data
  1673. mdos=(struct map_data_other_server *)aCalloc(1,sizeof(struct map_data_other_server));
  1674. memcpy(mdos->name,name,24);
  1675. mdos->gat = NULL;
  1676. mdos->ip = ip;
  1677. mdos->port = port;
  1678. mdos->map = NULL;
  1679. strdb_insert(map_db,mdos->name,mdos);
  1680. } else if(md->gat){
  1681. if(ip!=clif_getip() || port!=clif_getport()){
  1682. // 読み込んでいたけど、担当外になったマップ
  1683. mdos=(struct map_data_other_server *)aCalloc(1,sizeof(struct map_data_other_server));
  1684. memcpy(mdos->name,name,24);
  1685. mdos->gat = NULL;
  1686. mdos->ip = ip;
  1687. mdos->port = port;
  1688. mdos->map = md;
  1689. strdb_insert(map_db,mdos->name,mdos);
  1690. // printf("from char server : %s -> %08lx:%d\n",name,ip,port);
  1691. } else {
  1692. // 読み込んでいて、担当になったマップ(何もしない)
  1693. ;
  1694. }
  1695. } else {
  1696. mdos=(struct map_data_other_server *)md;
  1697. if(ip == clif_getip() && port == clif_getport()) {
  1698. // 自分の担当になったマップ
  1699. if(mdos->map == NULL) {
  1700. // 読み込んでいないので終了する
  1701. printf("map_setipport : %s is not loaded.\n",name);
  1702. exit(1);
  1703. } else {
  1704. // 読み込んでいるので置き換える
  1705. md = mdos->map;
  1706. aFree(mdos);
  1707. strdb_insert(map_db,md->name,md);
  1708. }
  1709. } else {
  1710. // 他の鯖の担当マップなので置き換えるだけ
  1711. mdos->ip = ip;
  1712. mdos->port = port;
  1713. }
  1714. }
  1715. return 0;
  1716. }
  1717. /*==========================================
  1718. * 他鯖管理のマップを全て削除
  1719. *------------------------------------------
  1720. */
  1721. int map_eraseallipport_sub(void *key,void *data,va_list va) {
  1722. struct map_data_other_server *mdos = (struct map_data_other_server*)data;
  1723. if(mdos->gat == NULL && mdos->map == NULL) {
  1724. strdb_erase(map_db,key);
  1725. aFree(mdos);
  1726. }
  1727. return 0;
  1728. }
  1729. int map_eraseallipport(void) {
  1730. strdb_foreach(map_db,map_eraseallipport_sub);
  1731. return 1;
  1732. }
  1733. /*==========================================
  1734. * 他鯖管理のマップをdbから削除
  1735. *------------------------------------------
  1736. */
  1737. int map_eraseipport(char *name,unsigned long ip,int port)
  1738. {
  1739. struct map_data *md;
  1740. struct map_data_other_server *mdos;
  1741. // unsigned char *p=(unsigned char *)&ip;
  1742. md=strdb_search(map_db,name);
  1743. if(md){
  1744. if(md->gat) // local -> check data
  1745. return 0;
  1746. else {
  1747. mdos=(struct map_data_other_server *)md;
  1748. if(mdos->ip==ip && mdos->port == port) {
  1749. if(mdos->map) {
  1750. // このマップ鯖でも読み込んでいるので移動できる
  1751. return 1; // 呼び出し元で chrif_sendmap() をする
  1752. } else {
  1753. strdb_erase(map_db,name);
  1754. aFree(mdos);
  1755. }
  1756. // if(battle_config.etc_log)
  1757. // printf("erase map %s %d.%d.%d.%d:%d\n",name,p[0],p[1],p[2],p[3],port);
  1758. }
  1759. }
  1760. }
  1761. return 0;
  1762. }
  1763. // 初期化周り
  1764. /*==========================================
  1765. * 水場高さ設定
  1766. *------------------------------------------
  1767. */
  1768. static struct waterlist_ {
  1769. char mapname[24];
  1770. int waterheight;
  1771. } *waterlist=NULL;
  1772. #define NO_WATER 1000000
  1773. static int map_waterheight(char *mapname) {
  1774. if(waterlist){
  1775. int i;
  1776. for(i=0;waterlist[i].mapname[0] && i < MAX_MAP_PER_SERVER;i++)
  1777. if(strcmp(waterlist[i].mapname,mapname)==0)
  1778. return waterlist[i].waterheight;
  1779. }
  1780. return NO_WATER;
  1781. }
  1782. static void map_readwater(char *watertxt) {
  1783. char line[1024],w1[1024];
  1784. FILE *fp=NULL;
  1785. int n=0;
  1786. fp=fopen(watertxt,"r");
  1787. if(fp==NULL){
  1788. printf("file not found: %s\n",watertxt);
  1789. return;
  1790. }
  1791. if(waterlist==NULL)
  1792. waterlist = (struct waterlist_*)aCallocA(MAX_MAP_PER_SERVER,sizeof(*waterlist));
  1793. while(fgets(line,1020,fp) && n < MAX_MAP_PER_SERVER){
  1794. int wh,count;
  1795. if(line[0] == '/' && line[1] == '/')
  1796. continue;
  1797. if((count=sscanf(line,"%s%d",w1,&wh)) < 1){
  1798. continue;
  1799. }
  1800. strcpy(waterlist[n].mapname,w1);
  1801. if(count >= 2)
  1802. waterlist[n].waterheight = wh;
  1803. else
  1804. waterlist[n].waterheight = 3;
  1805. n++;
  1806. }
  1807. fclose(fp);
  1808. }
  1809. /*==========================================
  1810. * マップキャッシュに追加する
  1811. *===========================================*/
  1812. // マップキャッシュの最大値
  1813. #define MAX_MAP_CACHE 768
  1814. //各マップごとの最小限情報を入れるもの、READ_FROM_BITMAP用
  1815. struct map_cache_info {
  1816. char fn[32];//ファイル名
  1817. int xs,ys; //幅と高さ
  1818. int water_height;
  1819. int pos; // データが入れてある場所
  1820. int compressed; // zilb通せるようにする為の予約
  1821. int compressed_len; // zilb通せるようにする為の予約
  1822. }; // 56 byte
  1823. struct map_cache_head {
  1824. int sizeof_header;
  1825. int sizeof_map;
  1826. // 上の2つ改変不可
  1827. int nmaps; // マップの個数
  1828. int filesize;
  1829. };
  1830. struct {
  1831. struct map_cache_head head;
  1832. struct map_cache_info *map;
  1833. FILE *fp;
  1834. int dirty;
  1835. } map_cache;
  1836. static int map_cache_open(char *fn);
  1837. static void map_cache_close(void);
  1838. static int map_cache_read(struct map_data *m);
  1839. static int map_cache_write(struct map_data *m);
  1840. static int map_cache_open(char *fn)
  1841. {
  1842. atexit(map_cache_close);
  1843. if(map_cache.fp) {
  1844. map_cache_close();
  1845. }
  1846. map_cache.fp = fopen(fn,"r+b");
  1847. if(map_cache.fp) {
  1848. fread(&map_cache.head,1,sizeof(struct map_cache_head),map_cache.fp);
  1849. fseek(map_cache.fp,0,SEEK_END);
  1850. if(
  1851. map_cache.head.sizeof_header == sizeof(struct map_cache_head) &&
  1852. map_cache.head.sizeof_map == sizeof(struct map_cache_info) &&
  1853. map_cache.head.nmaps == MAX_MAP_CACHE &&
  1854. map_cache.head.filesize == ftell(map_cache.fp)
  1855. ) {
  1856. // キャッシュ読み込み成功
  1857. map_cache.map = (struct map_cache_info *) aMalloc(sizeof(struct map_cache_info) * map_cache.head.nmaps);
  1858. fseek(map_cache.fp,sizeof(struct map_cache_head),SEEK_SET);
  1859. fread(map_cache.map,sizeof(struct map_cache_info),map_cache.head.nmaps,map_cache.fp);
  1860. return 1;
  1861. }
  1862. fclose(map_cache.fp);
  1863. }
  1864. // 読み込みに失敗したので新規に作成する
  1865. map_cache.fp = fopen(fn,"wb");
  1866. if(map_cache.fp) {
  1867. memset(&map_cache.head,0,sizeof(struct map_cache_head));
  1868. map_cache.map = (struct map_cache_info *) aCalloc(sizeof(struct map_cache_info),MAX_MAP_CACHE);
  1869. map_cache.head.nmaps = MAX_MAP_CACHE;
  1870. map_cache.head.sizeof_header = sizeof(struct map_cache_head);
  1871. map_cache.head.sizeof_map = sizeof(struct map_cache_info);
  1872. map_cache.head.filesize = sizeof(struct map_cache_head);
  1873. map_cache.head.filesize += sizeof(struct map_cache_info) * map_cache.head.nmaps;
  1874. map_cache.dirty = 1;
  1875. return 1;
  1876. }
  1877. return 0;
  1878. }
  1879. static void map_cache_close(void)
  1880. {
  1881. if(!map_cache.fp) { return; }
  1882. if(map_cache.dirty) {
  1883. fseek(map_cache.fp,0,SEEK_SET);
  1884. fwrite(&map_cache.head,1,sizeof(struct map_cache_head),map_cache.fp);
  1885. fwrite(map_cache.map,map_cache.head.nmaps,sizeof(struct map_cache_info),map_cache.fp);
  1886. }
  1887. fclose(map_cache.fp);
  1888. aFree(map_cache.map);
  1889. map_cache.fp = NULL;
  1890. return;
  1891. }
  1892. int map_cache_read(struct map_data *m)
  1893. {
  1894. int i;
  1895. if(!map_cache.fp) { return 0; }
  1896. for(i = 0;i < map_cache.head.nmaps ; i++) {
  1897. if(!strcmp(m->name,map_cache.map[i].fn)) {
  1898. if(map_cache.map[i].water_height != map_waterheight(m->name)) {
  1899. // 水場の高さが違うので読み直し
  1900. return 0;
  1901. } else if(map_cache.map[i].compressed == 0) {
  1902. // 非圧縮ファイル
  1903. int size = map_cache.map[i].xs * map_cache.map[i].ys;
  1904. m->xs = map_cache.map[i].xs;
  1905. m->ys = map_cache.map[i].ys;
  1906. m->gat = (unsigned char *)aCalloc(m->xs * m->ys,sizeof(unsigned char));
  1907. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  1908. if(fread(m->gat,1,size,map_cache.fp) == size) {
  1909. // 成功
  1910. return 1;
  1911. } else {
  1912. // なぜかファイル後半が欠けてるので読み直し
  1913. m->xs = 0; m->ys = 0; m->gat = NULL; aFree(m->gat);
  1914. return 0;
  1915. }
  1916. } else if(map_cache.map[i].compressed == 1) {
  1917. // 圧縮フラグ=1 : zlib
  1918. unsigned char *buf;
  1919. unsigned long dest_len;
  1920. int size_compress = map_cache.map[i].compressed_len;
  1921. m->xs = map_cache.map[i].xs;
  1922. m->ys = map_cache.map[i].ys;
  1923. m->gat = (unsigned char *)aMalloc(m->xs * m->ys * sizeof(unsigned char));
  1924. buf = (unsigned char*)aMalloc(size_compress);
  1925. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  1926. if(fread(buf,1,size_compress,map_cache.fp) != size_compress) {
  1927. // なぜかファイル後半が欠けてるので読み直し
  1928. printf("fread error\n");
  1929. aFree(m->gat); m->xs = 0; m->ys = 0; m->gat = NULL;
  1930. aFree(buf);
  1931. return 0;
  1932. }
  1933. dest_len = m->xs * m->ys;
  1934. decode_zip(m->gat,&dest_len,buf,size_compress);
  1935. if(dest_len != map_cache.map[i].xs * map_cache.map[i].ys) {
  1936. // 正常に解凍が出来てない
  1937. aFree(m->gat); m->xs = 0; m->ys = 0; m->gat = NULL;
  1938. aFree(buf);
  1939. return 0;
  1940. }
  1941. aFree(buf);
  1942. return 1;
  1943. }
  1944. }
  1945. }
  1946. return 0;
  1947. }
  1948. static int map_cache_write(struct map_data *m)
  1949. {
  1950. int i;
  1951. unsigned long len_new , len_old;
  1952. char *write_buf;
  1953. if(!map_cache.fp) { return 0; }
  1954. for(i = 0;i < map_cache.head.nmaps ; i++) {
  1955. if(!strcmp(m->name,map_cache.map[i].fn)) {
  1956. // 同じエントリーがあれば上書き
  1957. if(map_cache.map[i].compressed == 0) {
  1958. len_old = map_cache.map[i].xs * map_cache.map[i].ys;
  1959. } else if(map_cache.map[i].compressed == 1) {
  1960. len_old = map_cache.map[i].compressed_len;
  1961. } else {
  1962. // サポートされてない形式なので長さ0
  1963. len_old = 0;
  1964. }
  1965. if(map_read_flag == 2) {
  1966. // 圧縮保存
  1967. // さすがに2倍に膨れる事はないという事で
  1968. write_buf = (char *) aMalloc(m->xs * m->ys * 2);
  1969. len_new = m->xs * m->ys * 2;
  1970. encode_zip((unsigned char *) write_buf,&len_new,m->gat,m->xs * m->ys);
  1971. map_cache.map[i].compressed = 1;
  1972. map_cache.map[i].compressed_len = len_new;
  1973. } else {
  1974. len_new = m->xs * m->ys;
  1975. write_buf = (char *) m->gat;
  1976. map_cache.map[i].compressed = 0;
  1977. map_cache.map[i].compressed_len = 0;
  1978. }
  1979. if(len_new <= len_old) {
  1980. // サイズが同じか小さくなったので場所は変わらない
  1981. fseek(map_cache.fp,map_cache.map[i].pos,SEEK_SET);
  1982. fwrite(write_buf,1,len_new,map_cache.fp);
  1983. } else {
  1984. // 新しい場所に登録
  1985. fseek(map_cache.fp,map_cache.head.filesize,SEEK_SET);
  1986. fwrite(write_buf,1,len_new,map_cache.fp);
  1987. map_cache.map[i].pos = map_cache.head.filesize;
  1988. map_cache.head.filesize += len_new;
  1989. }
  1990. map_cache.map[i].xs = m->xs;
  1991. map_cache.map[i].ys = m->ys;
  1992. map_cache.map[i].water_height = map_waterheight(m->name);
  1993. map_cache.dirty = 1;
  1994. if(map_read_flag == 2) {
  1995. aFree(write_buf);
  1996. }
  1997. return 0;
  1998. }
  1999. }
  2000. // 同じエントリが無ければ書き込める場所を探す
  2001. for(i = 0;i < map_cache.head.nmaps ; i++) {
  2002. if(map_cache.map[i].fn[0] == 0) {
  2003. // 新しい場所に登録
  2004. if(map_read_flag == 2) {
  2005. write_buf = (char *) aMalloc(m->xs * m->ys * 2);
  2006. len_new = m->xs * m->ys * 2;
  2007. encode_zip((unsigned char *) write_buf,&len_new,m->gat,m->xs * m->ys);
  2008. map_cache.map[i].compressed = 1;
  2009. map_cache.map[i].compressed_len = len_new;
  2010. } else {
  2011. len_new = m->xs * m->ys;
  2012. write_buf = (char *) m->gat;
  2013. map_cache.map[i].compressed = 0;
  2014. map_cache.map[i].compressed_len = 0;
  2015. }
  2016. strncpy(map_cache.map[i].fn,m->name,sizeof(map_cache.map[0].fn));
  2017. fseek(map_cache.fp,map_cache.head.filesize,SEEK_SET);
  2018. fwrite(write_buf,1,len_new,map_cache.fp);
  2019. map_cache.map[i].pos = map_cache.head.filesize;
  2020. map_cache.map[i].xs = m->xs;
  2021. map_cache.map[i].ys = m->ys;
  2022. map_cache.map[i].water_height = map_waterheight(m->name);
  2023. map_cache.head.filesize += len_new;
  2024. map_cache.dirty = 1;
  2025. if(map_read_flag == 2) {
  2026. aFree(write_buf);
  2027. }
  2028. return 0;
  2029. }
  2030. }
  2031. // 書き込めなかった
  2032. return 1;
  2033. }
  2034. #ifdef USE_AFM
  2035. static int map_readafm(int m,char *fn) {
  2036. /*
  2037. Advanced Fusion Maps Support
  2038. (c) 2003-2004, The Fusion Project
  2039. - AlexKreuz
  2040. The following code has been provided by me for eAthena
  2041. under the GNU GPL. It provides Advanced Fusion
  2042. Map, the map format desgined by me for Fusion, support
  2043. for the eAthena emulator.
  2044. I understand that because it is under the GPL
  2045. that other emulators may very well use this code in their
  2046. GNU project as well.
  2047. The AFM map format was not originally a part of the GNU
  2048. GPL. It originated from scratch by my own hand. I understand
  2049. that distributing this code to read the AFM maps with eAthena
  2050. causes the GPL to apply to this code. But the actual AFM
  2051. maps are STILL copyrighted to the Fusion Project. By choosing
  2052. In exchange for that 'act of faith' I ask for the following.
  2053. A) Give credit where it is due. If you use this code, do not
  2054. place your name on the changelog. Credit should be given
  2055. to AlexKreuz.
  2056. B) As an act of courtesy, ask me and let me know that you are putting
  2057. AFM support in your project. You will have my blessings if you do.
  2058. C) Use the code in its entirety INCLUDING the copyright message.
  2059. Although the code provided may now be GPL, the AFM maps are not
  2060. and so I ask you to display the copyright message on the STARTUP
  2061. SCREEN as I have done here. (refer to core.c)
  2062. "Advanced Fusion Maps (c) 2003-2004 The Fusion Project"
  2063. Without this copyright, you are NOT entitled to bundle or distribute
  2064. the AFM maps at all. On top of that, your "support" for AFM maps
  2065. becomes just as shady as your "support" for Gravity GRF files.
  2066. The bottom line is this. I know that there are those of you who
  2067. would like to use this code but aren't going to want to provide the
  2068. proper credit. I know this because I speak frome experience. If
  2069. you are one of those people who is going to try to get around my
  2070. requests, then save your breath because I don't want to hear it.
  2071. I have zero faith in GPL and I know and accept that if you choose to
  2072. not display the copyright for the AFMs then there is absolutely nothing
  2073. I can do about it. I am not about to start a legal battle over something
  2074. this silly.
  2075. Provide the proper credit because you believe in the GPL. If you choose
  2076. not to and would rather argue about it, consider the GPL failed.
  2077. October 18th, 2004
  2078. - AlexKreuz
  2079. - The Fusion Project
  2080. */
  2081. int s;
  2082. int x,y,xs,ys;
  2083. size_t size;
  2084. char afm_line[65535];
  2085. int afm_size[1];
  2086. FILE *afm_file;
  2087. char *str;
  2088. afm_file = fopen(fn, "r");
  2089. if (afm_file != NULL) {
  2090. // printf("\rLoading Maps [%d/%d]: %-50s ",m,map_num,fn);
  2091. // fflush(stdout);
  2092. str=fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2093. str=fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2094. str=fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2095. sscanf(str , "%d%d", &afm_size[0], &afm_size[1]);
  2096. map[m].m = m;
  2097. xs = map[m].xs = afm_size[0];
  2098. ys = map[m].ys = afm_size[1];
  2099. // check this, unsigned where it might not need to be
  2100. map[m].gat = (unsigned char*)aCallocA(s = map[m].xs * map[m].ys, 1);
  2101. if(map[m].gat==NULL){
  2102. printf("out of memory : map_readmap gat\n");
  2103. exit(1);
  2104. }
  2105. map[m].npc_num=0;
  2106. map[m].users=0;
  2107. memset(&map[m].flag,0,sizeof(map[m].flag));
  2108. if(battle_config.pk_mode) map[m].flag.pvp = 1; // make all maps pvp for pk_mode [Valaris]
  2109. for (y = 0; y < ys; y++) {
  2110. str=fgets(afm_line, sizeof(afm_line)-1, afm_file);
  2111. for (x = 0; x < xs; x++) {
  2112. map[m].gat[x+y*xs] = str[x]-48;
  2113. }
  2114. }
  2115. map[m].bxs=(xs+BLOCK_SIZE-1)/BLOCK_SIZE;
  2116. map[m].bys=(ys+BLOCK_SIZE-1)/BLOCK_SIZE;
  2117. size = map[m].bxs * map[m].bys * sizeof(struct block_list*);
  2118. map[m].block = (struct block_list**)aCalloc(size, 1);
  2119. if(map[m].block == NULL){
  2120. printf("out of memory : map_readmap block\n");
  2121. exit(1);
  2122. }
  2123. map[m].block_mob = (struct block_list**)aCalloc(size, 1);
  2124. if (map[m].block_mob == NULL) {
  2125. printf("out of memory : map_readmap block_mob\n");
  2126. exit(1);
  2127. }
  2128. size = map[m].bxs*map[m].bys*sizeof(int);
  2129. map[m].block_count = (int*)aCallocA(size, 1);
  2130. if(map[m].block_count==NULL){
  2131. printf("out of memory : map_readmap block\n");
  2132. exit(1);
  2133. }
  2134. memset(map[m].block_count,0,size);
  2135. map[m].block_mob_count = (int*)aCallocA(size, 1);
  2136. if(map[m].block_mob_count==NULL){
  2137. printf("out of memory : map_readmap block_mob\n");
  2138. exit(1);
  2139. }
  2140. memset(map[m].block_mob_count,0,size);
  2141. strdb_insert(map_db,map[m].name,&map[m]);
  2142. fclose(afm_file);
  2143. }
  2144. return 0;
  2145. }
  2146. #endif
  2147. /*==========================================
  2148. * マップ1枚読み込み
  2149. * ===================================================*/
  2150. static int map_readmap(int m,char *fn, char *alias, int *map_cache, int maxmap) {
  2151. char *gat="";
  2152. size_t size;
  2153. int i = 0;
  2154. int e = 0;
  2155. char progress[21] = " ";
  2156. //printf("\rLoading Maps [%d/%d]: %-50s ",m,map_num,fn);
  2157. if (maxmap) { //avoid map-server crashing if there are 0 maps
  2158. char c = '-';
  2159. static int lasti = -1;
  2160. static int last_time = -1;
  2161. i=m*20/maxmap;
  2162. if ((i != lasti) || (last_time != time(0))) {
  2163. lasti = i;
  2164. printf("\r");
  2165. ShowStatus("Progress: ");
  2166. printf("[");
  2167. for (e=0;e<i;e++) progress[e] = '#';
  2168. printf(progress);
  2169. printf("] Working: [");
  2170. last_time = time(0);
  2171. switch(last_time % 4) {
  2172. case 0: c='\\'; break;
  2173. case 1: c='|'; break;
  2174. case 2: c='/'; break;
  2175. case 3: c='-'; break;
  2176. }
  2177. printf("%c]",c);
  2178. fflush(stdout);
  2179. }
  2180. }
  2181. if(map_cache_read(&map[m])) {
  2182. // キャッシュから読み込めた
  2183. (*map_cache)++;
  2184. } else {
  2185. int s;
  2186. int wh;
  2187. int x,y,xs,ys;
  2188. struct gat_1cell {float high[4]; int type;} *p=NULL;
  2189. // read & convert fn
  2190. // again, might not need to be unsigned char
  2191. gat = (char*)grfio_read(fn);
  2192. if(gat==NULL) {
  2193. return -1;
  2194. // さすがにマップが読めないのはまずいので終了する
  2195. //printf("Can't load map %s\n",fn);
  2196. //exit(1);
  2197. }
  2198. xs=map[m].xs=*(int*)(gat+6);
  2199. ys=map[m].ys=*(int*)(gat+10);
  2200. map[m].gat = (unsigned char *)aCallocA(s = map[m].xs * map[m].ys,sizeof(unsigned char));
  2201. wh=map_waterheight(map[m].name);
  2202. for(y=0;y<ys;y++){
  2203. p=(struct gat_1cell*)(gat+y*xs*20+14);
  2204. for(x=0;x<xs;x++){
  2205. if(wh!=NO_WATER && p->type==0){
  2206. // 水場判定
  2207. map[m].gat[x+y*xs]=(p->high[0]>wh || p->high[1]>wh || p->high[2]>wh || p->high[3]>wh) ? 3 : 0;
  2208. } else {
  2209. map[m].gat[x+y*xs]=p->type;
  2210. }
  2211. p++;
  2212. }
  2213. }
  2214. map_cache_write(&map[m]);
  2215. aFree(gat);
  2216. }
  2217. map[m].m=m;
  2218. map[m].npc_num=0;
  2219. map[m].users=0;
  2220. memset(&map[m].flag,0,sizeof(map[m].flag));
  2221. if(battle_config.pk_mode)
  2222. map[m].flag.pvp = 1; // make all maps pvp for pk_mode [Valaris]
  2223. map[m].bxs=(map[m].xs+BLOCK_SIZE-1)/BLOCK_SIZE;
  2224. map[m].bys=(map[m].ys+BLOCK_SIZE-1)/BLOCK_SIZE;
  2225. size = map[m].bxs * map[m].bys * sizeof(struct block_list*);
  2226. map[m].block = (struct block_list **)aCalloc(1,size);
  2227. map[m].block_mob = (struct block_list **)aCalloc(1,size);
  2228. size = map[m].bxs*map[m].bys*sizeof(int);
  2229. map[m].block_count = (int *)aCallocA(1,size);
  2230. map[m].block_mob_count=(int *)aCallocA(1,size);
  2231. if (alias)
  2232. strdb_insert(map_db,alias,&map[m]);
  2233. else
  2234. strdb_insert(map_db,map[m].name,&map[m]);
  2235. // printf("%s read done\n",fn);
  2236. return 0;
  2237. }
  2238. /*==========================================
  2239. * 全てのmapデ?タを?み?む
  2240. *------------------------------------------
  2241. */
  2242. int map_readallmap(void) {
  2243. int i,maps_removed=0;
  2244. char fn[256];
  2245. #ifdef USE_AFM
  2246. FILE *afm_file;
  2247. #endif
  2248. int map_cache = 0;
  2249. // マップキャッシュを開く
  2250. if(map_read_flag >= READ_FROM_BITMAP) {
  2251. map_cache_open(map_cache_file);
  2252. }
  2253. sprintf(tmp_output, "Loading Maps%s...\n",
  2254. (map_read_flag == CREATE_BITMAP_COMPRESSED ? " (Generating Map Cache w/ Compression)" :
  2255. map_read_flag == CREATE_BITMAP ? " (Generating Map Cache)" :
  2256. map_read_flag >= READ_FROM_BITMAP ? " (w/ Map Cache)" :
  2257. map_read_flag == READ_FROM_AFM ? " (w/ AFM)" : ""));
  2258. ShowStatus(tmp_output);
  2259. // 先に全部のャbプの存在を確認
  2260. for(i=0;i<map_num;i++){
  2261. #ifdef USE_AFM
  2262. char afm_name[256] = "";
  2263. char *p;
  2264. if(!strstr(map[i].name,".afm")) {
  2265. // check if it's necessary to replace the extension - speeds up loading abit
  2266. strncpy(afm_name, map[i].name, strlen(map[i].name) - 4);
  2267. strcat(afm_name, ".afm");
  2268. }
  2269. map[i].alias = NULL;
  2270. sprintf(fn,"%s\\%s",afm_dir,afm_name);
  2271. for(p=&fn[0];*p!=0;p++) if (*p=='\\') *p = '/'; // * At the time of Unix
  2272. afm_file = fopen(fn, "r");
  2273. if (afm_file != NULL) {
  2274. map_readafm(i,fn);
  2275. fclose(afm_file);
  2276. }
  2277. else if(strstr(map[i].name,".gat")!=NULL) {
  2278. #else
  2279. if(strstr(map[i].name,".gat")!=NULL) {
  2280. #endif
  2281. char *p = strstr(map[i].name, "<"); // [MouseJstr]
  2282. if (p != NULL) {
  2283. char buf[64];
  2284. *p++ = '\0';
  2285. sprintf(buf,"data\\%s", p);
  2286. map[i].alias = aStrdup(buf);
  2287. } else
  2288. map[i].alias = NULL;
  2289. sprintf(fn,"data\\%s",map[i].name);
  2290. if(map_readmap(i,fn, p, &map_cache, map_num) == -1) {
  2291. map_delmap(map[i].name);
  2292. maps_removed++;
  2293. i--;
  2294. }
  2295. }
  2296. }
  2297. aFree(waterlist);
  2298. printf("\r");
  2299. snprintf(tmp_output,sizeof(tmp_output),"Successfully loaded '"CL_WHITE"%d"CL_RESET"' maps.%30s\n",map_num,"");
  2300. ShowInfo(tmp_output);
  2301. map_cache_close();
  2302. if(map_read_flag == CREATE_BITMAP || map_read_flag == CREATE_BITMAP_COMPRESSED) {
  2303. --map_read_flag;
  2304. }
  2305. if (maps_removed) {
  2306. snprintf(tmp_output,sizeof(tmp_output),"Maps Removed: '"CL_WHITE"%d"CL_RESET"'\n",maps_removed);
  2307. ShowNotice(tmp_output);
  2308. }
  2309. return 0;
  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. snprintf(tmp_output,sizeof(tmp_output),"Could not add map '"
  2322. CL_WHITE"%s"CL_RESET"', the limit of maps has been reached.\n",mapname);
  2323. ShowError(tmp_output);
  2324. return 1;
  2325. }
  2326. memcpy(map[map_num].name, mapname, 24);
  2327. map_num++;
  2328. return 0;
  2329. }
  2330. /*==========================================
  2331. * ?み?むmapを削除する
  2332. *------------------------------------------
  2333. */
  2334. int map_delmap(char *mapname) {
  2335. int i;
  2336. if (strcmpi(mapname, "all") == 0) {
  2337. map_num = 0;
  2338. return 0;
  2339. }
  2340. for(i = 0; i < map_num; i++) {
  2341. if (strcmp(map[i].name, mapname) == 0) {
  2342. printf("Removing map [ %s ] from maplist\n",map[i].name);
  2343. memmove(map+i, map+i+1, sizeof(map[0])*(map_num-i-1));
  2344. map_num--;
  2345. }
  2346. }
  2347. return 0;
  2348. }
  2349. static int map_ip_set_ = 0;
  2350. static int char_ip_set_ = 0;
  2351. //static int bind_ip_set_ = 0;
  2352. /*==========================================
  2353. * Console Command Parser [Wizputer]
  2354. *------------------------------------------
  2355. */
  2356. int parse_console(char *buf) {
  2357. char *type,*command,*map, *buf2;
  2358. int x = 0, y = 0;
  2359. int m, n;
  2360. struct map_session_data *sd;
  2361. sd = (struct map_session_data*)aCalloc(sizeof(*sd), 1);
  2362. sd->fd = 0;
  2363. strcpy( sd->status.name , "console");
  2364. type = (char *)aMallocA(64);
  2365. command = (char *)aMallocA(64);
  2366. map = (char *)aMallocA(64);
  2367. buf2 = (char *)aMallocA(72);
  2368. memset(type,0,64);
  2369. memset(command,0,64);
  2370. memset(map,0,64);
  2371. memset(buf2,0,72);
  2372. if ( ( n = sscanf(buf, "%[^:]:%[^:]:%99s %d %d[^\n]", type , command , map , &x , &y )) < 5 )
  2373. if ( ( n = sscanf(buf, "%[^:]:%[^\n]", type , command )) < 2 )
  2374. n = sscanf(buf,"%[^\n]",type);
  2375. if ( n == 5 ) {
  2376. if (x <= 0) {
  2377. x = rand() % 399 + 1;
  2378. sd->bl.x = x;
  2379. } else {
  2380. sd->bl.x = x;
  2381. }
  2382. if (y <= 0) {
  2383. y = rand() % 399 + 1;
  2384. sd->bl.y = y;
  2385. } else {
  2386. sd->bl.y = y;
  2387. }
  2388. m = map_mapname2mapid(map);
  2389. if ( m >= 0 )
  2390. sd->bl.m = m;
  2391. else {
  2392. printf("Console: Unknown map\n");
  2393. goto end;
  2394. }
  2395. }
  2396. printf("Type of command: %s || Command: %s || Map: %s Coords: %d %d\n",type,command,map,x,y);
  2397. if ( strcmpi("admin",type) == 0 && n == 5 ) {
  2398. sprintf(buf2,"console: %s",command);
  2399. if( is_atcommand(sd->fd,sd,buf2,99) == AtCommand_None )
  2400. printf("Console: not atcommand\n");
  2401. } else if ( strcmpi("server",type) == 0 && n == 2 ) {
  2402. if ( strcmpi("shutdown", command) == 0 || strcmpi("exit",command) == 0 || strcmpi("quit",command) == 0 ) {
  2403. exit(0);
  2404. }
  2405. } else if ( strcmpi("help",type) == 0 ) {
  2406. printf("To use GM commands:\n");
  2407. printf("admin:<gm command>:<map of \"gm\"> <x> <y>\n");
  2408. printf("You can use any GM command that doesn't require the GM.\n");
  2409. printf("No using @item or @warp however you can use @charwarp\n");
  2410. printf("The <map of \"gm\"> <x> <y> is for commands that need coords of the GM\n");
  2411. printf("IE: @spawn\n");
  2412. printf("To shutdown the server:\n");
  2413. printf("server:shutdown\n");
  2414. }
  2415. end:
  2416. aFree(buf);
  2417. aFree(type);
  2418. aFree(command);
  2419. aFree(map);
  2420. aFree(buf2);
  2421. aFree(sd);
  2422. return 0;
  2423. }
  2424. /*==========================================
  2425. * 設定ファイルを?み?む
  2426. *------------------------------------------
  2427. */
  2428. int map_config_read(char *cfgName) {
  2429. char line[1024], w1[1024], w2[1024];
  2430. FILE *fp;
  2431. struct hostent *h = NULL;
  2432. fp = fopen(cfgName,"r");
  2433. if (fp == NULL) {
  2434. printf("Map configuration file not found at: %s\n", cfgName);
  2435. exit(1);
  2436. }
  2437. while(fgets(line, sizeof(line) -1, fp)) {
  2438. if (line[0] == '/' && line[1] == '/')
  2439. continue;
  2440. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2) {
  2441. if (strcmpi(w1, "userid")==0){
  2442. chrif_setuserid(w2);
  2443. } else if (strcmpi(w1, "passwd") == 0) {
  2444. chrif_setpasswd(w2);
  2445. } else if (strcmpi(w1, "char_ip") == 0) {
  2446. char_ip_set_ = 1;
  2447. h = gethostbyname (w2);
  2448. if(h != NULL) {
  2449. snprintf(tmp_output,sizeof(tmp_output),"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]);
  2450. ShowInfo(tmp_output);
  2451. 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]);
  2452. }
  2453. chrif_setip(w2);
  2454. } else if (strcmpi(w1, "char_port") == 0) {
  2455. chrif_setport(atoi(w2));
  2456. } else if (strcmpi(w1, "map_ip") == 0) {
  2457. map_ip_set_ = 1;
  2458. h = gethostbyname (w2);
  2459. if (h != NULL) {
  2460. snprintf(tmp_output,sizeof(tmp_output),"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]);
  2461. ShowInfo(tmp_output);
  2462. 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]);
  2463. }
  2464. clif_setip(w2);
  2465. } else if (strcmpi(w1, "bind_ip") == 0) {
  2466. //bind_ip_set_ = 1;
  2467. h = gethostbyname (w2);
  2468. if (h != NULL) {
  2469. snprintf(tmp_output,sizeof(tmp_output),"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]);
  2470. ShowInfo(tmp_output);
  2471. 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]);
  2472. }
  2473. clif_setbindip(w2);
  2474. } else if (strcmpi(w1, "map_port") == 0) {
  2475. clif_setport(atoi(w2));
  2476. map_port = (atoi(w2));
  2477. } else if (strcmpi(w1, "water_height") == 0) {
  2478. map_readwater(w2);
  2479. } else if (strcmpi(w1, "map") == 0) {
  2480. map_addmap(w2);
  2481. } else if (strcmpi(w1, "delmap") == 0) {
  2482. map_delmap(w2);
  2483. } else if (strcmpi(w1, "npc") == 0) {
  2484. npc_addsrcfile(w2);
  2485. } else if (strcmpi(w1, "delnpc") == 0) {
  2486. npc_delsrcfile(w2);
  2487. } else if (strcmpi(w1, "data_grf") == 0) {
  2488. grfio_setdatafile(w2);
  2489. } else if (strcmpi(w1, "sdata_grf") == 0) {
  2490. grfio_setsdatafile(w2);
  2491. } else if (strcmpi(w1, "adata_grf") == 0) {
  2492. grfio_setadatafile(w2);
  2493. } else if (strcmpi(w1, "autosave_time") == 0) {
  2494. autosave_interval = atoi(w2) * 1000;
  2495. if (autosave_interval <= 0)
  2496. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2497. } else if (strcmpi(w1, "motd_txt") == 0) {
  2498. strcpy(motd_txt, w2);
  2499. } else if (strcmpi(w1, "help_txt") == 0) {
  2500. strcpy(help_txt, w2);
  2501. } else if (strcmpi(w1, "mapreg_txt") == 0) {
  2502. strcpy(mapreg_txt, w2);
  2503. }else if(strcmpi(w1,"read_map_from_cache")==0){
  2504. if (atoi(w2) == 2)
  2505. map_read_flag = READ_FROM_BITMAP_COMPRESSED;
  2506. else if (atoi(w2) == 1)
  2507. map_read_flag = READ_FROM_BITMAP;
  2508. else
  2509. map_read_flag = READ_FROM_GAT;
  2510. }else if(strcmpi(w1,"map_cache_file")==0){
  2511. strncpy(map_cache_file,w2,255);
  2512. } else if (strcmpi(w1, "import") == 0) {
  2513. map_config_read(w2);
  2514. } else if (strcmpi(w1, "console") == 0) {
  2515. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 ) {
  2516. console = 1;
  2517. ShowNotice("Console Commands is enabled.\n");
  2518. }
  2519. } else if(strcmpi(w1,"imalive_on")==0){ //Added by Mugendai for I'm Alive mod
  2520. imalive_on = atoi(w2); //Added by Mugendai for I'm Alive mod
  2521. } else if(strcmpi(w1,"imalive_time")==0){ //Added by Mugendai for I'm Alive mod
  2522. imalive_time = atoi(w2); //Added by Mugendai for I'm Alive mod
  2523. } else if(strcmpi(w1,"flush_on")==0){ //Added by Mugendai for GUI
  2524. flush_on = atoi(w2); //Added by Mugendai for GUI
  2525. } else if(strcmpi(w1,"flush_time")==0){ //Added by Mugendai for GUI
  2526. flush_time = atoi(w2); //Added by Mugendai for GUI
  2527. }
  2528. }
  2529. }
  2530. fclose(fp);
  2531. return 0;
  2532. }
  2533. int inter_config_read(char *cfgName)
  2534. {
  2535. int i;
  2536. char line[1024],w1[1024],w2[1024];
  2537. FILE *fp;
  2538. fp=fopen(cfgName,"r");
  2539. if(fp==NULL){
  2540. snprintf(tmp_output,sizeof(tmp_output),"File not found: '%s'.\n",cfgName);
  2541. ShowError(tmp_output);
  2542. return 1;
  2543. }
  2544. while(fgets(line,1020,fp)){
  2545. if(line[0] == '/' && line[1] == '/')
  2546. continue;
  2547. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  2548. if(i!=2)
  2549. continue;
  2550. //support the import command, just like any other config
  2551. if(strcmpi(w1,"import")==0){
  2552. inter_config_read(w2);
  2553. #ifndef TXT_ONLY
  2554. } else if(strcmpi(w1,"item_db_db")==0){
  2555. strcpy(item_db_db,w2);
  2556. } else if(strcmpi(w1,"mob_db_db")==0){
  2557. strcpy(mob_db_db,w2);
  2558. } else if(strcmpi(w1,"login_db_level")==0){
  2559. strcpy(login_db_level,w2);
  2560. } else if(strcmpi(w1,"login_db_account_id")==0){
  2561. strcpy(login_db_account_id,w2);
  2562. } else if(strcmpi(w1,"login_db")==0){
  2563. strcpy(login_db,w2);
  2564. } else if (strcmpi(w1, "char_db") == 0) {
  2565. strcpy(char_db, w2);
  2566. } else if(strcmpi(w1,"gm_db_level")==0){
  2567. strcpy(gm_db_level,w2);
  2568. } else if(strcmpi(w1,"gm_db_account_id")==0){
  2569. strcpy(gm_db_account_id,w2);
  2570. } else if(strcmpi(w1,"gm_db")==0){
  2571. strcpy(gm_db,w2);
  2572. //Map Server SQL DB
  2573. } else if(strcmpi(w1,"map_server_ip")==0){
  2574. strcpy(map_server_ip, w2);
  2575. } else if(strcmpi(w1,"map_server_port")==0){
  2576. map_server_port=atoi(w2);
  2577. } else if(strcmpi(w1,"map_server_id")==0){
  2578. strcpy(map_server_id, w2);
  2579. } else if(strcmpi(w1,"map_server_pw")==0){
  2580. strcpy(map_server_pw, w2);
  2581. } else if(strcmpi(w1,"map_server_db")==0){
  2582. strcpy(map_server_db, w2);
  2583. } else if(strcmpi(w1,"use_sql_db")==0){
  2584. db_use_sqldbs = battle_config_switch(w2);
  2585. printf ("Using SQL dbs: %s\n",w2);
  2586. //Login Server SQL DB
  2587. } else if(strcmpi(w1,"login_server_ip")==0){
  2588. strcpy(login_server_ip, w2);
  2589. } else if(strcmpi(w1,"login_server_port")==0){
  2590. login_server_port = atoi(w2);
  2591. } else if(strcmpi(w1,"login_server_id")==0){
  2592. strcpy(login_server_id, w2);
  2593. } else if(strcmpi(w1,"login_server_pw")==0){
  2594. strcpy(login_server_pw, w2);
  2595. } else if(strcmpi(w1,"login_server_db")==0){
  2596. strcpy(login_server_db, w2);
  2597. } else if(strcmpi(w1,"lowest_gm_level")==0){
  2598. lowest_gm_level = atoi(w2);
  2599. } else if(strcmpi(w1,"read_gm_interval")==0){
  2600. read_gm_interval = ( atoi(w2) * 60 * 1000 ); // Minutes multiplied by 60 secs per min by 1000 milliseconds per second
  2601. } else if(strcmpi(w1,"log_db")==0) {
  2602. strcpy(log_db, w2);
  2603. } else if(strcmpi(w1,"log_db_ip")==0) {
  2604. strcpy(log_db_ip, w2);
  2605. } else if(strcmpi(w1,"log_db")==0) {
  2606. strcpy(log_db, w2);
  2607. } else if(strcmpi(w1,"log_db_id")==0) {
  2608. strcpy(log_db_id, w2);
  2609. } else if(strcmpi(w1,"log_db_pw")==0) {
  2610. strcpy(log_db_pw, w2);
  2611. } else if(strcmpi(w1,"log_db_port")==0) {
  2612. log_db_port = atoi(w2);
  2613. #endif
  2614. }
  2615. }
  2616. fclose(fp);
  2617. return 0;
  2618. }
  2619. #ifndef TXT_ONLY
  2620. /*=======================================
  2621. * MySQL Init
  2622. *---------------------------------------
  2623. */
  2624. int map_sql_init(void){
  2625. mysql_init(&mmysql_handle);
  2626. //DB connection start
  2627. printf("Connect Map DB Server....\n");
  2628. if(!mysql_real_connect(&mmysql_handle, map_server_ip, map_server_id, map_server_pw,
  2629. map_server_db ,map_server_port, (char *)NULL, 0)) {
  2630. //pointer check
  2631. printf("%s\n",mysql_error(&mmysql_handle));
  2632. exit(1);
  2633. }
  2634. else {
  2635. printf ("connect success! (Map Server Connection)\n");
  2636. }
  2637. mysql_init(&lmysql_handle);
  2638. //DB connection start
  2639. printf("Connect Login DB Server....\n");
  2640. if(!mysql_real_connect(&lmysql_handle, login_server_ip, login_server_id, login_server_pw,
  2641. login_server_db ,login_server_port, (char *)NULL, 0)) {
  2642. //pointer check
  2643. printf("%s\n",mysql_error(&lmysql_handle));
  2644. exit(1);
  2645. }
  2646. else {
  2647. printf ("connect success! (Login Server Connection)\n");
  2648. }
  2649. if(battle_config.mail_system) { // mail system [Valaris]
  2650. mysql_init(&mail_handle);
  2651. if(!mysql_real_connect(&mail_handle, map_server_ip, map_server_id, map_server_pw,
  2652. map_server_db ,map_server_port, (char *)NULL, 0)) {
  2653. printf("%s\n",mysql_error(&mail_handle));
  2654. exit(1);
  2655. }
  2656. }
  2657. return 0;
  2658. }
  2659. int map_sql_close(void){
  2660. mysql_close(&mmysql_handle);
  2661. printf("Close Map DB Connection....\n");
  2662. mysql_close(&lmysql_handle);
  2663. printf("Close Login DB Connection....\n");
  2664. return 0;
  2665. }
  2666. int log_sql_init(void){
  2667. mysql_init(&mmysql_handle);
  2668. //DB connection start
  2669. printf(""CL_WHITE"[SQL]"CL_RESET": Connecting to Log Database "CL_WHITE"%s"CL_RESET" At "CL_WHITE"%s"CL_RESET"...\n",log_db,log_db_ip);
  2670. if(!mysql_real_connect(&mmysql_handle, log_db_ip, log_db_id, log_db_pw,
  2671. log_db ,log_db_port, (char *)NULL, 0)) {
  2672. //pointer check
  2673. printf(""CL_WHITE"[SQL Error]"CL_RESET": %s\n",mysql_error(&mmysql_handle));
  2674. exit(1);
  2675. } else {
  2676. printf(""CL_WHITE"[SQL]"CL_RESET": Successfully '"CL_GREEN"connected"CL_RESET"' to Database '"CL_WHITE"%s"CL_RESET"'.\n", log_db);
  2677. }
  2678. return 0;
  2679. }
  2680. int online_timer(int tid,unsigned int tick,int id,int data)
  2681. {
  2682. if(check_online_timer != tid)
  2683. return 0;
  2684. char_online_check();
  2685. check_online_timer=add_timer(gettick()+CHECK_INTERVAL,online_timer,0,0);
  2686. return 0;
  2687. }
  2688. void char_online_check(void)
  2689. {
  2690. int i;
  2691. struct map_session_data *sd=NULL;
  2692. chrif_char_reset_offline();
  2693. for(i=0;i<fd_max;i++){
  2694. if (session[i] && (sd = (struct map_session_data*)session[i]->session_data) && sd && sd->state.auth &&
  2695. !(battle_config.hide_GM_session && pc_isGM(sd)))
  2696. if(sd->status.char_id) {
  2697. chrif_char_online(sd);
  2698. }
  2699. }
  2700. if(check_online_timer && check_online_timer != -1) {
  2701. delete_timer(check_online_timer,online_timer);
  2702. add_timer(gettick()+CHECK_INTERVAL,online_timer,0,0);
  2703. }
  2704. }
  2705. #endif /* not TXT_ONLY */
  2706. //-----------------------------------------------------
  2707. //I'm Alive Alert
  2708. //Used to output 'I'm Alive' every few seconds
  2709. //Intended to let frontends know if the app froze
  2710. //-----------------------------------------------------
  2711. int imalive_timer(int tid, unsigned int tick, int id, int data){
  2712. printf("I'm Alive\n");
  2713. return 0;
  2714. }
  2715. //-----------------------------------------------------
  2716. //Flush stdout
  2717. //stdout buffer needs flushed to be seen in GUI
  2718. //-----------------------------------------------------
  2719. int flush_timer(int tid, unsigned int tick, int id, int data){
  2720. fflush(stdout);
  2721. return 0;
  2722. }
  2723. int id_db_final(void *k,void *d,va_list ap)
  2724. {
  2725. struct mob_data *id;
  2726. nullpo_retr(0, id = (struct mob_data*)d);
  2727. if(id->lootitem)
  2728. aFree(id->lootitem);
  2729. if(id)
  2730. aFree(id);
  2731. return 0;
  2732. }
  2733. int map_db_final(void *k,void *d,va_list ap)
  2734. {
  2735. struct map_data *id;
  2736. nullpo_retr(0, id = (struct map_data*)d);
  2737. if(id->gat)
  2738. aFree(id->gat);
  2739. if(id)
  2740. aFree(id);
  2741. return 0;
  2742. }
  2743. int nick_db_final(void *k,void *d,va_list ap){ return 0; }
  2744. int charid_db_final(void *k,void *d,va_list ap){ return 0; }
  2745. int cleanup_sub(struct block_list *bl, va_list ap) {
  2746. nullpo_retr(0, bl);
  2747. switch(bl->type) {
  2748. case BL_PC:
  2749. map_quit((struct map_session_data *) bl);
  2750. break;
  2751. case BL_NPC:
  2752. npc_delete((struct npc_data *)bl);
  2753. break;
  2754. case BL_MOB:
  2755. mob_delete((struct mob_data *)bl);
  2756. break;
  2757. case BL_PET:
  2758. pet_remove_map((struct map_session_data *)bl);
  2759. break;
  2760. case BL_ITEM:
  2761. map_clearflooritem(bl->id);
  2762. break;
  2763. case BL_SKILL:
  2764. skill_delunit((struct skill_unit *) bl);
  2765. break;
  2766. }
  2767. return 0;
  2768. }
  2769. /*==========================================
  2770. * map鯖終了時?理
  2771. *------------------------------------------
  2772. */
  2773. void do_final(void) {
  2774. int map_id, i;
  2775. ShowStatus("Terminating...\n");
  2776. for (map_id = 0; map_id < map_num;map_id++)
  2777. if(map[map_id].m)
  2778. map_foreachinarea(cleanup_sub, map_id, 0, 0, map[map_id].xs, map[map_id].ys, 0, 0);
  2779. #ifndef TXT_ONLY
  2780. chrif_char_reset_offline();
  2781. #endif
  2782. chrif_flush_fifo();
  2783. for (i = 0; i < fd_max; i++)
  2784. delete_session(i);
  2785. #if 0
  2786. map_removenpc();
  2787. // do_final_timer(); (we used timer_final() instead)
  2788. timer_final();
  2789. // numdb_final(id_db, id_db_final);
  2790. strdb_final(map_db, map_db_final);
  2791. strdb_final(nick_db, nick_db_final);
  2792. numdb_final(charid_db, charid_db_final);
  2793. do_final_chrif(); // この内部でキャラを全て切断する
  2794. do_final_script();
  2795. do_final_itemdb();
  2796. do_final_storage();
  2797. do_final_guild();
  2798. do_final_pet();
  2799. /*
  2800. for(i=0;i<map_num;i++){
  2801. if(map[i].gat) {
  2802. aFree(map[i].gat);
  2803. map[i].gat=NULL; //isn't it NULL already o_O?
  2804. }
  2805. if(map[i].block) aFree(map[i].block);
  2806. if(map[i].block_mob) aFree(map[i].block_mob);
  2807. if(map[i].block_count) aFree(map[i].block_count);
  2808. if(map[i].block_mob_count) aFree(map[i].block_mob_count);
  2809. }
  2810. */
  2811. #endif
  2812. #ifndef TXT_ONLY
  2813. map_sql_close();
  2814. #endif /* not TXT_ONLY */
  2815. ShowStatus("Successfully terminated.\n");
  2816. }
  2817. /*======================================================
  2818. * Map-Server Version Screen [MC Cameri]
  2819. *------------------------------------------------------
  2820. */
  2821. void map_helpscreen(int flag) { // by MC Cameri
  2822. puts("Usage: map-server [options]");
  2823. puts("Options:");
  2824. puts(CL_WHITE" Commands\t\t\tDescription"CL_RESET);
  2825. puts("-----------------------------------------------------------------------------");
  2826. puts(" --help, --h, --?, /? Displays this help screen");
  2827. puts(" --map-config <file> Load map-server configuration from <file>");
  2828. puts(" --battle-config <file> Load battle configuration from <file>");
  2829. puts(" --atcommand-config <file> Load atcommand configuration from <file>");
  2830. puts(" --charcommand-config <file> Load charcommand configuration from <file>");
  2831. puts(" --script-config <file> Load script configuration from <file>");
  2832. puts(" --msg-config <file> Load message configuration from <file>");
  2833. puts(" --grf-path-file <file> Load grf path file configuration from <file>");
  2834. puts(" --sql-config <file> Load inter-server configuration from <file>");
  2835. puts(" (SQL Only)");
  2836. puts(" --log-config <file> Load logging configuration from <file>");
  2837. puts(" (SQL Only)");
  2838. puts(" --version, --v, -v, /v Displays the server's version");
  2839. puts("\n");
  2840. if (flag) exit(1);
  2841. }
  2842. /*======================================================
  2843. * Map-Server Version Screen [MC Cameri]
  2844. *------------------------------------------------------
  2845. */
  2846. void map_versionscreen(int flag) {
  2847. printf("CL_WHITE" "eAthena version %d.%02d.%02d, Athena Mod version %d" CL_RESET"\n",
  2848. ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION,
  2849. ATHENA_MOD_VERSION);
  2850. puts(CL_GREEN "Website/Forum:" CL_RESET "\thttp://eathena.deltaanime.net/");
  2851. puts(CL_GREEN "Download URL:" CL_RESET "\thttp://eathena.systeminplace.net/");
  2852. puts(CL_GREEN "IRC Channel:" CL_RESET "\tirc://irc.deltaanime.net/#athena");
  2853. puts("\nOpen " CL_WHITE "readme.html" CL_RESET " for more information.");
  2854. if (ATHENA_RELEASE_FLAG) ShowNotice("This version is not for release.\n");
  2855. if (flag) exit(1);
  2856. }
  2857. /*======================================================
  2858. * Map-Server Init and Command-line Arguments [Valaris]
  2859. *------------------------------------------------------
  2860. */
  2861. int do_init(int argc, char *argv[]) {
  2862. int i;
  2863. FILE *data_conf;
  2864. char line[1024], w1[1024], w2[1024];
  2865. #ifdef GCOLLECT
  2866. GC_enable_incremental();
  2867. #endif
  2868. char *INTER_CONF_NAME="conf/inter_athena.conf";
  2869. char *LOG_CONF_NAME="conf/log_athena.conf";
  2870. char *MAP_CONF_NAME = "conf/map_athena.conf";
  2871. char *BATTLE_CONF_FILENAME = "conf/battle_athena.conf";
  2872. char *ATCOMMAND_CONF_FILENAME = "conf/atcommand_athena.conf";
  2873. char *CHARCOMMAND_CONF_FILENAME = "conf/charcommand_athena.conf";
  2874. char *SCRIPT_CONF_NAME = "conf/script_athena.conf";
  2875. char *MSG_CONF_NAME = "conf/msg_athena.conf";
  2876. char *GRF_PATH_FILENAME = "conf/grf-files.txt";
  2877. chrif_connected = 0;
  2878. srand(gettick());
  2879. for (i = 1; i < argc ; i++) {
  2880. if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--h") == 0 || strcmp(argv[i], "--?") == 0 || strcmp(argv[i], "/?") == 0)
  2881. map_helpscreen(1);
  2882. if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "--v") == 0 || strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "/v") == 0)
  2883. map_versionscreen(1);
  2884. else if (strcmp(argv[i], "--map_config") == 0 || strcmp(argv[i], "--map-config") == 0)
  2885. MAP_CONF_NAME=argv[i+1];
  2886. else if (strcmp(argv[i],"--battle_config") == 0 || strcmp(argv[i],"--battle-config") == 0)
  2887. BATTLE_CONF_FILENAME = argv[i+1];
  2888. else if (strcmp(argv[i],"--atcommand_config") == 0 || strcmp(argv[i],"--atcommand-config") == 0)
  2889. ATCOMMAND_CONF_FILENAME = argv[i+1];
  2890. else if (strcmp(argv[i],"--charcommand_config") == 0 || strcmp(argv[i],"--charcommand-config") == 0)
  2891. CHARCOMMAND_CONF_FILENAME = argv[i+1];
  2892. else if (strcmp(argv[i],"--script_config") == 0 || strcmp(argv[i],"--script-config") == 0)
  2893. SCRIPT_CONF_NAME = argv[i+1];
  2894. else if (strcmp(argv[i],"--msg_config") == 0 || strcmp(argv[i],"--msg-config") == 0)
  2895. MSG_CONF_NAME = argv[i+1];
  2896. else if (strcmp(argv[i],"--grf_path_file") == 0 || strcmp(argv[i],"--grf-path-file") == 0)
  2897. GRF_PATH_FILENAME = argv[i+1];
  2898. #ifndef TXT_ONLY
  2899. else if (strcmp(argv[i],"--inter_config") == 0 || strcmp(argv[i],"--inter-config") == 0)
  2900. INTER_CONF_NAME = argv[i+1];
  2901. #endif /* not TXT_ONLY */
  2902. else if (strcmp(argv[i],"--log_config") == 0 || strcmp(argv[i],"--log-config") == 0)
  2903. LOG_CONF_NAME = argv[i+1];
  2904. else if (strcmp(argv[i],"--run_once") == 0) // close the map-server as soon as its done.. for testing [Celest]
  2905. runflag = 0;
  2906. }
  2907. map_config_read(MAP_CONF_NAME);
  2908. if ((naddr_ == 0) && (map_ip_set_ == 0 || char_ip_set_ == 0)) {
  2909. printf("\nUnable to determine your IP address... please edit\n");
  2910. printf("the map_athena.conf file and set it.\n");
  2911. printf("(127.0.0.1 is valid if you have no network interface)\n");
  2912. }
  2913. if (map_ip_set_ == 0 || char_ip_set_ == 0) {
  2914. // The map server should know what IP address it is running on
  2915. // - MouseJstr
  2916. int localaddr = ntohl(addr_[0]);
  2917. unsigned char *ptr = (unsigned char *) &localaddr;
  2918. char buf[16];
  2919. sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);;
  2920. if (naddr_ != 1)
  2921. printf("Multiple interfaces detected.. using %s as our IP address\n", buf);
  2922. else
  2923. printf("Defaulting to %s as our IP address\n", buf);
  2924. if (map_ip_set_ == 0)
  2925. clif_setip(buf);
  2926. if (char_ip_set_ == 0)
  2927. chrif_setip(buf);
  2928. if (ptr[0] == 192 && ptr[1] == 168)
  2929. printf("\nFirewall detected.. \n edit lan_support.conf and map_athena.conf\n\n");
  2930. }
  2931. if (SHOW_DEBUG_MSG) ShowNotice("Server running in '"CL_WHITE"Debug Mode"CL_RESET"'.\n");
  2932. battle_config_read(BATTLE_CONF_FILENAME);
  2933. msg_config_read(MSG_CONF_NAME);
  2934. atcommand_config_read(ATCOMMAND_CONF_FILENAME);
  2935. charcommand_config_read(CHARCOMMAND_CONF_FILENAME);
  2936. script_config_read(SCRIPT_CONF_NAME);
  2937. inter_config_read(INTER_CONF_NAME);
  2938. log_config_read(LOG_CONF_NAME);
  2939. atexit(do_final);
  2940. id_db = numdb_init();
  2941. map_db = strdb_init(16);
  2942. nick_db = strdb_init(24);
  2943. charid_db = numdb_init();
  2944. #ifndef TXT_ONLY
  2945. map_sql_init();
  2946. #endif /* not TXT_ONLY */
  2947. grfio_init(GRF_PATH_FILENAME);
  2948. data_conf = fopen(GRF_PATH_FILENAME, "r");
  2949. // It will read, if there is grf-files.txt.
  2950. if (data_conf) {
  2951. while(fgets(line, 1020, data_conf)) {
  2952. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2) {
  2953. if(strcmp(w1,"afm_dir") == 0)
  2954. strcpy(afm_dir, w2);
  2955. }
  2956. }
  2957. fclose(data_conf);
  2958. } // end of reading grf-files.txt for AFMs
  2959. map_readallmap();
  2960. add_timer_func_list(map_freeblock_timer,"map_freeblock_timer");
  2961. add_timer_func_list(map_clearflooritem_timer, "map_clearflooritem_timer");
  2962. add_timer_interval(gettick()+1000,map_freeblock_timer,0,0,60*1000);
  2963. //Added by Mugendai for GUI support
  2964. if (flush_on)
  2965. add_timer_interval(gettick()+10, flush_timer,0,0,flush_time);
  2966. //Added for Mugendais I'm Alive mod
  2967. if (imalive_on)
  2968. add_timer_interval(gettick()+10, imalive_timer,0,0,imalive_time*1000);
  2969. #ifndef TXT_ONLY // online status timer, checks every hour [Valaris]
  2970. add_timer_func_list(online_timer, "online_timer");
  2971. check_online_timer=add_timer(gettick()+CHECK_INTERVAL,online_timer,0,0);
  2972. #endif /* not TXT_ONLY */
  2973. do_init_chrif();
  2974. do_init_clif();
  2975. do_init_itemdb();
  2976. do_init_mob(); // npcの初期化時?でmob_spawnして、mob_dbを?照するのでinit_npcより先
  2977. do_init_script();
  2978. do_init_pc();
  2979. do_init_status();
  2980. do_init_party();
  2981. do_init_guild();
  2982. do_init_storage();
  2983. do_init_skill();
  2984. do_init_pet();
  2985. do_init_npc();
  2986. #ifndef TXT_ONLY /* mail system [Valaris] */
  2987. if(battle_config.mail_system)
  2988. do_init_mail();
  2989. if (log_config.sql_logs && (log_config.branch || log_config.drop || log_config.mvpdrop ||
  2990. log_config.present || log_config.produce || log_config.refine || log_config.trade))
  2991. {
  2992. log_sql_init();
  2993. }
  2994. #endif /* not TXT_ONLY */
  2995. npc_event_do_oninit(); // npcのOnInitイベント?行
  2996. if ( console ) {
  2997. set_defaultconsoleparse(parse_console);
  2998. start_console();
  2999. }
  3000. if (battle_config.pk_mode == 1)
  3001. ShowNotice("Server is running on '"CL_WHITE"PK Mode"CL_RESET"'.\n");
  3002. sprintf(tmp_output,"Server is '"CL_GREEN"ready"CL_RESET"' and listening on port '"CL_WHITE"%d"CL_RESET"'.\n\n", map_port);
  3003. ShowStatus(tmp_output);
  3004. return 0;
  3005. }
  3006. int compare_item(struct item *a, struct item *b) {
  3007. return (
  3008. (a->nameid == b->nameid) &&
  3009. (a->identify == b->identify) &&
  3010. (a->refine == b->refine) &&
  3011. (a->attribute == b->attribute) &&
  3012. (a->card[0] == b->card[0]) &&
  3013. (a->card[1] == b->card[1]) &&
  3014. (a->card[2] == b->card[2]) &&
  3015. (a->card[3] == b->card[3]));
  3016. }