npc.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038
  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 <ctype.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <limits.h>
  10. #include "../common/timer.h"
  11. #include "../common/nullpo.h"
  12. #include "../common/malloc.h"
  13. #include "../common/grfio.h"
  14. #include "../common/showmsg.h"
  15. #include "../common/ers.h"
  16. #include "../common/db.h"
  17. #include "map.h"
  18. #include "log.h"
  19. #include "npc.h"
  20. #include "clif.h"
  21. #include "intif.h"
  22. #include "pc.h"
  23. #include "status.h"
  24. #include "itemdb.h"
  25. #include "script.h"
  26. #include "mob.h"
  27. #include "pet.h"
  28. #include "battle.h"
  29. #include "skill.h"
  30. #include "unit.h"
  31. #ifdef _WIN32
  32. #undef isspace
  33. #define isspace(x) (x == ' ' || x == '\t')
  34. #endif
  35. struct npc_src_list {
  36. struct npc_src_list * next;
  37. // struct npc_src_list * prev; //[Shinomori]
  38. char name[4];
  39. };
  40. static struct npc_src_list *npc_src_first=NULL;
  41. static struct npc_src_list *npc_src_last=NULL;
  42. static int npc_id=START_NPC_NUM;
  43. static int npc_warp=0;
  44. static int npc_shop=0;
  45. static int npc_script=0;
  46. static int npc_mob=0;
  47. static int npc_delay_mob=0;
  48. static int npc_cache_mob=0;
  49. char *current_file = NULL;
  50. //int dummy_npc_id=0;
  51. int npc_get_new_npc_id(void){ return npc_id++; }
  52. static struct dbt *ev_db;
  53. static struct dbt *npcname_db;
  54. struct event_data {
  55. struct npc_data *nd;
  56. int pos;
  57. };
  58. static struct tm ev_tm_b; // 時計イベント用
  59. static struct eri *timer_event_ers; //For the npc timer data. [Skotlex]
  60. //For holding the view data of npc classes. [Skotlex]
  61. static struct view_data npc_viewdb[MAX_NPC_CLASS];
  62. static struct
  63. { //Holds pointers to the commonly executed scripts for speedup. [Skotlex]
  64. struct npc_data *nd;
  65. struct event_data *event[UCHAR_MAX];
  66. unsigned char *event_name[UCHAR_MAX];
  67. unsigned char event_count;
  68. } script_event[NPCE_MAX];
  69. struct view_data* npc_get_viewdata(int class_)
  70. { //Returns the viewdata for normal npc classes.
  71. if (class_ == INVISIBLE_CLASS)
  72. return &npc_viewdb[0];
  73. if (npcdb_checkid(class_) || class_ == WARP_CLASS)
  74. return &npc_viewdb[class_];
  75. return NULL;
  76. }
  77. /*==========================================
  78. * NPCの無効化/有効化
  79. * npc_enable
  80. * npc_enable_sub 有効時にOnTouchイベントを実行
  81. *------------------------------------------
  82. */
  83. int npc_enable_sub( struct block_list *bl, va_list ap )
  84. {
  85. struct map_session_data *sd;
  86. struct npc_data *nd;
  87. //char *name=(char *)aCallocA(50,sizeof(char)); // fixed [Shinomori]
  88. nullpo_retr(0, bl);
  89. nullpo_retr(0, ap);
  90. nullpo_retr(0, nd=va_arg(ap,struct npc_data *));
  91. if(bl->type == BL_PC && (sd=(struct map_session_data *)bl)){
  92. char name[50]; // need 24 + 9 for the "::OnTouch"
  93. if (nd->sc.option&OPTION_INVISIBLE) // 無効化されている
  94. return 1;
  95. if(sd->areanpc_id==nd->bl.id)
  96. return 1;
  97. sd->areanpc_id=nd->bl.id;
  98. snprintf(name, 50, "%s::OnTouch", nd->exname); // exname to be specific. exname is the unique identifier for script events. [Lance]
  99. npc_event(sd,name,0);
  100. }
  101. //aFree(name);
  102. return 0;
  103. }
  104. int npc_enable(const char *name,int flag)
  105. {
  106. struct npc_data *nd= strdb_get(npcname_db,(unsigned char*)name);
  107. if (nd==NULL)
  108. return 0;
  109. if (flag&1)
  110. nd->sc.option&=~OPTION_INVISIBLE;
  111. else if (flag&2)
  112. nd->sc.option&=~OPTION_HIDE;
  113. else if (flag&4)
  114. nd->sc.option|= OPTION_HIDE;
  115. else //Can't change the view_data to invisible class because the view_data for all npcs is shared! [Skotlex]
  116. nd->sc.option|= OPTION_INVISIBLE;
  117. if (nd->class_ == WARP_CLASS)
  118. { //Client won't display option changes for warp portals [Toms]
  119. if (nd->sc.option&(OPTION_HIDE|OPTION_INVISIBLE))
  120. clif_clearchar(&nd->bl, 0);
  121. else
  122. clif_spawn(&nd->bl);
  123. } else
  124. clif_changeoption(&nd->bl);
  125. if(flag&3 && (nd->u.scr.xs > 0 || nd->u.scr.ys >0))
  126. map_foreachinarea( npc_enable_sub,nd->bl.m,nd->bl.x-nd->u.scr.xs,nd->bl.y-nd->u.scr.ys,nd->bl.x+nd->u.scr.xs,nd->bl.y+nd->u.scr.ys,BL_PC,nd);
  127. return 0;
  128. }
  129. /*==========================================
  130. * NPCを名前で探す
  131. *------------------------------------------
  132. */
  133. struct npc_data* npc_name2id(const char *name)
  134. {
  135. return (struct npc_data *) strdb_get(npcname_db,(unsigned char*)name);
  136. }
  137. /*==========================================
  138. * イベントキューのイベント処理
  139. *------------------------------------------
  140. */
  141. int npc_event_dequeue(struct map_session_data *sd)
  142. {
  143. nullpo_retr(0, sd);
  144. sd->npc_id=0;
  145. if (sd->eventqueue[0][0]) { // キューのイベント処理
  146. size_t ev;
  147. // find an empty place in eventtimer list
  148. for(ev=0;ev<MAX_EVENTTIMER;ev++)
  149. if( sd->eventtimer[ev]==-1 )
  150. break;
  151. if(ev<MAX_EVENTTIMER)
  152. { // generate and insert the timer
  153. int i;
  154. // copy the first event name
  155. char *name=(char *)aMalloc(50*sizeof(char));
  156. memcpy(name,sd->eventqueue[0],50);
  157. // shift queued events down by one
  158. for(i=1;i<MAX_EVENTQUEUE;i++)
  159. memcpy(sd->eventqueue[i-1],sd->eventqueue[i],50);
  160. // clear the last event
  161. sd->eventqueue[MAX_EVENTQUEUE-1][0]=0;
  162. // add the timer
  163. sd->eventtimer[ev]=add_timer(gettick()+100,pc_eventtimer,sd->bl.id,(int)name);//!!todo!!
  164. }else
  165. ShowWarning("npc_event_dequeue: event timer is full !\n");
  166. }
  167. return 0;
  168. }
  169. /*==========================================
  170. * イベントの遅延実行
  171. *------------------------------------------
  172. */
  173. int npc_event_timer(int tid,unsigned int tick,int id,int data)
  174. {
  175. unsigned char *eventname = (unsigned char *)data;
  176. struct event_data *ev = strdb_get(ev_db,eventname);
  177. struct npc_data *nd;
  178. struct map_session_data *sd=map_id2sd(id);
  179. size_t i;
  180. if((ev==NULL || (nd=ev->nd)==NULL))
  181. {
  182. if(battle_config.error_log)
  183. ShowWarning("npc_event: event not found [%s]\n",eventname);
  184. }
  185. else
  186. {
  187. for(i=0;i<MAX_EVENTTIMER;i++) {
  188. if( nd->eventtimer[i]==tid ) {
  189. nd->eventtimer[i]=-1;
  190. npc_event(sd,eventname,0); // sd NULL check is within
  191. break;
  192. }
  193. }
  194. if(i==MAX_EVENTTIMER && battle_config.error_log)
  195. ShowWarning("npc_event_timer: event timer not found [%s]!\n",eventname);
  196. }
  197. aFree(eventname);
  198. return 0;
  199. }
  200. int npc_timer_event(const unsigned char *eventname) // Added by RoVeRT
  201. {
  202. struct event_data *ev=strdb_get(ev_db,(unsigned char*)eventname);
  203. struct npc_data *nd;
  204. // int xs,ys;
  205. if((ev==NULL || (nd=ev->nd)==NULL)){
  206. ShowWarning("npc_timer_event: event not found [%s]\n",eventname);
  207. return 0;
  208. }
  209. run_script(nd->u.scr.script,ev->pos,nd->bl.id,nd->bl.id);
  210. return 0;
  211. }
  212. /*
  213. int npc_timer_sub_sub(DBKey key,void *data,va_list ap) // Added by RoVeRT
  214. {
  215. char *p=(char *)key;
  216. struct event_data *ev=(struct event_data *)data;
  217. int *c=va_arg(ap,int *);
  218. int tick=0,ctick=gettick();
  219. char temp[10];
  220. char event[100];
  221. if(ev->nd->bl.id==(int)*c && (p=strchr(p,':')) && p && strncasecmp("::OnTimer",p,8)==0 ){
  222. sscanf(&p[9],"%s",temp);
  223. tick=atoi(temp);
  224. strcpy( event, ev->nd->name);
  225. strcat( event, p);
  226. if (ctick >= ev->nd->lastaction && ctick - ev->nd->timer >= tick) {
  227. npc_timer_event(event);
  228. ev->nd->lastaction = ctick;
  229. }
  230. }
  231. return 0;
  232. }
  233. int npc_timer_sub(DBKey key,void *data,va_list ap) // Added by RoVeRT
  234. {
  235. struct npc_data *nd=(struct npc_data*)data;
  236. if(nd->timer == -1)
  237. return 0;
  238. sv_db->foreach(ev_db,npc_timer_sub_sub,&nd->bl.id);
  239. return 0;
  240. }
  241. int npc_timer(int tid,unsigned int tick,int id,int data) // Added by RoVeRT
  242. {
  243. npcname_db->foreach(npcname_db,npc_timer_sub);
  244. aFree((void*)data);
  245. return 0;
  246. }*/
  247. /*==========================================
  248. * イベント用ラベルのエクスポート
  249. * npc_parse_script->strdb_foreachから呼ばれる
  250. *------------------------------------------
  251. */
  252. int npc_event_export(char *lname,void *data,va_list ap)
  253. {
  254. int pos=(int)data;
  255. struct npc_data *nd=va_arg(ap,struct npc_data *);
  256. if ((lname[0]=='O' || lname[0]=='o')&&(lname[1]=='N' || lname[1]=='n')) {
  257. struct event_data *ev;
  258. unsigned char buf[51];
  259. char *p=strchr(lname,':');
  260. // エクスポートされる
  261. ev=(struct event_data *) aMalloc(sizeof(struct event_data));
  262. if (ev==NULL) {
  263. ShowFatalError("npc_event_export: out of memory !\n");
  264. exit(1);
  265. }else if (p==NULL || (p-lname)>NAME_LENGTH) {
  266. ShowFatalError("npc_event_export: label name error !\n");
  267. exit(1);
  268. }else{
  269. ev->nd=nd;
  270. ev->pos=pos;
  271. *p='\0';
  272. sprintf(buf,"%s::%s",nd->exname,lname);
  273. *p=':';
  274. strdb_put(ev_db,buf,ev);
  275. }
  276. }
  277. return 0;
  278. }
  279. int npc_event_sub(struct map_session_data *, struct event_data *, const unsigned char *); //[Lance]
  280. /*==========================================
  281. * 全てのNPCのOn*イベント実行
  282. *------------------------------------------
  283. */
  284. int npc_event_doall_sub(DBKey key,void *data,va_list ap)
  285. {
  286. unsigned char*p = key.str;
  287. struct event_data *ev;
  288. int *c;
  289. int rid;
  290. unsigned char *name;
  291. ev=(struct event_data *)data;
  292. c=va_arg(ap,int *);
  293. name=va_arg(ap,unsigned char *);
  294. rid=va_arg(ap, int);
  295. if( (p=strchr(p,':')) && p && strcmpi(name,p)==0 ){
  296. if(rid)
  297. npc_event_sub(((struct map_session_data *)map_id2bl(rid)),ev,key.str);
  298. else
  299. run_script(ev->nd->u.scr.script,ev->pos,rid,ev->nd->bl.id);
  300. (*c)++;
  301. }
  302. return 0;
  303. }
  304. int npc_event_doall(const unsigned char *name)
  305. {
  306. int c=0;
  307. unsigned char buf[64]="::";
  308. strncpy(buf+2,name,62);
  309. ev_db->foreach(ev_db,npc_event_doall_sub,&c,buf,0);
  310. return c;
  311. }
  312. int npc_event_doall_id(const unsigned char *name, int rid)
  313. {
  314. int c=0;
  315. unsigned char buf[64]="::";
  316. strncpy(buf+2,name,62);
  317. ev_db->foreach(ev_db,npc_event_doall_sub,&c,buf,rid);
  318. return c;
  319. }
  320. int npc_event_do_sub(DBKey key,void *data,va_list ap)
  321. {
  322. unsigned char *p = key.str;
  323. struct event_data *ev;
  324. int *c;
  325. const unsigned char *name;
  326. nullpo_retr(0, ev=(struct event_data *)data);
  327. nullpo_retr(0, ap);
  328. nullpo_retr(0, c=va_arg(ap,int *));
  329. name=va_arg(ap,const unsigned char *);
  330. if (p && strcmpi(name,p)==0 ) {
  331. run_script(ev->nd->u.scr.script,ev->pos,0,ev->nd->bl.id);
  332. (*c)++;
  333. }
  334. return 0;
  335. }
  336. int npc_event_do(const unsigned char *name)
  337. {
  338. int c=0;
  339. if (*name==':' && name[1]==':') {
  340. return npc_event_doall(name+2);
  341. }
  342. ev_db->foreach(ev_db,npc_event_do_sub,&c,name);
  343. return c;
  344. }
  345. /*==========================================
  346. * 時計イベント実行
  347. *------------------------------------------
  348. */
  349. int npc_event_do_clock(int tid,unsigned int tick,int id,int data)
  350. {
  351. time_t timer;
  352. struct tm *t;
  353. char buf[64];
  354. char *day="";
  355. int c=0;
  356. time(&timer);
  357. t=localtime(&timer);
  358. switch (t->tm_wday) {
  359. case 0: day = "Sun"; break;
  360. case 1: day = "Mon"; break;
  361. case 2: day = "Tue"; break;
  362. case 3: day = "Wed"; break;
  363. case 4: day = "Thu"; break;
  364. case 5: day = "Fri"; break;
  365. case 6: day = "Sat"; break;
  366. }
  367. if (t->tm_min != ev_tm_b.tm_min ) {
  368. sprintf(buf,"OnMinute%02d",t->tm_min);
  369. c+=npc_event_doall(buf);
  370. sprintf(buf,"OnClock%02d%02d",t->tm_hour,t->tm_min);
  371. c+=npc_event_doall(buf);
  372. sprintf(buf,"On%s%02d%02d",day,t->tm_hour,t->tm_min);
  373. c+=npc_event_doall(buf);
  374. }
  375. if (t->tm_hour!= ev_tm_b.tm_hour) {
  376. sprintf(buf,"OnHour%02d",t->tm_hour);
  377. c+=npc_event_doall(buf);
  378. }
  379. if (t->tm_mday!= ev_tm_b.tm_mday) {
  380. sprintf(buf,"OnDay%02d%02d",t->tm_mon+1,t->tm_mday);
  381. c+=npc_event_doall(buf);
  382. }
  383. memcpy(&ev_tm_b,t,sizeof(ev_tm_b));
  384. return c;
  385. }
  386. /*==========================================
  387. * OnInitイベント実行(&時計イベント開始)
  388. *------------------------------------------
  389. */
  390. int npc_event_do_oninit(void)
  391. {
  392. // int c = npc_event_doall("OnInit");
  393. ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"
  394. CL_WHITE"%d"CL_RESET"' NPCs.\n",npc_event_doall("OnInit"));
  395. add_timer_interval(gettick()+100,
  396. npc_event_do_clock,0,0,1000);
  397. return 0;
  398. }
  399. /*==========================================
  400. * OnTimer NPC event - by RoVeRT
  401. *------------------------------------------
  402. */
  403. int npc_addeventtimer(struct npc_data *nd,int tick,const char *name)
  404. {
  405. int i;
  406. unsigned char *evname;
  407. for(i=0;i<MAX_EVENTTIMER;i++)
  408. if( nd->eventtimer[i]==-1 )
  409. break;
  410. if(i<MAX_EVENTTIMER){
  411. if (!strdb_get(ev_db,(unsigned char*)name)) {
  412. if (battle_config.error_log)
  413. ShowError("npc_addeventimer: Event %s does not exists.\n", name);
  414. return 1; //Event does not exists!
  415. }
  416. evname =(unsigned char *) aMallocA(NAME_LENGTH*sizeof(char));
  417. if(evname==NULL){
  418. ShowFatalError("npc_addeventtimer: out of memory !\n");exit(1);
  419. }
  420. memcpy(evname,name,NAME_LENGTH-1);
  421. evname[NAME_LENGTH-1] = '\0';
  422. nd->eventtimer[i]=add_timer(gettick()+tick,
  423. npc_event_timer,nd->bl.id,(int)evname);
  424. }else
  425. ShowWarning("npc_addtimer: event timer is full !\n");
  426. return 0;
  427. }
  428. int npc_deleventtimer(struct npc_data *nd,const unsigned char *name)
  429. {
  430. int i;
  431. for(i=0;i<MAX_EVENTTIMER;i++)
  432. if( nd->eventtimer[i]!=-1 && strcmp(
  433. (unsigned char *)(get_timer(nd->eventtimer[i])->data), name)==0 ){
  434. delete_timer(nd->eventtimer[i],npc_event_timer);
  435. nd->eventtimer[i]=-1;
  436. break;
  437. }
  438. return 0;
  439. }
  440. int npc_cleareventtimer(struct npc_data *nd)
  441. {
  442. int i;
  443. for(i=0;i<MAX_EVENTTIMER;i++)
  444. if( nd->eventtimer[i]!=-1 ){
  445. delete_timer(nd->eventtimer[i],npc_event_timer);
  446. nd->eventtimer[i]=-1;
  447. }
  448. return 0;
  449. }
  450. int npc_do_ontimer_sub(DBKey key,void *data,va_list ap)
  451. {
  452. unsigned char *p = key.str;
  453. struct event_data *ev = (struct event_data *)data;
  454. int *c = va_arg(ap,int *);
  455. // struct map_session_data *sd=va_arg(ap,struct map_session_data *);
  456. int option = va_arg(ap,int);
  457. int tick = 0;
  458. char temp[10];
  459. char event[50];
  460. if(ev->nd->bl.id == (int)*c && (p = strchr(p,':')) && strnicmp("::OnTimer",p,8) == 0){
  461. sscanf(&p[9], "%s", temp);
  462. tick = atoi(temp);
  463. strcpy(event, ev->nd->name);
  464. strcat(event, p);
  465. if (option!=0) {
  466. npc_addeventtimer(ev->nd, tick, event);
  467. } else {
  468. npc_deleventtimer(ev->nd, event);
  469. }
  470. }
  471. return 0;
  472. }
  473. int npc_do_ontimer(int npc_id, int option)
  474. {
  475. ev_db->foreach(ev_db, npc_do_ontimer_sub, &npc_id, option);
  476. return 0;
  477. }
  478. /*==========================================
  479. * タイマーイベント用ラベルの取り込み
  480. * npc_parse_script->strdb_foreachから呼ばれる
  481. *------------------------------------------
  482. */
  483. int npc_timerevent_import(char *lname,void *data,va_list ap)
  484. {
  485. int pos=(int)data;
  486. struct npc_data *nd=va_arg(ap,struct npc_data *);
  487. int t=0,i=0;
  488. if(sscanf(lname,"OnTimer%d%n",&t,&i)==1 && lname[i]==':') {
  489. // タイマーイベント
  490. struct npc_timerevent_list *te=nd->u.scr.timer_event;
  491. int j,i=nd->u.scr.timeramount;
  492. if(te==NULL) te=(struct npc_timerevent_list*)aMallocA(sizeof(struct npc_timerevent_list));
  493. else te= (struct npc_timerevent_list*)aRealloc( te, sizeof(struct npc_timerevent_list) * (i+1) );
  494. if(te==NULL){
  495. ShowFatalError("npc_timerevent_import: out of memory !\n");
  496. exit(1);
  497. }
  498. for(j=0;j<i;j++){
  499. if(te[j].timer>t){
  500. memmove(te+j+1,te+j,sizeof(struct npc_timerevent_list)*(i-j));
  501. break;
  502. }
  503. }
  504. te[j].timer=t;
  505. te[j].pos=pos;
  506. nd->u.scr.timer_event=te;
  507. nd->u.scr.timeramount++;
  508. }
  509. return 0;
  510. }
  511. struct timer_event_data {
  512. int rid; //Attached player for this timer.
  513. int next; //timer index (starts with 0, then goes up to nd->u.scr.timeramount
  514. int time; //holds total time elapsed for the script since time 0 (whenthe timers started)
  515. unsigned int otick; //Holds tick value at which timer sequence was started (that is, it stores the tick value for which T= 0
  516. };
  517. /*==========================================
  518. * タイマーイベント実行
  519. *------------------------------------------
  520. */
  521. int npc_timerevent(int tid,unsigned int tick,int id,int data)
  522. {
  523. int next,t,old_rid,old_timer;
  524. unsigned int old_tick;
  525. struct npc_data* nd=(struct npc_data *)map_id2bl(id);
  526. struct npc_timerevent_list *te;
  527. struct timer_event_data *ted = (struct timer_event_data*)data;
  528. struct map_session_data *sd=NULL;
  529. if( nd==NULL ){
  530. ShowError("npc_timerevent: NPC not found??\n");
  531. return 0;
  532. }
  533. if (ted->rid) {
  534. sd = map_id2sd(ted->rid);
  535. if (!sd) {
  536. if(battle_config.error_log)
  537. ShowError("npc_timerevent: Attached player not found.\n");
  538. ers_free(timer_event_ers, ted);
  539. return 0;
  540. }
  541. }
  542. old_rid = nd->u.scr.rid; //To restore it later.
  543. nd->u.scr.rid = sd?sd->bl.id:0;
  544. old_tick = nd->u.scr.timertick;
  545. nd->u.scr.timertick=ted->otick;
  546. te=nd->u.scr.timer_event+ ted->next;
  547. old_timer = nd->u.scr.timer;
  548. t = nd->u.scr.timer=ted->time;
  549. ted->next++;
  550. if( nd->u.scr.timeramount> ted->next){
  551. next= nd->u.scr.timer_event[ ted->next ].timer
  552. - nd->u.scr.timer_event[ ted->next-1 ].timer;
  553. ted->time+=next;
  554. if (sd)
  555. sd->npc_timer_id = add_timer(tick+next,npc_timerevent,id,(int)ted);
  556. else
  557. nd->u.scr.timerid = add_timer(tick+next,npc_timerevent,id,(int)ted);
  558. } else {
  559. if (sd)
  560. sd->npc_timer_id = -1;
  561. else
  562. nd->u.scr.timerid = -1;
  563. ers_free(timer_event_ers, ted);
  564. }
  565. run_script(nd->u.scr.script,te->pos,nd->u.scr.rid,nd->bl.id);
  566. //Restore previous data, only if this timer is a player-attached one.
  567. if (sd) {
  568. nd->u.scr.rid = old_rid;
  569. nd->u.scr.timer = old_timer;
  570. nd->u.scr.timertick = old_tick;
  571. }
  572. return 0;
  573. }
  574. /*==========================================
  575. * タイマーイベント開始
  576. *------------------------------------------
  577. */
  578. int npc_timerevent_start(struct npc_data *nd, int rid)
  579. {
  580. int j,n, next;
  581. struct map_session_data *sd=NULL; //Player to whom script is attached.
  582. struct timer_event_data *ted;
  583. nullpo_retr(0, nd);
  584. n=nd->u.scr.timeramount;
  585. if( n==0 )
  586. return 0;
  587. for(j=0;j<n;j++){
  588. if( nd->u.scr.timer_event[j].timer > nd->u.scr.timer )
  589. break;
  590. }
  591. if(j>=n) // check if there is a timer to use !!BEFORE!! you write stuff to the structures [Shinomori]
  592. return 0;
  593. if (nd->u.scr.rid > 0) {
  594. //Try to attach timer to this player.
  595. sd = map_id2sd(nd->u.scr.rid);
  596. if (!sd) {
  597. if(battle_config.error_log)
  598. ShowError("npc_timerevent_start: Attached player not found!\n");
  599. return 1;
  600. }
  601. }
  602. //Check if timer is already started.
  603. if (sd) {
  604. if (sd->npc_timer_id != -1)
  605. return 0;
  606. } else if (nd->u.scr.timerid != -1)
  607. return 0;
  608. ted = ers_alloc(timer_event_ers, struct timer_event_data);
  609. ted->next = j;
  610. nd->u.scr.timertick=ted->otick=gettick();
  611. //Attach only the player if attachplayerrid was used.
  612. ted->rid = sd?sd->bl.id:0;
  613. // Do not store it to make way to two types of timers: globals and personals.
  614. // if (rid >= 0) nd->u.scr.rid=rid; // changed to: attaching to given rid by default [Shinomori]
  615. // if rid is less than 0 leave it unchanged [celest]
  616. next = nd->u.scr.timer_event[j].timer - nd->u.scr.timer;
  617. ted->time = nd->u.scr.timer_event[j].timer;
  618. if (sd)
  619. sd->npc_timer_id = add_timer(gettick()+next,npc_timerevent,nd->bl.id,(int)ted);
  620. else
  621. nd->u.scr.timerid = add_timer(gettick()+next,npc_timerevent,nd->bl.id,(int)ted);
  622. return 0;
  623. }
  624. /*==========================================
  625. * タイマーイベント終了
  626. *------------------------------------------
  627. */
  628. int npc_timerevent_stop(struct npc_data *nd)
  629. {
  630. struct map_session_data *sd =NULL;
  631. struct TimerData *td = NULL;
  632. int *tid;
  633. nullpo_retr(0, nd);
  634. if (nd->u.scr.rid) {
  635. sd = map_id2sd(nd->u.scr.rid);
  636. if (!sd) {
  637. if(battle_config.error_log)
  638. ShowError("npc_timerevent_stop: Attached player not found!\n");
  639. return 1;
  640. }
  641. }
  642. tid = sd?&sd->npc_timer_id:&nd->u.scr.timerid;
  643. if (*tid == -1) //Nothing to stop
  644. return 0;
  645. td = get_timer(*tid);
  646. if (td && td->data)
  647. ers_free(timer_event_ers, (struct event_timer_data*)td->data);
  648. delete_timer(*tid,npc_timerevent);
  649. *tid = -1;
  650. //Set the timer tick to the time that has passed since the beginning of the timers and now.
  651. nd->u.scr.timer = DIFF_TICK(gettick(),nd->u.scr.timertick);
  652. // nd->u.scr.rid = 0; //Eh? why detach?
  653. return 0;
  654. }
  655. /*==========================================
  656. * Aborts a running npc timer that is attached to a player.
  657. *------------------------------------------
  658. */
  659. void npc_timerevent_quit(struct map_session_data *sd) {
  660. struct TimerData *td;
  661. if (sd->npc_timer_id == -1)
  662. return;
  663. td = get_timer(sd->npc_timer_id);
  664. if (!td) {
  665. sd->npc_timer_id = -1;
  666. return; //??
  667. }
  668. delete_timer(sd->npc_timer_id,npc_timerevent);
  669. sd->npc_timer_id = -1;
  670. ers_free(timer_event_ers, (struct event_timer_data*)td->data);
  671. }
  672. /*==========================================
  673. * タイマー値の所得
  674. *------------------------------------------
  675. */
  676. int npc_gettimerevent_tick(struct npc_data *nd)
  677. {
  678. int tick;
  679. nullpo_retr(0, nd);
  680. tick=nd->u.scr.timer;
  681. if (nd->u.scr.timertick)
  682. tick+=DIFF_TICK(gettick(), nd->u.scr.timertick);
  683. return tick;
  684. }
  685. /*==========================================
  686. * タイマー値の設定
  687. *------------------------------------------
  688. */
  689. int npc_settimerevent_tick(struct npc_data *nd,int newtimer)
  690. {
  691. int flag;
  692. struct map_session_data *sd=NULL;
  693. nullpo_retr(0, nd);
  694. if (nd->u.scr.rid) {
  695. sd = map_id2sd(nd->u.scr.rid);
  696. if (!sd) {
  697. if(battle_config.error_log)
  698. ShowError("npc_settimerevent_tick: Attached player not found!\n");
  699. return 1;
  700. }
  701. flag= sd->npc_timer_id != -1 ;
  702. } else
  703. flag= nd->u.scr.timerid != -1 ;
  704. if(flag)
  705. npc_timerevent_stop(nd);
  706. nd->u.scr.timer=newtimer;
  707. if(flag)
  708. npc_timerevent_start(nd, -1);
  709. return 0;
  710. }
  711. int npc_event_sub(struct map_session_data *sd, struct event_data *ev, const unsigned char *eventname){
  712. if ( sd->npc_id!=0) {
  713. // if (battle_config.error_log)
  714. // printf("npc_event: npc_id != 0\n");
  715. int i;
  716. for(i=0;i<MAX_EVENTQUEUE;i++)
  717. if (!sd->eventqueue[i][0])
  718. break;
  719. if (i==MAX_EVENTQUEUE) {
  720. if (battle_config.error_log)
  721. ShowWarning("npc_event: event queue is full !\n");
  722. }else{
  723. // if (battle_config.etc_log)
  724. // printf("npc_event: enqueue\n");
  725. memcpy(sd->eventqueue[i],eventname,50);
  726. }
  727. return 1;
  728. }
  729. if (ev->nd->sc.option&OPTION_INVISIBLE) { // 無効化されている
  730. npc_event_dequeue(sd);
  731. return 0;
  732. }
  733. run_script(ev->nd->u.scr.script,ev->pos,sd->bl.id,ev->nd->bl.id);
  734. return 0;
  735. }
  736. /*==========================================
  737. * イベント型のNPC処理
  738. *------------------------------------------
  739. */
  740. int npc_event (struct map_session_data *sd, const unsigned char *eventname, int mob_kill)
  741. {
  742. struct event_data *ev=strdb_get(ev_db,(unsigned char*)eventname);
  743. struct npc_data *nd;
  744. int xs,ys;
  745. unsigned char mobevent[100];
  746. if (sd == NULL) {
  747. nullpo_info(NLP_MARK);
  748. return 0;
  749. }
  750. if (ev == NULL && eventname && strcmp(((eventname)+strlen(eventname)-9),"::OnTouch") == 0)
  751. return 1;
  752. if (ev == NULL || (nd = ev->nd) == NULL) {
  753. if (mob_kill) {
  754. strcpy( mobevent, eventname);
  755. strcat( mobevent, "::OnMyMobDead");
  756. ev = strdb_get(ev_db, mobevent);
  757. if (ev == NULL || (nd = ev->nd) == NULL) {
  758. if (strnicmp(eventname, "GM_MONSTER",10) != 0)
  759. ShowError("npc_event: (mob_kill) event not found [%s]\n", mobevent);
  760. return 0;
  761. }
  762. } else {
  763. if (battle_config.error_log)
  764. ShowError("npc_event: event not found [%s]\n", eventname);
  765. return 0;
  766. }
  767. }
  768. xs=nd->u.scr.xs;
  769. ys=nd->u.scr.ys;
  770. if (xs>=0 && ys>=0 && (strcmp(((eventname)+strlen(eventname)-6),"Global") != 0) )
  771. {
  772. if (nd->bl.m >= 0) { //Non-invisible npc
  773. if (nd->bl.m != sd->bl.m )
  774. return 1;
  775. if ( xs>0 && (sd->bl.x<nd->bl.x-xs/2 || nd->bl.x+xs/2<sd->bl.x) )
  776. return 1;
  777. if ( ys>0 && (sd->bl.y<nd->bl.y-ys/2 || nd->bl.y+ys/2<sd->bl.y) )
  778. return 1;
  779. }
  780. }
  781. return npc_event_sub(sd,ev,eventname);
  782. }
  783. int npc_command_sub(DBKey key,void *data,va_list ap)
  784. {
  785. unsigned char *p = key.str;
  786. struct event_data *ev=(struct event_data *)data;
  787. unsigned char *npcname=va_arg(ap,char *);
  788. char *command=va_arg(ap,char *);
  789. unsigned char temp[100];
  790. if(strcmp(ev->nd->name,npcname)==0 && (p=strchr(p,':')) && p && strnicmp("::OnCommand",p,10)==0 ){
  791. sscanf(&p[11],"%s",temp);
  792. if (strcmp(command,temp)==0)
  793. run_script(ev->nd->u.scr.script,ev->pos,0,ev->nd->bl.id);
  794. }
  795. return 0;
  796. }
  797. int npc_command(struct map_session_data *sd,const unsigned char *npcname,char *command)
  798. {
  799. ev_db->foreach(ev_db,npc_command_sub,npcname,command);
  800. return 0;
  801. }
  802. /*==========================================
  803. * 接触型のNPC処理
  804. *------------------------------------------
  805. */
  806. int npc_touch_areanpc(struct map_session_data *sd,int m,int x,int y)
  807. {
  808. int i,f=1;
  809. int xs,ys;
  810. nullpo_retr(1, sd);
  811. if(sd->npc_id)
  812. return 1;
  813. for(i=0;i<map[m].npc_num;i++) {
  814. if (map[m].npc[i]->sc.option&OPTION_INVISIBLE) { // 無効化されている
  815. f=0;
  816. continue;
  817. }
  818. switch(map[m].npc[i]->bl.subtype) {
  819. case WARP:
  820. xs=map[m].npc[i]->u.warp.xs;
  821. ys=map[m].npc[i]->u.warp.ys;
  822. break;
  823. case SCRIPT:
  824. xs=map[m].npc[i]->u.scr.xs;
  825. ys=map[m].npc[i]->u.scr.ys;
  826. break;
  827. default:
  828. continue;
  829. }
  830. if (x >= map[m].npc[i]->bl.x-xs/2 && x < map[m].npc[i]->bl.x-xs/2+xs &&
  831. y >= map[m].npc[i]->bl.y-ys/2 && y < map[m].npc[i]->bl.y-ys/2+ys)
  832. break;
  833. }
  834. if (i==map[m].npc_num) {
  835. if (f) {
  836. if (battle_config.error_log)
  837. ShowError("npc_touch_areanpc : some bug \n");
  838. }
  839. return 1;
  840. }
  841. switch(map[m].npc[i]->bl.subtype) {
  842. case WARP:
  843. // hidden chars cannot use warps -- is it the same for scripts too?
  844. if (sd->sc.option&(OPTION_HIDE|OPTION_CLOAK|OPTION_CHASEWALK) ||
  845. (!battle_config.duel_allow_teleport && sd->duel_group)) // duel rstrct [LuzZza]
  846. break;
  847. pc_setpos(sd,map[m].npc[i]->u.warp.mapindex,map[m].npc[i]->u.warp.x,map[m].npc[i]->u.warp.y,0);
  848. break;
  849. case SCRIPT:
  850. {
  851. //char *name=(char *)aCallocA(50,sizeof(char)); // fixed [Shinomori]
  852. char name[50]; // need 24 max + 9 for "::OnTouch"
  853. if(sd->areanpc_id == map[m].npc[i]->bl.id)
  854. return 1;
  855. sd->areanpc_id = map[m].npc[i]->bl.id;
  856. sprintf(name,"%s::OnTouch", map[m].npc[i]->exname); // It goes here too. exname being the unique identifier. [Lance]
  857. if( npc_event(sd,name,0)>0 ) {
  858. pc_stop_walking(sd,1); //Make it stop walking!
  859. npc_click(sd,map[m].npc[i]);
  860. }
  861. //aFree(name);
  862. break;
  863. }
  864. }
  865. return 0;
  866. }
  867. int npc_touch_areanpc2(struct block_list *bl)
  868. {
  869. int i,m=bl->m;
  870. int xs,ys;
  871. for(i=0;i<map[m].npc_num;i++) {
  872. if (map[m].npc[i]->sc.option&OPTION_INVISIBLE)
  873. continue;
  874. if (map[m].npc[i]->bl.subtype!=WARP)
  875. continue;
  876. xs=map[m].npc[i]->u.warp.xs;
  877. ys=map[m].npc[i]->u.warp.ys;
  878. if (bl->x >= map[m].npc[i]->bl.x-xs/2 && bl->x < map[m].npc[i]->bl.x-xs/2+xs &&
  879. bl->y >= map[m].npc[i]->bl.y-ys/2 && bl->y < map[m].npc[i]->bl.y-ys/2+ys)
  880. break;
  881. }
  882. if (i==map[m].npc_num)
  883. return 0;
  884. xs = map_mapindex2mapid(map[m].npc[i]->u.warp.mapindex);
  885. if (xs < 0) // Can't warp object between map servers...
  886. return 0;
  887. if (unit_warp(bl, xs, map[m].npc[i]->u.warp.x,map[m].npc[i]->u.warp.y,0))
  888. return 0; //Failed to warp.
  889. return 1;
  890. }
  891. /*==========================================
  892. * 近くかどうかの判定
  893. *------------------------------------------
  894. */
  895. int npc_checknear2(struct map_session_data *sd,struct block_list *bl)
  896. {
  897. nullpo_retr(1, sd);
  898. if(bl == NULL) return 1;
  899. if(sd->state.using_fake_npc && sd->npc_id == bl->id)
  900. return 0;
  901. if (status_get_class(bl)<0) //Class-less npc, enable click from anywhere.
  902. return 0;
  903. if (bl->m!=sd->bl.m ||
  904. bl->x<sd->bl.x-AREA_SIZE-1 || bl->x>sd->bl.x+AREA_SIZE+1 ||
  905. bl->y<sd->bl.y-AREA_SIZE-1 || bl->y>sd->bl.y+AREA_SIZE+1)
  906. return 1;
  907. return 0;
  908. }
  909. TBL_NPC *npc_checknear(struct map_session_data *sd,struct block_list *bl)
  910. {
  911. struct npc_data *nd;
  912. nullpo_retr(NULL, sd);
  913. if(bl == NULL) return NULL;
  914. if(bl->type != BL_NPC) return NULL;
  915. nd = (TBL_NPC*)bl;
  916. if(sd->state.using_fake_npc && sd->npc_id == bl->id)
  917. return nd;
  918. if (nd->class_<0) //Class-less npc, enable click from anywhere.
  919. return nd;
  920. if (bl->m!=sd->bl.m ||
  921. bl->x<sd->bl.x-AREA_SIZE-1 || bl->x>sd->bl.x+AREA_SIZE+1 ||
  922. bl->y<sd->bl.y-AREA_SIZE-1 || bl->y>sd->bl.y+AREA_SIZE+1)
  923. return NULL;
  924. return nd;
  925. }
  926. /*==========================================
  927. * NPCのオープンチャット発言
  928. *------------------------------------------
  929. */
  930. int npc_globalmessage(const char *name,char *mes)
  931. {
  932. struct npc_data *nd=(struct npc_data *) strdb_get(npcname_db,(unsigned char*)name);
  933. char temp[100];
  934. if (!nd)
  935. return 0;
  936. snprintf(temp, sizeof temp ,"%s : %s",name,mes);
  937. clif_GlobalMessage(&nd->bl,temp);
  938. return 0;
  939. }
  940. /*==========================================
  941. * クリック時のNPC処理
  942. *------------------------------------------
  943. */
  944. int npc_click(struct map_session_data *sd,struct npc_data *nd)
  945. {
  946. nullpo_retr(1, sd);
  947. if (sd->npc_id != 0) {
  948. if (battle_config.error_log)
  949. ShowError("npc_click: npc_id != 0\n");
  950. return 1;
  951. }
  952. if(!nd) return 1;
  953. if ((nd = npc_checknear(sd,&nd->bl)) == NULL)
  954. return 1;
  955. //Hidden/Disabled npc.
  956. if (nd->class_ < 0 || nd->sc.option&OPTION_INVISIBLE)
  957. return 1;
  958. switch(nd->bl.subtype) {
  959. case SHOP:
  960. clif_npcbuysell(sd,nd->bl.id);
  961. npc_event_dequeue(sd);
  962. break;
  963. case SCRIPT:
  964. run_script(nd->u.scr.script,0,sd->bl.id,nd->bl.id);
  965. break;
  966. }
  967. return 0;
  968. }
  969. /*==========================================
  970. *
  971. *------------------------------------------
  972. */
  973. int npc_scriptcont(struct map_session_data *sd,int id)
  974. {
  975. nullpo_retr(1, sd);
  976. if (id!=sd->npc_id){
  977. ShowWarning("npc_scriptcont: sd->npc_id (%d) is not id (%d).\n", sd->npc_id, id);
  978. return 1;
  979. }
  980. if(id != fake_nd->bl.id) { // Not item script
  981. if ((npc_checknear(sd,map_id2bl(id))) == NULL){
  982. ShowWarning("npc_scriptcont: failed npc_checknear test.\n");
  983. return 1;
  984. }
  985. }
  986. run_script_main(sd->st);
  987. return 0;
  988. }
  989. /*==========================================
  990. *
  991. *------------------------------------------
  992. */
  993. int npc_buysellsel(struct map_session_data *sd,int id,int type)
  994. {
  995. struct npc_data *nd;
  996. nullpo_retr(1, sd);
  997. if ((nd = npc_checknear(sd,map_id2bl(id))) == NULL)
  998. return 1;
  999. if (nd->bl.subtype!=SHOP) {
  1000. if (battle_config.error_log)
  1001. ShowError("no such shop npc : %d\n",id);
  1002. if (sd->npc_id == id)
  1003. sd->npc_id=0;
  1004. return 1;
  1005. }
  1006. if (nd->sc.option&OPTION_INVISIBLE) // 無効化されている
  1007. return 1;
  1008. sd->npc_shopid=id;
  1009. if (type==0) {
  1010. clif_buylist(sd,nd);
  1011. } else {
  1012. clif_selllist(sd);
  1013. }
  1014. return 0;
  1015. }
  1016. /*==========================================
  1017. *
  1018. *------------------------------------------
  1019. */
  1020. int npc_buylist(struct map_session_data *sd,int n,unsigned short *item_list)
  1021. {
  1022. struct npc_data *nd;
  1023. double z;
  1024. int i,j,w,skill,itemamount=0,new_=0;
  1025. nullpo_retr(3, sd);
  1026. nullpo_retr(3, item_list);
  1027. if ((nd = npc_checknear(sd,map_id2bl(sd->npc_shopid))) == NULL)
  1028. return 3;
  1029. if (nd->bl.subtype!=SHOP)
  1030. return 3;
  1031. for(i=0,w=0,z=0;i<n;i++) {
  1032. for(j=0;nd->u.shop_item[j].nameid;j++) {
  1033. if (nd->u.shop_item[j].nameid==item_list[i*2+1] || //Normal items
  1034. itemdb_viewid(nd->u.shop_item[j].nameid)==item_list[i*2+1]) //item_avail replacement
  1035. break;
  1036. }
  1037. if (nd->u.shop_item[j].nameid==0)
  1038. return 3;
  1039. if (!itemdb_isstackable(nd->u.shop_item[j].nameid) && item_list[i*2] > 1)
  1040. { //Exploit? You can't buy more than 1 of equipment types o.O
  1041. ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of nonstackable item %d!\n",
  1042. sd->status.name, sd->status.account_id, sd->status.char_id, item_list[i*2], nd->u.shop_item[j].nameid);
  1043. item_list[i*2] = 1;
  1044. }
  1045. if (itemdb_value_notdc(nd->u.shop_item[j].nameid))
  1046. z+=(double)nd->u.shop_item[j].value * item_list[i*2];
  1047. else
  1048. z+=(double)pc_modifybuyvalue(sd,nd->u.shop_item[j].value) * item_list[i*2];
  1049. itemamount+=item_list[i*2];
  1050. switch(pc_checkadditem(sd,item_list[i*2+1],item_list[i*2])) {
  1051. case ADDITEM_EXIST:
  1052. break;
  1053. case ADDITEM_NEW:
  1054. new_++;
  1055. break;
  1056. case ADDITEM_OVERAMOUNT:
  1057. return 2;
  1058. }
  1059. w+=itemdb_weight(item_list[i*2+1]) * item_list[i*2];
  1060. }
  1061. if (z > (double)sd->status.zeny)
  1062. return 1; // zeny不足
  1063. if (w+sd->weight > sd->max_weight)
  1064. return 2; // 重量超過
  1065. if (pc_inventoryblank(sd)<new_)
  1066. return 3; // 種類数超過
  1067. //Logs (S)hopping Zeny [Lupus]
  1068. if(log_config.zeny > 0 )
  1069. log_zeny(sd, "S", sd, -(int)z);
  1070. //Logs
  1071. pc_payzeny(sd,(int)z);
  1072. for(i=0;i<n;i++) {
  1073. struct item item_tmp;
  1074. memset(&item_tmp,0,sizeof(item_tmp));
  1075. item_tmp.nameid = item_list[i*2+1];
  1076. item_tmp.identify = 1; // npc販売アイテムは鑑定済み
  1077. pc_additem(sd,&item_tmp,item_list[i*2]);
  1078. //Logs items, Bought in NPC (S)hop [Lupus]
  1079. if(log_config.enable_logs&0x20)
  1080. log_pick(sd, "S", 0, item_tmp.nameid, item_list[i*2], NULL);
  1081. //Logs
  1082. }
  1083. //商人経験値
  1084. if (battle_config.shop_exp > 0 && z > 0 && (skill = pc_checkskill(sd,MC_DISCOUNT)) > 0) {
  1085. if (sd->status.skill[MC_DISCOUNT].flag != 0)
  1086. skill = sd->status.skill[MC_DISCOUNT].flag - 2;
  1087. if (skill > 0) {
  1088. z = z * (double)skill * (double)battle_config.shop_exp/10000.;
  1089. if (z < 1)
  1090. z = 1;
  1091. pc_gainexp(sd,NULL,0,(int)z);
  1092. }
  1093. }
  1094. return 0;
  1095. }
  1096. /*==========================================
  1097. *
  1098. *------------------------------------------
  1099. */
  1100. int npc_selllist(struct map_session_data *sd,int n,unsigned short *item_list)
  1101. {
  1102. double z;
  1103. int i,skill,itemamount=0;
  1104. struct npc_data *nd;
  1105. nullpo_retr(1, sd);
  1106. nullpo_retr(1, item_list);
  1107. if ((nd = npc_checknear(sd,map_id2bl(sd->npc_shopid))) == NULL)
  1108. return 1;
  1109. nd = nd->master_nd; //For OnSell triggers.
  1110. for(i=0,z=0;i<n;i++) {
  1111. int nameid, idx, qty;
  1112. idx = item_list[i*2]-2;
  1113. qty = item_list[i*2+1];
  1114. if (idx <0 || idx >=MAX_INVENTORY || qty < 0)
  1115. break;
  1116. nameid=sd->status.inventory[idx].nameid;
  1117. if (nameid == 0 || !sd->inventory_data[idx] ||
  1118. sd->status.inventory[idx].amount < qty)
  1119. break;
  1120. if (sd->inventory_data[idx]->flag.value_notoc)
  1121. z+=(double)qty*sd->inventory_data[idx]->value_sell;
  1122. else
  1123. z+=(double)qty*pc_modifysellvalue(sd,sd->inventory_data[idx]->value_sell);
  1124. if(sd->inventory_data[idx]->type==7 && sd->status.inventory[idx].card[0] == (short)0xff00)
  1125. {
  1126. if(search_petDB_index(sd->status.inventory[idx].nameid, PET_EGG) >= 0)
  1127. intif_delete_petdata(MakeDWord(sd->status.inventory[idx].card[1],sd->status.inventory[idx].card[2]));
  1128. }
  1129. if(log_config.enable_logs&0x20) //Logs items, Sold to NPC (S)hop [Lupus]
  1130. log_pick(sd, "S", 0, nameid, -qty, &sd->status.inventory[idx]);
  1131. if(nd) {
  1132. pc_setreg(sd,add_str("@sold_nameid")+(i<<24),(int)sd->status.inventory[idx].nameid);
  1133. pc_setreg(sd,add_str("@sold_quantity")+(i<<24),qty);
  1134. }
  1135. itemamount+=qty;
  1136. pc_delitem(sd,idx,qty,0);
  1137. }
  1138. if (z > MAX_ZENY) z = MAX_ZENY;
  1139. if(log_config.zeny) //Logs (S)hopping Zeny [Lupus]
  1140. log_zeny(sd, "S", sd, (int)z);
  1141. pc_getzeny(sd,(int)z);
  1142. if (battle_config.shop_exp > 0 && z > 0 && (skill = pc_checkskill(sd,MC_OVERCHARGE)) > 0) {
  1143. if (sd->status.skill[MC_OVERCHARGE].flag != 0)
  1144. skill = sd->status.skill[MC_OVERCHARGE].flag - 2;
  1145. if (skill > 0) {
  1146. z = z * (double)skill * (double)battle_config.shop_exp/10000.;
  1147. if (z < 1)
  1148. z = 1;
  1149. pc_gainexp(sd,NULL,0,(int)z);
  1150. }
  1151. }
  1152. if(nd) {
  1153. unsigned char npc_ev[51];
  1154. sprintf(npc_ev, "%s::OnSellItem", nd->exname);
  1155. npc_event(sd, npc_ev, 0);
  1156. }
  1157. if (i<n) {
  1158. //Error/Exploit... of some sort. If we return 1, the client will not mark
  1159. //any item as deleted even though a few were sold. In such a case, we
  1160. //have no recourse but to kick them out so their inventory will refresh
  1161. //correctly on relog. [Skotlex]
  1162. if (i) clif_setwaitclose(sd->fd);
  1163. return 1;
  1164. }
  1165. return 0;
  1166. }
  1167. int npc_remove_map (struct npc_data *nd)
  1168. {
  1169. int m,i;
  1170. nullpo_retr(1, nd);
  1171. if(nd->bl.prev == NULL || nd->bl.m < 0)
  1172. return 1; //Not assigned to a map.
  1173. m = nd->bl.m;
  1174. #ifdef PCRE_SUPPORT
  1175. npc_chat_finalize(nd);
  1176. #endif
  1177. clif_clearchar_area(&nd->bl,2);
  1178. strdb_remove(npcname_db, (nd->bl.subtype < SCRIPT) ? nd->name : nd->exname);
  1179. //Remove corresponding NPC CELLs
  1180. if (nd->bl.subtype == WARP) {
  1181. int j, xs, ys, x, y;
  1182. x = nd->bl.x;
  1183. y = nd->bl.y;
  1184. xs = nd->u.warp.xs;
  1185. ys = nd->u.warp.ys;
  1186. for (i = 0; i < ys; i++) {
  1187. for (j = 0; j < xs; j++) {
  1188. if (map_getcell(m, x-xs/2+j, y-ys/2+i, CELL_CHKNPC))
  1189. map_setcell(m, x-xs/2+j, y-ys/2+i, CELL_CLRNPC);
  1190. }
  1191. }
  1192. }
  1193. map_delblock(&nd->bl);
  1194. map_deliddb(&nd->bl);
  1195. //Remove npc from map[].npc list. [Skotlex]
  1196. for(i=0;i<map[m].npc_num && map[m].npc[i] != nd;i++);
  1197. if (i >= map[m].npc_num) return 2; //failed to find it?
  1198. map[m].npc_num--;
  1199. for(; i<map[m].npc_num; i++)
  1200. map[m].npc[i]=map[m].npc[i+1];
  1201. return 0;
  1202. }
  1203. static int npc_unload_ev(DBKey key,void *data,va_list ap) {
  1204. struct event_data *ev=(struct event_data *)data;
  1205. unsigned char *npcname=va_arg(ap,unsigned char *);
  1206. if(strcmp(ev->nd->exname,npcname)==0){
  1207. db_remove(ev_db, key);
  1208. return 1;
  1209. }
  1210. return 0;
  1211. }
  1212. int npc_unload (struct npc_data *nd)
  1213. {
  1214. nullpo_ret(nd);
  1215. npc_remove_map (nd);
  1216. map_deliddb(&nd->bl);
  1217. if (nd->chat_id) {
  1218. struct chat_data *cd = (struct chat_data*)map_id2bl(nd->chat_id);
  1219. if (cd) aFree (cd);
  1220. cd = NULL;
  1221. }
  1222. if (nd->bl.subtype == SCRIPT) {
  1223. ev_db->foreach(ev_db,npc_unload_ev,nd->exname); //Clean up all events related.
  1224. if (nd->u.scr.timerid != -1)
  1225. delete_timer(nd->u.scr.timerid, npc_timerevent);
  1226. npc_cleareventtimer (nd);
  1227. if (nd->u.scr.timer_event)
  1228. aFree(nd->u.scr.timer_event);
  1229. if (nd->u.scr.src_id == 0) {
  1230. if(nd->u.scr.script) {
  1231. script_free_code(nd->u.scr.script);
  1232. nd->u.scr.script = NULL;
  1233. }
  1234. if (nd->u.scr.label_list) {
  1235. aFree(nd->u.scr.label_list);
  1236. nd->u.scr.label_list = NULL;
  1237. }
  1238. }
  1239. }
  1240. aFree(nd);
  1241. return 0;
  1242. }
  1243. //
  1244. // 初期化関係
  1245. //
  1246. /*==========================================
  1247. * 読み込むnpcファイルのクリア
  1248. *------------------------------------------
  1249. */
  1250. void npc_clearsrcfile (void)
  1251. {
  1252. struct npc_src_list *p = npc_src_first, *p2;
  1253. while (p) {
  1254. p2 = p;
  1255. p = p->next;
  1256. aFree(p2);
  1257. }
  1258. npc_src_first = NULL;
  1259. npc_src_last = NULL;
  1260. }
  1261. /*==========================================
  1262. * 読み込むnpcファイルの追加
  1263. *------------------------------------------
  1264. */
  1265. void npc_addsrcfile (char *name)
  1266. {
  1267. struct npc_src_list *nsl;
  1268. if (strcmpi(name, "clear") == 0) {
  1269. npc_clearsrcfile();
  1270. return;
  1271. }
  1272. // prevent multiple insert of source files
  1273. nsl = npc_src_first;
  1274. while (nsl)
  1275. { // found the file, no need to insert it again
  1276. if (0 == strcmp(name, nsl->name))
  1277. return;
  1278. nsl = nsl->next;
  1279. }
  1280. nsl = (struct npc_src_list *) aMalloc (sizeof(*nsl) + strlen(name));
  1281. nsl->next = NULL;
  1282. strncpy(nsl->name, name, strlen(name) + 1);
  1283. if (npc_src_first == NULL)
  1284. npc_src_first = nsl;
  1285. if (npc_src_last)
  1286. npc_src_last->next = nsl;
  1287. npc_src_last = nsl;
  1288. }
  1289. /*==========================================
  1290. * 読み込むnpcファイルの削除
  1291. *------------------------------------------
  1292. */
  1293. void npc_delsrcfile (char *name)
  1294. {
  1295. struct npc_src_list *p = npc_src_first, *pp = NULL, **lp = &npc_src_first;
  1296. if (strcmpi(name, "all") == 0) {
  1297. npc_clearsrcfile();
  1298. return;
  1299. }
  1300. while (p) {
  1301. if (strcmp(p->name, name) == 0) {
  1302. *lp = p->next;
  1303. if (npc_src_last == p)
  1304. npc_src_last = pp;
  1305. aFree(p);
  1306. break;
  1307. }
  1308. lp = &p->next;
  1309. pp = p;
  1310. p = p->next;
  1311. }
  1312. }
  1313. /*==========================================
  1314. * warp行解析
  1315. *------------------------------------------
  1316. */
  1317. int npc_parse_warp (char *w1,char *w2,char *w3,char *w4)
  1318. {
  1319. int x, y, xs, ys, to_x, to_y, m;
  1320. int i, j;
  1321. char mapname[MAP_NAME_LENGTH], to_mapname[MAP_NAME_LENGTH];
  1322. struct npc_data *nd;
  1323. // 引数の個数チェック
  1324. if (sscanf(w1, "%15[^,],%d,%d", mapname, &x, &y) != 3 ||
  1325. sscanf(w4, "%d,%d,%15[^,],%d,%d", &xs, &ys, to_mapname, &to_x, &to_y) != 5) {
  1326. ShowError("bad warp line : %s\n", w3);
  1327. return 1;
  1328. }
  1329. m = map_mapname2mapid(mapname);
  1330. i = mapindex_name2id(to_mapname);
  1331. if (!i) {
  1332. ShowError("bad warp line (destination map not found): %s\n", w3);
  1333. return 1;
  1334. }
  1335. nd = (struct npc_data *) aCalloc (1, sizeof(struct npc_data));
  1336. nd->bl.id = npc_get_new_npc_id();
  1337. nd->n = map_addnpc(m, nd);
  1338. nd->bl.prev = nd->bl.next = NULL;
  1339. nd->bl.m = m;
  1340. nd->bl.x = x;
  1341. nd->bl.y = y;
  1342. memcpy(nd->name, w3, NAME_LENGTH-1);
  1343. memcpy(nd->exname, w3, NAME_LENGTH-1);
  1344. if (!battle_config.warp_point_debug)
  1345. nd->class_ = WARP_CLASS;
  1346. else
  1347. nd->class_ = WARP_DEBUG_CLASS;
  1348. nd->speed = 200;
  1349. nd->u.warp.mapindex = (short)i;
  1350. xs += 2;
  1351. ys += 2;
  1352. nd->u.warp.x = to_x;
  1353. nd->u.warp.y = to_y;
  1354. nd->u.warp.xs = xs;
  1355. nd->u.warp.ys = ys;
  1356. for (i = 0; i < ys; i++) {
  1357. for (j = 0; j < xs; j++) {
  1358. if (map_getcell(m, x-xs/2+j, y-ys/2+i, CELL_CHKNOPASS))
  1359. continue;
  1360. map_setcell(m, x-xs/2+j, y-ys/2+i, CELL_SETNPC);
  1361. }
  1362. }
  1363. npc_warp++;
  1364. nd->bl.type = BL_NPC;
  1365. nd->bl.subtype = WARP;
  1366. map_addblock(&nd->bl);
  1367. status_set_viewdata(&nd->bl, nd->class_);
  1368. status_change_init(&nd->bl);
  1369. unit_dataset(&nd->bl);
  1370. clif_spawn(&nd->bl);
  1371. strdb_put(npcname_db, nd->name, nd);
  1372. return 0;
  1373. }
  1374. /*==========================================
  1375. * shop行解析
  1376. *------------------------------------------
  1377. */
  1378. static int npc_parse_shop (char *w1, char *w2, char *w3, char *w4)
  1379. {
  1380. #define MAX_SHOPITEM 100
  1381. char *p;
  1382. int x, y, dir, m, pos = 0;
  1383. char mapname[MAP_NAME_LENGTH];
  1384. struct npc_data *nd;
  1385. if (strcmp(w1, "-") == 0) {
  1386. x = 0; y = 0; dir = 0; m = -1;
  1387. } else {
  1388. // 引数の個数チェック
  1389. if (sscanf(w1, "%15[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 ||
  1390. strchr(w4, ',') == NULL) {
  1391. ShowError("bad shop line : %s\n", w3);
  1392. return 1;
  1393. }
  1394. m = map_mapname2mapid(mapname);
  1395. }
  1396. nd = (struct npc_data *) aCalloc (1, sizeof(struct npc_data) +
  1397. sizeof(nd->u.shop_item[0]) * (MAX_SHOPITEM + 1));
  1398. p = strchr(w4, ',');
  1399. while (p && pos < MAX_SHOPITEM) {
  1400. int nameid, value;
  1401. struct item_data *id;
  1402. p++;
  1403. if (sscanf(p, "%d:%d", &nameid, &value) != 2)
  1404. break;
  1405. nd->u.shop_item[pos].nameid = nameid;
  1406. id = itemdb_search(nameid);
  1407. if (value < 0)
  1408. value = id->value_buy;
  1409. nd->u.shop_item[pos].value = value;
  1410. // check for bad prices that can possibly cause exploits
  1411. if (value/124. < id->value_sell/75.) { //Clened up formula to prevent overflows.
  1412. printf("\r"); //Carriage return to clear the 'loading..' line. [Skotlex]
  1413. if (value < id->value_sell)
  1414. ShowWarning ("Item %s [%d] buying price (%d) is less than selling price (%d) at %s\n",
  1415. id->name, id->nameid, value, id->value_sell, current_file);
  1416. else
  1417. ShowWarning ("Item %s [%d] discounted buying price (%d) is less than overcharged selling price (%d) at %s\n",
  1418. id->name, id->nameid, value/100*75, id->value_sell/100*124, current_file);
  1419. }
  1420. //for logs filters, atcommands and iteminfo script command
  1421. if (id->maxchance<=0)
  1422. id->maxchance = 10000; //10000 (100% drop chance)would show that the item's sold in NPC Shop
  1423. pos++;
  1424. p = strchr(p, ',');
  1425. }
  1426. if (pos == 0) {
  1427. aFree(nd);
  1428. return 1;
  1429. }
  1430. nd->u.shop_item[pos++].nameid = 0;
  1431. nd->bl.prev = nd->bl.next = NULL;
  1432. nd->bl.m = m;
  1433. nd->bl.x = x;
  1434. nd->bl.y = y;
  1435. nd->bl.id = npc_get_new_npc_id();
  1436. memcpy(nd->name, w3, NAME_LENGTH-1);
  1437. nd->name[NAME_LENGTH-1] = '\0';
  1438. nd->class_ = m==-1?-1:atoi(w4);
  1439. nd->speed = 200;
  1440. nd = (struct npc_data *)aRealloc(nd,
  1441. sizeof(struct npc_data) + sizeof(nd->u.shop_item[0]) * pos);
  1442. npc_shop++;
  1443. nd->bl.type = BL_NPC;
  1444. nd->bl.subtype = SHOP;
  1445. if (m >= 0) {
  1446. nd->n = map_addnpc(m,nd);
  1447. map_addblock(&nd->bl);
  1448. status_set_viewdata(&nd->bl, nd->class_);
  1449. status_change_init(&nd->bl);
  1450. unit_dataset(&nd->bl);
  1451. nd->ud.dir = dir;
  1452. clif_spawn(&nd->bl);
  1453. } else
  1454. // we skip map_addnpc, but still add it to the list of ID's
  1455. map_addiddb(&nd->bl);
  1456. strdb_put(npcname_db, nd->name,nd);
  1457. return 0;
  1458. }
  1459. /*==========================================
  1460. * NPCのラベルデータコンバート
  1461. *------------------------------------------
  1462. */
  1463. int npc_convertlabel_db (DBKey key, void *data, va_list ap)
  1464. {
  1465. unsigned char *lname = key.str;
  1466. int pos = (int)data;
  1467. struct npc_data *nd;
  1468. struct npc_label_list *lst;
  1469. int num;
  1470. char *p;
  1471. char c;
  1472. nullpo_retr(0, ap);
  1473. nullpo_retr(0, nd = va_arg(ap,struct npc_data *));
  1474. lst = nd->u.scr.label_list;
  1475. num = nd->u.scr.label_list_num;
  1476. if (!lst) {
  1477. lst = (struct npc_label_list *) aCallocA (1, sizeof(struct npc_label_list));
  1478. num = 0;
  1479. } else
  1480. lst = (struct npc_label_list *) aRealloc (lst, sizeof(struct npc_label_list)*(num+1));
  1481. // In case of labels not terminated with ':', for user defined function support
  1482. p = lname;
  1483. while(isalnum(*(unsigned char*)p) || *p == '_') { p++; }
  1484. c = *p;
  1485. *p='\0';
  1486. // here we check if the label fit into the buffer
  1487. if (strlen(lname) > 23) {
  1488. ShowError("npc_parse_script: label name longer than 23 chars! '%s'\n (%s)", lname, current_file);
  1489. exit(1);
  1490. }
  1491. memcpy(lst[num].name, lname, strlen(lname)+1); //including EOS
  1492. *p = c;
  1493. lst[num].pos = pos;
  1494. nd->u.scr.label_list = lst;
  1495. nd->u.scr.label_list_num = num+1;
  1496. return 0;
  1497. }
  1498. /*==========================================
  1499. * script行解析
  1500. *------------------------------------------
  1501. */
  1502. static void npc_parse_script_line(unsigned char *p,int *curly_count,int line) {
  1503. int i = strlen((char *)p),j;
  1504. int string_flag = 0;
  1505. static int comment_flag = 0;
  1506. for(j = 0; j < i ; j++) {
  1507. if(comment_flag) {
  1508. if(p[j] == '*' && p[j+1] == '/') {
  1509. // マルチラインコメント終了
  1510. j++;
  1511. (*curly_count)--;
  1512. comment_flag = 0;
  1513. }
  1514. } else if(string_flag) {
  1515. if(p[j] == '"') {
  1516. string_flag = 0;
  1517. } else if(p[j] == '\\' && p[j-1]<=0x7e) {
  1518. // エスケープ
  1519. j++;
  1520. }
  1521. } else {
  1522. if(p[j] == '"') {
  1523. string_flag = 1;
  1524. } else if(p[j] == '}') {
  1525. if(*curly_count == 0) {
  1526. break;
  1527. } else {
  1528. (*curly_count)--;
  1529. }
  1530. } else if(p[j] == '{') {
  1531. (*curly_count)++;
  1532. } else if(p[j] == '/' && p[j+1] == '/') {
  1533. // コメント
  1534. break;
  1535. } else if(p[j] == '/' && p[j+1] == '*') {
  1536. // マルチラインコメント
  1537. j++;
  1538. (*curly_count)++;
  1539. comment_flag = 1;
  1540. }
  1541. }
  1542. }
  1543. if(string_flag) {
  1544. printf("Missing '\"' at file %s line %d\n",current_file,line);
  1545. exit(1);
  1546. }
  1547. }
  1548. // Like npc_parse_script, except it's sole use is to skip the contents of a script. [Skotlex]
  1549. static int npc_skip_script (char *w1,char *w2,char *w3,char *w4,char *first_line,FILE *fp,int *lines)
  1550. {
  1551. unsigned char *srcbuf = NULL;
  1552. int srcsize = 65536;
  1553. int startline = 0;
  1554. unsigned char line[1024];
  1555. int curly_count = 0;
  1556. srcbuf = (unsigned char *)aMallocA(srcsize*sizeof(char));
  1557. if (strchr(first_line, '{')) {
  1558. strcpy((char *)srcbuf, strchr(first_line, '{'));
  1559. startline = *lines;
  1560. } else
  1561. srcbuf[0] = 0;
  1562. npc_parse_script_line(srcbuf,&curly_count,*lines);
  1563. while (curly_count > 0) {
  1564. fgets ((char *)line, 1020, fp);
  1565. (*lines)++;
  1566. npc_parse_script_line(line,&curly_count,*lines);
  1567. if (feof(fp))
  1568. break;
  1569. if (strlen((char *)srcbuf) + strlen((char *)line) + 1 >= (size_t)srcsize) {
  1570. srcsize += 65536;
  1571. srcbuf = (unsigned char *)aRealloc(srcbuf, srcsize);
  1572. memset(srcbuf + srcsize - 65536, '\0', 65536);
  1573. }
  1574. if (srcbuf[0] != '{') {
  1575. if (strchr((char *) line,'{')) {
  1576. strcpy((char *) srcbuf, strchr((const char *) line, '{'));
  1577. startline = *lines;
  1578. }
  1579. } else
  1580. strcat((char *) srcbuf, (const char *) line);
  1581. }
  1582. if(curly_count > 0)
  1583. ShowError("Missing right curly at file %s, line %d\n",current_file, *lines);
  1584. aFree(srcbuf);
  1585. return 0;
  1586. }
  1587. static int npc_parse_script(char *w1,char *w2,char *w3,char *w4,char *first_line,FILE *fp,int *lines,const char* file)
  1588. {
  1589. int x, y, dir = 0, m, xs = 0, ys = 0, class_ = 0; // [Valaris] thanks to fov
  1590. char mapname[MAP_NAME_LENGTH];
  1591. unsigned char *srcbuf = NULL;
  1592. struct script_code *script;
  1593. int srcsize = 65536;
  1594. int startline = 0;
  1595. unsigned char line[1024];
  1596. int i;
  1597. struct npc_data *nd, *dnd;
  1598. struct dbt *label_db;
  1599. char *p;
  1600. struct npc_label_list *label_dup = NULL;
  1601. int label_dupnum = 0;
  1602. int src_id = 0;
  1603. if (strcmp(w1, "-") == 0) {
  1604. x = 0; y = 0; m = -1;
  1605. } else {
  1606. // 引数の個数チェック
  1607. if (sscanf(w1, "%15[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 ||
  1608. (strcmp(w2, "script") == 0 && strchr(w4,',') == NULL)) {
  1609. ShowError("bad script line (in file %s): %s\n", file, w3);
  1610. return 1;
  1611. }
  1612. m = map_mapname2mapid(mapname);
  1613. }
  1614. if (strcmp(w2, "script") == 0){
  1615. // parsing script with curly
  1616. int curly_count = 0;
  1617. srcbuf = (unsigned char *)aMallocA(srcsize*sizeof(char));
  1618. if (strchr(first_line, '{')) {
  1619. strcpy((char *)srcbuf, strchr(first_line, '{'));
  1620. startline = *lines;
  1621. } else
  1622. srcbuf[0] = 0;
  1623. npc_parse_script_line(srcbuf,&curly_count,*lines);
  1624. while (curly_count > 0) {
  1625. fgets ((char *)line, 1020, fp);
  1626. (*lines)++;
  1627. npc_parse_script_line(line,&curly_count,*lines);
  1628. if (feof(fp))
  1629. break;
  1630. if (strlen((char *)srcbuf) + strlen((char *)line) + 1 >= (size_t)srcsize) {
  1631. srcsize += 65536;
  1632. srcbuf = (unsigned char *)aRealloc(srcbuf, srcsize);
  1633. memset(srcbuf + srcsize - 65536, '\0', 65536);
  1634. }
  1635. if (srcbuf[0] != '{') {
  1636. if (strchr((char *) line,'{')) {
  1637. strcpy((char *) srcbuf, strchr((const char *) line, '{'));
  1638. startline = *lines;
  1639. }
  1640. } else
  1641. strcat((char *) srcbuf, (const char *) line);
  1642. }
  1643. if(curly_count > 0) {
  1644. ShowError("Missing right curly at file %s, line %d\n",file, *lines);
  1645. script = NULL;
  1646. } else {
  1647. // printf("Ok line %d\n",*lines);
  1648. script = parse_script((unsigned char *) srcbuf, file, startline);
  1649. }
  1650. if (script == NULL) {
  1651. // script parse error?
  1652. aFree(srcbuf);
  1653. return 1;
  1654. }
  1655. } else {
  1656. // duplicateする
  1657. char srcname[128];
  1658. struct npc_data *dnd;
  1659. if (sscanf(w2, "duplicate(%[^)])", srcname) != 1) {
  1660. ShowError("bad duplicate name (in %s)! : %s", file, w2);
  1661. return 0;
  1662. }
  1663. if ((dnd = npc_name2id(srcname)) == NULL) {
  1664. ShowError("bad duplicate name (in %s)! (not exist) : %s\n", file, srcname);
  1665. return 0;
  1666. }
  1667. script = dnd->u.scr.script;
  1668. label_dup = dnd->u.scr.label_list;
  1669. label_dupnum = dnd->u.scr.label_list_num;
  1670. src_id = dnd->bl.id;
  1671. }// end of スクリプト解析
  1672. nd = (struct npc_data *)aCalloc(1, sizeof(struct npc_data));
  1673. if (sscanf(w4, "%d,%d,%d", &class_, &xs, &ys) == 3) {
  1674. // 接触型NPC
  1675. int i, j;
  1676. if (xs >= 0) xs = xs * 2 + 1;
  1677. if (ys >= 0) ys = ys * 2 + 1;
  1678. if (m >= 0) {
  1679. for (i = 0; i < ys; i++) {
  1680. for (j = 0; j < xs; j++) {
  1681. if (map_getcell(m, x - xs/2 + j, y - ys/2 + i, CELL_CHKNOPASS))
  1682. continue;
  1683. map_setcell(m, x - xs/2 + j, y - ys/2 + i, CELL_SETNPC);
  1684. }
  1685. }
  1686. }
  1687. nd->u.scr.xs = xs;
  1688. nd->u.scr.ys = ys;
  1689. } else {
  1690. // クリック型NPC
  1691. class_ = atoi(w4);
  1692. nd->u.scr.xs = 0;
  1693. nd->u.scr.ys = 0;
  1694. }
  1695. while ((p = strchr(w3,':'))) {
  1696. if (p[1] == ':') break;
  1697. }
  1698. if (p) {
  1699. *p = 0;
  1700. memcpy(nd->name, w3, NAME_LENGTH-1);
  1701. memcpy(nd->exname, p+2, NAME_LENGTH-1);
  1702. } else {
  1703. memcpy(nd->name, w3, NAME_LENGTH-1);
  1704. memcpy(nd->exname, w3, NAME_LENGTH-1);
  1705. }
  1706. if((dnd = npc_name2id(nd->exname))){
  1707. if(battle_config.etc_log)
  1708. ShowInfo("npc_parse_script: Overriding NPC '%s::%s' to '%s::%d'.. (Duplicated System Name - Lazy scripters >_>) \n",nd->name,nd->exname,nd->name,npc_script);
  1709. sprintf(nd->exname, "%d", npc_script);
  1710. }
  1711. nd->bl.prev = nd->bl.next = NULL;
  1712. nd->bl.m = m;
  1713. nd->bl.x = x;
  1714. nd->bl.y = y;
  1715. nd->bl.id = npc_get_new_npc_id();
  1716. nd->class_ = class_;
  1717. nd->speed = 200;
  1718. nd->u.scr.script = script;
  1719. nd->u.scr.src_id = src_id;
  1720. npc_script++;
  1721. nd->bl.type = BL_NPC;
  1722. nd->bl.subtype = SCRIPT;
  1723. for (i = 0; i < MAX_EVENTTIMER; i++)
  1724. nd->eventtimer[i] = -1;
  1725. if (m >= 0) {
  1726. nd->n = map_addnpc(m, nd);
  1727. status_change_init(&nd->bl);
  1728. unit_dataset(&nd->bl);
  1729. nd->ud.dir = dir;
  1730. map_addblock(&nd->bl);
  1731. // Unused. You can always use xxx::OnXXXX events. Have this removed to improve perfomance.
  1732. /*if (evflag) { // イベント型
  1733. struct event_data *ev = (struct event_data *)aCalloc(1, sizeof(struct event_data));
  1734. ev->nd = nd;
  1735. ev->pos = 0;
  1736. strdb_put(ev_db, nd->exname, ev);
  1737. } else {
  1738. clif_spawn(&nd->bl);
  1739. }*/
  1740. if (class_ >= 0){
  1741. status_set_viewdata(&nd->bl, nd->class_);
  1742. clif_spawn(&nd->bl);
  1743. }
  1744. } else {
  1745. // we skip map_addnpc, but still add it to the list of ID's
  1746. map_addiddb(&nd->bl);
  1747. }
  1748. strdb_put(npcname_db, nd->exname, nd);
  1749. //-----------------------------------------
  1750. // ラベルデータの準備
  1751. if (srcbuf){
  1752. // script本体がある場合の処理
  1753. // ラベルデータのコンバート
  1754. label_db = script_get_label_db();
  1755. label_db->foreach(label_db, npc_convertlabel_db, nd);
  1756. // もう使わないのでバッファ解放
  1757. aFree(srcbuf);
  1758. } else {
  1759. // duplicate
  1760. nd->u.scr.label_list = label_dup; // ラベルデータ共有
  1761. nd->u.scr.label_list_num = label_dupnum;
  1762. }
  1763. //-----------------------------------------
  1764. // イベント用ラベルデータのエクスポート
  1765. for (i = 0; i < nd->u.scr.label_list_num; i++){
  1766. char *lname = nd->u.scr.label_list[i].name;
  1767. int pos = nd->u.scr.label_list[i].pos;
  1768. if ((lname[0] == 'O' || lname[0] == 'o') && (lname[1] == 'N' || lname[1] == 'n')) {
  1769. // this check is useless here because the buffer is only 24 chars
  1770. // and already overwritten if this is here is reached
  1771. // I leave the check anyway but place it correctly to npc_convertlabel_db
  1772. if (strlen(lname)>NAME_LENGTH-1) {
  1773. ShowError("npc_parse_script: label name longer than %d chars! '%s' (%s)\n", NAME_LENGTH-1, lname, file);
  1774. exit(1);
  1775. } else {
  1776. struct event_data *ev;
  1777. unsigned char buf[51];
  1778. // 51 comes from: 24 for npc name + 24 for label + 2 for a "::" and 1 for EOS
  1779. sprintf(buf,"%s::%s",nd->exname,lname);
  1780. // remember the label is max 50 chars + eos; see the strdb_init below
  1781. // generate the data and insert it
  1782. ev=(struct event_data *)aMalloc(sizeof(struct event_data));
  1783. ev->nd=nd;
  1784. ev->pos=pos;
  1785. if (strdb_put(ev_db,buf,ev) != NULL) //There was already another event of the same name?
  1786. ShowWarning("npc_parse_script : duplicate event %s (%s)\n",buf, file);
  1787. }
  1788. }
  1789. }
  1790. //-----------------------------------------
  1791. // ラベルデータからタイマーイベント取り込み
  1792. for (i = 0; i < nd->u.scr.label_list_num; i++){
  1793. int t = 0, k = 0;
  1794. char *lname = nd->u.scr.label_list[i].name;
  1795. int pos = nd->u.scr.label_list[i].pos;
  1796. if (sscanf(lname, "OnTimer%d%n", &t, &k) == 1 && lname[k] == '\0') {
  1797. // タイマーイベント
  1798. struct npc_timerevent_list *te = nd->u.scr.timer_event;
  1799. int j, k = nd->u.scr.timeramount;
  1800. if (te == NULL)
  1801. te = (struct npc_timerevent_list *)aMallocA(sizeof(struct npc_timerevent_list));
  1802. else
  1803. te = (struct npc_timerevent_list *)aRealloc( te, sizeof(struct npc_timerevent_list) * (k+1) );
  1804. for (j = 0; j < k; j++){
  1805. if (te[j].timer > t){
  1806. memmove(te+j+1, te+j, sizeof(struct npc_timerevent_list)*(k-j));
  1807. break;
  1808. }
  1809. }
  1810. te[j].timer = t;
  1811. te[j].pos = pos;
  1812. nd->u.scr.timer_event = te;
  1813. nd->u.scr.timeramount++;
  1814. }
  1815. }
  1816. nd->u.scr.timerid = -1;
  1817. return 0;
  1818. }
  1819. /*==========================================
  1820. * function行解析
  1821. *------------------------------------------
  1822. */
  1823. static int npc_parse_function (char *w1, char *w2, char *w3, char *w4, char *first_line, FILE *fp, int *lines,const char* file)
  1824. {
  1825. unsigned char *srcbuf, *p;
  1826. struct script_code *script;
  1827. int srcsize = 65536;
  1828. int startline = 0;
  1829. char line[1024];
  1830. int curly_count = 0;
  1831. struct dbt *user_db;
  1832. // スクリプトの解析
  1833. srcbuf = (unsigned char *) aMallocA (srcsize*sizeof(char));
  1834. if (strchr(first_line,'{')) {
  1835. strcpy(srcbuf, strchr(first_line,'{'));
  1836. startline = *lines;
  1837. } else
  1838. srcbuf[0] = 0;
  1839. npc_parse_script_line(srcbuf,&curly_count,*lines);
  1840. while (curly_count > 0) {
  1841. fgets(line, sizeof(line) - 1, fp);
  1842. (*lines)++;
  1843. npc_parse_script_line(line,&curly_count,*lines);
  1844. if (feof(fp))
  1845. break;
  1846. if (strlen(srcbuf)+strlen(line)+1 >= (unsigned int)srcsize) {
  1847. srcsize += 65536;
  1848. srcbuf = (char *)aRealloc(srcbuf, srcsize);
  1849. memset(srcbuf + srcsize - 65536, '\0', 65536);
  1850. }
  1851. if (srcbuf[0]!='{') {
  1852. if (strchr(line,'{')) {
  1853. strcpy(srcbuf, strchr(line,'{'));
  1854. startline = *lines;
  1855. }
  1856. } else
  1857. strcat(srcbuf,line);
  1858. }
  1859. if(curly_count > 0) {
  1860. ShowError("Missing right curly at file %s, line %d\n",file, *lines);
  1861. script = NULL;
  1862. } else {
  1863. script = parse_script(srcbuf, file, startline);
  1864. }
  1865. if (script == NULL) {
  1866. // script parse error?
  1867. aFree(srcbuf);
  1868. return 1;
  1869. }
  1870. p = (char *) aMallocA (50*sizeof(char));
  1871. strncpy(p, w3, 50);
  1872. user_db = script_get_userfunc_db();
  1873. if(strdb_get(user_db, p) != NULL) {
  1874. printf("\r"); //Carriage return to clear the 'loading..' line. [Skotlex]
  1875. ShowWarning("parse_function: Duplicate user function [%s] (%s:%d)\n", p, file, *lines);
  1876. aFree(p);
  1877. script_free_code(script);
  1878. } else
  1879. strdb_put(user_db, p, script);
  1880. // もう使わないのでバッファ解放
  1881. aFree(srcbuf);
  1882. // printf("function %s => %p\n",p,script);
  1883. return 0;
  1884. }
  1885. /*==========================================
  1886. * Parse Mob 1 - Parse mob list into each map
  1887. * Parse Mob 2 - Actually Spawns Mob
  1888. * [Wizputer]
  1889. * If cached =1, it is a dynamic cached mob
  1890. * index points to the index in the mob_list of the map_data cache.
  1891. * -1 indicates that it is not stored on the map.
  1892. *------------------------------------------
  1893. */
  1894. int npc_parse_mob2 (struct spawn_data *mob, int index)
  1895. {
  1896. int i;
  1897. struct mob_data *md;
  1898. for (i = 0; i < mob->num; i++) {
  1899. md = mob_spawn_dataset(mob);
  1900. md->spawn = mob;
  1901. md->spawn_n = index;
  1902. md->special_state.cached = (index>=0); //If mob is cached on map, it is dynamically removed
  1903. mob_spawn(md);
  1904. }
  1905. return 1;
  1906. }
  1907. int npc_parse_mob (char *w1, char *w2, char *w3, char *w4)
  1908. {
  1909. int level, num, class_, mode, x,y,xs,ys;
  1910. char mapname[MAP_NAME_LENGTH];
  1911. char mobname[NAME_LENGTH];
  1912. struct spawn_data mob, *data;
  1913. memset(&mob, 0, sizeof(struct spawn_data));
  1914. // 引数の個数チェック
  1915. if (sscanf(w1, "%15[^,],%d,%d,%d,%d", mapname, &x, &y, &xs, &ys) < 3 ||
  1916. sscanf(w4, "%d,%d,%u,%u,%23s", &class_, &num, &mob.delay1, &mob.delay2, mob.eventname) < 2 ) {
  1917. ShowError("bad monster line : %s %s %s (file %s)\n", w1, w3, w4, current_file);
  1918. return 1;
  1919. }
  1920. if (!mapindex_name2id(mapname)) {
  1921. ShowError("wrong map name : %s %s (file %s)\n", w1,w3, current_file);
  1922. return 1;
  1923. }
  1924. mode = map_mapname2mapid(mapname);
  1925. if (mode < 0) //Not loaded on this map-server instance.
  1926. return 1;
  1927. mob.m = (unsigned short)mode;
  1928. if (x < 0 || map[mob.m].xs <= x || y < 0 || map[mob.m].ys <= y) {
  1929. ShowError("Out of range spawn coordinates: %s (%d,%d), map size is (%d,%d) - %s %s (file %s)\n", map[mob.m].name, x, y, map[mob.m].xs-1, map[mob.m].ys-1, w1,w3, current_file);
  1930. return 1;
  1931. }
  1932. // check monster ID if exists!
  1933. if (mobdb_checkid(class_)==0) {
  1934. ShowError("bad monster ID : %s %s (file %s)\n", w3, w4, current_file);
  1935. return 1;
  1936. }
  1937. if (num < 1 || num>1000 ) {
  1938. ShowError("wrong number of monsters : %s %s (file %s)\n", w3, w4, current_file);
  1939. return 1;
  1940. }
  1941. mob.num = (unsigned short)num;
  1942. mob.class_ = (short) class_;
  1943. mob.x = (unsigned short)x;
  1944. mob.y = (unsigned short)y;
  1945. mob.xs = (unsigned short)xs;
  1946. mob.ys = (unsigned short)ys;
  1947. if (mob.num > 1 && battle_config.mob_count_rate != 100) {
  1948. if ((mob.num = mob.num * battle_config.mob_count_rate / 100) < 1)
  1949. mob.num = 1;
  1950. }
  1951. //Apply the spawn delay fix [Skotlex]
  1952. mode = mob_db(class_)->status.mode;
  1953. if (mode & MD_BOSS) { //Bosses
  1954. if (battle_config.boss_spawn_delay != 100)
  1955. {
  1956. mob.delay1 = mob.delay1*battle_config.boss_spawn_delay/100;
  1957. mob.delay2 = mob.delay2*battle_config.boss_spawn_delay/100;
  1958. }
  1959. } else if (mode&MD_PLANT) { //Plants
  1960. if (battle_config.plant_spawn_delay != 100)
  1961. {
  1962. mob.delay1 = mob.delay1*battle_config.plant_spawn_delay/100;
  1963. mob.delay2 = mob.delay2*battle_config.plant_spawn_delay/100;
  1964. }
  1965. } else if (battle_config.mob_spawn_delay != 100)
  1966. { //Normal mobs
  1967. mob.delay1 = mob.delay1*battle_config.mob_spawn_delay/100;
  1968. mob.delay2 = mob.delay2*battle_config.mob_spawn_delay/100;
  1969. }
  1970. // parse MOB_NAME,[MOB LEVEL]
  1971. if (sscanf(w3, "%23[^,],%d", mobname, &level) > 1)
  1972. mob.level = level;
  1973. if(mob.delay1>0xfffffff || mob.delay2>0xfffffff) {
  1974. ShowError("wrong monsters spawn delays : %s %s (file %s)\n", w3, w4, current_file);
  1975. return 1;
  1976. }
  1977. //Use db names instead of the spawn file ones.
  1978. if(battle_config.override_mob_names==1)
  1979. strcpy(mob.name,"--en--");
  1980. else if (battle_config.override_mob_names==2)
  1981. strcpy(mob.name,"--ja--");
  1982. else
  1983. strncpy(mob.name, mobname, NAME_LENGTH-1);
  1984. if (!mob_parse_dataset(&mob)) //Verify dataset.
  1985. return 1;
  1986. //Now that all has been validated. We allocate the actual memory
  1987. //that the re-spawn data will use.
  1988. data = aMalloc(sizeof(struct spawn_data));
  1989. memcpy(data, &mob, sizeof(struct spawn_data));
  1990. if( !battle_config.dynamic_mobs || mob.delay1 || mob.delay2 ) {
  1991. npc_parse_mob2(data,-1);
  1992. npc_delay_mob += mob.num;
  1993. } else {
  1994. int index = map_addmobtolist(data->m, data);
  1995. if( index >= 0 ) {
  1996. // check if target map has players
  1997. // (usually shouldn't occur when map server is just starting,
  1998. // but not the case when we do @reloadscript
  1999. if (map[mob.m].users > 0)
  2000. npc_parse_mob2(data,index);
  2001. npc_cache_mob += mob.num;
  2002. } else {
  2003. // mobcache is full
  2004. // create them as delayed with one second
  2005. mob.delay1 = 1000;
  2006. mob.delay2 = 1000;
  2007. npc_parse_mob2(data,-1);
  2008. npc_delay_mob += mob.num;
  2009. }
  2010. }
  2011. npc_mob++;
  2012. return 0;
  2013. }
  2014. /*==========================================
  2015. * マップフラグ行の解析
  2016. *------------------------------------------
  2017. */
  2018. static int npc_parse_mapflag (char *w1, char *w2, char *w3, char *w4)
  2019. {
  2020. int m;
  2021. char mapname[MAP_NAME_LENGTH];
  2022. int state = 1;
  2023. // 引数の個数チェック
  2024. if (sscanf(w1, "%15[^,]",mapname) != 1)
  2025. return 1;
  2026. m = map_mapname2mapid(mapname);
  2027. if (m < 0)
  2028. return 1;
  2029. if (w4 && strcmpi(w4, "off") == 0)
  2030. state = 0; //Disable mapflag rather than enable it. [Skotlex]
  2031. //マップフラグ
  2032. if (strcmpi(w3, "nosave") == 0) {
  2033. char savemap[MAP_NAME_LENGTH];
  2034. int savex, savey;
  2035. if (state == 0)
  2036. ; //Map flag disabled.
  2037. else if (strcmp(w4, "SavePoint") == 0) {
  2038. map[m].save.map = 0;
  2039. map[m].save.x = -1;
  2040. map[m].save.y = -1;
  2041. } else if (sscanf(w4, "%15[^,],%d,%d", savemap, &savex, &savey) == 3) {
  2042. map[m].save.map = mapindex_name2id(savemap);
  2043. map[m].save.x = savex;
  2044. map[m].save.y = savey;
  2045. if (!map[m].save.map) {
  2046. ShowWarning("Specified save point map '%s' for mapflag 'nosave' not found (file %s), using 'SavePoint'.\n",savemap,current_file);
  2047. map[m].save.x = -1;
  2048. map[m].save.y = -1;
  2049. }
  2050. }
  2051. map[m].flag.nosave = state;
  2052. }
  2053. else if (strcmpi(w3,"nomemo")==0) {
  2054. map[m].flag.nomemo=state;
  2055. }
  2056. else if (strcmpi(w3,"noteleport")==0) {
  2057. map[m].flag.noteleport=state;
  2058. }
  2059. else if (strcmpi(w3,"nowarp")==0) {
  2060. map[m].flag.nowarp=state;
  2061. }
  2062. else if (strcmpi(w3,"nowarpto")==0) {
  2063. map[m].flag.nowarpto=state;
  2064. }
  2065. else if (strcmpi(w3,"noreturn")==0) {
  2066. map[m].flag.noreturn=state;
  2067. }
  2068. else if (strcmpi(w3,"monster_noteleport")==0) {
  2069. map[m].flag.monster_noteleport=state;
  2070. }
  2071. else if (strcmpi(w3,"nobranch")==0) {
  2072. map[m].flag.nobranch=state;
  2073. }
  2074. else if (strcmpi(w3,"nopenalty")==0) {
  2075. map[m].flag.nopenalty=state;
  2076. }
  2077. else if (strcmpi(w3,"pvp")==0) {
  2078. map[m].flag.pvp=state;
  2079. if (state) {
  2080. map[m].flag.gvg=0;
  2081. map[m].flag.gvg=0;
  2082. map[m].flag.gvg_dungeon=0;
  2083. map[m].flag.gvg_castle=0;
  2084. }
  2085. }
  2086. else if (strcmpi(w3,"pvp_noparty")==0) {
  2087. map[m].flag.pvp_noparty=state;
  2088. }
  2089. else if (strcmpi(w3,"pvp_noguild")==0) {
  2090. map[m].flag.pvp_noguild=state;
  2091. }
  2092. else if (strcmpi(w3, "pvp_nightmaredrop") == 0) {
  2093. char drop_arg1[16], drop_arg2[16];
  2094. int drop_id = 0, drop_type = 0, drop_per = 0;
  2095. if (sscanf(w4, "%[^,],%[^,],%d", drop_arg1, drop_arg2, &drop_per) == 3) {
  2096. int i;
  2097. if (strcmp(drop_arg1, "random") == 0)
  2098. drop_id = -1;
  2099. else if (itemdb_exists((drop_id = atoi(drop_arg1))) == NULL)
  2100. drop_id = 0;
  2101. if (strcmp(drop_arg2, "inventory") == 0)
  2102. drop_type = 1;
  2103. else if (strcmp(drop_arg2,"equip") == 0)
  2104. drop_type = 2;
  2105. else if (strcmp(drop_arg2,"all") == 0)
  2106. drop_type = 3;
  2107. if (drop_id != 0){
  2108. for (i = 0; i < MAX_DROP_PER_MAP; i++) {
  2109. if (map[m].drop_list[i].drop_id == 0){
  2110. map[m].drop_list[i].drop_id = drop_id;
  2111. map[m].drop_list[i].drop_type = drop_type;
  2112. map[m].drop_list[i].drop_per = drop_per;
  2113. break;
  2114. }
  2115. }
  2116. map[m].flag.pvp_nightmaredrop = 1;
  2117. }
  2118. } else if (!state) //Disable
  2119. map[m].flag.pvp_nightmaredrop = 0;
  2120. }
  2121. else if (strcmpi(w3,"pvp_nocalcrank")==0) {
  2122. map[m].flag.pvp_nocalcrank=state;
  2123. }
  2124. else if (strcmpi(w3,"gvg")==0) {
  2125. map[m].flag.gvg=state;
  2126. if (state) map[m].flag.pvp=0;
  2127. }
  2128. else if (strcmpi(w3,"gvg_noparty")==0) {
  2129. map[m].flag.gvg_noparty=state;
  2130. }
  2131. else if (strcmpi(w3,"gvg_dungeon")==0) {
  2132. map[m].flag.gvg_dungeon=state;
  2133. if (state) map[m].flag.pvp=0;
  2134. }
  2135. else if (strcmpi(w3,"gvg_castle")==0) {
  2136. map[m].flag.gvg_castle=state;
  2137. if (state) map[m].flag.pvp=0;
  2138. }
  2139. else if (strcmpi(w3,"nozenypenalty")==0) {
  2140. map[m].flag.nozenypenalty=state;
  2141. }
  2142. else if (strcmpi(w3,"notrade")==0) {
  2143. map[m].flag.notrade=state;
  2144. }
  2145. else if (strcmpi(w3,"novending")==0) {
  2146. map[m].flag.novending=state;
  2147. }
  2148. else if (strcmpi(w3,"nodrop")==0) {
  2149. map[m].flag.nodrop=state;
  2150. }
  2151. else if (strcmpi(w3,"noskill")==0) {
  2152. map[m].flag.noskill=state;
  2153. }
  2154. else if (strcmpi(w3,"noicewall")==0) { // noicewall [Valaris]
  2155. map[m].flag.noicewall=state;
  2156. }
  2157. else if (strcmpi(w3,"snow")==0) { // snow [Valaris]
  2158. map[m].flag.snow=state;
  2159. }
  2160. else if (strcmpi(w3,"clouds")==0) {
  2161. map[m].flag.clouds=state;
  2162. }
  2163. else if (strcmpi(w3,"clouds2")==0) { // clouds2 [Valaris]
  2164. map[m].flag.clouds2=state;
  2165. }
  2166. else if (strcmpi(w3,"fog")==0) { // fog [Valaris]
  2167. map[m].flag.fog=state;
  2168. }
  2169. else if (strcmpi(w3,"fireworks")==0) {
  2170. map[m].flag.fireworks=state;
  2171. }
  2172. else if (strcmpi(w3,"sakura")==0) { // sakura [Valaris]
  2173. map[m].flag.sakura=state;
  2174. }
  2175. else if (strcmpi(w3,"leaves")==0) { // leaves [Valaris]
  2176. map[m].flag.leaves=state;
  2177. }
  2178. else if (strcmpi(w3,"rain")==0) { // rain [Valaris]
  2179. map[m].flag.rain=state;
  2180. }
  2181. else if (strcmpi(w3,"indoors")==0) { // celest
  2182. map[m].flag.indoors=state;
  2183. }
  2184. else if (strcmpi(w3,"nightenabled")==0) { // Skotlex
  2185. map[m].flag.nightenabled=state;
  2186. }
  2187. else if (strcmpi(w3,"nogo")==0) { // celest
  2188. map[m].flag.nogo=state;
  2189. }
  2190. else if (strcmpi(w3,"noexp")==0) { // Lorky
  2191. map[m].flag.nobaseexp=state;
  2192. map[m].flag.nojobexp=state;
  2193. }
  2194. else if (strcmpi(w3,"nobaseexp")==0) { // Lorky
  2195. map[m].flag.nobaseexp=state;
  2196. }
  2197. else if (strcmpi(w3,"nojobexp")==0) { // Lorky
  2198. map[m].flag.nojobexp=state;
  2199. }
  2200. else if (strcmpi(w3,"noloot")==0) { // Lorky
  2201. map[m].flag.nomobloot=state;
  2202. map[m].flag.nomvploot=state;
  2203. }
  2204. else if (strcmpi(w3,"nomobloot")==0) { // Lorky
  2205. map[m].flag.nomobloot=state;
  2206. }
  2207. else if (strcmpi(w3,"nomvploot")==0) { // Lorky
  2208. map[m].flag.nomvploot=state;
  2209. }
  2210. else if (strcmpi(w3,"nocommand")==0) { // Skotlex
  2211. map[m].flag.nocommand=state;
  2212. }
  2213. else if (strcmpi(w3,"restricted")==0) { // Komurka
  2214. if (state) {
  2215. map[m].flag.restricted=1;
  2216. sscanf(w4, "%d", &state);
  2217. map[m].zone |= 1<<(state+1);
  2218. } else {
  2219. map[m].flag.restricted=0;
  2220. map[m].zone = 0;
  2221. }
  2222. }
  2223. else if (strcmpi(w3,"jexp")==0) {
  2224. map[m].jexp = (state) ? atoi(w4) : 100;
  2225. if( map[m].jexp < 0 ) map[m].jexp = 100;
  2226. map[m].flag.nojobexp = (map[m].jexp==0)?1:0;
  2227. }
  2228. else if (strcmpi(w3,"bexp")==0) {
  2229. map[m].bexp = (state) ? atoi(w4) : 100;
  2230. if( map[m].bexp < 0 ) map[m].bexp = 100;
  2231. map[m].flag.nobaseexp = (map[m].bexp==0)?1:0;
  2232. }
  2233. else if (strcmpi(w3,"loadevent")==0) { // Skotlex
  2234. map[m].flag.loadevent=state;
  2235. }
  2236. else if (strcmpi(w3,"nochat")==0) { // Skotlex
  2237. map[m].flag.nochat=state;
  2238. }
  2239. return 0;
  2240. }
  2241. /*==========================================
  2242. * Setting up map cells
  2243. *------------------------------------------
  2244. */
  2245. static int npc_parse_mapcell (char *w1, char *w2, char *w3, char *w4)
  2246. {
  2247. int m, cell, x, y, x0, y0, x1, y1;
  2248. char type[24], mapname[MAP_NAME_LENGTH];
  2249. if (sscanf(w1, "%15[^,]", mapname) != 1)
  2250. return 1;
  2251. m = map_mapname2mapid(mapname);
  2252. if (m < 0)
  2253. return 1;
  2254. if (sscanf(w3, "%23[^,],%d,%d,%d,%d", type, &x0, &y0, &x1, &y1) < 4) {
  2255. ShowError("Bad setcell line : %s\n",w3);
  2256. return 1;
  2257. }
  2258. cell = strtol(type, (char **)NULL, 0);
  2259. //printf ("0x%x %d %d %d %d\n", cell, x0, y0, x1, y1);
  2260. if (x0 > x1) { int t = x0; x0 = x1; x1 = t; }
  2261. if (y0 > y1) { int t = y0; y0 = y1; y1 = t; }
  2262. for (x = x0; x <= x1; x++) {
  2263. for (y = y0; y <= y1; y++) {
  2264. map_setcell(m, x, y, cell);
  2265. //printf ("setcell 0x%x %d %d %d\n", cell, m, x, y);
  2266. }
  2267. }
  2268. return 0;
  2269. }
  2270. void npc_parsesrcfile (char *name)
  2271. {
  2272. int m, lines = 0;
  2273. char line[1024];
  2274. FILE *fp = fopen (name,"r");
  2275. if (fp == NULL) {
  2276. ShowError ("File not found : %s\n", name);
  2277. return;
  2278. }
  2279. current_file = name;
  2280. while (fgets(line, sizeof(line) - 1, fp)) {
  2281. char w1[1024], w2[1024], w3[1024], w4[1024], mapname[1024];
  2282. int i, j, w4pos, count;
  2283. lines++;
  2284. if (line[0] == '/' && line[1] == '/')
  2285. continue;
  2286. // 不要なスペースやタブの連続は詰める
  2287. for (i = j = 0; line[i]; i++) {
  2288. if (line[i]==' ') {
  2289. if (!((line[i+1] && (isspace((unsigned char)line[i+1]) || line[i+1]==',')) ||
  2290. (j && line[j-1]==',')))
  2291. line[j++]=' ';
  2292. } else if (line[i]=='\t') {
  2293. if (!(j && line[j-1]=='\t'))
  2294. line[j++]='\t';
  2295. } else
  2296. line[j++]=line[i];
  2297. }
  2298. line[j] = '\0'; //Forget to terminate the string. From [jA 1091]
  2299. // 最初はタブ区切りでチェックしてみて、ダメならスペース区切りで確認
  2300. w1[0] = w2[0] = w3[0] = w4[0] = '\0'; //It's best to initialize values
  2301. //to prevent passing previously parsed values to the parsers when not all
  2302. //fields are specified. [Skotlex]
  2303. if ((count = sscanf(line,"%[^\t]\t%[^\t]\t%[^\t\r\n]\t%n%[^\t\r\n]", w1, w2, w3, &w4pos, w4)) < 3 &&
  2304. (count = sscanf(line,"%s%s%s%n%s", w1, w2, w3, &w4pos, w4)) < 3) {
  2305. continue;
  2306. }
  2307. // マップの存在確認
  2308. if (strcmp(w1,"-") !=0 && strcmpi(w1,"function") != 0 ){
  2309. sscanf(w1,"%[^,]",mapname);
  2310. if (!mapindex_name2id(mapname)) { //Incorrect map
  2311. ShowError("Invalid map '%s' in line %d, file %s\n", mapname, lines, current_file);
  2312. if (strcmpi(w2,"script") == 0 && count > 3) //we must skip the script info...
  2313. npc_skip_script(w1,w2,w3,w4,line+w4pos,fp,&lines);
  2314. continue;
  2315. }
  2316. if ((m = map_mapname2mapid(mapname)) < 0) {
  2317. // "mapname" is not assigned to this server
  2318. // we must skip the script info...
  2319. if (strcmpi(w2,"script") == 0 && count > 3)
  2320. npc_skip_script(w1,w2,w3,w4,line+w4pos,fp,&lines);
  2321. continue;
  2322. }
  2323. }
  2324. if (strcmpi(w2,"warp") == 0 && count > 3) {
  2325. npc_parse_warp(w1,w2,w3,w4);
  2326. } else if (strcmpi(w2,"shop") == 0 && count > 3) {
  2327. npc_parse_shop(w1,w2,w3,w4);
  2328. } else if (strcmpi(w2,"script") == 0 && count > 3) {
  2329. if (strcmpi(w1,"function") == 0) {
  2330. npc_parse_function(w1,w2,w3,w4,line+w4pos,fp,&lines,name);
  2331. } else {
  2332. npc_parse_script(w1,w2,w3,w4,line+w4pos,fp,&lines,name);
  2333. }
  2334. } else if ((i = 0, sscanf(w2,"duplicate%n",&i), (i > 0 && w2[i] == '(')) && count > 3) {
  2335. npc_parse_script(w1,w2,w3,w4,line+w4pos,fp,&lines,name);
  2336. } else if (strcmpi(w2,"monster") == 0 && count > 3) {
  2337. npc_parse_mob(w1,w2,w3,w4);
  2338. } else if (strcmpi(w2,"mapflag") == 0 && count >= 3) {
  2339. npc_parse_mapflag(w1,w2,w3,w4);
  2340. } else if (strcmpi(w2,"setcell") == 0 && count >= 3) {
  2341. npc_parse_mapcell(w1,w2,w3,w4);
  2342. } else {
  2343. ShowError("Probably TAB is missing: %s %s %s %s line '%i', file '%s'\n",w1,w2,w3,w4,lines,current_file); //Lupus
  2344. }
  2345. }
  2346. fclose(fp);
  2347. return;
  2348. }
  2349. int npc_script_event(TBL_PC* sd, int type) {
  2350. int i;
  2351. if (type < 0 || type >= NPCE_MAX)
  2352. return 0;
  2353. if (!sd) {
  2354. if (battle_config.error_log)
  2355. ShowError("npc_script_event: NULL sd. Event Type %d\n", type);
  2356. return 0;
  2357. }
  2358. if (script_event[type].nd) {
  2359. TBL_NPC *nd = script_event[type].nd;
  2360. run_script(nd->u.scr.script,0,sd->bl.id,nd->bl.id);
  2361. return 1;
  2362. } else if (script_event[type].event_count) {
  2363. for (i = 0; i<script_event[type].event_count; i++) {
  2364. npc_event_sub(sd,script_event[type].event[i],script_event[type].event_name[i]);
  2365. }
  2366. return i;
  2367. }
  2368. return 0;
  2369. }
  2370. static int npc_read_event_script_sub(DBKey key,void *data,va_list ap)
  2371. {
  2372. unsigned char *p = key.str;
  2373. unsigned char *name = va_arg(ap,unsigned char *);
  2374. struct event_data **event_buf = va_arg(ap,struct event_data**);
  2375. unsigned char **event_name = va_arg(ap,unsigned char **);
  2376. unsigned char *count = va_arg(ap,char *);;
  2377. if (*count >= UCHAR_MAX) return 0;
  2378. if((p=strchr(p,':')) && p && strcmpi(name,p)==0 )
  2379. {
  2380. event_buf[*count] = (struct event_data *)data;
  2381. event_name[*count] = key.str;
  2382. (*count)++;
  2383. return 1;
  2384. }
  2385. return 0;
  2386. }
  2387. static void npc_read_event_script(void)
  2388. {
  2389. int i;
  2390. unsigned char buf[64]="::";
  2391. struct {
  2392. char *name;
  2393. char *event_name;
  2394. } config[] = {
  2395. {"Login Event",script_config.login_event_name},
  2396. {"Logout Event",script_config.logout_event_name},
  2397. {"Load Map Event",script_config.loadmap_event_name},
  2398. {"Base LV Up Event",script_config.baselvup_event_name},
  2399. {"Job LV Up Event",script_config.joblvup_event_name},
  2400. {"Die Event",script_config.die_event_name},
  2401. {"Kill PC Event",script_config.kill_pc_event_name},
  2402. {"Kill NPC Event",script_config.kill_mob_event_name},
  2403. };
  2404. for (i = 0; i < NPCE_MAX; i++) {
  2405. if (script_event[i].nd)
  2406. script_event[i].nd = NULL;
  2407. if (script_event[i].event_count)
  2408. script_event[i].event_count = 0;
  2409. if (!script_config.event_script_type) {
  2410. //Use a single NPC as event source.
  2411. script_event[i].nd = npc_name2id(config[i].event_name);
  2412. } else {
  2413. //Use an array of Events
  2414. strncpy(buf+2,config[i].event_name,62);
  2415. ev_db->foreach(ev_db,npc_read_event_script_sub,buf,
  2416. &script_event[i].event,
  2417. &script_event[i].event_name,
  2418. &script_event[i].event_count);
  2419. }
  2420. }
  2421. if (battle_config.etc_log) {
  2422. //Print summary.
  2423. for (i = 0; i < NPCE_MAX; i++) {
  2424. if(!script_config.event_script_type) {
  2425. if (script_event[i].nd)
  2426. ShowInfo("%s: Using NPC named '%s'.\n", config[i].name, config[i].event_name);
  2427. else
  2428. ShowInfo("%s: No NPC found with name '%s'.\n", config[i].name, config[i].event_name);
  2429. } else
  2430. ShowInfo("%s: %d '%s' events.\n", config[i].name, script_event[i].event_count, config[i].event_name);
  2431. }
  2432. }
  2433. }
  2434. static int npc_read_indoors (void)
  2435. {
  2436. char *buf, *p;
  2437. int s, m;
  2438. buf = (char *)grfio_reads("data\\indoorrswtable.txt",&s);
  2439. if (buf == NULL)
  2440. return -1;
  2441. buf[s] = 0;
  2442. for (p = buf; p - buf < s; ) {
  2443. char map_name[64];
  2444. if (sscanf(p, "%15[^#]#", map_name) == 1) {
  2445. size_t pos = strlen(map_name) - 4; // replace '.xxx' extension
  2446. memcpy(map_name+pos,".gat",4); // with '.gat'
  2447. if ((m = map_mapname2mapid(map_name)) >= 0)
  2448. map[m].flag.indoors = 1;
  2449. }
  2450. p = strchr(p, 10);
  2451. if (!p) break;
  2452. p++;
  2453. }
  2454. aFree(buf);
  2455. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","data\\indoorrswtable.txt");
  2456. return 0;
  2457. }
  2458. /*==========================================
  2459. *
  2460. *------------------------------------------
  2461. */
  2462. static int npc_cleanup_sub (struct block_list *bl, va_list ap) {
  2463. nullpo_retr(0, bl);
  2464. switch(bl->type) {
  2465. case BL_NPC:
  2466. npc_unload((struct npc_data *)bl);
  2467. break;
  2468. case BL_MOB:
  2469. unit_free(bl);
  2470. break;
  2471. }
  2472. return 0;
  2473. }
  2474. static int npc_cleanup_dbsub(DBKey key,void * data,va_list app) {
  2475. return npc_cleanup_sub((struct block_list*)data, 0);
  2476. }
  2477. int npc_reload (void)
  2478. {
  2479. struct npc_src_list *nsl;
  2480. int m, i;
  2481. time_t last_time = time(0);
  2482. int busy = 0, npc_new_min = npc_id;
  2483. char c = '-';
  2484. //Remove all npcs/mobs. [Skotlex]
  2485. map_foreachiddb(npc_cleanup_dbsub);
  2486. for (m = 0; m < map_num; m++) {
  2487. if(battle_config.dynamic_mobs) { //dynamic check by [random]
  2488. for (i = 0; i < MAX_MOB_LIST_PER_MAP; i++)
  2489. if (map[m].moblist[i]) aFree(map[m].moblist[i]);
  2490. memset (map[m].moblist, 0, sizeof(map[m].moblist));
  2491. }
  2492. if (map[m].npc_num > 0 && battle_config.error_log)
  2493. ShowWarning("npc_reload: %d npcs weren't removed at map %s!\n", map[m].npc_num, map[m].name);
  2494. }
  2495. // anything else we should cleanup?
  2496. // Reloading npc's now
  2497. ev_db->clear(ev_db,NULL);
  2498. npcname_db->clear(npcname_db,NULL);
  2499. npc_warp = npc_shop = npc_script = 0;
  2500. npc_mob = npc_cache_mob = npc_delay_mob = 0;
  2501. for (nsl = npc_src_first; nsl; nsl = nsl->next) {
  2502. npc_parsesrcfile(nsl->name);
  2503. if (script_config.verbose_mode) {
  2504. printf("\r");
  2505. ShowStatus("Loading NPCs... %-53s", nsl->name);
  2506. } else {
  2507. if (last_time != time(0)) {
  2508. printf("\r");
  2509. ShowStatus("Loading NPCs... Working: ");
  2510. last_time = time(0);
  2511. switch(busy) {
  2512. case 0: c='\\'; busy++; break;
  2513. case 1: c='|'; busy++; break;
  2514. case 2: c='/'; busy++; break;
  2515. case 3: c='-'; busy=0;
  2516. }
  2517. printf("[%c]",c);
  2518. }
  2519. }
  2520. fflush(stdout);
  2521. }
  2522. printf("\r");
  2523. ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:%30s\n\t-'"
  2524. CL_WHITE"%d"CL_RESET"' Warps\n\t-'"
  2525. CL_WHITE"%d"CL_RESET"' Shops\n\t-'"
  2526. CL_WHITE"%d"CL_RESET"' Scripts\n\t-'"
  2527. CL_WHITE"%d"CL_RESET"' Mobs\n\t-'"
  2528. CL_WHITE"%d"CL_RESET"' Mobs Cached\n\t-'"
  2529. CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
  2530. npc_id - npc_new_min, "", npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
  2531. //Re-read the NPC Script Events cache.
  2532. npc_read_event_script();
  2533. //Execute the OnInit event for freshly loaded npcs. [Skotlex]
  2534. ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"
  2535. CL_WHITE"%d"CL_RESET"' NPCs.\n",npc_event_doall("OnInit"));
  2536. // Execute rest of the startup events if connected to char-server. [Lance]
  2537. if(!CheckForCharServer()){
  2538. ShowStatus("Event '"CL_WHITE"OnCharIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnCharIfInit"));
  2539. ShowStatus("Event '"CL_WHITE"OnInterIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInit"));
  2540. ShowStatus("Event '"CL_WHITE"OnInterIfInitOnce"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInitOnce"));
  2541. }
  2542. return 0;
  2543. }
  2544. /*==========================================
  2545. * 終了
  2546. *------------------------------------------
  2547. */
  2548. int do_final_npc(void)
  2549. {
  2550. int i;
  2551. struct block_list *bl;
  2552. struct npc_data *nd;
  2553. struct mob_data *md;
  2554. struct pet_data *pd;
  2555. for (i = START_NPC_NUM; i < npc_id; i++){
  2556. if ((bl = map_id2bl(i))){
  2557. if (bl->type == BL_NPC && (nd = (struct npc_data *)bl)){
  2558. npc_unload(nd);
  2559. } else if (bl->type == BL_MOB && (md = (struct mob_data *)bl)){
  2560. if (md->lootitem)
  2561. aFree(md->lootitem);
  2562. aFree(md);
  2563. } else if (bl->type == BL_PET && (pd = (struct pet_data *)bl)){
  2564. aFree(pd);
  2565. }
  2566. }
  2567. }
  2568. ev_db->destroy(ev_db, NULL);
  2569. //There is no free function for npcname_db because at this point there shouldn't be any npcs left!
  2570. //So if there is anything remaining, let the memory manager catch it and report it.
  2571. npcname_db->destroy(npcname_db, NULL);
  2572. ers_destroy(timer_event_ers);
  2573. npc_clearsrcfile();
  2574. return 0;
  2575. }
  2576. static void npc_debug_warps_sub(struct npc_data *nd)
  2577. {
  2578. int m;
  2579. if (nd->bl.type != BL_NPC || nd->bl.subtype != WARP || nd->bl.m < 0)
  2580. return;
  2581. m = map_mapindex2mapid(nd->u.warp.mapindex);
  2582. if (m < 0) return; //Warps to another map, nothing to do about it.
  2583. if (map_getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNPC)) {
  2584. ShowWarning("Warp %s at %s(%d,%d) warps directly on top of an area npc at %s(%d,%d)\n",
  2585. nd->name,
  2586. map[nd->bl.m].name, nd->bl.x, nd->bl.y,
  2587. map[m].name, nd->u.warp.x, nd->u.warp.y
  2588. );
  2589. }
  2590. if (map_getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNOPASS)) {
  2591. ShowWarning("Warp %s at %s(%d,%d) warps to a non-walkable tile at %s(%d,%d)\n",
  2592. nd->name,
  2593. map[nd->bl.m].name, nd->bl.x, nd->bl.y,
  2594. map[m].name, nd->u.warp.x, nd->u.warp.y
  2595. );
  2596. }
  2597. }
  2598. static void npc_debug_warps(void)
  2599. {
  2600. int m, i;
  2601. for (m = 0; m < map_num; m++)
  2602. for (i = 0; i < map[m].npc_num; i++)
  2603. npc_debug_warps_sub(map[m].npc[i]);
  2604. }
  2605. /*==========================================
  2606. * npc初期化
  2607. *------------------------------------------
  2608. */
  2609. int do_init_npc(void)
  2610. {
  2611. struct npc_src_list *nsl;
  2612. time_t last_time = time(0);
  2613. int busy, i;
  2614. char c = '-';
  2615. //Stock view data for normal npcs.
  2616. memset(&npc_viewdb, 0, sizeof(npc_viewdb));
  2617. npc_viewdb[0].class_ = INVISIBLE_CLASS; //Invisible class is stored here.
  2618. for (busy = 1; busy < MAX_NPC_CLASS; busy++)
  2619. npc_viewdb[busy].class_ = busy;
  2620. busy = 0;
  2621. // indoorrswtable.txt and etcinfo.txt [Celest]
  2622. if (battle_config.indoors_override_grffile)
  2623. npc_read_indoors();
  2624. // comparing only the first 24 chars of labels that are 50 chars long isn't that nice
  2625. // will cause "duplicated" labels where actually no dup is...
  2626. ev_db = db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA,51);
  2627. npcname_db = db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_BASE,NAME_LENGTH);
  2628. memset(&ev_tm_b, -1, sizeof(ev_tm_b));
  2629. timer_event_ers = ers_new((uint32)sizeof(struct timer_event_data));
  2630. for (nsl = npc_src_first; nsl; nsl = nsl->next) {
  2631. npc_parsesrcfile(nsl->name);
  2632. current_file = NULL;
  2633. printf("\r");
  2634. if (script_config.verbose_mode)
  2635. ShowStatus ("Loading NPCs... %-53s", nsl->name);
  2636. else {
  2637. ShowStatus("Loading NPCs... Working: ");
  2638. if (last_time != time(0)) {
  2639. last_time = time(0);
  2640. switch(busy) {
  2641. case 0: c='\\'; busy++; break;
  2642. case 1: c='|'; busy++; break;
  2643. case 2: c='/'; busy++; break;
  2644. case 3: c='-'; busy=0;
  2645. }
  2646. }
  2647. printf("[%c]",c);
  2648. }
  2649. fflush(stdout);
  2650. }
  2651. printf("\r");
  2652. ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:%30s\n\t-'"
  2653. CL_WHITE"%d"CL_RESET"' Warps\n\t-'"
  2654. CL_WHITE"%d"CL_RESET"' Shops\n\t-'"
  2655. CL_WHITE"%d"CL_RESET"' Scripts\n\t-'"
  2656. CL_WHITE"%d"CL_RESET"' Mobs\n\t-'"
  2657. CL_WHITE"%d"CL_RESET"' Mobs Cached\n\t-'"
  2658. CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
  2659. npc_id - START_NPC_NUM, "", npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
  2660. memset(script_event, 0, sizeof(script_event));
  2661. npc_read_event_script();
  2662. //Debug function to locate all endless loop warps.
  2663. if (battle_config.warp_point_debug)
  2664. npc_debug_warps();
  2665. add_timer_func_list(npc_event_timer,"npc_event_timer");
  2666. add_timer_func_list(npc_event_do_clock,"npc_event_do_clock");
  2667. add_timer_func_list(npc_timerevent,"npc_timerevent");
  2668. // Init dummy NPC
  2669. fake_nd = (struct npc_data *)aCalloc(sizeof(struct npc_data),1);
  2670. fake_nd->bl.prev = fake_nd->bl.next = NULL;
  2671. fake_nd->bl.m = -1;
  2672. fake_nd->bl.x = 0;
  2673. fake_nd->bl.y = 0;
  2674. fake_nd->bl.id = npc_get_new_npc_id();
  2675. fake_nd->class_ = -1;
  2676. fake_nd->speed = 200;
  2677. fake_nd->u.scr.script = NULL;
  2678. fake_nd->u.scr.src_id = 0;
  2679. fake_nd->chatdb = NULL;
  2680. for (i = 0; i < MAX_EVENTTIMER; i++)
  2681. fake_nd->eventtimer[i] = -1;
  2682. strcpy(fake_nd->name,"FAKE_NPC");
  2683. memcpy(fake_nd->exname, fake_nd->name, 9);
  2684. npc_script++;
  2685. fake_nd->bl.type = BL_NPC;
  2686. fake_nd->bl.subtype = SCRIPT;
  2687. strdb_put(npcname_db, fake_nd->exname, fake_nd);
  2688. fake_nd->u.scr.timerid = -1;
  2689. map_addiddb(&fake_nd->bl);
  2690. // End of initialization
  2691. return 0;
  2692. }
  2693. // [Lance]
  2694. int npc_changename(const char *name, const char *newname, short look){
  2695. struct npc_data *nd= (struct npc_data *) strdb_remove(npcname_db,(unsigned char*)name);
  2696. if (nd==NULL)
  2697. return 0;
  2698. npc_enable(name,0);
  2699. strcpy(nd->name,newname);
  2700. nd->class_ = look;
  2701. npc_enable(newname,1);
  2702. return 0;
  2703. }