map.c 78 KB

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