map.c 63 KB

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