npc.c 76 KB

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