mob.c 138 KB

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