npc.c 82 KB

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