mob.c 124 KB

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