mob.c 119 KB

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