npc.c 84 KB

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