mob.c 124 KB

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