mob.c 122 KB

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