npc.c 81 KB

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