npc.c 82 KB

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