map.c 113 KB

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