npc.c 83 KB

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