mob.c 126 KB

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