npc.c 84 KB

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