npc.c 93 KB

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