mob.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/timer.h"
  5. #include "../common/db.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/malloc.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/ers.h"
  10. #include "../common/strlib.h"
  11. #include "../common/utils.h"
  12. #include "../common/socket.h"
  13. #include "map.h"
  14. #include "path.h"
  15. #include "clif.h"
  16. #include "intif.h"
  17. #include "pc.h"
  18. #include "pet.h"
  19. #include "status.h"
  20. #include "mob.h"
  21. #include "homunculus.h"
  22. #include "guild.h"
  23. #include "itemdb.h"
  24. #include "skill.h"
  25. #include "battle.h"
  26. #include "party.h"
  27. #include "npc.h"
  28. #include "log.h"
  29. #include "script.h"
  30. #include "atcommand.h"
  31. #include "date.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <string.h>
  36. #include <math.h>
  37. #define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode.
  38. #define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME)
  39. #define MOB_LAZYSKILLPERC 0 // Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
  40. // Move probability for mobs away from players (rate of 1000 minute)
  41. // in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
  42. #define MOB_LAZYMOVEPERC(md) (md->state.spotted?1000:0)
  43. #define MOB_LAZYWARPPERC 20 // Warp probability in the negligent mode MOB (rate of 1000 minute)
  44. #define MAX_MINCHASE 30 //Max minimum chase value to use for mobs.
  45. #define RUDE_ATTACKED_COUNT 2 //After how many rude-attacks should the skill be used?
  46. //Used to determine default enemy type of mobs (for use in eachinrange calls)
  47. #define DEFAULT_ENEMY_TYPE(md) (md->special_state.ai?BL_CHAR:BL_PC|BL_HOM|BL_MER)
  48. //Dynamic mob database, allows saving of memory when there's big gaps in the mob_db [Skotlex]
  49. struct mob_db *mob_db_data[MAX_MOB_DB+1];
  50. struct mob_db *mob_dummy = NULL; //Dummy mob to be returned when a non-existant one is requested.
  51. struct mob_db *mob_db(int index) { if (index < 0 || index > MAX_MOB_DB || mob_db_data[index] == NULL) return mob_dummy; return mob_db_data[index]; }
  52. static struct eri *item_drop_ers; //For loot drops delay structures.
  53. static struct eri *item_drop_list_ers;
  54. static struct {
  55. int qty;
  56. int class_[350];
  57. } summon[MAX_RANDOMMONSTER];
  58. #define CLASSCHANGE_BOSS_NUM 21
  59. /*==========================================
  60. * Local prototype declaration (only required thing)
  61. *------------------------------------------*/
  62. static int mob_makedummymobdb(int);
  63. static int mob_spawn_guardian_sub(int tid, unsigned int tick, int id, intptr data);
  64. int mobskill_use(struct mob_data *md,unsigned int tick,int event);
  65. int mob_skillid2skillidx(int class_,int skillid);
  66. /*==========================================
  67. * Mob is searched with a name.
  68. *------------------------------------------*/
  69. int mobdb_searchname(const char *str)
  70. {
  71. int i;
  72. struct mob_db* mob;
  73. for(i=0;i<=MAX_MOB_DB;i++){
  74. mob = mob_db(i);
  75. if(mob == mob_dummy) //Skip dummy mobs.
  76. continue;
  77. if(strcmpi(mob->name,str)==0 || strcmpi(mob->jname,str)==0 || strcmpi(mob->sprite,str)==0)
  78. return i;
  79. }
  80. return 0;
  81. }
  82. static int mobdb_searchname_array_sub(struct mob_db* mob, const char *str)
  83. {
  84. if (mob == mob_dummy)
  85. return 1; //Invalid mob.
  86. if(!mob->base_exp && !mob->job_exp)
  87. return 1; //Discount slave-mobs (no exp) as requested by Playtester. [Skotlex]
  88. if(stristr(mob->jname,str))
  89. return 0;
  90. if(stristr(mob->name,str))
  91. return 0;
  92. return strcmpi(mob->jname,str);
  93. }
  94. /*==========================================
  95. * Founds up to N matches. Returns number of matches [Skotlex]
  96. *------------------------------------------*/
  97. int mobdb_searchname_array(struct mob_db** data, int size, const char *str)
  98. {
  99. int count = 0, i;
  100. struct mob_db* mob;
  101. for(i=0;i<=MAX_MOB_DB;i++){
  102. mob = mob_db(i);
  103. if (mob == mob_dummy)
  104. continue;
  105. if (!mobdb_searchname_array_sub(mob, str)) {
  106. if (count < size)
  107. data[count] = mob;
  108. count++;
  109. }
  110. }
  111. return count;
  112. }
  113. /*==========================================
  114. * Id Mob is checked.
  115. *------------------------------------------*/
  116. int mobdb_checkid(const int id)
  117. {
  118. if (mob_db(id) == mob_dummy)
  119. return 0;
  120. if (mob_is_clone(id)) //checkid is used mostly for random ID based code, therefore clone mobs are out of the question.
  121. return 0;
  122. return id;
  123. }
  124. /*==========================================
  125. * Returns the view data associated to this mob class.
  126. *------------------------------------------*/
  127. struct view_data * mob_get_viewdata(int class_)
  128. {
  129. if (mob_db(class_) == mob_dummy)
  130. return 0;
  131. return &mob_db(class_)->vd;
  132. }
  133. /*==========================================
  134. * Cleans up mob-spawn data to make it "valid"
  135. *------------------------------------------*/
  136. int mob_parse_dataset(struct spawn_data *data)
  137. {
  138. int i;
  139. //FIXME: This implementation is not stable, npc scripts will stop working once MAX_MOB_DB changes value! [Skotlex]
  140. if(data->class_ > 2*MAX_MOB_DB){ // large/tiny mobs [Valaris]
  141. data->state.size=2;
  142. data->class_ -= 2*MAX_MOB_DB;
  143. } else if (data->class_ > MAX_MOB_DB) {
  144. data->state.size=1;
  145. data->class_ -= MAX_MOB_DB;
  146. }
  147. if ((!mobdb_checkid(data->class_) && !mob_is_clone(data->class_)) || !data->num)
  148. return 0;
  149. //better safe than sorry, current md->npc_event has a size of 50
  150. if ((i=strlen(data->eventname)) >= 50)
  151. return 0;
  152. if (data->eventname[0])
  153. {
  154. if(i <= 2)
  155. { //Portable monster big/small implementation. [Skotlex]
  156. i = atoi(data->eventname);
  157. if (i) {
  158. if (i&2)
  159. data->state.size=1;
  160. else if (i&4)
  161. data->state.size=2;
  162. if (i&8)
  163. data->state.ai=1;
  164. data->eventname[0] = '\0'; //Clear event as it is not used.
  165. }
  166. } else {
  167. if (data->eventname[i-1] == '"')
  168. data->eventname[i-1] = '\0'; //Remove trailing quote.
  169. if (data->eventname[0] == '"') //Strip leading quotes
  170. memmove(data->eventname, data->eventname+1, i-1);
  171. }
  172. }
  173. if (!data->level)
  174. data->level = mob_db(data->class_)->lv;
  175. if(strcmp(data->name,"--en--")==0)
  176. strncpy(data->name,mob_db(data->class_)->name,NAME_LENGTH-1);
  177. else if(strcmp(data->name,"--ja--")==0)
  178. strncpy(data->name,mob_db(data->class_)->jname,NAME_LENGTH-1);
  179. return 1;
  180. }
  181. /*==========================================
  182. * Generates the basic mob data using the spawn_data provided.
  183. *------------------------------------------*/
  184. struct mob_data* mob_spawn_dataset(struct spawn_data *data)
  185. {
  186. struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
  187. md->bl.id= npc_get_new_npc_id();
  188. md->bl.type = BL_MOB;
  189. md->bl.m = data->m;
  190. md->bl.x = data->x;
  191. md->bl.y = data->y;
  192. md->class_ = data->class_;
  193. md->db = mob_db(md->class_);
  194. memcpy(md->name, data->name, NAME_LENGTH);
  195. if (data->state.ai)
  196. md->special_state.ai = data->state.ai;
  197. if (data->state.size)
  198. md->special_state.size = data->state.size;
  199. if (data->eventname[0] && strlen(data->eventname) >= 4)
  200. memcpy(md->npc_event, data->eventname, 50);
  201. md->level = data->level;
  202. if(md->db->status.mode&MD_LOOTER)
  203. md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  204. md->deletetimer = INVALID_TIMER;
  205. md->skillidx = -1;
  206. status_set_viewdata(&md->bl, md->class_);
  207. status_change_init(&md->bl);
  208. unit_dataset(&md->bl);
  209. map_addiddb(&md->bl);
  210. return md;
  211. }
  212. /*==========================================
  213. * Fetches a random mob_id [Skotlex]
  214. * type: Where to fetch from:
  215. * 0: dead branch list
  216. * 1: poring list
  217. * 2: bloody branch list
  218. * flag:
  219. * &1: Apply the summon success chance found in the list (otherwise get any monster from the db)
  220. * &2: Apply a monster check level.
  221. * &4: Selected monster should not be a boss type
  222. * &8: Selected monster must have normal spawn.
  223. * lv: Mob level to check against
  224. *------------------------------------------*/
  225. int mob_get_random_id(int type, int flag, int lv)
  226. {
  227. struct mob_db *mob;
  228. int i=0, class_;
  229. if(type < 0 || type >= MAX_RANDOMMONSTER) {
  230. ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
  231. return 0;
  232. }
  233. do {
  234. if (type)
  235. class_ = summon[type].class_[rand()%summon[type].qty];
  236. else //Dead branch
  237. class_ = rand() % MAX_MOB_DB;
  238. mob = mob_db(class_);
  239. } while ((mob == mob_dummy ||
  240. mob_is_clone(class_) ||
  241. (flag&1 && mob->summonper[type] <= rand() % 1000000) ||
  242. (flag&2 && lv < mob->lv) ||
  243. (flag&4 && mob->status.mode&MD_BOSS) ||
  244. (flag&8 && mob->spawn[0].qty < 1)
  245. ) && (i++) < MAX_MOB_DB);
  246. if(i >= MAX_MOB_DB)
  247. class_ = mob_db_data[0]->summonper[type];
  248. return class_;
  249. }
  250. /*==========================================
  251. * Kill Steal Protection [Zephyrus]
  252. *------------------------------------------*/
  253. bool mob_ksprotected (struct block_list *src, struct block_list *target)
  254. {
  255. struct block_list *s_bl, *t_bl;
  256. struct map_session_data
  257. *sd, // Source
  258. *pl_sd, // Owner
  259. *t_sd; // Mob Target
  260. struct status_change_entry *sce;
  261. struct mob_data *md;
  262. unsigned int tick = gettick();
  263. char output[128];
  264. if( !battle_config.ksprotection )
  265. return false; // KS Protection Disabled
  266. if( !(md = BL_CAST(BL_MOB,target)) )
  267. return false; // Tarjet is not MOB
  268. if( (s_bl = battle_get_master(src)) == NULL )
  269. s_bl = src;
  270. if( !(sd = BL_CAST(BL_PC,s_bl)) )
  271. return false; // Master is not PC
  272. t_bl = map_id2bl(md->target_id);
  273. if( !t_bl || (s_bl = battle_get_master(t_bl)) == NULL )
  274. s_bl = t_bl;
  275. t_sd = BL_CAST(BL_PC,s_bl);
  276. do {
  277. if( map[md->bl.m].flag.allowks || map_flag_ks(md->bl.m) )
  278. return false; // Ignores GVG, PVP and AllowKS map flags
  279. if( md->db->mexp || md->master_id )
  280. return false; // MVP, Slaves mobs ignores KS
  281. if( (sce = md->sc.data[SC_KSPROTECTED]) == NULL )
  282. break; // No KS Protected
  283. if( sd->bl.id == sce->val1 || // Same Owner
  284. (sce->val2 == 2 && sd->status.party_id && sd->status.party_id == sce->val3) || // Party KS allowed
  285. (sce->val2 == 3 && sd->status.guild_id && sd->status.guild_id == sce->val4) ) // Guild KS allowed
  286. break;
  287. if( t_sd && (
  288. (sce->val2 == 1 && sce->val1 != t_sd->bl.id) ||
  289. (sce->val2 == 2 && sce->val3 && sce->val3 != t_sd->status.party_id) ||
  290. (sce->val2 == 3 && sce->val4 && sce->val4 != t_sd->status.guild_id)) )
  291. break;
  292. if( (pl_sd = map_id2sd(sce->val1)) == NULL || pl_sd->bl.m != md->bl.m )
  293. break;
  294. if( !pl_sd->state.noks )
  295. return false; // No KS Protected, but normal players should be protected too
  296. // Message to KS
  297. if( DIFF_TICK(sd->ks_floodprotect_tick, tick) <= 0 )
  298. {
  299. sprintf(output, "[KS Warning!! - Owner : %s]", pl_sd->status.name);
  300. clif_disp_onlyself(sd, output, strlen(output));
  301. sd->ks_floodprotect_tick = tick + 2000;
  302. }
  303. // Message to Owner
  304. if( DIFF_TICK(pl_sd->ks_floodprotect_tick, tick) <= 0 )
  305. {
  306. sprintf(output, "[Watch out! %s is trying to KS you!]", sd->status.name);
  307. clif_disp_onlyself(pl_sd, output, strlen(output));
  308. pl_sd->ks_floodprotect_tick = tick + 2000;
  309. }
  310. return true;
  311. } while(0);
  312. status_change_start(target, SC_KSPROTECTED, 10000, sd->bl.id, sd->state.noks, sd->status.party_id, sd->status.guild_id, battle_config.ksprotection, 0);
  313. return false;
  314. }
  315. struct mob_data *mob_once_spawn_sub(struct block_list *bl, int m, short x, short y, const char *mobname, int class_, const char *event)
  316. {
  317. struct spawn_data data;
  318. memset(&data, 0, sizeof(struct spawn_data));
  319. data.m = m;
  320. data.num = 1;
  321. data.class_ = class_;
  322. if (mobname)
  323. safestrncpy(data.name, mobname, sizeof(data.name));
  324. else
  325. if(battle_config.override_mob_names==1)
  326. strcpy(data.name,"--en--");
  327. else
  328. strcpy(data.name,"--ja--");
  329. if (event)
  330. safestrncpy(data.eventname, event, sizeof(data.eventname));
  331. // Locate spot next to player.
  332. if (bl && (x < 0 || y < 0))
  333. map_search_freecell(bl, m, &x, &y, 1, 1, 0);
  334. // if none found, pick random position on map
  335. if (x <= 0 || y <= 0 || map_getcell(m,x,y,CELL_CHKNOREACH))
  336. map_search_freecell(NULL, m, &x, &y, -1, -1, 1);
  337. data.x = x;
  338. data.y = y;
  339. if (!mob_parse_dataset(&data))
  340. return NULL;
  341. return mob_spawn_dataset(&data);
  342. }
  343. /*==========================================
  344. * Spawn a single mob on the specified coordinates.
  345. *------------------------------------------*/
  346. int mob_once_spawn(struct map_session_data* sd, int m, short x, short y, const char* mobname, int class_, int amount, const char* event)
  347. {
  348. struct mob_data* md = NULL;
  349. int count, lv;
  350. if (m < 0 || amount <= 0)
  351. return 0; // invalid input
  352. if(sd)
  353. lv = sd->status.base_level;
  354. else
  355. lv = 255;
  356. for (count = 0; count < amount; count++)
  357. {
  358. int c = ( class_ >= 0 ) ? class_ : mob_get_random_id(-class_-1, battle_config.random_monster_checklv?3:1, lv);
  359. md = mob_once_spawn_sub(sd?&sd->bl:NULL, m, x, y, mobname, c, event);
  360. if (!md) continue;
  361. if(class_ == MOBID_EMPERIUM) {
  362. struct guild_castle* gc = guild_mapindex2gc(map[m].index);
  363. struct guild* g = gc?guild_search(gc->guild_id):NULL;
  364. if(gc) {
  365. md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
  366. md->guardian_data->castle = gc;
  367. md->guardian_data->number = MAX_GUARDIANS;
  368. md->guardian_data->guild_id = gc->guild_id;
  369. if (g)
  370. {
  371. md->guardian_data->emblem_id = g->emblem_id;
  372. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  373. }
  374. else if (gc->guild_id) //Guild not yet available, retry in 5.
  375. add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
  376. }
  377. } // end addition [Valaris]
  378. mob_spawn(md);
  379. if (class_ < 0 && battle_config.dead_branch_active)
  380. //Behold Aegis's masterful decisions yet again...
  381. //"I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" - Poki#3
  382. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE, 0, 60000);
  383. }
  384. return (md)?md->bl.id : 0; // id of last spawned mob
  385. }
  386. /*==========================================
  387. * Spawn mobs in the specified area.
  388. *------------------------------------------*/
  389. int mob_once_spawn_area(struct map_session_data* sd,int m,int x0,int y0,int x1,int y1,const char* mobname,int class_,int amount,const char* event)
  390. {
  391. int i,max,id=0;
  392. int lx=-1,ly=-1;
  393. if (m < 0 || amount <= 0)
  394. return 0; // invalid input
  395. // normalize x/y coordinates
  396. if( x0 > x1 ) swap(x0,x1);
  397. if( y0 > y1 ) swap(y0,y1);
  398. // choose a suitable max. number of attempts
  399. max = (y1-y0+1)*(x1-x0+1)*3;
  400. if( max > 1000 )
  401. max = 1000;
  402. // spawn mobs, one by one
  403. for( i = 0; i < amount; i++)
  404. {
  405. int x,y;
  406. int j = 0;
  407. // find a suitable map cell
  408. do {
  409. x = rand()%(x1-x0+1)+x0;
  410. y = rand()%(y1-y0+1)+y0;
  411. j++;
  412. } while( map_getcell(m,x,y,CELL_CHKNOPASS) && j < max );
  413. if( j == max )
  414. {// attempt to find an available cell failed
  415. if( lx == -1 && ly == -1 )
  416. return 0; // total failure
  417. // fallback to last good x/y pair
  418. x = lx;
  419. y = ly;
  420. }
  421. // record last successful coordinates
  422. lx = x;
  423. ly = y;
  424. id = mob_once_spawn(sd,m,x,y,mobname,class_,1,event);
  425. }
  426. return id; // id of last spawned mob
  427. }
  428. /*==========================================
  429. * Set a Guardian's guild data [Skotlex]
  430. *------------------------------------------*/
  431. static int mob_spawn_guardian_sub(int tid, unsigned int tick, int id, intptr data)
  432. { //Needed because the guild_data may not be available at guardian spawn time.
  433. struct block_list* bl = map_id2bl(id);
  434. struct mob_data* md;
  435. struct guild* g;
  436. int guardup_lv;
  437. if (bl == NULL) //It is possible mob was already removed from map when the castle has no owner. [Skotlex]
  438. return 0;
  439. if (bl->type != BL_MOB)
  440. {
  441. ShowError("mob_spawn_guardian_sub: Block error!\n");
  442. return 0;
  443. }
  444. md = (struct mob_data*)bl;
  445. nullpo_retr(0, md->guardian_data);
  446. g = guild_search((int)data);
  447. if (g == NULL)
  448. { //Liberate castle, if the guild is not found this is an error! [Skotlex]
  449. ShowError("mob_spawn_guardian_sub: Couldn't load guild %d!\n", (int)data);
  450. if (md->class_ == MOBID_EMPERIUM)
  451. { //Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
  452. md->guardian_data->guild_id = 0;
  453. if (md->guardian_data->castle->guild_id) //Free castle up.
  454. {
  455. ShowNotice("Clearing ownership of castle %d (%s)\n", md->guardian_data->castle->castle_id, md->guardian_data->castle->castle_name);
  456. guild_castledatasave(md->guardian_data->castle->castle_id, 1, 0);
  457. }
  458. } else {
  459. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS && md->guardian_data->castle->guardian[md->guardian_data->number].visible )
  460. { //Safe removal of guardian.
  461. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  462. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  463. }
  464. unit_free(&md->bl,0); //Remove guardian.
  465. }
  466. return 0;
  467. }
  468. guardup_lv = guild_checkskill(g,GD_GUARDUP);
  469. md->guardian_data->emblem_id = g->emblem_id;
  470. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  471. md->guardian_data->guardup_lv = guardup_lv;
  472. if( guardup_lv )
  473. status_calc_mob(md, 0); //Give bonuses.
  474. return 0;
  475. }
  476. /*==========================================
  477. * Summoning Guardians [Valaris]
  478. *------------------------------------------*/
  479. int mob_spawn_guardian(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, int guardian, bool has_index)
  480. {
  481. struct mob_data *md=NULL;
  482. struct spawn_data data;
  483. struct guild *g=NULL;
  484. struct guild_castle *gc;
  485. int m;
  486. memset(&data, 0, sizeof(struct spawn_data));
  487. data.num = 1;
  488. m=map_mapname2mapid(mapname);
  489. if(m<0)
  490. {
  491. ShowWarning("mob_spawn_guardian: Map [%s] not found.\n", mapname);
  492. return 0;
  493. }
  494. data.m = m;
  495. data.num = 1;
  496. if(class_<=0) {
  497. class_ = mob_get_random_id(-class_-1, 1, 99);
  498. if (!class_) return 0;
  499. }
  500. data.class_ = class_;
  501. if( !has_index )
  502. {
  503. guardian = -1;
  504. }
  505. else if( guardian < 0 || guardian >= MAX_GUARDIANS )
  506. {
  507. ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d (castle map %s)\n", guardian, class_, map[m].name);
  508. return 0;
  509. }
  510. if((x<=0 || y<=0) && !map_search_freecell(NULL, m, &x, &y, -1,-1, 0))
  511. {
  512. ShowWarning("mob_spawn_guardian: Couldn't locate a spawn cell for guardian class %d (index %d) at castle map %s\n",class_, guardian, map[m].name);
  513. return 0;
  514. }
  515. data.x = x;
  516. data.y = y;
  517. safestrncpy(data.name, mobname, sizeof(data.name));
  518. safestrncpy(data.eventname, event, sizeof(data.eventname));
  519. if (!mob_parse_dataset(&data))
  520. return 0;
  521. gc=guild_mapname2gc(map[m].name);
  522. if (gc == NULL)
  523. {
  524. ShowError("mob_spawn_guardian: No castle set at map %s\n", map[m].name);
  525. return 0;
  526. }
  527. if (!gc->guild_id)
  528. ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle with no guild (castle map %s)\n", class_, map[m].name);
  529. else
  530. g = guild_search(gc->guild_id);
  531. if( has_index && gc->guardian[guardian].id )
  532. { //Check if guardian already exists, refuse to spawn if so.
  533. struct mob_data *md2 = (TBL_MOB*)map_id2bl(gc->guardian[guardian].id);
  534. if (md2 && md2->bl.type == BL_MOB &&
  535. md2->guardian_data && md2->guardian_data->number == guardian)
  536. {
  537. ShowError("mob_spawn_guardian: Attempted to spawn guardian in position %d which already has a guardian (castle map %s)\n", guardian, map[m].name);
  538. return 0;
  539. }
  540. }
  541. md = mob_spawn_dataset(&data);
  542. md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
  543. md->guardian_data->number = guardian;
  544. md->guardian_data->guild_id = gc->guild_id;
  545. md->guardian_data->castle = gc;
  546. if( has_index )
  547. {// permanent guardian
  548. gc->guardian[guardian].id = md->bl.id;
  549. }
  550. else
  551. {// temporary guardian
  552. int i;
  553. ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == 0);
  554. if( i == gc->temp_guardians_max )
  555. {
  556. ++(gc->temp_guardians_max);
  557. RECREATE(gc->temp_guardians, int, gc->temp_guardians_max);
  558. }
  559. gc->temp_guardians[i] = md->bl.id;
  560. }
  561. if (g)
  562. {
  563. md->guardian_data->emblem_id = g->emblem_id;
  564. memcpy (md->guardian_data->guild_name, g->name, NAME_LENGTH);
  565. md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
  566. } else if (md->guardian_data->guild_id)
  567. add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
  568. mob_spawn(md);
  569. return md->bl.id;
  570. }
  571. /*==========================================
  572. * Reachability to a Specification ID existence place
  573. * state indicates type of 'seek' mob should do:
  574. * - MSS_LOOT: Looking for item, path must be easy.
  575. * - MSS_RUSH: Chasing attacking player, path is complex
  576. * - MSS_FOLLOW: Initiative/support seek, path is complex
  577. *------------------------------------------*/
  578. int mob_can_reach(struct mob_data *md,struct block_list *bl,int range, int state)
  579. {
  580. int easy = 0;
  581. nullpo_retr(0, md);
  582. nullpo_retr(0, bl);
  583. switch (state) {
  584. case MSS_RUSH:
  585. case MSS_FOLLOW:
  586. easy = 0; //(battle_config.mob_ai&0x1?0:1);
  587. break;
  588. case MSS_LOOT:
  589. default:
  590. easy = 1;
  591. break;
  592. }
  593. return unit_can_reach_bl(&md->bl, bl, range, easy, NULL, NULL);
  594. }
  595. /*==========================================
  596. * Links nearby mobs (supportive mobs)
  597. *------------------------------------------*/
  598. int mob_linksearch(struct block_list *bl,va_list ap)
  599. {
  600. struct mob_data *md;
  601. int class_;
  602. struct block_list *target;
  603. unsigned int tick;
  604. nullpo_retr(0, bl);
  605. nullpo_retr(0, ap);
  606. md=(struct mob_data *)bl;
  607. class_ = va_arg(ap, int);
  608. target = va_arg(ap, struct block_list *);
  609. tick=va_arg(ap, unsigned int);
  610. if (md->class_ == class_ && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME
  611. && !md->target_id)
  612. {
  613. md->last_linktime = tick;
  614. if( mob_can_reach(md,target,md->db->range2, MSS_FOLLOW) ){ // Reachability judging
  615. md->target_id = target->id;
  616. md->min_chase=md->db->range3;
  617. return 1;
  618. }
  619. }
  620. return 0;
  621. }
  622. /*==========================================
  623. * mob spawn with delay (timer function)
  624. *------------------------------------------*/
  625. static int mob_delayspawn(int tid, unsigned int tick, int id, intptr data)
  626. {
  627. struct block_list *bl = map_id2bl(id);
  628. if (bl && bl->type == BL_MOB && bl->prev == NULL)
  629. mob_spawn((TBL_MOB*)bl);
  630. return 0;
  631. }
  632. /*==========================================
  633. * spawn timing calculation
  634. *------------------------------------------*/
  635. int mob_setdelayspawn(struct mob_data *md)
  636. {
  637. unsigned int spawntime;
  638. if (!md->spawn) //Doesn't has respawn data!
  639. return unit_free(&md->bl,1);
  640. spawntime = md->spawn->delay1; //Base respawn time
  641. if (md->spawn->delay2) //random variance
  642. spawntime+= rand()%md->spawn->delay2;
  643. if (spawntime < 5000) //Min respawn time (is it needed?)
  644. spawntime = 5000;
  645. md->spawn_timer = add_timer(gettick()+spawntime, mob_delayspawn, md->bl.id, 0);
  646. return 0;
  647. }
  648. static int mob_count_sub(struct block_list *bl,va_list ap)
  649. {
  650. return 1;
  651. }
  652. /*==========================================
  653. * Mob spawning. Initialization is also variously here.
  654. *------------------------------------------*/
  655. int mob_spawn (struct mob_data *md)
  656. {
  657. int i=0;
  658. unsigned int c =0, tick = gettick();
  659. md->last_thinktime = tick;
  660. if (md->bl.prev != NULL)
  661. unit_remove_map(&md->bl,2);
  662. else
  663. if (md->spawn && md->class_ != md->spawn->class_)
  664. {
  665. md->class_ = md->spawn->class_;
  666. status_set_viewdata(&md->bl, md->class_);
  667. md->db = mob_db(md->class_);
  668. memcpy(md->name,md->spawn->name,NAME_LENGTH);
  669. }
  670. if (md->spawn) { //Respawn data
  671. md->bl.m = md->spawn->m;
  672. md->bl.x = md->spawn->x;
  673. md->bl.y = md->spawn->y;
  674. if ((md->bl.x == 0 && md->bl.y == 0) || md->spawn->xs || md->spawn->ys)
  675. { //Monster can be spawned on an area.
  676. if (!map_search_freecell(&md->bl, -1,
  677. &md->bl.x, &md->bl.y, md->spawn->xs, md->spawn->ys,
  678. battle_config.no_spawn_on_player?4:0)) {
  679. // retry again later
  680. add_timer(tick+5000,mob_delayspawn,md->bl.id,0);
  681. return 1;
  682. }
  683. } else if (battle_config.no_spawn_on_player>99 &&
  684. map_foreachinrange(mob_count_sub, &md->bl, AREA_SIZE, BL_PC))
  685. { //retry again later (players on sight)
  686. add_timer(tick+5000,mob_delayspawn,md->bl.id,0);
  687. return 1;
  688. }
  689. }
  690. memset(&md->state, 0, sizeof(md->state));
  691. status_calc_mob(md, 1);
  692. md->attacked_id = 0;
  693. md->target_id = 0;
  694. md->move_fail_count = 0;
  695. md->spawn_timer = INVALID_TIMER;
  696. // md->master_id = 0;
  697. md->master_dist = 0;
  698. md->state.aggressive = md->status.mode&MD_ANGRY?1:0;
  699. md->state.skillstate = MSS_IDLE;
  700. md->next_walktime = tick+rand()%5000+1000;
  701. md->last_linktime = tick;
  702. md->last_pcneartime = 0;
  703. for (i = 0, c = tick-1000*3600*10; i < MAX_MOBSKILL; i++)
  704. md->skilldelay[i] = c;
  705. memset(md->dmglog, 0, sizeof(md->dmglog));
  706. md->tdmg = 0;
  707. if (md->lootitem)
  708. memset(md->lootitem, 0, sizeof(md->lootitem));
  709. md->lootitem_count = 0;
  710. if(md->db->option)
  711. // Added for carts, falcons and pecos for cloned monsters. [Valaris]
  712. md->sc.option = md->db->option;
  713. map_addblock(&md->bl);
  714. clif_spawn(&md->bl);
  715. skill_unit_move(&md->bl,tick,1);
  716. mobskill_use(md, tick, MSC_SPAWN);
  717. return 0;
  718. }
  719. /*==========================================
  720. * Determines if the mob can change target. [Skotlex]
  721. *------------------------------------------*/
  722. static int mob_can_changetarget(struct mob_data* md, struct block_list* target, int mode)
  723. {
  724. // if the monster was provoked ignore the above rule [celest]
  725. if(md->state.provoke_flag)
  726. {
  727. if (md->state.provoke_flag == target->id)
  728. return 1;
  729. else if (!(battle_config.mob_ai&0x4))
  730. return 0;
  731. }
  732. switch (md->state.skillstate) {
  733. case MSS_BERSERK:
  734. if (!(mode&MD_CHANGETARGET_MELEE))
  735. return 0;
  736. return (battle_config.mob_ai&0x4 || check_distance_bl(&md->bl, target, 3));
  737. case MSS_RUSH:
  738. return (mode&MD_CHANGETARGET_CHASE);
  739. case MSS_FOLLOW:
  740. case MSS_ANGRY:
  741. case MSS_IDLE:
  742. case MSS_WALK:
  743. case MSS_LOOT:
  744. return 1;
  745. default:
  746. return 0;
  747. }
  748. }
  749. /*==========================================
  750. * Determination for an attack of a monster
  751. *------------------------------------------*/
  752. int mob_target(struct mob_data *md,struct block_list *bl,int dist)
  753. {
  754. nullpo_retr(0, md);
  755. nullpo_retr(0, bl);
  756. // Nothing will be carried out if there is no mind of changing TAGE by TAGE ending.
  757. if(md->target_id && !mob_can_changetarget(md, bl, status_get_mode(&md->bl)))
  758. return 0;
  759. if(!status_check_skilluse(&md->bl, bl, 0, 0))
  760. return 0;
  761. md->target_id = bl->id; // Since there was no disturbance, it locks on to target.
  762. if (md->state.provoke_flag && bl->id != md->state.provoke_flag)
  763. md->state.provoke_flag = 0;
  764. md->min_chase=dist+md->db->range3;
  765. if(md->min_chase>MAX_MINCHASE)
  766. md->min_chase=MAX_MINCHASE;
  767. return 0;
  768. }
  769. /*==========================================
  770. * The ?? routine of an active monster
  771. *------------------------------------------*/
  772. static int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap)
  773. {
  774. struct mob_data *md;
  775. struct block_list **target;
  776. int dist;
  777. nullpo_retr(0, bl);
  778. nullpo_retr(0, ap);
  779. md=va_arg(ap,struct mob_data *);
  780. target= va_arg(ap,struct block_list**);
  781. //If can't seek yet, not an enemy, or you can't attack it, skip.
  782. if ((*target) == bl || !status_check_skilluse(&md->bl, bl, 0, 0))
  783. return 0;
  784. if(battle_check_target(&md->bl,bl,BCT_ENEMY)<=0)
  785. return 0;
  786. switch (bl->type)
  787. {
  788. case BL_PC:
  789. if (((TBL_PC*)bl)->state.gangsterparadise &&
  790. !(status_get_mode(&md->bl)&MD_BOSS))
  791. return 0; //Gangster paradise protection.
  792. default:
  793. if (battle_config.hom_setting&0x4 &&
  794. (*target) && (*target)->type == BL_HOM && bl->type != BL_HOM)
  795. return 0; //For some reason Homun targets are never overriden.
  796. dist = distance_bl(&md->bl, bl);
  797. if(
  798. ((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) &&
  799. battle_check_range(&md->bl,bl,md->db->range2)
  800. ) { //Pick closest target?
  801. (*target) = bl;
  802. md->target_id=bl->id;
  803. md->min_chase= dist + md->db->range3;
  804. if(md->min_chase>MAX_MINCHASE)
  805. md->min_chase=MAX_MINCHASE;
  806. return 1;
  807. }
  808. break;
  809. }
  810. return 0;
  811. }
  812. /*==========================================
  813. * chase target-change routine.
  814. *------------------------------------------*/
  815. static int mob_ai_sub_hard_changechase(struct block_list *bl,va_list ap)
  816. {
  817. struct mob_data *md;
  818. struct block_list **target;
  819. nullpo_retr(0, bl);
  820. nullpo_retr(0, ap);
  821. md=va_arg(ap,struct mob_data *);
  822. target= va_arg(ap,struct block_list**);
  823. //If can't seek yet, not an enemy, or you can't attack it, skip.
  824. if ((*target) == bl ||
  825. battle_check_target(&md->bl,bl,BCT_ENEMY)<=0 ||
  826. !status_check_skilluse(&md->bl, bl, 0, 0))
  827. return 0;
  828. if(battle_check_range (&md->bl, bl, md->status.rhw.range))
  829. {
  830. (*target) = bl;
  831. md->target_id=bl->id;
  832. md->min_chase= md->db->range3;
  833. }
  834. return 1;
  835. }
  836. /*==========================================
  837. * loot monster item search
  838. *------------------------------------------*/
  839. static int mob_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  840. {
  841. struct mob_data* md;
  842. struct block_list **target;
  843. int dist;
  844. md=va_arg(ap,struct mob_data *);
  845. target= va_arg(ap,struct block_list**);
  846. dist=distance_bl(&md->bl, bl);
  847. if(mob_can_reach(md,bl,dist+1, MSS_LOOT) &&
  848. ((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) //New target closer than previous one.
  849. ) {
  850. (*target) = bl;
  851. md->target_id=bl->id;
  852. md->min_chase=md->db->range3;
  853. }
  854. return 0;
  855. }
  856. static int mob_warpchase_sub(struct block_list *bl,va_list ap)
  857. {
  858. struct mob_data* md;
  859. struct block_list *target;
  860. struct npc_data **target_nd;
  861. struct npc_data *nd;
  862. int *min_distance;
  863. int cur_distance;
  864. md=va_arg(ap,struct mob_data *);
  865. target= va_arg(ap, struct block_list*);
  866. target_nd= va_arg(ap, struct npc_data**);
  867. min_distance= va_arg(ap, int*);
  868. nd = (TBL_NPC*) bl;
  869. if(nd->subtype != WARP)
  870. return 0; //Not a warp
  871. if(nd->u.warp.mapindex != map[target->m].index)
  872. return 0; //Does not lead to the same map.
  873. cur_distance = distance_blxy(target, nd->u.warp.x, nd->u.warp.y);
  874. if (cur_distance < *min_distance)
  875. { //Pick warp that leads closest to target.
  876. *target_nd = nd;
  877. *min_distance = cur_distance;
  878. return 1;
  879. }
  880. return 0;
  881. }
  882. /*==========================================
  883. * Processing of slave monsters
  884. *------------------------------------------*/
  885. static int mob_ai_sub_hard_slavemob(struct mob_data *md,unsigned int tick)
  886. {
  887. struct block_list *bl;
  888. int old_dist;
  889. bl=map_id2bl(md->master_id);
  890. if (!bl || status_isdead(bl)) {
  891. status_kill(&md->bl);
  892. return 1;
  893. }
  894. if (bl->prev == NULL)
  895. return 0; //Master not on a map? Could be warping, do not process.
  896. if(status_get_mode(&md->bl)&MD_CANMOVE)
  897. { //If the mob can move, follow around. [Check by Skotlex]
  898. // Distance with between slave and master is measured.
  899. old_dist=md->master_dist;
  900. md->master_dist=distance_bl(&md->bl, bl);
  901. // Since the master was in near immediately before, teleport is carried out and it pursues.
  902. if(bl->m != md->bl.m ||
  903. (old_dist<10 && md->master_dist>18) ||
  904. md->master_dist > MAX_MINCHASE
  905. ){
  906. md->master_dist = 0;
  907. unit_warp(&md->bl,bl->m,bl->x,bl->y,3);
  908. return 1;
  909. }
  910. if(md->target_id) //Slave is busy with a target.
  911. return 0;
  912. // Approach master if within view range, chase back to Master's area also if standing on top of the master.
  913. if((md->master_dist>MOB_SLAVEDISTANCE || md->master_dist == 0) &&
  914. unit_can_move(&md->bl))
  915. {
  916. short x = bl->x, y = bl->y;
  917. mob_stop_attack(md);
  918. if(map_search_freecell(&md->bl, bl->m, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 1)
  919. && unit_walktoxy(&md->bl, x, y, 0))
  920. return 1;
  921. }
  922. } else if (bl->m != md->bl.m && map_flag_gvg(md->bl.m)) {
  923. //Delete the summoned mob if it's in a gvg ground and the master is elsewhere. [Skotlex]
  924. status_kill(&md->bl);
  925. return 1;
  926. }
  927. //Avoid attempting to lock the master's target too often to avoid unnecessary overload. [Skotlex]
  928. if (DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME && !md->target_id)
  929. {
  930. struct unit_data *ud = unit_bl2ud(bl);
  931. md->last_linktime = tick;
  932. if (ud) {
  933. struct block_list *tbl=NULL;
  934. if (ud->target && ud->state.attack_continue)
  935. tbl=map_id2bl(ud->target);
  936. else if (ud->skilltarget) {
  937. tbl = map_id2bl(ud->skilltarget);
  938. //Required check as skilltarget is not always an enemy. [Skotlex]
  939. if (tbl && battle_check_target(&md->bl, tbl, BCT_ENEMY) <= 0)
  940. tbl = NULL;
  941. }
  942. if (tbl && status_check_skilluse(&md->bl, tbl, 0, 0)) {
  943. md->target_id=tbl->id;
  944. md->min_chase=md->db->range3+distance_bl(&md->bl, tbl);
  945. if(md->min_chase>MAX_MINCHASE)
  946. md->min_chase=MAX_MINCHASE;
  947. return 1;
  948. }
  949. }
  950. }
  951. return 0;
  952. }
  953. /*==========================================
  954. * A lock of target is stopped and mob moves to a standby state.
  955. * This also triggers idle skill/movement since the AI can get stuck
  956. * when trying to pick new targets when the current chosen target is
  957. * unreachable.
  958. *------------------------------------------*/
  959. int mob_unlocktarget(struct mob_data *md, unsigned int tick)
  960. {
  961. nullpo_retr(0, md);
  962. switch (md->state.skillstate) {
  963. case MSS_WALK:
  964. if (md->ud.walktimer != -1)
  965. break;
  966. //Because it is not unset when the mob finishes walking.
  967. md->state.skillstate = MSS_IDLE;
  968. case MSS_IDLE:
  969. // Idle skill.
  970. if ((md->target_id || !(++md->ud.walk_count%IDLE_SKILL_INTERVAL)) &&
  971. mobskill_use(md, tick, -1))
  972. break;
  973. //Random walk.
  974. if (!md->master_id &&
  975. DIFF_TICK(md->next_walktime, tick) <= 0 &&
  976. !mob_randomwalk(md,tick))
  977. //Delay next random walk when this one failed.
  978. md->next_walktime=tick+rand()%3000;
  979. break;
  980. default:
  981. mob_stop_attack(md);
  982. if (battle_config.mob_ai&0x8)
  983. mob_stop_walking(md,1); //Immediately stop chasing.
  984. md->state.skillstate = MSS_IDLE;
  985. md->next_walktime=tick+rand()%3000+3000;
  986. break;
  987. }
  988. if (md->target_id) {
  989. md->target_id=0;
  990. md->ud.target = 0;
  991. }
  992. return 0;
  993. }
  994. /*==========================================
  995. * Random walk
  996. *------------------------------------------*/
  997. int mob_randomwalk(struct mob_data *md,unsigned int tick)
  998. {
  999. const int retrycount=20;
  1000. int i,x,y,c,d;
  1001. int speed;
  1002. nullpo_retr(0, md);
  1003. if(DIFF_TICK(md->next_walktime,tick)>0 ||
  1004. !unit_can_move(&md->bl) ||
  1005. !(status_get_mode(&md->bl)&MD_CANMOVE))
  1006. return 0;
  1007. d =12-md->move_fail_count;
  1008. if(d<5) d=5;
  1009. for(i=0;i<retrycount;i++){ // Search of a movable place
  1010. int r=rand();
  1011. x=r%(d*2+1)-d;
  1012. y=r/(d*2+1)%(d*2+1)-d;
  1013. x+=md->bl.x;
  1014. y+=md->bl.y;
  1015. if((map_getcell(md->bl.m,x,y,CELL_CHKPASS)) && unit_walktoxy(&md->bl,x,y,1)){
  1016. break;
  1017. }
  1018. }
  1019. if(i==retrycount){
  1020. md->move_fail_count++;
  1021. if(md->move_fail_count>1000){
  1022. ShowWarning("MOB can't move. random spawn %d, class = %d, at %s (%d,%d)\n",md->bl.id,md->class_,map[md->bl.m].name, md->bl.x, md->bl.y);
  1023. md->move_fail_count=0;
  1024. mob_spawn(md);
  1025. }
  1026. return 0;
  1027. }
  1028. speed=status_get_speed(&md->bl);
  1029. for(i=c=0;i<md->ud.walkpath.path_len;i++){ // The next walk start time is calculated.
  1030. if(md->ud.walkpath.path[i]&1)
  1031. c+=speed*14/10;
  1032. else
  1033. c+=speed;
  1034. }
  1035. md->state.skillstate=MSS_WALK;
  1036. md->move_fail_count=0;
  1037. md->next_walktime = tick+rand()%3000+3000+c;
  1038. return 1;
  1039. }
  1040. int mob_warpchase(struct mob_data *md, struct block_list *target)
  1041. {
  1042. struct npc_data *warp = NULL;
  1043. int distance = AREA_SIZE;
  1044. if (!(target && battle_config.mob_ai&0x40 && battle_config.mob_warp&1))
  1045. return 0; //Can't warp chase.
  1046. if (target->m == md->bl.m && check_distance_bl(&md->bl, target, AREA_SIZE))
  1047. return 0; //No need to do a warp chase.
  1048. if (md->ud.walktimer != -1 &&
  1049. map_getcell(md->bl.m,md->ud.to_x,md->ud.to_y,CELL_CHKNPC))
  1050. return 1; //Already walking to a warp.
  1051. //Search for warps within mob's viewing range.
  1052. map_foreachinrange (mob_warpchase_sub, &md->bl,
  1053. md->db->range2, BL_NPC, md, target, &warp, &distance);
  1054. if (warp && unit_walktobl(&md->bl, &warp->bl, 0, 1))
  1055. return 1;
  1056. return 0;
  1057. }
  1058. /*==========================================
  1059. * AI of MOB whose is near a Player
  1060. *------------------------------------------*/
  1061. static bool mob_ai_sub_hard(struct mob_data *md, unsigned int tick)
  1062. {
  1063. struct block_list *tbl = NULL, *abl = NULL;
  1064. int dist;
  1065. int mode;
  1066. int search_size;
  1067. int view_range, can_move;
  1068. if(md->bl.prev == NULL || md->status.hp <= 0)
  1069. return false;
  1070. if (DIFF_TICK(tick, md->last_thinktime) < MIN_MOBTHINKTIME)
  1071. return false;
  1072. md->last_thinktime = tick;
  1073. if (md->ud.skilltimer != -1)
  1074. return false;
  1075. if(md->ud.walktimer != -1 && md->ud.walkpath.path_pos <= 3)
  1076. return false;
  1077. // Abnormalities
  1078. if((md->sc.opt1 > 0 && md->sc.opt1 != OPT1_STONEWAIT) || md->sc.data[SC_BLADESTOP])
  1079. { //Should reset targets.
  1080. md->target_id = md->attacked_id = 0;
  1081. return false;
  1082. }
  1083. if (md->sc.count && md->sc.data[SC_BLIND])
  1084. view_range = 3;
  1085. else
  1086. view_range = md->db->range2;
  1087. mode = status_get_mode(&md->bl);
  1088. can_move = (mode&MD_CANMOVE)&&unit_can_move(&md->bl);
  1089. if (md->target_id)
  1090. { //Check validity of current target. [Skotlex]
  1091. tbl = map_id2bl(md->target_id);
  1092. if (!tbl || tbl->m != md->bl.m ||
  1093. (md->ud.attacktimer == -1 && !status_check_skilluse(&md->bl, tbl, 0, 0)) ||
  1094. (md->ud.walktimer != -1 && !(battle_config.mob_ai&0x1) && !check_distance_bl(&md->bl, tbl, md->min_chase)) ||
  1095. (
  1096. tbl->type == BL_PC &&
  1097. ((((TBL_PC*)tbl)->state.gangsterparadise && !(mode&MD_BOSS)) ||
  1098. ((TBL_PC*)tbl)->invincible_timer != INVALID_TIMER)
  1099. )) { //Unlock current target.
  1100. if (mob_warpchase(md, tbl))
  1101. return true; //Chasing this target.
  1102. mob_unlocktarget(md, tick-(battle_config.mob_ai&0x8?3000:0)); //Imediately do random walk.
  1103. tbl = NULL;
  1104. }
  1105. }
  1106. // Check for target change.
  1107. if( md->attacked_id && mode&MD_CANATTACK )
  1108. {
  1109. if( md->attacked_id == md->target_id )
  1110. { //Rude attacked check.
  1111. if( !battle_check_range(&md->bl, tbl, md->status.rhw.range)
  1112. && ( //Can't attack back and can't reach back.
  1113. (!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 && (battle_config.mob_ai&0x2 || md->sc.data[SC_SPIDERWEB]))
  1114. || !mob_can_reach(md, tbl, md->min_chase, MSS_RUSH)
  1115. )
  1116. && md->state.attacked_count++ >= RUDE_ATTACKED_COUNT
  1117. && !mobskill_use(md, tick, MSC_RUDEATTACKED) //If can't rude Attack
  1118. && can_move && unit_escape(&md->bl, tbl, rand()%10 +1)) //Attempt escape
  1119. { //Escaped
  1120. md->attacked_id = 0;
  1121. return true;
  1122. }
  1123. }
  1124. else
  1125. if( (abl= map_id2bl(md->attacked_id)) && (!tbl || mob_can_changetarget(md, abl, mode)) )
  1126. {
  1127. if (md->bl.m != abl->m || abl->prev == NULL
  1128. || (dist = distance_bl(&md->bl, abl)) >= MAX_MINCHASE
  1129. || battle_check_target(&md->bl, abl, BCT_ENEMY) <= 0
  1130. || (battle_config.mob_ai&0x2 && !status_check_skilluse(&md->bl, abl, 0, 0)) //Retaliate check
  1131. || (!battle_check_range(&md->bl, abl, md->status.rhw.range)
  1132. &&
  1133. ( //Reach check
  1134. (!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 && (battle_config.mob_ai&0x2 || md->sc.data[SC_SPIDERWEB]))
  1135. || !mob_can_reach(md, abl, dist+md->db->range3, MSS_RUSH)
  1136. )
  1137. )
  1138. )
  1139. { //Rude attacked
  1140. if (md->state.attacked_count++ >= RUDE_ATTACKED_COUNT
  1141. && !mobskill_use(md, tick, MSC_RUDEATTACKED) && can_move
  1142. && !tbl && unit_escape(&md->bl, abl, rand()%10 +1))
  1143. { //Escaped.
  1144. //TODO: Maybe it shouldn't attempt to run if it has another, valid target?
  1145. md->attacked_id = 0;
  1146. return true;
  1147. }
  1148. }
  1149. else
  1150. if (!(battle_config.mob_ai&0x2) && !status_check_skilluse(&md->bl, abl, 0, 0))
  1151. {
  1152. //Can't attack back, but didn't invoke a rude attacked skill...
  1153. }
  1154. else
  1155. { //Attackable
  1156. if (!tbl || dist < md->status.rhw.range || !check_distance_bl(&md->bl, tbl, dist)
  1157. || battle_gettarget(tbl) != md->bl.id)
  1158. { //Change if the new target is closer than the actual one
  1159. //or if the previous target is not attacking the mob. [Skotlex]
  1160. md->target_id = md->attacked_id; // set target
  1161. if (md->state.attacked_count)
  1162. md->state.attacked_count--; //Should we reset rude attack count?
  1163. md->min_chase = dist+md->db->range3;
  1164. if(md->min_chase>MAX_MINCHASE)
  1165. md->min_chase=MAX_MINCHASE;
  1166. tbl = abl; //Set the new target
  1167. }
  1168. }
  1169. }
  1170. //Clear it since it's been checked for already.
  1171. md->attacked_id = 0;
  1172. }
  1173. // Processing of slave monster
  1174. if (md->master_id > 0 && mob_ai_sub_hard_slavemob(md, tick))
  1175. return true;
  1176. // Scan area for targets
  1177. if (!tbl && mode&MD_LOOTER && md->lootitem && DIFF_TICK(tick, md->ud.canact_tick) > 0 &&
  1178. (md->lootitem_count < LOOTITEM_SIZE || battle_config.monster_loot_type != 1))
  1179. { // Scan area for items to loot, avoid trying to loot of the mob is full and can't consume the items.
  1180. map_foreachinrange (mob_ai_sub_hard_lootsearch, &md->bl, view_range, BL_ITEM, md, &tbl);
  1181. }
  1182. if ((!tbl && mode&MD_AGGRESSIVE) || md->state.skillstate == MSS_FOLLOW)
  1183. {
  1184. map_foreachinrange (mob_ai_sub_hard_activesearch, &md->bl, view_range, DEFAULT_ENEMY_TYPE(md), md, &tbl);
  1185. }
  1186. else
  1187. if (mode&MD_CHANGECHASE && (md->state.skillstate == MSS_RUSH || md->state.skillstate == MSS_FOLLOW))
  1188. {
  1189. search_size = view_range<md->status.rhw.range ? view_range:md->status.rhw.range;
  1190. map_foreachinrange (mob_ai_sub_hard_changechase, &md->bl, search_size, DEFAULT_ENEMY_TYPE(md), md, &tbl);
  1191. }
  1192. if (!tbl) { //No targets available.
  1193. if (mode&MD_ANGRY && !md->state.aggressive)
  1194. md->state.aggressive = 1; //Restore angry state when no targets are available.
  1195. //This handles triggering idle walk/skill.
  1196. mob_unlocktarget(md, tick);
  1197. return true;
  1198. }
  1199. //Target exists, attack or loot as applicable.
  1200. if (tbl->type == BL_ITEM)
  1201. { //Loot time.
  1202. struct flooritem_data *fitem;
  1203. if (md->ud.target == tbl->id && md->ud.walktimer != -1)
  1204. return true; //Already locked.
  1205. if (md->lootitem == NULL)
  1206. { //Can't loot...
  1207. mob_unlocktarget (md, tick);
  1208. return true;
  1209. }
  1210. if (!check_distance_bl(&md->bl, tbl, 1))
  1211. { //Still not within loot range.
  1212. if (!(mode&MD_CANMOVE))
  1213. { //A looter that can't move? Real smart.
  1214. mob_unlocktarget(md,tick);
  1215. return true;
  1216. }
  1217. if (!can_move) //Stuck. Wait before walking.
  1218. return true;
  1219. md->state.skillstate = MSS_LOOT;
  1220. if (!unit_walktobl(&md->bl, tbl, 0, 1))
  1221. mob_unlocktarget(md, tick); //Can't loot...
  1222. return true;
  1223. }
  1224. //Within looting range.
  1225. if (md->ud.attacktimer != -1)
  1226. return true; //Busy attacking?
  1227. fitem = (struct flooritem_data *)tbl;
  1228. if(log_config.enable_logs&0x10) //Logs items, taken by (L)ooter Mobs [Lupus]
  1229. log_pick_mob(md, "L", fitem->item_data.nameid, fitem->item_data.amount, &fitem->item_data);
  1230. if (md->lootitem_count < LOOTITEM_SIZE) {
  1231. memcpy (&md->lootitem[md->lootitem_count++], &fitem->item_data, sizeof(md->lootitem[0]));
  1232. } else { //Destroy first looted item...
  1233. if (md->lootitem[0].card[0] == CARD0_PET)
  1234. intif_delete_petdata( MakeDWord(md->lootitem[0].card[1],md->lootitem[0].card[2]) );
  1235. memmove(&md->lootitem[0], &md->lootitem[1], (LOOTITEM_SIZE-1)*sizeof(md->lootitem[0]));
  1236. memcpy (&md->lootitem[LOOTITEM_SIZE-1], &fitem->item_data, sizeof(md->lootitem[0]));
  1237. }
  1238. if (pcdb_checkid(md->vd->class_))
  1239. { //Give them walk act/delay to properly mimic players. [Skotlex]
  1240. clif_takeitem(&md->bl,tbl);
  1241. md->ud.canact_tick = tick + md->status.amotion;
  1242. unit_set_walkdelay(&md->bl, tick, md->status.amotion, 1);
  1243. }
  1244. //Clear item.
  1245. map_clearflooritem (tbl->id);
  1246. mob_unlocktarget (md,tick);
  1247. return true;
  1248. }
  1249. //Attempt to attack.
  1250. //At this point we know the target is attackable, we just gotta check if the range matches.
  1251. if (md->ud.target == tbl->id && md->ud.attacktimer != -1) //Already locked.
  1252. return true;
  1253. if (battle_check_range (&md->bl, tbl, md->status.rhw.range))
  1254. { //Target within range, engage
  1255. unit_attack(&md->bl,tbl->id,1);
  1256. return true;
  1257. }
  1258. //Out of range...
  1259. if (!(mode&MD_CANMOVE))
  1260. { //Can't chase. Attempt an idle skill before unlocking.
  1261. md->state.skillstate = MSS_IDLE;
  1262. if (!mobskill_use(md, tick, -1))
  1263. mob_unlocktarget(md,tick);
  1264. return true;
  1265. }
  1266. if (!can_move)
  1267. { //Stuck. Attempt an idle skill
  1268. md->state.skillstate = MSS_IDLE;
  1269. if (!(++md->ud.walk_count%IDLE_SKILL_INTERVAL))
  1270. mobskill_use(md, tick, -1);
  1271. return true;
  1272. }
  1273. if (md->ud.walktimer != -1 && md->ud.target == tbl->id &&
  1274. (
  1275. !(battle_config.mob_ai&0x1) ||
  1276. check_distance_blxy(tbl, md->ud.to_x, md->ud.to_y, md->status.rhw.range)
  1277. )) //Current target tile is still within attack range.
  1278. return true;
  1279. //Follow up if possible.
  1280. if(!mob_can_reach(md, tbl, md->min_chase, MSS_RUSH) ||
  1281. !unit_walktobl(&md->bl, tbl, md->status.rhw.range, 2))
  1282. mob_unlocktarget(md,tick);
  1283. return true;
  1284. }
  1285. static int mob_ai_sub_hard_timer(struct block_list *bl,va_list ap)
  1286. {
  1287. struct mob_data *md = (struct mob_data*)bl;
  1288. unsigned int tick = va_arg(ap, unsigned int);
  1289. if (mob_ai_sub_hard(md, tick))
  1290. { //Hard AI triggered.
  1291. if(!md->state.spotted)
  1292. md->state.spotted = 1;
  1293. md->last_pcneartime = tick;
  1294. }
  1295. return 0;
  1296. }
  1297. /*==========================================
  1298. * Serious processing for mob in PC field of view (foreachclient)
  1299. *------------------------------------------*/
  1300. static int mob_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
  1301. {
  1302. unsigned int tick;
  1303. tick=va_arg(ap,unsigned int);
  1304. map_foreachinrange(mob_ai_sub_hard_timer,&sd->bl, AREA_SIZE+ACTIVE_AI_RANGE, BL_MOB,tick);
  1305. return 0;
  1306. }
  1307. /*==========================================
  1308. * Negligent mode MOB AI (PC is not in near)
  1309. *------------------------------------------*/
  1310. static int mob_ai_sub_lazy(struct mob_data *md, va_list args)
  1311. {
  1312. unsigned int tick;
  1313. int mode;
  1314. nullpo_retr(0, md);
  1315. if(md->bl.prev == NULL)
  1316. return 0;
  1317. tick = va_arg(args,unsigned int);
  1318. if (battle_config.mob_ai&0x20 && map[md->bl.m].users>0)
  1319. return (int)mob_ai_sub_hard(md, tick);
  1320. if (md->bl.prev==NULL || md->status.hp == 0)
  1321. return 1;
  1322. if(battle_config.mob_active_time &&
  1323. md->last_pcneartime &&
  1324. !(md->status.mode&MD_BOSS) &&
  1325. DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME)
  1326. {
  1327. if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.mob_active_time)
  1328. return (int)mob_ai_sub_hard(md, tick);
  1329. md->last_pcneartime = 0;
  1330. }
  1331. if(battle_config.boss_active_time &&
  1332. md->last_pcneartime &&
  1333. (md->status.mode&MD_BOSS) &&
  1334. DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME)
  1335. {
  1336. if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.boss_active_time)
  1337. return (int)mob_ai_sub_hard(md, tick);
  1338. md->last_pcneartime = 0;
  1339. }
  1340. if(DIFF_TICK(tick,md->last_thinktime)< 10*MIN_MOBTHINKTIME)
  1341. return 0;
  1342. md->last_thinktime=tick;
  1343. if (md->master_id) {
  1344. mob_ai_sub_hard_slavemob (md,tick);
  1345. return 0;
  1346. }
  1347. mode = status_get_mode(&md->bl);
  1348. if(DIFF_TICK(md->next_walktime,tick)<0 &&
  1349. (mode&MD_CANMOVE) && unit_can_move(&md->bl) ){
  1350. if( map[md->bl.m].users>0 ){
  1351. // Since PC is in the same map, somewhat better negligent processing is carried out.
  1352. // It sometimes moves.
  1353. if(rand()%1000<MOB_LAZYMOVEPERC(md))
  1354. mob_randomwalk(md,tick);
  1355. else if(rand()%1000<MOB_LAZYSKILLPERC) //Chance to do a mob's idle skill.
  1356. mobskill_use(md, tick, -1);
  1357. // MOB which is not not the summons MOB but BOSS, either sometimes reboils.
  1358. // People don't want this, it seems custom, noone can prove it....
  1359. // else if( rand()%1000<MOB_LAZYWARPPERC
  1360. // && (md->spawn && !md->spawn->x && !md->spawn->y)
  1361. // && !md->target_id && !(mode&MD_BOSS))
  1362. // unit_warp(&md->bl,-1,-1,-1,0);
  1363. }else{
  1364. // Since PC is not even in the same map, suitable processing is carried out even if it takes.
  1365. // MOB which is not BOSS which is not Summons MOB, either -- a case -- sometimes -- leaping
  1366. if( rand()%1000<MOB_LAZYWARPPERC
  1367. && (md->spawn && !md->spawn->x && !md->spawn->y)
  1368. && !(mode&MD_BOSS))
  1369. unit_warp(&md->bl,-1,-1,-1,0);
  1370. }
  1371. md->next_walktime = tick+rand()%10000+5000;
  1372. }
  1373. return 0;
  1374. }
  1375. /*==========================================
  1376. * Negligent processing for mob outside PC field of view (interval timer function)
  1377. *------------------------------------------*/
  1378. static int mob_ai_lazy(int tid, unsigned int tick, int id, intptr data)
  1379. {
  1380. map_foreachmob(mob_ai_sub_lazy,tick);
  1381. return 0;
  1382. }
  1383. /*==========================================
  1384. * Serious processing for mob in PC field of view (interval timer function)
  1385. *------------------------------------------*/
  1386. static int mob_ai_hard(int tid, unsigned int tick, int id, intptr data)
  1387. {
  1388. if (battle_config.mob_ai&0x20)
  1389. map_foreachmob(mob_ai_sub_lazy,tick);
  1390. else
  1391. map_foreachpc(mob_ai_sub_foreachclient,tick);
  1392. return 0;
  1393. }
  1394. /*==========================================
  1395. * Initializes the delay drop structure for mob-dropped items.
  1396. *------------------------------------------*/
  1397. static struct item_drop* mob_setdropitem(int nameid, int qty)
  1398. {
  1399. struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
  1400. memset(&drop->item_data, 0, sizeof(struct item));
  1401. drop->item_data.nameid = nameid;
  1402. drop->item_data.amount = qty;
  1403. drop->item_data.identify = itemdb_isidentified(nameid);
  1404. drop->next = NULL;
  1405. return drop;
  1406. };
  1407. /*==========================================
  1408. * Initializes the delay drop structure for mob-looted items.
  1409. *------------------------------------------*/
  1410. static struct item_drop* mob_setlootitem(struct item* item)
  1411. {
  1412. struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
  1413. memcpy(&drop->item_data, item, sizeof(struct item));
  1414. drop->next = NULL;
  1415. return drop;
  1416. };
  1417. /*==========================================
  1418. * item drop with delay (timer function)
  1419. *------------------------------------------*/
  1420. static int mob_delay_item_drop(int tid, unsigned int tick, int id, intptr data)
  1421. {
  1422. struct item_drop_list *list;
  1423. struct item_drop *ditem, *ditem_prev;
  1424. list=(struct item_drop_list *)id;
  1425. ditem = list->item;
  1426. while (ditem) {
  1427. map_addflooritem(&ditem->item_data,ditem->item_data.amount,
  1428. list->m,list->x,list->y,
  1429. list->first_charid,list->second_charid,list->third_charid,0);
  1430. ditem_prev = ditem;
  1431. ditem = ditem->next;
  1432. ers_free(item_drop_ers, ditem_prev);
  1433. }
  1434. ers_free(item_drop_list_ers, list);
  1435. return 0;
  1436. }
  1437. /*==========================================
  1438. * Sets the item_drop into the item_drop_list.
  1439. * Also performs logging and autoloot if enabled.
  1440. * rate is the drop-rate of the item, required for autoloot.
  1441. * flag : Killed only by homunculus?
  1442. *------------------------------------------*/
  1443. static void mob_item_drop(struct mob_data *md, struct item_drop_list *dlist, struct item_drop *ditem, int loot, int drop_rate, unsigned short flag)
  1444. {
  1445. TBL_PC* sd;
  1446. if(log_config.enable_logs&0x10)
  1447. { //Logs items, dropped by mobs [Lupus]
  1448. if (loot)
  1449. log_pick_mob(md, "L", ditem->item_data.nameid, -ditem->item_data.amount, &ditem->item_data);
  1450. else
  1451. log_pick_mob(md, "M", ditem->item_data.nameid, -ditem->item_data.amount, NULL);
  1452. }
  1453. sd = map_charid2sd(dlist->first_charid);
  1454. if( sd == NULL ) sd = map_charid2sd(dlist->second_charid);
  1455. if( sd == NULL ) sd = map_charid2sd(dlist->third_charid);
  1456. if( sd
  1457. && (drop_rate <= sd->state.autoloot || ditem->item_data.nameid == sd->state.autolootid)
  1458. && (battle_config.idle_no_autoloot == 0 || DIFF_TICK(last_tick, sd->idletime) < battle_config.idle_no_autoloot)
  1459. && (battle_config.homunculus_autoloot?1:!flag)
  1460. #ifdef AUTOLOOT_DISTANCE
  1461. && check_distance_blxy(&sd->bl, dlist->x, dlist->y, AUTOLOOT_DISTANCE)
  1462. #endif
  1463. ) { //Autoloot.
  1464. if (party_share_loot(party_search(sd->status.party_id),
  1465. sd, &ditem->item_data, sd->status.char_id) == 0
  1466. ) {
  1467. ers_free(item_drop_ers, ditem);
  1468. return;
  1469. }
  1470. }
  1471. ditem->next = dlist->item;
  1472. dlist->item = ditem;
  1473. }
  1474. int mob_timer_delete(int tid, unsigned int tick, int id, intptr data)
  1475. {
  1476. struct block_list *bl=map_id2bl(id);
  1477. nullpo_retr(0, bl);
  1478. if (bl->type != BL_MOB)
  1479. return 0; //??
  1480. //for Alchemist CANNIBALIZE [Lupus]
  1481. ((TBL_MOB*)bl)->deletetimer = INVALID_TIMER;
  1482. unit_free(bl,3);
  1483. return 0;
  1484. }
  1485. /*==========================================
  1486. *
  1487. *------------------------------------------*/
  1488. int mob_deleteslave_sub(struct block_list *bl,va_list ap)
  1489. {
  1490. struct mob_data *md;
  1491. int id;
  1492. nullpo_retr(0, bl);
  1493. nullpo_retr(0, ap);
  1494. nullpo_retr(0, md = (struct mob_data *)bl);
  1495. id=va_arg(ap,int);
  1496. if(md->master_id > 0 && md->master_id == id )
  1497. status_kill(bl);
  1498. return 0;
  1499. }
  1500. /*==========================================
  1501. *
  1502. *------------------------------------------*/
  1503. int mob_deleteslave(struct mob_data *md)
  1504. {
  1505. nullpo_retr(0, md);
  1506. map_foreachinmap(mob_deleteslave_sub, md->bl.m, BL_MOB,md->bl.id);
  1507. return 0;
  1508. }
  1509. // Mob respawning through KAIZEL or NPC_REBIRTH [Skotlex]
  1510. int mob_respawn(int tid, unsigned int tick, int id, intptr data)
  1511. {
  1512. struct block_list *bl = map_id2bl(id);
  1513. if(!bl) return 0;
  1514. status_revive(bl, (uint8)data, 0);
  1515. return 1;
  1516. }
  1517. void mob_log_damage(struct mob_data *md, struct block_list *src, int damage)
  1518. {
  1519. int char_id = 0, flag = 0;
  1520. if(damage < 0) return; //Do nothing for absorbed damage.
  1521. if(!damage && !(src->type&DEFAULT_ENEMY_TYPE(md)))
  1522. return; //Do not log non-damaging effects from non-enemies.
  1523. switch (src->type) {
  1524. case BL_PC:
  1525. {
  1526. struct map_session_data *sd = (TBL_PC*)src;
  1527. char_id = sd->status.char_id;
  1528. if (damage)
  1529. md->attacked_id = src->id;
  1530. break;
  1531. }
  1532. case BL_HOM: //[orn]
  1533. {
  1534. struct homun_data *hd = (TBL_HOM*)src;
  1535. flag = 1;
  1536. if (hd->master)
  1537. char_id = hd->master->status.char_id;
  1538. if (damage)
  1539. md->attacked_id = src->id;
  1540. break;
  1541. }
  1542. case BL_PET:
  1543. {
  1544. struct pet_data *pd = (TBL_PET*)src;
  1545. if (battle_config.pet_attack_exp_to_master && pd->msd) {
  1546. char_id = pd->msd->status.char_id;
  1547. damage=(damage*battle_config.pet_attack_exp_rate)/100; //Modify logged damage accordingly.
  1548. }
  1549. //Let mobs retaliate against the pet's master [Skotlex]
  1550. if(pd->msd && damage)
  1551. md->attacked_id = pd->msd->bl.id;
  1552. break;
  1553. }
  1554. case BL_MOB:
  1555. {
  1556. struct mob_data* md2 = (TBL_MOB*)src;
  1557. if(md2->special_state.ai && md2->master_id) {
  1558. struct map_session_data* msd = map_id2sd(md2->master_id);
  1559. if (msd) char_id = msd->status.char_id;
  1560. }
  1561. if (!damage)
  1562. break;
  1563. //Let players decide whether to retaliate versus the master or the mob. [Skotlex]
  1564. if (md2->master_id && battle_config.retaliate_to_master)
  1565. md->attacked_id = md2->master_id;
  1566. else
  1567. md->attacked_id = src->id;
  1568. break;
  1569. }
  1570. default: //For all unhandled types.
  1571. md->attacked_id = src->id;
  1572. }
  1573. //Log damage...
  1574. if(char_id) {
  1575. int i,minpos;
  1576. unsigned int mindmg;
  1577. for(i=0,minpos=DAMAGELOG_SIZE-1,mindmg=UINT_MAX;i<DAMAGELOG_SIZE;i++){
  1578. if(md->dmglog[i].id==char_id &&
  1579. md->dmglog[i].flag==flag)
  1580. break;
  1581. if(md->dmglog[i].id==0) { //Store data in first empty slot.
  1582. md->dmglog[i].id = char_id;
  1583. md->dmglog[i].flag= flag;
  1584. break;
  1585. }
  1586. if(md->dmglog[i].dmg<mindmg && i)
  1587. { //Never overwrite first hit slot (he gets double exp bonus)
  1588. minpos=i;
  1589. mindmg=md->dmglog[i].dmg;
  1590. }
  1591. }
  1592. if(i<DAMAGELOG_SIZE)
  1593. md->dmglog[i].dmg+=damage;
  1594. else {
  1595. md->dmglog[minpos].id = char_id;
  1596. md->dmglog[minpos].flag= flag;
  1597. md->dmglog[minpos].dmg = damage;
  1598. }
  1599. }
  1600. return;
  1601. }
  1602. //Call when a mob has received damage.
  1603. void mob_damage(struct mob_data *md, struct block_list *src, int damage)
  1604. {
  1605. if (damage > 0)
  1606. { //Store total damage...
  1607. if (UINT_MAX - (unsigned int)damage > md->tdmg)
  1608. md->tdmg+=damage;
  1609. else if (md->tdmg == UINT_MAX)
  1610. damage = 0; //Stop recording damage once the cap has been reached.
  1611. else { //Cap damage log...
  1612. damage = (int)(UINT_MAX - md->tdmg);
  1613. md->tdmg = UINT_MAX;
  1614. }
  1615. if (md->state.aggressive)
  1616. { //No longer aggressive, change to retaliate AI.
  1617. md->state.aggressive = 0;
  1618. if(md->state.skillstate== MSS_ANGRY)
  1619. md->state.skillstate = MSS_BERSERK;
  1620. if(md->state.skillstate== MSS_FOLLOW)
  1621. md->state.skillstate = MSS_RUSH;
  1622. }
  1623. //Log damage
  1624. if (src) mob_log_damage(md, src, damage);
  1625. }
  1626. if (battle_config.show_mob_info&3)
  1627. clif_charnameack (0, &md->bl);
  1628. if (!src)
  1629. return;
  1630. if(md->special_state.ai==2/* && md->master_id == src->id*/)
  1631. { //LOne WOlf explained that ANYONE can trigger the marine countdown skill. [Skotlex]
  1632. md->state.alchemist = 1;
  1633. mobskill_use(md, gettick(), MSC_ALCHEMIST);
  1634. }
  1635. }
  1636. /*==========================================
  1637. * Signals death of mob.
  1638. * type&1 -> no drops, type&2 -> no exp
  1639. *------------------------------------------*/
  1640. int mob_dead(struct mob_data *md, struct block_list *src, int type)
  1641. {
  1642. struct status_data *status;
  1643. struct map_session_data *sd = NULL, *tmpsd[DAMAGELOG_SIZE];
  1644. struct map_session_data *mvp_sd = NULL, *second_sd = NULL, *third_sd = NULL;
  1645. struct {
  1646. struct party_data *p;
  1647. int id,zeny;
  1648. unsigned int base_exp,job_exp;
  1649. } pt[DAMAGELOG_SIZE];
  1650. int i,temp,count,pnum=0,m=md->bl.m;
  1651. unsigned int mvp_damage, tick = gettick();
  1652. unsigned short flaghom = 1; // [Zephyrus] Does the mob only received damage from homunculus?
  1653. if(src && src->type == BL_PC)
  1654. {
  1655. sd = (struct map_session_data *)src;
  1656. mvp_sd = sd;
  1657. }
  1658. status = &md->status;
  1659. if( md->guardian_data && md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  1660. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  1661. if( src )
  1662. { // Use Dead skill only if not killed by Script or Command
  1663. md->state.skillstate = MSS_DEAD;
  1664. mobskill_use(md,tick,-1);
  1665. }
  1666. map_freeblock_lock();
  1667. memset(pt,0,sizeof(pt));
  1668. if(src && src->type == BL_MOB)
  1669. mob_unlocktarget((struct mob_data *)src,tick);
  1670. if(sd) {
  1671. int sp = 0, hp = 0;
  1672. sp += sd->sp_gain_value;
  1673. sp += sd->sp_gain_race[status->race];
  1674. sp += sd->sp_gain_race[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  1675. hp += sd->hp_gain_value;
  1676. if (hp||sp)
  1677. status_heal(src, hp, sp, battle_config.show_hp_sp_gain?2:0);
  1678. if (sd->mission_mobid == md->class_) { //TK_MISSION [Skotlex]
  1679. if (++sd->mission_count >= 100 && (temp = mob_get_random_id(0, 0xE, sd->status.base_level)))
  1680. {
  1681. pc_addfame(sd, 1);
  1682. sd->mission_mobid = temp;
  1683. pc_setglobalreg(sd,"TK_MISSION_ID", temp);
  1684. sd->mission_count = 0;
  1685. clif_mission_info(sd, temp, 0);
  1686. }
  1687. pc_setglobalreg(sd,"TK_MISSION_COUNT", sd->mission_count);
  1688. }
  1689. }
  1690. // filter out entries not eligible for exp distribution
  1691. memset(tmpsd,0,sizeof(tmpsd));
  1692. for(i = 0, count = 0, mvp_damage = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++)
  1693. {
  1694. struct map_session_data* tsd = map_charid2sd(md->dmglog[i].id);
  1695. if(tsd == NULL)
  1696. continue; // skip empty entries
  1697. if(tsd->bl.m != m)
  1698. continue; // skip players not on this map
  1699. count++; //Only logged into same map chars are counted for the total.
  1700. if (pc_isdead(tsd))
  1701. continue; // skip dead players
  1702. if(md->dmglog[i].flag && !merc_is_hom_active(tsd->hd))
  1703. continue; // skip homunc's share if inactive
  1704. if(md->dmglog[i].dmg > mvp_damage)
  1705. {
  1706. third_sd = second_sd;
  1707. second_sd = mvp_sd;
  1708. mvp_sd = tsd;
  1709. mvp_damage = md->dmglog[i].dmg;
  1710. }
  1711. tmpsd[i] = tsd; // record as valid damage-log entry
  1712. if(!md->dmglog[i].flag && flaghom)
  1713. flaghom = 0; // Damage received from other Types != Homunculus
  1714. }
  1715. if(!battle_config.exp_calc_type && count > 1)
  1716. { //Apply first-attacker 200% exp share bonus
  1717. //TODO: Determine if this should go before calculating the MVP player instead of after.
  1718. if (UINT_MAX - md->dmglog[0].dmg > md->tdmg) {
  1719. md->tdmg += md->dmglog[0].dmg;
  1720. md->dmglog[0].dmg<<=1;
  1721. } else {
  1722. md->dmglog[0].dmg+= UINT_MAX - md->tdmg;
  1723. md->tdmg = UINT_MAX;
  1724. }
  1725. }
  1726. if(!(type&2) && //No exp
  1727. (!map[m].flag.pvp || battle_config.pvp_exp) && //Pvp no exp rule [MouseJstr]
  1728. (!md->master_id || !md->special_state.ai) && //Only player-summoned mobs do not give exp. [Skotlex]
  1729. (!map[m].flag.nobaseexp || !map[m].flag.nojobexp) //Gives Exp
  1730. ) { //Experience calculation.
  1731. int bonus = 100; //Bonus on top of your share (common to all attackers).
  1732. if (md->sc.data[SC_RICHMANKIM])
  1733. bonus += md->sc.data[SC_RICHMANKIM]->val2;
  1734. if(sd) {
  1735. temp = status_get_class(&md->bl);
  1736. if(sd->sc.data[SC_MIRACLE]) i = 2; //All mobs are Star Targets
  1737. else
  1738. ARR_FIND(0, 3, i, temp == sd->hate_mob[i] &&
  1739. (battle_config.allow_skill_without_day || sg_info[i].day_func()));
  1740. if(i<3 && (temp=pc_checkskill(sd,sg_info[i].bless_id)))
  1741. bonus += (i==2?20:10)*temp;
  1742. }
  1743. if(battle_config.mobs_level_up && md->level > md->db->lv) // [Valaris]
  1744. bonus += (md->level-md->db->lv)*battle_config.mobs_level_up_exp_rate;
  1745. for(i = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++)
  1746. {
  1747. int flag=1,zeny=0;
  1748. unsigned int base_exp, job_exp;
  1749. double per; //Your share of the mob's exp
  1750. if (!tmpsd[i]) continue;
  1751. if (!battle_config.exp_calc_type && md->tdmg)
  1752. //jAthena's exp formula based on total damage.
  1753. per = (double)md->dmglog[i].dmg/(double)md->tdmg;
  1754. else {
  1755. //eAthena's exp formula based on max hp.
  1756. per = (double)md->dmglog[i].dmg/(double)status->max_hp;
  1757. if (per > 2) per = 2; // prevents unlimited exp gain
  1758. }
  1759. if (count>1 && battle_config.exp_bonus_attacker) {
  1760. //Exp bonus per additional attacker.
  1761. if (count > battle_config.exp_bonus_max_attacker)
  1762. count = battle_config.exp_bonus_max_attacker;
  1763. per += per*((count-1)*battle_config.exp_bonus_attacker)/100.;
  1764. }
  1765. // change experience for different sized monsters [Valaris]
  1766. if(md->special_state.size==1)
  1767. per /=2.;
  1768. else if(md->special_state.size==2)
  1769. per *=2.;
  1770. if(battle_config.zeny_from_mobs && md->level) {
  1771. // zeny calculation moblv + random moblv [Valaris]
  1772. zeny=(int) ((md->level+rand()%md->level)*per*bonus/100.);
  1773. if(md->db->mexp > 0)
  1774. zeny*=rand()%250;
  1775. }
  1776. if (map[m].flag.nobaseexp || !md->db->base_exp)
  1777. base_exp = 0;
  1778. else
  1779. base_exp = (unsigned int)cap_value(md->db->base_exp * per * bonus/100. * map[m].bexp/100., 1, UINT_MAX);
  1780. if (map[m].flag.nojobexp || !md->db->job_exp || md->dmglog[i].flag) //Homun earned job-exp is always lost.
  1781. job_exp = 0;
  1782. else
  1783. job_exp = (unsigned int)cap_value(md->db->job_exp * per * bonus/100. * map[m].jexp/100., 1, UINT_MAX);
  1784. if((temp = tmpsd[i]->status.party_id )>0 && !md->dmglog[i].flag) //Homun-done damage (flag 1) is not given to party
  1785. {
  1786. int j;
  1787. for(j=0;j<pnum && pt[j].id!=temp;j++); //Locate party.
  1788. if(j==pnum){ //Possibly add party.
  1789. pt[pnum].p = party_search(temp);
  1790. if(pt[pnum].p && pt[pnum].p->party.exp)
  1791. {
  1792. pt[pnum].id=temp;
  1793. pt[pnum].base_exp=base_exp;
  1794. pt[pnum].job_exp=job_exp;
  1795. pt[pnum].zeny=zeny; // zeny share [Valaris]
  1796. pnum++;
  1797. flag=0;
  1798. }
  1799. }else{ //Add to total
  1800. if (pt[j].base_exp > UINT_MAX - base_exp)
  1801. pt[j].base_exp=UINT_MAX;
  1802. else
  1803. pt[j].base_exp+=base_exp;
  1804. if (pt[j].job_exp > UINT_MAX - job_exp)
  1805. pt[j].job_exp=UINT_MAX;
  1806. else
  1807. pt[j].job_exp+=job_exp;
  1808. pt[j].zeny+=zeny; // zeny share [Valaris]
  1809. flag=0;
  1810. }
  1811. }
  1812. if(flag) {
  1813. if(base_exp && md->dmglog[i].flag) //tmpsd[i] is null if it has no homunc.
  1814. merc_hom_gainexp(tmpsd[i]->hd, base_exp);
  1815. if(base_exp || job_exp)
  1816. pc_gainexp(tmpsd[i], &md->bl, base_exp, job_exp);
  1817. if(zeny) // zeny from mobs [Valaris]
  1818. pc_getzeny(tmpsd[i], zeny);
  1819. }
  1820. }
  1821. for(i=0;i<pnum;i++) //Party share.
  1822. party_exp_share(pt[i].p, &md->bl, pt[i].base_exp,pt[i].job_exp,pt[i].zeny);
  1823. } //End EXP giving.
  1824. if( !(type&1) && !map[m].flag.nomobloot && !md->state.rebirth && (
  1825. !md->special_state.ai || //Non special mob
  1826. battle_config.alchemist_summon_reward == 2 || //All summoned give drops
  1827. (md->special_state.ai==2 && battle_config.alchemist_summon_reward == 1) //Marine Sphere Drops items.
  1828. ) )
  1829. { // Item Drop
  1830. struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  1831. struct item_drop *ditem;
  1832. int drop_rate;
  1833. dlist->m = md->bl.m;
  1834. dlist->x = md->bl.x;
  1835. dlist->y = md->bl.y;
  1836. dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
  1837. dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
  1838. dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
  1839. dlist->item = NULL;
  1840. for (i = 0; i < MAX_MOB_DROP; i++)
  1841. {
  1842. if (md->db->dropitem[i].nameid <= 0)
  1843. continue;
  1844. if (!itemdb_exists(md->db->dropitem[i].nameid))
  1845. continue;
  1846. drop_rate = md->db->dropitem[i].p;
  1847. if (drop_rate <= 0) {
  1848. if (battle_config.drop_rate0item)
  1849. continue;
  1850. drop_rate = 1;
  1851. }
  1852. // change drops depending on monsters size [Valaris]
  1853. if(md->special_state.size==1 && drop_rate >= 2)
  1854. drop_rate/=2;
  1855. else if(md->special_state.size==2)
  1856. drop_rate*=2;
  1857. if (src) {
  1858. //Drops affected by luk as a fixed increase [Valaris]
  1859. if (battle_config.drops_by_luk)
  1860. drop_rate += status_get_luk(src)*battle_config.drops_by_luk/100;
  1861. //Drops affected by luk as a % increase [Skotlex]
  1862. if (battle_config.drops_by_luk2)
  1863. drop_rate += (int)(0.5+drop_rate*status_get_luk(src)*battle_config.drops_by_luk2/10000.);
  1864. }
  1865. if (sd && battle_config.pk_mode &&
  1866. (int)(md->level - sd->status.base_level) >= 20)
  1867. drop_rate = (int)(drop_rate*1.25); // pk_mode increase drops if 20 level difference [Valaris]
  1868. // attempt to drop the item
  1869. if (rand() % 10000 >= drop_rate)
  1870. { // Double try by Bubble Gum
  1871. if (!(sd && sd->sc.data[SC_ITEMBOOST] && rand() % 10000 < drop_rate))
  1872. continue;
  1873. }
  1874. ditem = mob_setdropitem(md->db->dropitem[i].nameid, 1);
  1875. //A Rare Drop Global Announce by Lupus
  1876. if(drop_rate<=battle_config.rare_drop_announce) {
  1877. struct item_data *i_data;
  1878. char message[128];
  1879. i_data = itemdb_search(ditem->item_data.nameid);
  1880. sprintf (message, msg_txt(541), (mvp_sd?mvp_sd->status.name:"???"), md->name, i_data->jname, (float)drop_rate/100);
  1881. //MSG: "'%s' won %s's %s (chance: %0.02f%%)"
  1882. intif_GMmessage(message,strlen(message)+1,0);
  1883. }
  1884. // Announce first, or else ditem will be freed. [Lance]
  1885. // By popular demand, use base drop rate for autoloot code. [Skotlex]
  1886. mob_item_drop(md, dlist, ditem, 0, md->db->dropitem[i].p, flaghom);
  1887. }
  1888. // Ore Discovery [Celest]
  1889. if (sd == mvp_sd && pc_checkskill(sd,BS_FINDINGORE)>0 && battle_config.finding_ore_rate/10 >= rand()%10000) {
  1890. ditem = mob_setdropitem(itemdb_searchrandomid(IG_FINDINGORE), 1);
  1891. mob_item_drop(md, dlist, ditem, 0, battle_config.finding_ore_rate/10, 0);
  1892. }
  1893. if(sd) {
  1894. // process script-granted extra drop bonuses
  1895. int itemid = 0;
  1896. for (i = 0; i < ARRAYLENGTH(sd->add_drop) && (sd->add_drop[i].id || sd->add_drop[i].group); i++)
  1897. {
  1898. if (sd->add_drop[i].race & (1<<status->race) ||
  1899. sd->add_drop[i].race & 1<<(status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS))
  1900. {
  1901. //check if the bonus item drop rate should be multiplied with mob level/10 [Lupus]
  1902. if(sd->add_drop[i].rate < 0) {
  1903. //it's negative, then it should be multiplied. e.g. for Mimic,Myst Case Cards, etc
  1904. // rate = base_rate * (mob_level/10) + 1
  1905. drop_rate = -sd->add_drop[i].rate*(md->level/10)+1;
  1906. drop_rate = cap_value(drop_rate, battle_config.item_drop_adddrop_min, battle_config.item_drop_adddrop_max);
  1907. if (drop_rate > 10000) drop_rate = 10000;
  1908. }
  1909. else
  1910. //it's positive, then it goes as it is
  1911. drop_rate = sd->add_drop[i].rate;
  1912. if (rand()%10000 >= drop_rate)
  1913. continue;
  1914. itemid = (sd->add_drop[i].id > 0) ? sd->add_drop[i].id : itemdb_searchrandomid(sd->add_drop[i].group);
  1915. mob_item_drop(md, dlist, mob_setdropitem(itemid,1), 0, drop_rate, 0);
  1916. }
  1917. }
  1918. // process script-granted zeny bonus (get_zeny_num) [Skotlex]
  1919. if(sd->get_zeny_num && rand()%100 < sd->get_zeny_rate)
  1920. {
  1921. i = sd->get_zeny_num > 0?sd->get_zeny_num:-md->level*sd->get_zeny_num;
  1922. if (!i) i = 1;
  1923. pc_getzeny(sd, 1+rand()%i);
  1924. }
  1925. }
  1926. // process items looted by the mob
  1927. if(md->lootitem) {
  1928. for(i = 0; i < md->lootitem_count; i++)
  1929. mob_item_drop(md, dlist, mob_setlootitem(&md->lootitem[i]), 1, 10000, 0);
  1930. }
  1931. if (dlist->item) //There are drop items.
  1932. add_timer(tick + (!battle_config.delay_battle_damage?500:0), mob_delay_item_drop, (int)dlist, 0);
  1933. else //No drops
  1934. ers_free(item_drop_list_ers, dlist);
  1935. } else if (md->lootitem && md->lootitem_count) { //Loot MUST drop!
  1936. struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  1937. dlist->m = md->bl.m;
  1938. dlist->x = md->bl.x;
  1939. dlist->y = md->bl.y;
  1940. dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
  1941. dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
  1942. dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
  1943. dlist->item = NULL;
  1944. for(i = 0; i < md->lootitem_count; i++)
  1945. mob_item_drop(md, dlist, mob_setlootitem(&md->lootitem[i]), 1, 10000, 0);
  1946. add_timer(tick + (!battle_config.delay_battle_damage?500:0), mob_delay_item_drop, (int)dlist, 0);
  1947. }
  1948. if(mvp_sd && md->db->mexp > 0 && !md->special_state.ai)
  1949. {
  1950. int log_mvp[2] = {0};
  1951. int j;
  1952. unsigned int mexp;
  1953. struct item item;
  1954. double exp;
  1955. //mapflag: noexp check [Lorky]
  1956. if (map[m].flag.nobaseexp || type&2)
  1957. exp =1;
  1958. else {
  1959. exp = md->db->mexp;
  1960. if (count > 1)
  1961. exp += exp*(battle_config.exp_bonus_attacker*(count-1))/100.; //[Gengar]
  1962. }
  1963. mexp = (unsigned int)cap_value(exp, 1, UINT_MAX);
  1964. clif_mvp_effect(mvp_sd);
  1965. clif_mvp_exp(mvp_sd,mexp);
  1966. pc_gainexp(mvp_sd, &md->bl, mexp,0);
  1967. log_mvp[1] = mexp;
  1968. if(map[m].flag.nomvploot || type&1)
  1969. ; //No drops.
  1970. else
  1971. for(j=0;j<3;j++)
  1972. {
  1973. i = rand() % 3;
  1974. if(md->db->mvpitem[i].nameid <= 0)
  1975. continue;
  1976. if(!itemdb_exists(md->db->mvpitem[i].nameid))
  1977. continue;
  1978. temp = md->db->mvpitem[i].p;
  1979. if(temp <= 0 && !battle_config.drop_rate0item)
  1980. temp = 1;
  1981. if(temp <= rand()%10000+1) //if ==0, then it doesn't drop
  1982. continue;
  1983. memset(&item,0,sizeof(item));
  1984. item.nameid=md->db->mvpitem[i].nameid;
  1985. item.identify= itemdb_isidentified(item.nameid);
  1986. clif_mvp_item(mvp_sd,item.nameid);
  1987. log_mvp[0] = item.nameid;
  1988. //A Rare MVP Drop Global Announce by Lupus
  1989. if(temp<=battle_config.rare_drop_announce) {
  1990. struct item_data *i_data;
  1991. char message[128];
  1992. i_data = itemdb_exists(item.nameid);
  1993. sprintf (message, msg_txt(541), mvp_sd->status.name, md->name, i_data->jname, temp/100.);
  1994. //MSG: "'%s' won %s's %s (chance: %0.02f%%)"
  1995. intif_GMmessage(message,strlen(message)+1,0);
  1996. }
  1997. if((temp = pc_additem(mvp_sd,&item,1)) != 0) {
  1998. clif_additem(mvp_sd,0,0,temp);
  1999. map_addflooritem(&item,1,mvp_sd->bl.m,mvp_sd->bl.x,mvp_sd->bl.y,mvp_sd->status.char_id,(second_sd?second_sd->status.char_id:0),(third_sd?third_sd->status.char_id:0),1);
  2000. }
  2001. if(log_config.enable_logs&0x200) {//Logs items, MVP prizes [Lupus]
  2002. log_pick_mob(md, "M", item.nameid, -1, NULL);
  2003. if (!temp)
  2004. log_pick_pc(mvp_sd, "P", item.nameid, 1, NULL);
  2005. }
  2006. break;
  2007. }
  2008. if(log_config.mvpdrop > 0)
  2009. log_mvpdrop(mvp_sd, md->class_, log_mvp);
  2010. }
  2011. if (type&2 && !sd && md->class_ == MOBID_EMPERIUM)
  2012. //Emperium destroyed by script. Discard mvp character. [Skotlex]
  2013. mvp_sd = NULL;
  2014. if( !md->sc.data[SC_KAIZEL] )
  2015. { // Only trigger event on final kill
  2016. md->status.hp = 0; //So that npc_event invoked functions KNOW that mob is dead
  2017. if( src )
  2018. switch( src->type )
  2019. {
  2020. case BL_PET: sd = ((TBL_PET*)src)->msd; break;
  2021. case BL_HOM: sd = ((TBL_HOM*)src)->master; break;
  2022. }
  2023. if( md->npc_event[0] && !md->state.npc_killmonster )
  2024. {
  2025. if( sd && battle_config.mob_npc_event_type )
  2026. {
  2027. pc_setglobalreg(sd,"killerrid",sd->bl.id);
  2028. npc_event(sd,md->npc_event,0);
  2029. }
  2030. else if( mvp_sd )
  2031. {
  2032. pc_setglobalreg(mvp_sd,"killerrid",sd?sd->bl.id:0);
  2033. npc_event(mvp_sd,md->npc_event,0);
  2034. }
  2035. else
  2036. npc_event_do(md->npc_event);
  2037. }
  2038. else if( mvp_sd && !md->state.npc_killmonster )
  2039. {
  2040. pc_setglobalreg(mvp_sd,"killedrid",md->class_);
  2041. npc_script_event(mvp_sd, NPCE_KILLNPC); // PCKillNPC [Lance]
  2042. }
  2043. md->status.hp = 1;
  2044. }
  2045. if(md->deletetimer!=-1) {
  2046. delete_timer(md->deletetimer,mob_timer_delete);
  2047. md->deletetimer = INVALID_TIMER;
  2048. }
  2049. mob_deleteslave(md);
  2050. map_freeblock_unlock();
  2051. if(pcdb_checkid(md->vd->class_))
  2052. { //Player mobs are not removed automatically by the client.
  2053. clif_clearunit_delayed(&md->bl, tick+3000);
  2054. }
  2055. if(!md->spawn) //Tell status_damage to remove it from memory.
  2056. return 5; // Note: Actually, it's 4. Oh well...
  2057. mob_setdelayspawn(md); //Set respawning.
  2058. return 3; //Remove from map.
  2059. }
  2060. void mob_revive(struct mob_data *md, unsigned int hp)
  2061. {
  2062. unsigned int tick = gettick();
  2063. md->state.skillstate = MSS_IDLE;
  2064. md->last_thinktime = tick;
  2065. md->next_walktime = tick+rand()%50+5000;
  2066. md->last_linktime = tick;
  2067. md->last_pcneartime = 0;
  2068. if (!md->bl.prev)
  2069. map_addblock(&md->bl);
  2070. clif_spawn(&md->bl);
  2071. skill_unit_move(&md->bl,tick,1);
  2072. mobskill_use(md, tick, MSC_SPAWN);
  2073. if (battle_config.show_mob_info&3)
  2074. clif_charnameack (0, &md->bl);
  2075. }
  2076. int mob_guardian_guildchange(struct block_list *bl,va_list ap)
  2077. {
  2078. struct mob_data *md;
  2079. struct guild* g;
  2080. nullpo_retr(0, bl);
  2081. nullpo_retr(0, md = (struct mob_data *)bl);
  2082. if (!md->guardian_data)
  2083. return 0;
  2084. if (md->guardian_data->castle->guild_id == 0)
  2085. { //Castle with no owner? Delete the guardians.
  2086. if (md->class_ == MOBID_EMPERIUM)
  2087. { //But don't delete the emperium, just clear it's guild-data
  2088. md->guardian_data->guild_id = 0;
  2089. md->guardian_data->emblem_id = 0;
  2090. md->guardian_data->guild_name[0] = '\0';
  2091. } else {
  2092. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS && md->guardian_data->castle->guardian[md->guardian_data->number].visible )
  2093. { //Safe removal of guardian.
  2094. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  2095. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2096. }
  2097. unit_free(&md->bl,0); //Remove guardian.
  2098. }
  2099. return 0;
  2100. }
  2101. g = guild_search(md->guardian_data->castle->guild_id);
  2102. if (g == NULL)
  2103. { //Properly remove guardian info from Castle data.
  2104. ShowError("mob_guardian_guildchange: New Guild (id %d) does not exists!\n", md->guardian_data->guild_id);
  2105. if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  2106. {
  2107. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  2108. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2109. }
  2110. unit_free(&md->bl,0);
  2111. return 0;
  2112. }
  2113. md->guardian_data->guild_id = g->guild_id;
  2114. md->guardian_data->emblem_id = g->emblem_id;
  2115. md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
  2116. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  2117. return 1;
  2118. }
  2119. /*==========================================
  2120. * Pick a random class for the mob
  2121. *------------------------------------------*/
  2122. int mob_random_class (int *value, size_t count)
  2123. {
  2124. nullpo_retr(0, value);
  2125. // no count specified, look into the array manually, but take only max 5 elements
  2126. if (count < 1) {
  2127. count = 0;
  2128. while(count < 5 && mobdb_checkid(value[count])) count++;
  2129. if(count < 1) // nothing found
  2130. return 0;
  2131. } else {
  2132. // check if at least the first value is valid
  2133. if(mobdb_checkid(value[0]) == 0)
  2134. return 0;
  2135. }
  2136. //Pick a random value, hoping it exists. [Skotlex]
  2137. return mobdb_checkid(value[rand()%count]);
  2138. }
  2139. /*==========================================
  2140. * Change mob base class
  2141. *------------------------------------------*/
  2142. int mob_class_change (struct mob_data *md, int class_)
  2143. {
  2144. unsigned int tick = gettick();
  2145. int i, c, hp_rate;
  2146. nullpo_retr(0, md);
  2147. if (md->bl.prev == NULL)
  2148. return 0;
  2149. //Disable class changing for some targets...
  2150. if (md->guardian_data)
  2151. return 0; //Guardians/Emperium
  2152. if (md->class_ >= 1324 && md->class_ <= 1363)
  2153. return 0; //Treasure Boxes
  2154. if (md->special_state.ai > 1)
  2155. return 0; //Marine Spheres and Floras.
  2156. if (mob_is_clone(md->class_))
  2157. return 0; //Clones
  2158. if (md->class_ == class_)
  2159. return 0; //Nothing to change.
  2160. hp_rate = get_percentage(md->status.hp, md->status.max_hp);
  2161. md->class_ = class_;
  2162. md->db = mob_db(class_);
  2163. if (battle_config.override_mob_names==1)
  2164. memcpy(md->name,md->db->name,NAME_LENGTH);
  2165. else
  2166. memcpy(md->name,md->db->jname,NAME_LENGTH);
  2167. mob_stop_attack(md);
  2168. mob_stop_walking(md, 0);
  2169. unit_skillcastcancel(&md->bl, 0);
  2170. status_set_viewdata(&md->bl, class_);
  2171. clif_mob_class_change(md,class_);
  2172. status_calc_mob(md, 3);
  2173. md->ud.state.speed_changed = 1; //Speed change update.
  2174. if (battle_config.monster_class_change_recover) {
  2175. memset(md->dmglog, 0, sizeof(md->dmglog));
  2176. md->tdmg = 0;
  2177. } else {
  2178. md->status.hp = md->status.max_hp*hp_rate/100;
  2179. if(md->status.hp < 1) md->status.hp = 1;
  2180. }
  2181. for(i=0,c=tick-1000*3600*10;i<MAX_MOBSKILL;i++)
  2182. md->skilldelay[i] = c;
  2183. if(md->lootitem == NULL && md->db->status.mode&MD_LOOTER)
  2184. md->lootitem=(struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  2185. //Targets should be cleared no morph
  2186. md->target_id = md->attacked_id = 0;
  2187. //Need to update name display.
  2188. clif_charnameack(0, &md->bl);
  2189. return 0;
  2190. }
  2191. /*==========================================
  2192. * mob回復
  2193. *------------------------------------------*/
  2194. void mob_heal(struct mob_data *md,unsigned int heal)
  2195. {
  2196. if (battle_config.show_mob_info&3)
  2197. clif_charnameack (0, &md->bl);
  2198. }
  2199. /*==========================================
  2200. * Added by RoVeRT
  2201. *------------------------------------------*/
  2202. int mob_warpslave_sub(struct block_list *bl,va_list ap)
  2203. {
  2204. struct mob_data *md=(struct mob_data *)bl;
  2205. struct block_list *master;
  2206. short x,y,range=0;
  2207. master = va_arg(ap, struct block_list*);
  2208. range = va_arg(ap, int);
  2209. if(md->master_id!=master->id)
  2210. return 0;
  2211. map_search_freecell(master, 0, &x, &y, range, range, 0);
  2212. unit_warp(&md->bl, master->m, x, y,2);
  2213. return 1;
  2214. }
  2215. /*==========================================
  2216. * Added by RoVeRT
  2217. * Warps slaves. Range is the area around the master that they can
  2218. * appear in randomly.
  2219. *------------------------------------------*/
  2220. int mob_warpslave(struct block_list *bl, int range)
  2221. {
  2222. if (range < 1)
  2223. range = 1; //Min range needed to avoid crashes and stuff. [Skotlex]
  2224. return map_foreachinmap(mob_warpslave_sub, bl->m, BL_MOB, bl, range);
  2225. }
  2226. /*==========================================
  2227. * 画面内の取り巻きの数計算用(foreachinarea)
  2228. *------------------------------------------*/
  2229. int mob_countslave_sub(struct block_list *bl,va_list ap)
  2230. {
  2231. int id;
  2232. struct mob_data *md;
  2233. id=va_arg(ap,int);
  2234. md = (struct mob_data *)bl;
  2235. if( md->master_id==id )
  2236. return 1;
  2237. return 0;
  2238. }
  2239. /*==========================================
  2240. * 画面内の取り巻きの数計算
  2241. *------------------------------------------*/
  2242. int mob_countslave(struct block_list *bl)
  2243. {
  2244. return map_foreachinmap(mob_countslave_sub, bl->m, BL_MOB,bl->id);
  2245. }
  2246. /*==========================================
  2247. * Summons amount slaves contained in the value[5] array using round-robin. [adapted by Skotlex]
  2248. *------------------------------------------*/
  2249. int mob_summonslave(struct mob_data *md2,int *value,int amount,int skill_id)
  2250. {
  2251. struct mob_data *md;
  2252. struct spawn_data data;
  2253. int count = 0,k=0,hp_rate=0;
  2254. nullpo_retr(0, md2);
  2255. nullpo_retr(0, value);
  2256. memset(&data, 0, sizeof(struct spawn_data));
  2257. data.m = md2->bl.m;
  2258. data.x = md2->bl.x;
  2259. data.y = md2->bl.y;
  2260. data.num = 1;
  2261. data.state.size = md2->special_state.size;
  2262. data.state.ai = md2->special_state.ai;
  2263. if(mobdb_checkid(value[0]) == 0)
  2264. return 0;
  2265. while(count < 5 && mobdb_checkid(value[count])) count++;
  2266. if(count < 1) return 0;
  2267. if (amount > 0 && amount < count) { //Do not start on 0, pick some random sub subset [Skotlex]
  2268. k = rand()%count;
  2269. amount+=k; //Increase final value by same amount to preserve total number to summon.
  2270. }
  2271. if (!battle_config.monster_class_change_recover &&
  2272. (skill_id == NPC_TRANSFORMATION || skill_id == NPC_METAMORPHOSIS))
  2273. hp_rate = get_percentage(md2->status.hp, md2->status.max_hp);
  2274. for(;k<amount;k++) {
  2275. short x,y;
  2276. data.class_ = value[k%count]; //Summon slaves in round-robin fashion. [Skotlex]
  2277. if (mobdb_checkid(data.class_) == 0)
  2278. continue;
  2279. if (map_search_freecell(&md2->bl, 0, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 0)) {
  2280. data.x = x;
  2281. data.y = y;
  2282. } else {
  2283. data.x = md2->bl.x;
  2284. data.y = md2->bl.y;
  2285. }
  2286. //These two need to be loaded from the db for each slave.
  2287. if(battle_config.override_mob_names==1)
  2288. strcpy(data.name,"--en--");
  2289. else
  2290. strcpy(data.name,"--ja--");
  2291. data.level = 0;
  2292. if (!mob_parse_dataset(&data))
  2293. continue;
  2294. md= mob_spawn_dataset(&data);
  2295. if(skill_id == NPC_SUMMONSLAVE){
  2296. md->master_id=md2->bl.id;
  2297. md->special_state.ai = md2->special_state.ai;
  2298. }
  2299. mob_spawn(md);
  2300. if (hp_rate) //Scale HP
  2301. md->status.hp = md->status.max_hp*hp_rate/100;
  2302. //Inherit the aggressive mode of the master.
  2303. if (battle_config.slaves_inherit_mode && md->master_id)
  2304. {
  2305. switch (battle_config.slaves_inherit_mode) {
  2306. case 1: //Always aggressive
  2307. if (!(md->status.mode&MD_AGGRESSIVE))
  2308. sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
  2309. break;
  2310. case 2: //Always passive
  2311. if (md->status.mode&MD_AGGRESSIVE)
  2312. sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
  2313. break;
  2314. default: //Copy master.
  2315. if (md2->status.mode&MD_AGGRESSIVE)
  2316. sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
  2317. else
  2318. sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
  2319. break;
  2320. }
  2321. }
  2322. clif_skill_nodamage(&md->bl,&md->bl,skill_id,amount,1);
  2323. }
  2324. return 0;
  2325. }
  2326. /*==========================================
  2327. *MOBskillから該当skillidのskillidxを返す
  2328. *------------------------------------------*/
  2329. int mob_skillid2skillidx(int class_,int skillid)
  2330. {
  2331. int i, max = mob_db(class_)->maxskill;
  2332. struct mob_skill *ms=mob_db(class_)->skill;
  2333. if(ms==NULL)
  2334. return -1;
  2335. ARR_FIND( 0, max, i, ms[i].skill_id == skillid );
  2336. return ( i < max ) ? i : -1;
  2337. }
  2338. /*==========================================
  2339. * Friendly Mob whose HP is decreasing by a nearby MOB is looked for.
  2340. *------------------------------------------*/
  2341. int mob_getfriendhprate_sub(struct block_list *bl,va_list ap)
  2342. {
  2343. int min_rate, max_rate,rate;
  2344. struct block_list **fr;
  2345. struct mob_data *md;
  2346. md = va_arg(ap,struct mob_data *);
  2347. min_rate=va_arg(ap,int);
  2348. max_rate=va_arg(ap,int);
  2349. fr=va_arg(ap,struct block_list **);
  2350. if( md->bl.id == bl->id && !(battle_config.mob_ai&0x10))
  2351. return 0;
  2352. if ((*fr) != NULL) //A friend was already found.
  2353. return 0;
  2354. if (battle_check_target(&md->bl,bl,BCT_ENEMY)>0)
  2355. return 0;
  2356. rate = get_percentage(status_get_hp(bl), status_get_max_hp(bl));
  2357. if (rate >= min_rate && rate <= max_rate)
  2358. (*fr) = bl;
  2359. return 1;
  2360. }
  2361. static struct block_list *mob_getfriendhprate(struct mob_data *md,int min_rate,int max_rate)
  2362. {
  2363. struct block_list *fr=NULL;
  2364. int type = BL_MOB;
  2365. nullpo_retr(NULL, md);
  2366. if (md->special_state.ai) //Summoned creatures. [Skotlex]
  2367. type = BL_PC;
  2368. map_foreachinrange(mob_getfriendhprate_sub, &md->bl, 8, type,md,min_rate,max_rate,&fr);
  2369. return fr;
  2370. }
  2371. /*==========================================
  2372. * Check hp rate of its master
  2373. *------------------------------------------*/
  2374. struct block_list *mob_getmasterhpltmaxrate(struct mob_data *md,int rate)
  2375. {
  2376. if( md && md->master_id > 0 )
  2377. {
  2378. struct block_list *bl = map_id2bl(md->master_id);
  2379. if( bl && get_percentage(status_get_hp(bl), status_get_max_hp(bl)) < rate )
  2380. return bl;
  2381. }
  2382. return NULL;
  2383. }
  2384. /*==========================================
  2385. * What a status state suits by nearby MOB is looked for.
  2386. *------------------------------------------*/
  2387. int mob_getfriendstatus_sub(struct block_list *bl,va_list ap)
  2388. {
  2389. int cond1,cond2;
  2390. struct mob_data **fr, *md, *mmd;
  2391. int flag=0;
  2392. nullpo_retr(0, bl);
  2393. nullpo_retr(0, ap);
  2394. nullpo_retr(0, md=(struct mob_data *)bl);
  2395. nullpo_retr(0, mmd=va_arg(ap,struct mob_data *));
  2396. if( mmd->bl.id == bl->id && !(battle_config.mob_ai&0x10) )
  2397. return 0;
  2398. if (battle_check_target(&mmd->bl,bl,BCT_ENEMY)>0)
  2399. return 0;
  2400. cond1=va_arg(ap,int);
  2401. cond2=va_arg(ap,int);
  2402. fr=va_arg(ap,struct mob_data **);
  2403. if( cond2==-1 ){
  2404. int j;
  2405. for(j=SC_COMMON_MIN;j<=SC_COMMON_MAX && !flag;j++){
  2406. if ((flag=(md->sc.data[j] != NULL))) //Once an effect was found, break out. [Skotlex]
  2407. break;
  2408. }
  2409. }else
  2410. flag=( md->sc.data[cond2] != NULL );
  2411. if( flag^( cond1==MSC_FRIENDSTATUSOFF ) )
  2412. (*fr)=md;
  2413. return 0;
  2414. }
  2415. struct mob_data *mob_getfriendstatus(struct mob_data *md,int cond1,int cond2)
  2416. {
  2417. struct mob_data* fr = NULL;
  2418. nullpo_retr(0, md);
  2419. map_foreachinrange(mob_getfriendstatus_sub, &md->bl, 8,BL_MOB, md,cond1,cond2,&fr);
  2420. return fr;
  2421. }
  2422. /*==========================================
  2423. * Skill use judging
  2424. *------------------------------------------*/
  2425. int mobskill_use(struct mob_data *md, unsigned int tick, int event)
  2426. {
  2427. struct mob_skill *ms;
  2428. struct block_list *fbl = NULL; //Friend bl, which can either be a BL_PC or BL_MOB depending on the situation. [Skotlex]
  2429. struct block_list *bl;
  2430. struct mob_data *fmd = NULL;
  2431. int i,j,n;
  2432. nullpo_retr (0, md);
  2433. nullpo_retr (0, ms = md->db->skill);
  2434. if (!battle_config.mob_skill_rate || md->ud.skilltimer != -1 || !md->db->maxskill)
  2435. return 0;
  2436. if (event == -1 && DIFF_TICK(md->ud.canact_tick, tick) > 0)
  2437. return 0; //Skill act delay only affects non-event skills.
  2438. //Pick a starting position and loop from that.
  2439. i = battle_config.mob_ai&0x100?rand()%md->db->maxskill:0;
  2440. for (n = 0; n < md->db->maxskill; i++, n++) {
  2441. int c2, flag = 0;
  2442. if (i == md->db->maxskill)
  2443. i = 0;
  2444. if (DIFF_TICK(tick, md->skilldelay[i]) < ms[i].delay)
  2445. continue;
  2446. c2 = ms[i].cond2;
  2447. if (ms[i].state != md->state.skillstate) {
  2448. if (md->state.skillstate != MSS_DEAD && (ms[i].state == MSS_ANY ||
  2449. (ms[i].state == MSS_ANYTARGET && md->target_id && md->state.skillstate != MSS_LOOT)
  2450. )) //ANYTARGET works with any state as long as there's a target. [Skotlex]
  2451. ;
  2452. else
  2453. continue;
  2454. }
  2455. if (rand() % 10000 > ms[i].permillage) //Lupus (max value = 10000)
  2456. continue;
  2457. if (ms[i].cond1 == event)
  2458. flag = 1; //Trigger skill.
  2459. else if (ms[i].cond1 == MSC_SKILLUSED)
  2460. flag = ((event & 0xffff) == MSC_SKILLUSED && ((event >> 16) == c2 || c2 == 0));
  2461. else if(event == -1){
  2462. //Avoid entering on defined events to avoid "hyper-active skill use" due to the overflow of calls to this function in battle.
  2463. switch (ms[i].cond1)
  2464. {
  2465. case MSC_ALWAYS:
  2466. flag = 1; break;
  2467. case MSC_MYHPLTMAXRATE: // HP< maxhp%
  2468. flag = get_percentage(md->status.hp, md->status.max_hp);
  2469. flag = (flag <= c2);
  2470. break;
  2471. case MSC_MYHPINRATE:
  2472. flag = get_percentage(md->status.hp, md->status.max_hp);
  2473. flag = (flag >= c2 && flag <= ms[i].val[0]);
  2474. break;
  2475. case MSC_MYSTATUSON: // status[num] on
  2476. case MSC_MYSTATUSOFF: // status[num] off
  2477. if (!md->sc.count) {
  2478. flag = 0;
  2479. } else if (ms[i].cond2 == -1) {
  2480. for (j = SC_COMMON_MIN; j <= SC_COMMON_MAX; j++)
  2481. if ((flag = (md->sc.data[j]!=NULL)) != 0)
  2482. break;
  2483. } else {
  2484. flag = (md->sc.data[ms[i].cond2]!=NULL);
  2485. }
  2486. flag ^= (ms[i].cond1 == MSC_MYSTATUSOFF); break;
  2487. case MSC_FRIENDHPLTMAXRATE: // friend HP < maxhp%
  2488. flag = ((fbl = mob_getfriendhprate(md, 0, ms[i].cond2)) != NULL); break;
  2489. case MSC_FRIENDHPINRATE :
  2490. flag = ((fbl = mob_getfriendhprate(md, ms[i].cond2, ms[i].val[0])) != NULL); break;
  2491. case MSC_FRIENDSTATUSON: // friend status[num] on
  2492. case MSC_FRIENDSTATUSOFF: // friend status[num] off
  2493. flag = ((fmd = mob_getfriendstatus(md, ms[i].cond1, ms[i].cond2)) != NULL); break;
  2494. case MSC_SLAVELT: // slave < num
  2495. flag = (mob_countslave(&md->bl) < c2 ); break;
  2496. case MSC_ATTACKPCGT: // attack pc > num
  2497. flag = (unit_counttargeted(&md->bl, 0) > c2); break;
  2498. case MSC_SLAVELE: // slave <= num
  2499. flag = (mob_countslave(&md->bl) <= c2 ); break;
  2500. case MSC_ATTACKPCGE: // attack pc >= num
  2501. flag = (unit_counttargeted(&md->bl, 0) >= c2); break;
  2502. case MSC_AFTERSKILL:
  2503. flag = (md->ud.skillid == c2); break;
  2504. case MSC_RUDEATTACKED:
  2505. flag = (md->state.attacked_count >= RUDE_ATTACKED_COUNT);
  2506. if (flag) md->state.attacked_count = 0; //Rude attacked count should be reset after the skill condition is met. Thanks to Komurka [Skotlex]
  2507. break;
  2508. case MSC_MASTERHPLTMAXRATE:
  2509. flag = ((fbl = mob_getmasterhpltmaxrate(md, ms[i].cond2)) != NULL); break;
  2510. case MSC_MASTERATTACKED:
  2511. flag = (md->master_id > 0 && (fbl=map_id2bl(md->master_id)) && unit_counttargeted(fbl, 0) > 0); break;
  2512. case MSC_ALCHEMIST:
  2513. flag = (md->state.alchemist);
  2514. break;
  2515. }
  2516. }
  2517. if (!flag)
  2518. continue; //Skill requisite failed to be fulfilled.
  2519. //Execute skill
  2520. if (skill_get_casttype(ms[i].skill_id) == CAST_GROUND)
  2521. { //Ground skill.
  2522. short x, y;
  2523. switch (ms[i].target) {
  2524. case MST_RANDOM: //Pick a random enemy within skill range.
  2525. bl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
  2526. skill_get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
  2527. break;
  2528. case MST_TARGET:
  2529. case MST_AROUND5:
  2530. case MST_AROUND6:
  2531. case MST_AROUND7:
  2532. case MST_AROUND8:
  2533. bl = map_id2bl(md->target_id);
  2534. break;
  2535. case MST_MASTER:
  2536. bl = &md->bl;
  2537. if (md->master_id)
  2538. bl = map_id2bl(md->master_id);
  2539. if (bl) //Otherwise, fall through.
  2540. break;
  2541. case MST_FRIEND:
  2542. bl = fbl?fbl:(fmd?&fmd->bl:&md->bl);
  2543. break;
  2544. default:
  2545. bl = &md->bl;
  2546. break;
  2547. }
  2548. if (!bl) continue;
  2549. x = bl->x;
  2550. y = bl->y;
  2551. // Look for an area to cast the spell around...
  2552. if (ms[i].target >= MST_AROUND1 || ms[i].target >= MST_AROUND5) {
  2553. j = ms[i].target >= MST_AROUND1?
  2554. (ms[i].target-MST_AROUND1) +1:
  2555. (ms[i].target-MST_AROUND5) +1;
  2556. map_search_freecell(&md->bl, md->bl.m, &x, &y, j, j, 3);
  2557. }
  2558. md->skillidx = i;
  2559. if (!unit_skilluse_pos2(&md->bl, x, y,
  2560. ms[i].skill_id, ms[i].skill_lv,
  2561. ms[i].casttime, ms[i].cancel))
  2562. continue;
  2563. } else {
  2564. //Targetted skill
  2565. switch (ms[i].target) {
  2566. case MST_RANDOM: //Pick a random enemy within skill range.
  2567. bl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
  2568. skill_get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
  2569. break;
  2570. case MST_TARGET:
  2571. bl = map_id2bl(md->target_id);
  2572. break;
  2573. case MST_MASTER:
  2574. bl = &md->bl;
  2575. if (md->master_id)
  2576. bl = map_id2bl(md->master_id);
  2577. if (bl) //Otherwise, fall through.
  2578. break;
  2579. case MST_FRIEND:
  2580. if (fbl) {
  2581. bl = fbl;
  2582. break;
  2583. } else if (fmd) {
  2584. bl = &fmd->bl;
  2585. break;
  2586. } // else fall through
  2587. default:
  2588. bl = &md->bl;
  2589. break;
  2590. }
  2591. if (!bl) continue;
  2592. md->skillidx = i;
  2593. if (!unit_skilluse_id2(&md->bl, bl->id,
  2594. ms[i].skill_id, ms[i].skill_lv,
  2595. ms[i].casttime, ms[i].cancel))
  2596. continue;
  2597. }
  2598. //Skill used. Post-setups...
  2599. if(battle_config.mob_ai&0x200)
  2600. { //pass on delay to same skill.
  2601. for (j = 0; j < md->db->maxskill; j++)
  2602. if (md->db->skill[j].skill_id == ms[i].skill_id)
  2603. md->skilldelay[j]=tick;
  2604. } else
  2605. md->skilldelay[i]=tick;
  2606. return 1;
  2607. }
  2608. //No skill was used.
  2609. md->skillidx = -1;
  2610. return 0;
  2611. }
  2612. /*==========================================
  2613. * Skill use event processing
  2614. *------------------------------------------*/
  2615. int mobskill_event(struct mob_data *md, struct block_list *src, unsigned int tick, int flag)
  2616. {
  2617. int target_id, res = 0;
  2618. target_id = md->target_id;
  2619. if (!target_id || battle_config.mob_changetarget_byskill)
  2620. md->target_id = src->id;
  2621. if (flag == -1)
  2622. res = mobskill_use(md, tick, MSC_CASTTARGETED);
  2623. else if ((flag&0xffff) == MSC_SKILLUSED)
  2624. res = mobskill_use(md, tick, flag);
  2625. else if (flag&BF_SHORT)
  2626. res = mobskill_use(md, tick, MSC_CLOSEDATTACKED);
  2627. else if (flag&BF_LONG && !(flag&BF_MAGIC)) //Long-attacked should not include magic.
  2628. res = mobskill_use(md, tick, MSC_LONGRANGEATTACKED);
  2629. if (!res)
  2630. //Restore previous target only if skill condition failed to trigger. [Skotlex]
  2631. md->target_id = target_id;
  2632. //Otherwise check if the target is an enemy, and unlock if needed.
  2633. else if (battle_check_target(&md->bl, src, BCT_ENEMY) <= 0)
  2634. md->target_id = target_id;
  2635. return res;
  2636. }
  2637. // Player cloned mobs. [Valaris]
  2638. int mob_is_clone(int class_)
  2639. {
  2640. if(class_ < MOB_CLONE_START || class_ > MOB_CLONE_END)
  2641. return 0;
  2642. if (mob_db(class_) == mob_dummy)
  2643. return 0;
  2644. return class_;
  2645. }
  2646. //Flag values:
  2647. //&1: Set special ai (fight mobs, not players)
  2648. //If mode is not passed, a default aggressive mode is used.
  2649. //If master_id is passed, clone is attached to him.
  2650. //Returns: ID of newly crafted copy.
  2651. int mob_clone_spawn(struct map_session_data *sd, int m, int x, int y, const char *event, int master_id, int mode, int flag, unsigned int duration)
  2652. {
  2653. int class_;
  2654. int i,j,inf,skill_id;
  2655. struct mob_data *md;
  2656. struct mob_skill *ms;
  2657. struct mob_db* db;
  2658. struct status_data *status;
  2659. nullpo_retr(0, sd);
  2660. ARR_FIND( MOB_CLONE_START, MOB_CLONE_END, class_, mob_db_data[class_] == NULL );
  2661. if(class_ >= MOB_CLONE_END)
  2662. return 0;
  2663. db = mob_db_data[class_]=(struct mob_db*)aCalloc(1, sizeof(struct mob_db));
  2664. status = &db->status;
  2665. sprintf(db->sprite,sd->status.name);
  2666. sprintf(db->name,sd->status.name);
  2667. sprintf(db->jname,sd->status.name);
  2668. db->lv=status_get_lv(&sd->bl);
  2669. memcpy(status, &sd->base_status, sizeof(struct status_data));
  2670. status->rhw.atk2= status->dex + status->rhw.atk + status->rhw.atk2; //Max ATK
  2671. status->rhw.atk = status->dex; //Min ATK
  2672. if (status->lhw.atk) {
  2673. status->lhw.atk2= status->dex + status->lhw.atk + status->lhw.atk2; //Max ATK
  2674. status->lhw.atk = status->dex; //Min ATK
  2675. }
  2676. if (mode) //User provided mode.
  2677. status->mode = mode;
  2678. else if (flag&1) //Friendly Character, remove looting.
  2679. status->mode &= ~MD_LOOTER;
  2680. status->hp = status->max_hp;
  2681. status->sp = status->max_sp;
  2682. memcpy(&db->vd, &sd->vd, sizeof(struct view_data));
  2683. db->base_exp=1;
  2684. db->job_exp=1;
  2685. db->range2=AREA_SIZE; //Let them have the same view-range as players.
  2686. db->range3=AREA_SIZE; //Min chase of a screen.
  2687. db->option=sd->sc.option;
  2688. //Skill copy [Skotlex]
  2689. ms = &db->skill[0];
  2690. //Go Backwards to give better priority to advanced skills.
  2691. for (i=0,j = MAX_SKILL_TREE-1;j>=0 && i< MAX_MOBSKILL ;j--) {
  2692. skill_id = skill_tree[pc_class2idx(sd->status.class_)][j].id;
  2693. if (!skill_id || sd->status.skill[skill_id].lv < 1 ||
  2694. (skill_get_inf2(skill_id)&(INF2_WEDDING_SKILL|INF2_GUILD_SKILL)) ||
  2695. skill_get_nocast(skill_id)&16
  2696. )
  2697. continue;
  2698. //Normal aggressive mob, disable skills that cannot help them fight
  2699. //against players (those with flags UF_NOMOB and UF_NOPC are specific
  2700. //to always aid players!) [Skotlex]
  2701. if (!(flag&1) &&
  2702. skill_get_unit_id(skill_id, 0) &&
  2703. skill_get_unit_flag(skill_id)&(UF_NOMOB|UF_NOPC))
  2704. continue;
  2705. memset (&ms[i], 0, sizeof(struct mob_skill));
  2706. ms[i].skill_id = skill_id;
  2707. ms[i].skill_lv = sd->status.skill[skill_id].lv;
  2708. ms[i].state = MSS_ANY;
  2709. ms[i].permillage = 500*battle_config.mob_skill_rate/100; //Default chance of all skills: 5%
  2710. ms[i].emotion = -1;
  2711. ms[i].cancel = 0;
  2712. ms[i].casttime = skill_castfix(&sd->bl,skill_id, ms[i].skill_lv);
  2713. ms[i].delay = 5000+skill_delayfix(&sd->bl,skill_id, ms[i].skill_lv);
  2714. inf = skill_get_inf(skill_id);
  2715. if (inf&INF_ATTACK_SKILL) {
  2716. ms[i].target = MST_TARGET;
  2717. ms[i].cond1 = MSC_ALWAYS;
  2718. if (skill_get_range(skill_id, ms[i].skill_lv) > 3)
  2719. ms[i].state = MSS_ANYTARGET;
  2720. else
  2721. ms[i].state = MSS_BERSERK;
  2722. } else if(inf&INF_GROUND_SKILL) {
  2723. if (skill_get_inf2(skill_id)&INF2_TRAP) { //Traps!
  2724. ms[i].state = MSS_IDLE;
  2725. ms[i].target = MST_AROUND2;
  2726. ms[i].delay = 60000;
  2727. } else if (skill_get_unit_target(skill_id) == BCT_ENEMY) { //Target Enemy
  2728. ms[i].state = MSS_ANYTARGET;
  2729. ms[i].target = MST_TARGET;
  2730. ms[i].cond1 = MSC_ALWAYS;
  2731. } else { //Target allies
  2732. ms[i].target = MST_FRIEND;
  2733. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  2734. ms[i].cond2 = 95;
  2735. }
  2736. } else if (inf&INF_SELF_SKILL) {
  2737. if (skill_get_inf2(skill_id)&INF2_NO_TARGET_SELF) { //auto-select target skill.
  2738. ms[i].target = MST_TARGET;
  2739. ms[i].cond1 = MSC_ALWAYS;
  2740. if (skill_get_range(skill_id, ms[i].skill_lv) > 3) {
  2741. ms[i].state = MSS_ANYTARGET;
  2742. } else {
  2743. ms[i].state = MSS_BERSERK;
  2744. }
  2745. } else { //Self skill
  2746. ms[i].target = MST_SELF;
  2747. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  2748. ms[i].cond2 = 90;
  2749. ms[i].permillage = 2000;
  2750. //Delay: Remove the stock 5 secs and add half of the support time.
  2751. ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
  2752. if (ms[i].delay < 5000)
  2753. ms[i].delay = 5000; //With a minimum of 5 secs.
  2754. }
  2755. } else if (inf&INF_SUPPORT_SKILL) {
  2756. ms[i].target = MST_FRIEND;
  2757. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  2758. ms[i].cond2 = 90;
  2759. if (skill_id == AL_HEAL)
  2760. ms[i].permillage = 5000; //Higher skill rate usage for heal.
  2761. else if (skill_id == ALL_RESURRECTION)
  2762. ms[i].cond2 = 1;
  2763. //Delay: Remove the stock 5 secs and add half of the support time.
  2764. ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
  2765. if (ms[i].delay < 2000)
  2766. ms[i].delay = 2000; //With a minimum of 2 secs.
  2767. if (i+1 < MAX_MOBSKILL) { //duplicate this so it also triggers on self.
  2768. memcpy(&ms[i+1], &ms[i], sizeof(struct mob_skill));
  2769. db->maxskill = ++i;
  2770. ms[i].target = MST_SELF;
  2771. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  2772. }
  2773. } else {
  2774. switch (skill_id) { //Certain Special skills that are passive, and thus, never triggered.
  2775. case MO_TRIPLEATTACK:
  2776. case TF_DOUBLE:
  2777. case GS_CHAINACTION:
  2778. ms[i].state = MSS_BERSERK;
  2779. ms[i].target = MST_TARGET;
  2780. ms[i].cond1 = MSC_ALWAYS;
  2781. ms[i].permillage = skill_id==MO_TRIPLEATTACK?(3000-ms[i].skill_lv*100):(ms[i].skill_lv*500);
  2782. ms[i].delay -= 5000; //Remove the added delay as these could trigger on "all hits".
  2783. break;
  2784. default: //Untreated Skill
  2785. continue;
  2786. }
  2787. }
  2788. if (battle_config.mob_skill_rate!= 100)
  2789. ms[i].permillage = ms[i].permillage*battle_config.mob_skill_rate/100;
  2790. if (battle_config.mob_skill_delay != 100)
  2791. ms[i].delay = ms[i].delay*battle_config.mob_skill_delay/100;
  2792. db->maxskill = ++i;
  2793. }
  2794. //Finally, spawn it.
  2795. md = mob_once_spawn_sub(&sd->bl, m, x, y, "--en--",class_,event);
  2796. if (!md) return 0; //Failed?
  2797. if (master_id || flag || duration) { //Further manipulate crafted char.
  2798. if (flag&1) //Friendly Character
  2799. md->special_state.ai = 1;
  2800. if (master_id) //Attach to Master
  2801. md->master_id = master_id;
  2802. if (duration) //Auto Delete after a while.
  2803. md->deletetimer = add_timer (gettick() + duration, mob_timer_delete, md->bl.id, 0);
  2804. }
  2805. mob_spawn(md);
  2806. return md->bl.id;
  2807. }
  2808. int mob_clone_delete(int class_)
  2809. {
  2810. if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END
  2811. && mob_db_data[class_]!=NULL) {
  2812. aFree(mob_db_data[class_]);
  2813. mob_db_data[class_]=NULL;
  2814. return 1;
  2815. }
  2816. return 0;
  2817. }
  2818. //
  2819. // 初期化
  2820. //
  2821. /*==========================================
  2822. * Since un-setting [ mob ] up was used, it is an initial provisional value setup.
  2823. *------------------------------------------*/
  2824. static int mob_makedummymobdb(int class_)
  2825. {
  2826. if (mob_dummy != NULL)
  2827. {
  2828. if (mob_db(class_) == mob_dummy)
  2829. return 1; //Using the mob_dummy data already. [Skotlex]
  2830. if (class_ > 0 && class_ <= MAX_MOB_DB)
  2831. { //Remove the mob data so that it uses the dummy data instead.
  2832. aFree(mob_db_data[class_]);
  2833. mob_db_data[class_] = NULL;
  2834. }
  2835. return 0;
  2836. }
  2837. //Initialize dummy data.
  2838. mob_dummy = (struct mob_db*)aCalloc(1, sizeof(struct mob_db)); //Initializing the dummy mob.
  2839. sprintf(mob_dummy->sprite,"DUMMY");
  2840. sprintf(mob_dummy->name,"Dummy");
  2841. sprintf(mob_dummy->jname,"Dummy");
  2842. mob_dummy->lv=1;
  2843. mob_dummy->status.max_hp=1000;
  2844. mob_dummy->status.max_sp=1;
  2845. mob_dummy->status.rhw.range=1;
  2846. mob_dummy->status.rhw.atk=7;
  2847. mob_dummy->status.rhw.atk2=10;
  2848. mob_dummy->status.str=1;
  2849. mob_dummy->status.agi=1;
  2850. mob_dummy->status.vit=1;
  2851. mob_dummy->status.int_=1;
  2852. mob_dummy->status.dex=6;
  2853. mob_dummy->status.luk=2;
  2854. mob_dummy->status.speed=300;
  2855. mob_dummy->status.adelay=1000;
  2856. mob_dummy->status.amotion=500;
  2857. mob_dummy->status.dmotion=500;
  2858. mob_dummy->base_exp=2;
  2859. mob_dummy->job_exp=1;
  2860. mob_dummy->range2=10;
  2861. mob_dummy->range3=10;
  2862. return 0;
  2863. }
  2864. //Adjusts the drop rate of item according to the criteria given. [Skotlex]
  2865. static unsigned int mob_drop_adjust(int baserate, int rate_adjust, unsigned short rate_min, unsigned short rate_max)
  2866. {
  2867. double rate = baserate;
  2868. if (battle_config.logarithmic_drops && rate_adjust > 0 && baserate > 0) //Logarithmic drops equation by Ishizu-Chan
  2869. //Equation: Droprate(x,y) = x * (5 - log(x)) ^ (ln(y) / ln(5))
  2870. //x is the normal Droprate, y is the Modificator.
  2871. rate = rate * pow((5.0 - log10(rate)), (log(rate_adjust/100.) / log(5.0))) + 0.5;
  2872. else
  2873. //Classical linear rate adjustment.
  2874. rate = rate * rate_adjust/100;
  2875. return (unsigned int)cap_value(rate,rate_min,rate_max);
  2876. }
  2877. /*==========================================
  2878. * processes one mobdb entry
  2879. *------------------------------------------*/
  2880. static bool mob_parse_dbrow(char** str)
  2881. {
  2882. struct mob_db *db;
  2883. struct status_data *status;
  2884. int class_, i, k;
  2885. double exp, maxhp;
  2886. struct mob_data data;
  2887. class_ = str[0] ? atoi(str[0]) : 0;
  2888. if (class_ == 0)
  2889. return false; //Leave blank lines alone... [Skotlex]
  2890. if (class_ <= 1000 || class_ > MAX_MOB_DB) {
  2891. ShowWarning("Mob with ID: %d not loaded. ID must be in range [%d-%d]\n", class_, 1000, MAX_MOB_DB);
  2892. return false;
  2893. }
  2894. if (pcdb_checkid(class_)) {
  2895. ShowWarning("Mob with ID: %d not loaded. That ID is reserved for player classes.\n");
  2896. return false;
  2897. }
  2898. if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END) {
  2899. ShowWarning("Mob with ID: %d not loaded. Range %d-%d is reserved for player clones. Please increase MAX_MOB_DB (%d)\n", class_, MOB_CLONE_START, MOB_CLONE_END-1, MAX_MOB_DB);
  2900. return false;
  2901. }
  2902. if (mob_db_data[class_] == NULL)
  2903. mob_db_data[class_] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db));
  2904. db = mob_db_data[class_];
  2905. status = &db->status;
  2906. db->vd.class_ = class_;
  2907. strncpy(db->sprite, str[1], NAME_LENGTH);
  2908. strncpy(db->jname, str[2], NAME_LENGTH);
  2909. strncpy(db->name, str[3], NAME_LENGTH);
  2910. db->lv = atoi(str[4]);
  2911. db->lv = cap_value(db->lv, 1, USHRT_MAX);
  2912. status->max_hp = atoi(str[5]);
  2913. status->max_sp = atoi(str[6]);
  2914. exp = (double)atoi(str[7]) * (double)battle_config.base_exp_rate / 100.;
  2915. db->base_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  2916. exp = (double)atoi(str[8]) * (double)battle_config.job_exp_rate / 100.;
  2917. db->job_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  2918. status->rhw.range = atoi(str[9]);
  2919. status->rhw.atk = atoi(str[10]);
  2920. status->rhw.atk2 = atoi(str[11]);
  2921. status->def = atoi(str[12]);
  2922. status->mdef = atoi(str[13]);
  2923. status->str = atoi(str[14]);
  2924. status->agi = atoi(str[15]);
  2925. status->vit = atoi(str[16]);
  2926. status->int_ = atoi(str[17]);
  2927. status->dex = atoi(str[18]);
  2928. status->luk = atoi(str[19]);
  2929. //All status should be min 1 to prevent divisions by zero from some skills. [Skotlex]
  2930. if (status->str < 1) status->str = 1;
  2931. if (status->agi < 1) status->agi = 1;
  2932. if (status->vit < 1) status->vit = 1;
  2933. if (status->int_< 1) status->int_= 1;
  2934. if (status->dex < 1) status->dex = 1;
  2935. if (status->luk < 1) status->luk = 1;
  2936. db->range2 = atoi(str[20]);
  2937. db->range3 = atoi(str[21]);
  2938. if (battle_config.view_range_rate != 100) {
  2939. db->range2 = db->range2 * battle_config.view_range_rate / 100;
  2940. if (db->range2 < 1)
  2941. db->range2 = 1;
  2942. }
  2943. if (battle_config.chase_range_rate != 100) {
  2944. db->range3 = db->range3 * battle_config.chase_range_rate / 100;
  2945. if (db->range3 < db->range2)
  2946. db->range3 = db->range2;
  2947. }
  2948. status->size = atoi(str[22]);
  2949. status->race = atoi(str[23]);
  2950. i = atoi(str[24]); //Element
  2951. status->def_ele = i%10;
  2952. status->ele_lv = i/20;
  2953. if (status->def_ele >= ELE_MAX) {
  2954. ShowWarning("Mob with ID: %d has invalid element type %d (max element is %d)\n", class_, status->def_ele, ELE_MAX-1);
  2955. status->def_ele = ELE_NEUTRAL;
  2956. }
  2957. if (status->ele_lv < 1 || status->ele_lv > 4) {
  2958. ShowWarning("Mob with ID: %d has invalid element level %d (max is 4)\n", class_, status->ele_lv);
  2959. status->ele_lv = 1;
  2960. }
  2961. status->mode = (int)strtol(str[25], NULL, 0);
  2962. if (!battle_config.monster_active_enable)
  2963. status->mode &= ~MD_AGGRESSIVE;
  2964. status->speed = atoi(str[26]);
  2965. status->aspd_rate = 1000;
  2966. status->adelay = atoi(str[27]);
  2967. status->amotion = atoi(str[28]);
  2968. //If the attack animation is longer than the delay, the client crops the attack animation!
  2969. //On aegis there is no real visible effect of having a recharge-time less than amotion anyway.
  2970. if (status->adelay < status->amotion)
  2971. status->adelay = status->amotion;
  2972. status->dmotion = atoi(str[29]);
  2973. if(battle_config.monster_damage_delay_rate != 100)
  2974. status->dmotion = status->dmotion * battle_config.monster_damage_delay_rate / 100;
  2975. data.bl.type = BL_MOB;
  2976. data.level = db->lv;
  2977. memcpy(&data.status, status, sizeof(struct status_data));
  2978. status_calc_misc(&data.bl, status, db->lv);
  2979. // MVP EXP Bonus, Chance: MEXP,ExpPer
  2980. // Some new MVP's MEXP multipled by high exp-rate cause overflow. [LuzZza]
  2981. exp = (double)atoi(str[30]) * (double)battle_config.mvp_exp_rate / 100.;
  2982. db->mexp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  2983. db->mexpper = atoi(str[31]);
  2984. //Now that we know if it is an mvp or not, apply battle_config modifiers [Skotlex]
  2985. maxhp = (double)status->max_hp;
  2986. if (db->mexp > 0) { //Mvp
  2987. if (battle_config.mvp_hp_rate != 100)
  2988. maxhp = maxhp * (double)battle_config.mvp_hp_rate / 100.;
  2989. } else //Normal mob
  2990. if (battle_config.monster_hp_rate != 100)
  2991. maxhp = maxhp * (double)battle_config.monster_hp_rate / 100.;
  2992. status->max_hp = (unsigned int)cap_value(maxhp, 1, UINT_MAX);
  2993. if(status->max_sp < 1) status->max_sp = 1;
  2994. //Since mobs always respawn with full life...
  2995. status->hp = status->max_hp;
  2996. status->sp = status->max_sp;
  2997. // MVP Drops: MVP1id,MVP1per,MVP2id,MVP2per,MVP3id,MVP3per
  2998. for(i = 0; i < 3; i++) {
  2999. struct item_data *id;
  3000. db->mvpitem[i].nameid = atoi(str[32+i*2]);
  3001. if (!db->mvpitem[i].nameid) {
  3002. db->mvpitem[i].p = 0; //No item....
  3003. continue;
  3004. }
  3005. db->mvpitem[i].p = mob_drop_adjust(atoi(str[33+i*2]), battle_config.item_rate_mvp, battle_config.item_drop_mvp_min, battle_config.item_drop_mvp_max);
  3006. //calculate and store Max available drop chance of the MVP item
  3007. if (db->mvpitem[i].p) {
  3008. id = itemdb_search(db->mvpitem[i].nameid);
  3009. if (id->maxchance == 10000 || (id->maxchance < db->mvpitem[i].p/10 + 1) ) {
  3010. //item has bigger drop chance or sold in shops
  3011. id->maxchance = db->mvpitem[i].p/10 + 1; //reduce MVP drop info to not spoil common drop rate
  3012. }
  3013. }
  3014. }
  3015. for(i = 0; i < MAX_MOB_DROP; i++) {
  3016. int rate = 0, rate_adjust, type;
  3017. unsigned short ratemin, ratemax;
  3018. struct item_data *id;
  3019. k = 38+i*2;
  3020. db->dropitem[i].nameid = atoi(str[k]);
  3021. if (!db->dropitem[i].nameid) {
  3022. db->dropitem[i].p = 0; //No drop.
  3023. continue;
  3024. }
  3025. type = itemdb_type(db->dropitem[i].nameid);
  3026. rate = atoi(str[k+1]);
  3027. if (class_ >= 1324 && class_ <= 1363)
  3028. { //Treasure box drop rates [Skotlex]
  3029. rate_adjust = battle_config.item_rate_treasure;
  3030. ratemin = battle_config.item_drop_treasure_min;
  3031. ratemax = battle_config.item_drop_treasure_max;
  3032. }
  3033. else switch (type)
  3034. { // Added suport to restrict normal drops of MVP's [Reddozen]
  3035. case IT_HEALING:
  3036. rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_heal_boss : battle_config.item_rate_heal;
  3037. ratemin = battle_config.item_drop_heal_min;
  3038. ratemax = battle_config.item_drop_heal_max;
  3039. break;
  3040. case IT_USABLE:
  3041. rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_use_boss : battle_config.item_rate_use;
  3042. ratemin = battle_config.item_drop_use_min;
  3043. ratemax = battle_config.item_drop_use_max;
  3044. break;
  3045. case IT_WEAPON:
  3046. case IT_ARMOR:
  3047. case IT_PETARMOR:
  3048. rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_equip_boss : battle_config.item_rate_equip;
  3049. ratemin = battle_config.item_drop_equip_min;
  3050. ratemax = battle_config.item_drop_equip_max;
  3051. break;
  3052. case IT_CARD:
  3053. rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_card_boss : battle_config.item_rate_card;
  3054. ratemin = battle_config.item_drop_card_min;
  3055. ratemax = battle_config.item_drop_card_max;
  3056. break;
  3057. default:
  3058. rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_common_boss : battle_config.item_rate_common;
  3059. ratemin = battle_config.item_drop_common_min;
  3060. ratemax = battle_config.item_drop_common_max;
  3061. break;
  3062. }
  3063. db->dropitem[i].p = mob_drop_adjust(rate, rate_adjust, ratemin, ratemax);
  3064. //calculate and store Max available drop chance of the item
  3065. if (db->dropitem[i].p && (class_ < 1324 || class_ > 1363)) { //Skip treasure chests.
  3066. id = itemdb_search(db->dropitem[i].nameid);
  3067. if (id->maxchance == 10000 || (id->maxchance < db->dropitem[i].p) ) {
  3068. id->maxchance = db->dropitem[i].p; //item has bigger drop chance or sold in shops
  3069. }
  3070. for (k = 0; k< MAX_SEARCH; k++) {
  3071. if (id->mob[k].chance <= db->dropitem[i].p)
  3072. break;
  3073. }
  3074. if (k == MAX_SEARCH)
  3075. continue;
  3076. if (id->mob[k].id != class_)
  3077. memmove(&id->mob[k+1], &id->mob[k], (MAX_SEARCH-k-1)*sizeof(id->mob[0]));
  3078. id->mob[k].chance = db->dropitem[i].p;
  3079. id->mob[k].id = class_;
  3080. }
  3081. }
  3082. return true;
  3083. }
  3084. /*==========================================
  3085. * mob_db.txt reading
  3086. *------------------------------------------*/
  3087. static int mob_readdb(void)
  3088. {
  3089. const char* filename[] = { "mob_db.txt", "mob_db2.txt" };
  3090. int fi;
  3091. for( fi = 0; fi < ARRAYLENGTH(filename); ++fi )
  3092. {
  3093. uint32 lines = 0, count = 0;
  3094. char line[1024];
  3095. char path[256];
  3096. FILE* fp;
  3097. sprintf(path, "%s/%s", db_path, filename[fi]);
  3098. fp = fopen(path, "r");
  3099. if(fp == NULL) {
  3100. if(fi > 0)
  3101. continue;
  3102. return -1;
  3103. }
  3104. // process rows one by one
  3105. while(fgets(line, sizeof(line), fp))
  3106. {
  3107. char *str[38+2*MAX_MOB_DROP], *p, *np;
  3108. int i;
  3109. lines++;
  3110. if(line[0] == '/' && line[1] == '/')
  3111. continue;
  3112. for(i = 0, p = line; i < 38 + 2*MAX_MOB_DROP; i++)
  3113. {
  3114. str[i] = p;
  3115. if((np = strchr(p, ',')) != NULL) {
  3116. *np = '\0'; p = np + 1;
  3117. }
  3118. }
  3119. if(i < 38 + 2*MAX_MOB_DROP) {
  3120. ShowWarning("mob_readdb: Insufficient columns for mob with id: %d, skipping.\n", atoi(str[0]));
  3121. continue;
  3122. }
  3123. if (!mob_parse_dbrow(str))
  3124. continue;
  3125. count++;
  3126. }
  3127. fclose(fp);
  3128. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename[fi]);
  3129. }
  3130. return 0;
  3131. }
  3132. #ifndef TXT_ONLY
  3133. /*==========================================
  3134. * mob_db table reading
  3135. *------------------------------------------*/
  3136. static int mob_read_sqldb(void)
  3137. {
  3138. const char* mob_db_name[] = { mob_db_db, mob_db2_db };
  3139. int fi;
  3140. for( fi = 0; fi < ARRAYLENGTH(mob_db_name); ++fi )
  3141. {
  3142. uint32 lines = 0, count = 0;
  3143. // retrieve all rows from the mob database
  3144. if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", mob_db_name[fi]) )
  3145. {
  3146. Sql_ShowDebug(mmysql_handle);
  3147. continue;
  3148. }
  3149. // process rows one by one
  3150. while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) )
  3151. {
  3152. // wrap the result into a TXT-compatible format
  3153. char line[1024];
  3154. char* str[38+2*MAX_MOB_DROP];
  3155. char* p;
  3156. int i;
  3157. lines++;
  3158. for(i = 0, p = line; i < 38 + 2*MAX_MOB_DROP; i++)
  3159. {
  3160. char* data;
  3161. size_t len;
  3162. Sql_GetData(mmysql_handle, i, &data, &len);
  3163. strcpy(p, data);
  3164. str[i] = p;
  3165. p+= len + 1;
  3166. }
  3167. if (!mob_parse_dbrow(str))
  3168. continue;
  3169. count++;
  3170. }
  3171. // free the query result
  3172. Sql_FreeResult(mmysql_handle);
  3173. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_db_name[fi]);
  3174. count = 0;
  3175. }
  3176. return 0;
  3177. }
  3178. #endif /* not TXT_ONLY */
  3179. /*==========================================
  3180. * MOB display graphic change data reading
  3181. *------------------------------------------*/
  3182. static int mob_readdb_mobavail(void)
  3183. {
  3184. FILE *fp;
  3185. char line[1024];
  3186. int ln=0;
  3187. int class_,j,k;
  3188. char *str[20],*p,*np;
  3189. sprintf(line, "%s/mob_avail.txt", db_path);
  3190. if( (fp=fopen(line,"r"))==NULL ){
  3191. ShowError("can't read %s\n", line);
  3192. return -1;
  3193. }
  3194. while(fgets(line, sizeof(line), fp))
  3195. {
  3196. if(line[0]=='/' && line[1]=='/')
  3197. continue;
  3198. memset(str,0,sizeof(str));
  3199. for(j=0,p=line;j<12;j++){
  3200. if((np=strchr(p,','))!=NULL){
  3201. str[j]=p;
  3202. *np=0;
  3203. p=np+1;
  3204. } else
  3205. str[j]=p;
  3206. }
  3207. if(str[0]==NULL)
  3208. continue;
  3209. class_=atoi(str[0]);
  3210. if (class_ == 0)
  3211. continue; //Leave blank lines alone... [Skotlex]
  3212. if(mob_db(class_) == mob_dummy) // 値が異常なら処理しない。
  3213. continue;
  3214. k=atoi(str[1]);
  3215. if(k < 0)
  3216. continue;
  3217. memset(&mob_db_data[class_]->vd, 0, sizeof(struct view_data));
  3218. mob_db_data[class_]->vd.class_=k;
  3219. //Player sprites
  3220. if(pcdb_checkid(k) && j>=12) {
  3221. mob_db_data[class_]->vd.sex=atoi(str[2]);
  3222. mob_db_data[class_]->vd.hair_style=atoi(str[3]);
  3223. mob_db_data[class_]->vd.hair_color=atoi(str[4]);
  3224. mob_db_data[class_]->vd.weapon=atoi(str[5]);
  3225. mob_db_data[class_]->vd.shield=atoi(str[6]);
  3226. mob_db_data[class_]->vd.head_top=atoi(str[7]);
  3227. mob_db_data[class_]->vd.head_mid=atoi(str[8]);
  3228. mob_db_data[class_]->vd.head_bottom=atoi(str[9]);
  3229. mob_db_data[class_]->option=atoi(str[10])&~(OPTION_HIDE|OPTION_CLOAK|OPTION_INVISIBLE);
  3230. mob_db_data[class_]->vd.cloth_color=atoi(str[11]); // Monster player dye option - Valaris
  3231. }
  3232. else if(str[2] && atoi(str[2]) > 0)
  3233. mob_db_data[class_]->vd.head_bottom=atoi(str[2]); // mob equipment [Valaris]
  3234. ln++;
  3235. }
  3236. fclose(fp);
  3237. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,"mob_avail.txt");
  3238. return 0;
  3239. }
  3240. /*==========================================
  3241. * Reading of random monster data
  3242. *------------------------------------------*/
  3243. static int mob_read_randommonster(void)
  3244. {
  3245. FILE *fp;
  3246. char line[1024];
  3247. char *str[10],*p;
  3248. int i,j;
  3249. const char* mobfile[] = {
  3250. "mob_branch.txt",
  3251. "mob_poring.txt",
  3252. "mob_boss.txt",
  3253. "mob_pouch.txt"};
  3254. memset(&summon, 0, sizeof(summon));
  3255. for( i = 0; i < ARRAYLENGTH(mobfile) && i < MAX_RANDOMMONSTER; i++ )
  3256. {
  3257. mob_db_data[0]->summonper[i] = 1002; // 設定し忘れた場合はポリンが出るようにしておく
  3258. sprintf(line, "%s/%s", db_path, mobfile[i]);
  3259. fp=fopen(line,"r");
  3260. if(fp==NULL){
  3261. ShowError("can't read %s\n",line);
  3262. return -1;
  3263. }
  3264. while(fgets(line, sizeof(line), fp))
  3265. {
  3266. int class_;
  3267. if(line[0] == '/' && line[1] == '/')
  3268. continue;
  3269. memset(str,0,sizeof(str));
  3270. for(j=0,p=line;j<3 && p;j++){
  3271. str[j]=p;
  3272. p=strchr(p,',');
  3273. if(p) *p++=0;
  3274. }
  3275. if(str[0]==NULL || str[2]==NULL)
  3276. continue;
  3277. class_ = atoi(str[0]);
  3278. if(mob_db(class_) == mob_dummy)
  3279. continue;
  3280. mob_db_data[class_]->summonper[i]=atoi(str[2]);
  3281. if (i) {
  3282. if( summon[i].qty < ARRAYLENGTH(summon[i].class_) ) //MvPs
  3283. summon[i].class_[summon[i].qty++] = class_;
  3284. else {
  3285. ShowDebug("Can't store more random mobs from %s, increase size of mob.c:summon variable!\n", mobfile[i]);
  3286. break;
  3287. }
  3288. }
  3289. }
  3290. if (i && !summon[i].qty)
  3291. { //At least have the default here.
  3292. summon[i].class_[0] = mob_db_data[0]->summonper[i];
  3293. summon[i].qty = 1;
  3294. }
  3295. fclose(fp);
  3296. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",mobfile[i]);
  3297. }
  3298. return 0;
  3299. }
  3300. /*==========================================
  3301. * mob_skill_db.txt reading
  3302. *------------------------------------------*/
  3303. static int mob_readskilldb(void)
  3304. {
  3305. FILE *fp;
  3306. char line[1024];
  3307. int i,tmp, count;
  3308. const struct {
  3309. char str[32];
  3310. enum MobSkillState id;
  3311. } cond1[] = {
  3312. { "always", MSC_ALWAYS },
  3313. { "myhpltmaxrate", MSC_MYHPLTMAXRATE },
  3314. { "myhpinrate", MSC_MYHPINRATE },
  3315. { "friendhpltmaxrate",MSC_FRIENDHPLTMAXRATE },
  3316. { "friendhpinrate", MSC_FRIENDHPINRATE },
  3317. { "mystatuson", MSC_MYSTATUSON },
  3318. { "mystatusoff", MSC_MYSTATUSOFF },
  3319. { "friendstatuson", MSC_FRIENDSTATUSON },
  3320. { "friendstatusoff", MSC_FRIENDSTATUSOFF },
  3321. { "attackpcgt", MSC_ATTACKPCGT },
  3322. { "attackpcge", MSC_ATTACKPCGE },
  3323. { "slavelt", MSC_SLAVELT },
  3324. { "slavele", MSC_SLAVELE },
  3325. { "closedattacked", MSC_CLOSEDATTACKED },
  3326. { "longrangeattacked",MSC_LONGRANGEATTACKED },
  3327. { "skillused", MSC_SKILLUSED },
  3328. { "afterskill", MSC_AFTERSKILL },
  3329. { "casttargeted", MSC_CASTTARGETED },
  3330. { "rudeattacked", MSC_RUDEATTACKED },
  3331. { "masterhpltmaxrate",MSC_MASTERHPLTMAXRATE },
  3332. { "masterattacked", MSC_MASTERATTACKED },
  3333. { "alchemist", MSC_ALCHEMIST },
  3334. { "onspawn", MSC_SPAWN},
  3335. }, cond2[] ={
  3336. { "anybad", -1 },
  3337. { "stone", SC_STONE },
  3338. { "freeze", SC_FREEZE },
  3339. { "stun", SC_STUN },
  3340. { "sleep", SC_SLEEP },
  3341. { "poison", SC_POISON },
  3342. { "curse", SC_CURSE },
  3343. { "silence", SC_SILENCE },
  3344. { "confusion", SC_CONFUSION },
  3345. { "blind", SC_BLIND },
  3346. { "hiding", SC_HIDING },
  3347. { "sight", SC_SIGHT },
  3348. }, state[] = {
  3349. { "any", MSS_ANY }, //All states except Dead
  3350. { "idle", MSS_IDLE },
  3351. { "walk", MSS_WALK },
  3352. { "loot", MSS_LOOT },
  3353. { "dead", MSS_DEAD },
  3354. { "attack", MSS_BERSERK }, //Retaliating attack
  3355. { "angry", MSS_ANGRY }, //Preemptive attack (aggressive mobs)
  3356. { "chase", MSS_RUSH }, //Chase escaping target
  3357. { "follow", MSS_FOLLOW }, //Preemptive chase (aggressive mobs)
  3358. { "anytarget",MSS_ANYTARGET }, //Berserk+Angry+Rush+Follow
  3359. }, target[] = {
  3360. { "target", MST_TARGET },
  3361. { "randomtarget", MST_RANDOM },
  3362. { "self", MST_SELF },
  3363. { "friend", MST_FRIEND },
  3364. { "master", MST_MASTER },
  3365. { "around5", MST_AROUND5 },
  3366. { "around6", MST_AROUND6 },
  3367. { "around7", MST_AROUND7 },
  3368. { "around8", MST_AROUND8 },
  3369. { "around1", MST_AROUND1 },
  3370. { "around2", MST_AROUND2 },
  3371. { "around3", MST_AROUND3 },
  3372. { "around4", MST_AROUND4 },
  3373. { "around", MST_AROUND },
  3374. };
  3375. int x;
  3376. const char* filename[] = { "mob_skill_db.txt","mob_skill_db2.txt" };
  3377. if( battle_config.mob_skill_rate == 0 ) {
  3378. ShowStatus("Mob skill use disabled. Not reading mob skills.\n");
  3379. return 0;
  3380. }
  3381. for( x = 0; x < ARRAYLENGTH(filename); ++x )
  3382. {
  3383. int last_mob_id = 0;
  3384. count = 0;
  3385. sprintf(line, "%s/%s", db_path, filename[x]);
  3386. fp=fopen(line,"r");
  3387. if(fp==NULL){
  3388. if(x==0)
  3389. ShowError("can't read %s\n",line);
  3390. continue;
  3391. }
  3392. while(fgets(line, sizeof(line), fp))
  3393. {
  3394. char *sp[20],*p;
  3395. int mob_id;
  3396. struct mob_skill *ms, gms;
  3397. int j=0;
  3398. count++;
  3399. if(line[0] == '/' && line[1] == '/')
  3400. continue;
  3401. memset(sp,0,sizeof(sp));
  3402. for(i=0,p=line;i<18 && p;i++){
  3403. sp[i]=p;
  3404. if((p=strchr(p,','))!=NULL)
  3405. *p++=0;
  3406. }
  3407. if(i == 0 || (mob_id=atoi(sp[0]))== 0)
  3408. continue;
  3409. if(i < 18) {
  3410. ShowError("mob_skill: Insufficient number of fields for skill at %s, line %d\n", filename[x], count);
  3411. continue;
  3412. }
  3413. if (mob_id > 0 && mob_db(mob_id) == mob_dummy)
  3414. {
  3415. if (mob_id != last_mob_id) {
  3416. ShowError("mob_skill: Non existant Mob id %d at %s, line %d\n", mob_id, filename[x], count);
  3417. last_mob_id = mob_id;
  3418. }
  3419. continue;
  3420. }
  3421. if( strcmp(sp[1],"clear")==0 ){
  3422. if (mob_id < 0)
  3423. continue;
  3424. memset(mob_db_data[mob_id]->skill,0,sizeof(struct mob_skill));
  3425. mob_db_data[mob_id]->maxskill=0;
  3426. continue;
  3427. }
  3428. if (mob_id < 0)
  3429. { //Prepare global skill. [Skotlex]
  3430. memset(&gms, 0, sizeof (struct mob_skill));
  3431. ms = &gms;
  3432. } else {
  3433. ARR_FIND( 0, MAX_MOBSKILL, i, (ms = &mob_db_data[mob_id]->skill[i])->skill_id == 0 );
  3434. if( i == MAX_MOBSKILL ) {
  3435. if (mob_id != last_mob_id) {
  3436. ShowError("mob_skill: readdb: too many skills! Line %d in %d[%s]\n", count,mob_id,mob_db_data[mob_id]->sprite);
  3437. last_mob_id = mob_id;
  3438. }
  3439. continue;
  3440. }
  3441. }
  3442. //State
  3443. ARR_FIND( 0, ARRAYLENGTH(state), j, strcmp(sp[2],state[j].str) == 0 );
  3444. if( j < ARRAYLENGTH(state) )
  3445. ms->state = state[j].id;
  3446. else {
  3447. ShowWarning("mob_skill: Unrecognized state %s at %s, line %d\n", sp[2], filename[x], count);
  3448. ms->state = MSS_ANY;
  3449. }
  3450. //Skill ID
  3451. j=atoi(sp[3]);
  3452. if (j<=0 || j>MAX_SKILL_DB) //fixed Lupus
  3453. {
  3454. if (mob_id < 0)
  3455. ShowError("Invalid Skill ID (%d) for all mobs\n", j);
  3456. else
  3457. ShowError("Invalid Skill ID (%d) for mob %d (%s)\n", j, mob_id, mob_db_data[mob_id]->sprite);
  3458. continue;
  3459. }
  3460. ms->skill_id=j;
  3461. //Skill lvl
  3462. j= atoi(sp[4])<=0 ? 1 : atoi(sp[4]);
  3463. ms->skill_lv= j>battle_config.mob_max_skilllvl ? battle_config.mob_max_skilllvl : j; //we strip max skill level
  3464. //Apply battle_config modifiers to rate (permillage) and delay [Skotlex]
  3465. tmp = atoi(sp[5]);
  3466. if (battle_config.mob_skill_rate != 100)
  3467. tmp = tmp*battle_config.mob_skill_rate/100;
  3468. if (tmp > 10000)
  3469. ms->permillage= 10000;
  3470. else
  3471. ms->permillage= tmp;
  3472. ms->casttime=atoi(sp[6]);
  3473. ms->delay=atoi(sp[7]);
  3474. if (battle_config.mob_skill_delay != 100)
  3475. ms->delay = ms->delay*battle_config.mob_skill_delay/100;
  3476. if (ms->delay < 0) //time overflow?
  3477. ms->delay = INT_MAX;
  3478. ms->cancel=atoi(sp[8]);
  3479. if( strcmp(sp[8],"yes")==0 ) ms->cancel=1;
  3480. //Target
  3481. ARR_FIND( 0, ARRAYLENGTH(target), j, strcmp(sp[9],target[j].str) == 0 );
  3482. if( j < ARRAYLENGTH(target) )
  3483. ms->target = target[j].id;
  3484. else {
  3485. ShowWarning("mob_skill: Unrecognized target %s at %s, line %d\n", sp[9], filename[x], count);
  3486. ms->target = MST_TARGET;
  3487. }
  3488. //Check that the target condition is right for the skill type. [Skotlex]
  3489. if (skill_get_casttype(ms->skill_id) == CAST_GROUND)
  3490. { //Ground skill.
  3491. if (ms->target > MST_AROUND)
  3492. {
  3493. ShowWarning("Wrong mob skill target for ground skill %d (%s) for %s.\n",
  3494. ms->skill_id, skill_get_name(ms->skill_id),
  3495. mob_id < 0?"all mobs":mob_db_data[mob_id]->sprite);
  3496. ms->target = MST_TARGET;
  3497. }
  3498. } else if (ms->target > MST_MASTER) {
  3499. ShowWarning("Wrong mob skill target 'around' for non-ground skill %d (%s) for %s\n.",
  3500. ms->skill_id, skill_get_name(ms->skill_id),
  3501. mob_id < 0?"all mobs":mob_db_data[mob_id]->sprite);
  3502. ms->target = MST_TARGET;
  3503. }
  3504. //Cond1
  3505. ARR_FIND( 0, ARRAYLENGTH(cond1), j, strcmp(sp[10],cond1[j].str) == 0 );
  3506. if( j < ARRAYLENGTH(cond1) )
  3507. ms->cond1 = cond1[j].id;
  3508. else {
  3509. ShowWarning("mob_skill: Unrecognized condition 1 %s at %s, line %d\n", sp[10], filename[x], count);
  3510. ms->cond1 = -1;
  3511. }
  3512. //Cond2
  3513. // numeric value
  3514. ms->cond2 = atoi(sp[11]);
  3515. // or special constant
  3516. ARR_FIND( 0, ARRAYLENGTH(cond2), j, strcmp(sp[11],cond2[j].str) == 0 );
  3517. if( j < ARRAYLENGTH(cond2) )
  3518. ms->cond2 = cond2[j].id;
  3519. ms->val[0]=(int)strtol(sp[12],NULL,0);
  3520. ms->val[1]=(int)strtol(sp[13],NULL,0);
  3521. ms->val[2]=(int)strtol(sp[14],NULL,0);
  3522. ms->val[3]=(int)strtol(sp[15],NULL,0);
  3523. ms->val[4]=(int)strtol(sp[16],NULL,0);
  3524. if(ms->skill_id == NPC_EMOTION && mob_id>0 &&
  3525. ms->val[1] == mob_db(mob_id)->status.mode)
  3526. {
  3527. ms->val[1] = 0;
  3528. ms->val[4] = 1; //request to return mode to normal.
  3529. }
  3530. if(ms->skill_id == NPC_EMOTION_ON && mob_id>0 && ms->val[1])
  3531. { //Adds a mode to the mob.
  3532. //Remove aggressive mode when the new mob type is passive.
  3533. if (!(ms->val[1]&MD_AGGRESSIVE))
  3534. ms->val[3]|=MD_AGGRESSIVE;
  3535. ms->val[2]|= ms->val[1]; //Add the new mode.
  3536. ms->val[1] = 0; //Do not "set" it.
  3537. }
  3538. if(sp[17] != NULL && strlen(sp[17])>2)
  3539. ms->emotion=atoi(sp[17]);
  3540. else
  3541. ms->emotion=-1;
  3542. if (mob_id < 0)
  3543. { //Set this skill to ALL mobs. [Skotlex]
  3544. mob_id *= -1;
  3545. for (i = 1; i < MAX_MOB_DB; i++)
  3546. {
  3547. if (mob_db_data[i] == NULL)
  3548. continue;
  3549. if (mob_db_data[i]->status.mode&MD_BOSS)
  3550. {
  3551. if (!(mob_id&2)) //Skill not for bosses
  3552. continue;
  3553. } else
  3554. if (!(mob_id&1)) //Skill not for normal enemies.
  3555. continue;
  3556. for(j=0;j<MAX_MOBSKILL;j++)
  3557. if( mob_db_data[i]->skill[j].skill_id == 0)
  3558. break;
  3559. if(j==MAX_MOBSKILL)
  3560. continue;
  3561. memcpy (&mob_db_data[i]->skill[j], ms, sizeof(struct mob_skill));
  3562. mob_db_data[i]->maxskill=j+1;
  3563. }
  3564. } else //Skill set on a single mob.
  3565. mob_db_data[mob_id]->maxskill=i+1;
  3566. }
  3567. fclose(fp);
  3568. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",filename[x]);
  3569. }
  3570. return 0;
  3571. }
  3572. /*==========================================
  3573. * mob_race_db.txt reading
  3574. *------------------------------------------*/
  3575. static int mob_readdb_race(void)
  3576. {
  3577. FILE *fp;
  3578. char line[1024];
  3579. int race,j,k;
  3580. char *str[20],*p,*np;
  3581. sprintf(line, "%s/mob_race2_db.txt", db_path);
  3582. if( (fp=fopen(line,"r"))==NULL ){
  3583. ShowError("can't read %s\n", line);
  3584. return -1;
  3585. }
  3586. while(fgets(line, sizeof(line), fp))
  3587. {
  3588. if(line[0]=='/' && line[1]=='/')
  3589. continue;
  3590. memset(str,0,sizeof(str));
  3591. for(j=0,p=line;j<12;j++){
  3592. if((np=strchr(p,','))!=NULL){
  3593. str[j]=p;
  3594. *np=0;
  3595. p=np+1;
  3596. } else
  3597. str[j]=p;
  3598. }
  3599. if(str[0]==NULL)
  3600. continue;
  3601. race=atoi(str[0]);
  3602. if (race < 0 || race >= MAX_MOB_RACE_DB)
  3603. continue;
  3604. for (j=1; j<20; j++) {
  3605. if (!str[j])
  3606. break;
  3607. k=atoi(str[j]);
  3608. if (mob_db(k) == mob_dummy)
  3609. continue;
  3610. mob_db_data[k]->race2 = race;
  3611. }
  3612. }
  3613. fclose(fp);
  3614. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","mob_race2_db.txt");
  3615. return 0;
  3616. }
  3617. void mob_reload(void)
  3618. {
  3619. int i;
  3620. #ifndef TXT_ONLY
  3621. if(db_use_sqldbs)
  3622. mob_read_sqldb();
  3623. else
  3624. #endif /* TXT_ONLY */
  3625. mob_readdb();
  3626. mob_readdb_mobavail();
  3627. mob_read_randommonster();
  3628. //Mob skills need to be cleared before re-reading them. [Skotlex]
  3629. for (i = 0; i < MAX_MOB_DB; i++)
  3630. if (mob_db_data[i])
  3631. {
  3632. memset(&mob_db_data[i]->skill,0,sizeof(mob_db_data[i]->skill));
  3633. mob_db_data[i]->maxskill=0;
  3634. }
  3635. mob_readskilldb();
  3636. mob_readdb_race();
  3637. }
  3638. void mob_clear_spawninfo()
  3639. { //Clears spawn related information for a script reload.
  3640. int i;
  3641. for (i = 0; i < MAX_MOB_DB; i++)
  3642. if (mob_db_data[i])
  3643. memset(&mob_db_data[i]->spawn,0,sizeof(mob_db_data[i]->spawn));
  3644. }
  3645. /*==========================================
  3646. * Circumference initialization of mob
  3647. *------------------------------------------*/
  3648. int do_init_mob(void)
  3649. { //Initialize the mob database
  3650. memset(mob_db_data,0,sizeof(mob_db_data)); //Clear the array
  3651. mob_db_data[0] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db)); //This mob is used for random spawns
  3652. mob_makedummymobdb(0); //The first time this is invoked, it creates the dummy mob
  3653. item_drop_ers = ers_new(sizeof(struct item_drop));
  3654. item_drop_list_ers = ers_new(sizeof(struct item_drop_list));
  3655. #ifndef TXT_ONLY
  3656. if(db_use_sqldbs)
  3657. mob_read_sqldb();
  3658. else
  3659. #endif /* TXT_ONLY */
  3660. mob_readdb();
  3661. mob_readdb_mobavail();
  3662. mob_read_randommonster();
  3663. mob_readskilldb();
  3664. mob_readdb_race();
  3665. add_timer_func_list(mob_delayspawn,"mob_delayspawn");
  3666. add_timer_func_list(mob_delay_item_drop,"mob_delay_item_drop");
  3667. add_timer_func_list(mob_ai_hard,"mob_ai_hard");
  3668. add_timer_func_list(mob_ai_lazy,"mob_ai_lazy");
  3669. add_timer_func_list(mob_timer_delete,"mob_timer_delete");
  3670. add_timer_func_list(mob_spawn_guardian_sub,"mob_spawn_guardian_sub");
  3671. add_timer_func_list(mob_respawn,"mob_respawn");
  3672. add_timer_interval(gettick()+MIN_MOBTHINKTIME,mob_ai_hard,0,0,MIN_MOBTHINKTIME);
  3673. add_timer_interval(gettick()+MIN_MOBTHINKTIME*10,mob_ai_lazy,0,0,MIN_MOBTHINKTIME*10);
  3674. return 0;
  3675. }
  3676. /*==========================================
  3677. * Clean memory usage.
  3678. *------------------------------------------*/
  3679. int do_final_mob(void)
  3680. {
  3681. int i;
  3682. if (mob_dummy)
  3683. {
  3684. aFree(mob_dummy);
  3685. mob_dummy = NULL;
  3686. }
  3687. for (i = 0; i <= MAX_MOB_DB; i++)
  3688. {
  3689. if (mob_db_data[i] != NULL)
  3690. {
  3691. aFree(mob_db_data[i]);
  3692. mob_db_data[i] = NULL;
  3693. }
  3694. }
  3695. ers_destroy(item_drop_ers);
  3696. ers_destroy(item_drop_list_ers);
  3697. return 0;
  3698. }