npc.c 73 KB

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