npc.c 80 KB

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