npc.c 82 KB

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