mob.c 125 KB

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