npc.c 77 KB

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