mob.c 130 KB

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