npc.c 81 KB

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