mob.c 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080
  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 "timer.h"
  10. #include "socket.h"
  11. #include "db.h"
  12. #include "nullpo.h"
  13. #include "malloc.h"
  14. #include "map.h"
  15. #include "clif.h"
  16. #include "intif.h"
  17. #include "pc.h"
  18. #include "status.h"
  19. #include "mob.h"
  20. #include "guild.h"
  21. #include "itemdb.h"
  22. #include "skill.h"
  23. #include "battle.h"
  24. #include "party.h"
  25. #include "npc.h"
  26. #include "log.h"
  27. #include "showmsg.h"
  28. #include "script.h"
  29. #include "atcommand.h"
  30. #include "date.h"
  31. #include "irc.h"
  32. #define MIN_MOBTHINKTIME 100
  33. #define MIN_MOBLINKTIME 1000
  34. #define MOB_LAZYSKILLPERC 10 // Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
  35. #define MOB_LAZYMOVEPERC 50 // Move probability in the negligent mode MOB (rate of 1000 minute)
  36. #define MOB_LAZYWARPPERC 20 // Warp probability in the negligent mode MOB (rate of 1000 minute)
  37. #define MOB_SLAVEDISTANCE 2 //Distance that slaves should keep from their master.
  38. //Dynamic mob database, allows saving of memory when there's big gaps in the mob_db [Skotlex]
  39. struct mob_db *mob_db_data[MAX_MOB_DB+1];
  40. struct mob_db *mob_dummy = NULL; //Dummy mob to be returned when a non-existant one is requested.
  41. 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]; }
  42. #define CLASSCHANGE_BOSS_NUM 21
  43. /*==========================================
  44. * Local prototype declaration (only required thing)
  45. *------------------------------------------
  46. */
  47. static int mob_makedummymobdb(int);
  48. static int mob_timer(int,unsigned int,int,int);
  49. static int mob_spawn_guardian_sub(int,unsigned int,int,int);
  50. int mobskill_use(struct mob_data *md,unsigned int tick,int event);
  51. int mobskill_deltimer(struct mob_data *md );
  52. int mob_skillid2skillidx(int class_,int skillid);
  53. int mobskill_use_id(struct mob_data *md,struct block_list *target,int skill_idx);
  54. /*==========================================
  55. * Mob is searched with a name.
  56. *------------------------------------------
  57. */
  58. int mobdb_searchname(const char *str)
  59. {
  60. int i;
  61. struct mob_db* mob;
  62. for(i=0;i<=MAX_MOB_DB;i++){
  63. mob = mob_db(i);
  64. if(mob == mob_dummy) //Skip dummy mobs.
  65. continue;
  66. if(strcmpi(mob->name,str)==0 || strcmp(mob->jname,str)==0 ||
  67. memcmp(mob->name,str,NAME_LENGTH)==0 || memcmp(mob->jname,str,NAME_LENGTH)==0)
  68. return i;
  69. }
  70. return 0;
  71. }
  72. /*==========================================
  73. * Id Mob is checked.
  74. *------------------------------------------
  75. */
  76. int mobdb_checkid(const int id)
  77. {
  78. if (mob_db(id) == mob_dummy)
  79. return 0;
  80. if (mob_is_clone(id)) //checkid is used mostly for random ID based code, therefore clone mobs are out of the question.
  81. return 0;
  82. return id;
  83. }
  84. /*==========================================
  85. * The minimum data set for MOB spawning
  86. *------------------------------------------
  87. */
  88. int mob_spawn_dataset(struct mob_data *md,const char *mobname,int class_)
  89. {
  90. nullpo_retr(0, md);
  91. md->bl.prev=NULL;
  92. md->bl.next=NULL;
  93. md->base_class = md->class_ = class_;
  94. md->db = mob_db(class_);
  95. if(strcmp(mobname,"--en--")==0)
  96. strncpy(md->name,md->db->name,NAME_LENGTH-1);
  97. else if(strcmp(mobname,"--ja--")==0)
  98. strncpy(md->name,md->db->jname,NAME_LENGTH-1);
  99. else
  100. strncpy(md->name,mobname,NAME_LENGTH-1);
  101. md->n = 0;
  102. md->bl.id= npc_get_new_npc_id();
  103. memset(&md->state,0,sizeof(md->state));
  104. md->timer = -1;
  105. md->target_id=0;
  106. md->attacked_id=0;
  107. md->attacked_count=0;
  108. md->speed=md->db->speed;
  109. return 0;
  110. }
  111. /*==========================================
  112. * Fetches a random mob_id [Skotlex]
  113. * type: Where to fetch from:
  114. * 0: dead branch list
  115. * 1: poring list
  116. * 2: bloody branch list
  117. * flag:
  118. * &1: Apply the summon success chance found in the list.
  119. * &2: Apply a monster check level.
  120. * lv: Mob level to check against
  121. *------------------------------------------
  122. */
  123. int mob_get_random_id(int type, int flag, int lv) {
  124. struct mob_db *mob;
  125. int i=0, k=0, class_;
  126. if(type < 0 || type >= MAX_RANDOMMONSTER) {
  127. if (battle_config.error_log)
  128. ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
  129. return 0;
  130. }
  131. do {
  132. class_ = rand() % MAX_MOB_DB;
  133. if (flag&1)
  134. k = rand() % 1000000;
  135. mob = mob_db(class_);
  136. } while ((mob == mob_dummy || mob->summonper[type] <= k ||
  137. (flag&2 && lv < mob->lv)) && (i++) < MAX_MOB_DB);
  138. if(i >= MAX_MOB_DB)
  139. class_ = mob_db_data[0]->summonper[type];
  140. return class_;
  141. }
  142. /*==========================================
  143. * The MOB appearance for one time (for scripts)
  144. *------------------------------------------
  145. */
  146. int mob_once_spawn (struct map_session_data *sd, char *mapname,
  147. int x, int y, const char *mobname, int class_, int amount, const char *event)
  148. {
  149. struct mob_data *md = NULL;
  150. int m, count, lv = 255;
  151. int i, j;
  152. if(sd) lv = sd->status.base_level;
  153. if(sd && strcmp(mapname,"this")==0)
  154. m = sd->bl.m;
  155. else
  156. m = map_mapname2mapid(mapname);
  157. if (m < 0 || amount <= 0 || (class_ >= 0 && class_ <= 1000) || class_ > MAX_MOB_DB + 2*MAX_MOB_DB) // 値が異常なら召喚を止める
  158. return 0;
  159. if (sd) { //even if the coords were wrong, spawn mob anyways (but look for most suitable coords first) Got from Freya [Lupus]
  160. if (x <= 0 || y <= 0) {
  161. if (x <= 0) x = sd->bl.x + rand() % 3 - 1;
  162. if (y <= 0) y = sd->bl.y + rand() % 3 - 1;
  163. if (map_getcell(m, x, y, CELL_CHKNOPASS)) {
  164. x = sd->bl.x;
  165. y = sd->bl.y;
  166. }
  167. }
  168. } else if (x <= 0 || y <= 0) {
  169. i = j = 0;
  170. do {
  171. x = rand() % (map[m].xs - 2) + 1;
  172. y = rand() % (map[m].ys - 2) + 1;
  173. } while ((i = map_getcell(m, x, y, CELL_CHKNOPASS)) && j++ < 64);
  174. if (i) { // not solved?
  175. x = 0;
  176. y = 0;
  177. }
  178. }
  179. for (count = 0; count < amount; count++) {
  180. md = (struct mob_data *)aCalloc(1,sizeof(struct mob_data));
  181. if (class_ > 2*MAX_MOB_DB) { // large/tiny mobs [Valaris]
  182. md->special_state.size = 2;
  183. class_ -= 2*MAX_MOB_DB;
  184. } else if (class_ > MAX_MOB_DB) {
  185. md->special_state.size = 1;
  186. class_ -= MAX_MOB_DB;
  187. }
  188. if (class_ < 0) {
  189. class_ = mob_get_random_id(-class_ -1, battle_config.random_monster_checklv?3:1, lv);
  190. if (!class_) {
  191. aFree(md);
  192. return 0;
  193. }
  194. if (battle_config.dead_branch_active)
  195. //Behold Aegis's masterful decisions yet again...
  196. //"I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" - Poki#3
  197. md->mode = mob_db(class_)->mode|MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE;
  198. }
  199. if(mob_db(class_)->mode & MD_LOOTER)
  200. md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  201. mob_spawn_dataset (md, mobname, class_);
  202. md->bl.m = m;
  203. md->bl.x = x;
  204. md->bl.y = y;
  205. md->m = m;
  206. md->x0 = x;
  207. md->y0 = y;
  208. //md->xs = 0;
  209. //md->ys = 0;
  210. md->spawndelay1 = -1; // 一度のみフラグ
  211. md->spawndelay2 = -1; // 一度のみフラグ
  212. //better safe than sorry, current md->npc_event has a size of 50
  213. if (strlen(event) < 50)
  214. memcpy(md->npc_event, event, strlen(event));
  215. md->bl.type = BL_MOB;
  216. map_addiddb (&md->bl);
  217. mob_spawn (md->bl.id);
  218. if(class_ == MOBID_EMPERIUM) { // emperium hp based on defense level [Valaris]
  219. struct guild_castle *gc = guild_mapname2gc(map[md->bl.m].name);
  220. struct guild *g = gc?guild_search(gc->guild_id):NULL;
  221. if(gc) {
  222. md->max_hp += 2000 * gc->defense;
  223. md->hp = md->max_hp;
  224. md->guardian_data = aCalloc(1, sizeof(struct guardian_data));
  225. md->guardian_data->castle = gc;
  226. md->guardian_data->number = MAX_GUARDIANS;
  227. md->guardian_data->guild_id = gc->guild_id;
  228. if (g)
  229. {
  230. md->guardian_data->emblem_id = g->emblem_id;
  231. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  232. }
  233. else if (gc->guild_id) //Guild not yet available, retry in 5.
  234. add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
  235. }
  236. } // end addition [Valaris]
  237. }
  238. return (amount > 0) ? md->bl.id : 0;
  239. }
  240. /*==========================================
  241. * The MOB appearance for one time (& area specification for scripts)
  242. *------------------------------------------
  243. */
  244. int mob_once_spawn_area(struct map_session_data *sd,char *mapname,
  245. int x0,int y0,int x1,int y1,
  246. const char *mobname,int class_,int amount,const char *event)
  247. {
  248. int x,y,i,max,lx=-1,ly=-1,id=0;
  249. int m;
  250. if(strcmp(mapname,"this")==0)
  251. m=sd->bl.m;
  252. else
  253. m=map_mapname2mapid(mapname);
  254. max=(y1-y0+1)*(x1-x0+1)*3;
  255. if(max>1000)max=1000;
  256. if (m < 0 || amount <= 0 || (class_ >= 0 && class_ <= 1000) || class_ > MAX_MOB_DB + 2*MAX_MOB_DB) // 値が異常なら召喚を止める
  257. return 0;
  258. for(i=0;i<amount;i++){
  259. int j=0;
  260. do{
  261. x=rand()%(x1-x0+1)+x0;
  262. y=rand()%(y1-y0+1)+y0;
  263. } while (map_getcell(m,x,y,CELL_CHKNOPASS) && (++j)<max);
  264. // freya }while( ( (c=map_getcell(m,x,y))==1 || c==5)&& (++j)<max );
  265. if(j>=max){
  266. if(lx>=0){ // Since reference went wrong, the place which boiled before is used.
  267. x=lx;
  268. y=ly;
  269. }else
  270. return 0; // Since reference of the place which boils first went wrong, it stops.
  271. }
  272. 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);
  273. id=mob_once_spawn(sd,mapname,x,y,mobname,class_,1,event);
  274. lx=x;
  275. ly=y;
  276. }
  277. return id;
  278. }
  279. /*==========================================
  280. * Set a Guardian's guild data [Skotlex]
  281. *------------------------------------------
  282. */
  283. static int mob_spawn_guardian_sub(int tid,unsigned int tick,int id,int data)
  284. { //Needed because the guild_data may not be available at guardian spawn time.
  285. struct block_list* bl = map_id2bl(id);
  286. struct mob_data* md;
  287. struct guild* g;
  288. if (bl == NULL) //It is possible mob was already removed from map when the castle has no owner. [Skotlex]
  289. return 0;
  290. if (bl->type != BL_MOB || (md = (struct mob_data*)bl) == NULL)
  291. {
  292. ShowError("mob_spawn_guardian_sub: Block error!\n");
  293. return 0;
  294. }
  295. nullpo_retr(0, md->guardian_data);
  296. g = guild_search(data);
  297. if (g == NULL)
  298. { //Liberate castle, if the guild is not found this is an error! [Skotlex]
  299. ShowError("mob_spawn_guardian_sub: Couldn't load guild %d!\n",data);
  300. if (md->class_ == MOBID_EMPERIUM)
  301. { //Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
  302. md->guardian_data->guild_id = 0;
  303. if (md->guardian_data->castle->guild_id) //Free castle up.
  304. {
  305. ShowNotice("Clearing ownership of castle %d (%s)\n", md->guardian_data->castle->castle_id, md->guardian_data->castle->castle_name);
  306. guild_castledatasave(md->guardian_data->castle->castle_id, 1, 0);
  307. }
  308. } else {
  309. if (md->guardian_data->castle->guardian[md->guardian_data->number].visible)
  310. { //Safe removal of guardian.
  311. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  312. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  313. guild_castledatasave(md->guardian_data->castle->castle_id, 18+md->guardian_data->number,0);
  314. }
  315. mob_delete(md); //Remove guardian.
  316. }
  317. return 0;
  318. }
  319. md->guardian_data->emblem_id = g->emblem_id;
  320. memcpy (md->guardian_data->guild_name, g->name, NAME_LENGTH);
  321. md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
  322. return 0;
  323. }
  324. /*==========================================
  325. * Summoning Guardians [Valaris]
  326. *------------------------------------------
  327. */
  328. int mob_spawn_guardian(struct map_session_data *sd,char *mapname,
  329. int x,int y,const char *mobname,int class_,int amount,const char *event,int guardian)
  330. {
  331. struct mob_data *md=NULL;
  332. struct guild *g=NULL;
  333. struct guild_castle *gc;
  334. int m,count=1;
  335. if( sd && strcmp(mapname,"this")==0)
  336. m=sd->bl.m;
  337. else
  338. m=map_mapname2mapid(mapname);
  339. if(m<0 || amount<=0 || (class_>=0 && class_<=1000) || class_>MAX_MOB_DB) // Invalid monster classes
  340. return 0;
  341. if(class_<0)
  342. return 0;
  343. if(guardian < 0 || guardian >= MAX_GUARDIANS)
  344. {
  345. ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d (castle map %s)\n", guardian, class_, map[m].name);
  346. return 0;
  347. }
  348. if (amount > 1)
  349. ShowWarning("mob_spawn_guardian: Spawning %d guardians in position %d (castle map %s)\n", amount, map[m].name);
  350. if(sd){
  351. if(x<=0) x=sd->bl.x;
  352. if(y<=0) y=sd->bl.y;
  353. }
  354. else if(x<=0 || y<=0)
  355. ShowWarning("mob_spawn_guardian: Invalid coordinates (%d,%d)\n",x,y);
  356. gc=guild_mapname2gc(map[m].name);
  357. if (gc == NULL)
  358. {
  359. ShowError("mob_spawn_guardian: No castle set at map %s\n", map[m].name);
  360. return 0;
  361. }
  362. if (!gc->guild_id)
  363. ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle with no guild (castle map %s)\n", class_, map[m].name);
  364. else
  365. g = guild_search(gc->guild_id);
  366. if (gc->guardian[guardian].id)
  367. ShowWarning("mob_spawn_guardian: Spawning guardian in position %d which already has a guardian (castle map %s)\n", guardian, map[m].name);
  368. for(count=0;count<amount;count++){
  369. md=(struct mob_data *) aCalloc(1, sizeof(struct mob_data));
  370. mob_spawn_dataset(md,mobname,class_);
  371. md->bl.m=m;
  372. md->bl.x=x;
  373. md->bl.y=y;
  374. md->m =m;
  375. md->x0=x;
  376. md->y0=y;
  377. md->xs=0;
  378. md->ys=0;
  379. md->spawndelay1=-1; // Only once is a flag.
  380. md->spawndelay2=-1; // Only once is a flag.
  381. //better safe than sorry, current md->npc_event has a size of 50 [Skotlex]
  382. if (strlen(event) < 50)
  383. memcpy(md->npc_event, event, strlen(event));
  384. md->bl.type=BL_MOB;
  385. map_addiddb(&md->bl);
  386. mob_spawn(md->bl.id);
  387. md->max_hp += 2000 * gc->defense;
  388. md->guardian_data = aCalloc(1, sizeof(struct guardian_data));
  389. md->guardian_data->number = guardian;
  390. md->guardian_data->guild_id = gc->guild_id;
  391. md->guardian_data->castle = gc;
  392. md->hp = gc->guardian[guardian].hp;
  393. gc->guardian[guardian].id = md->bl.id;
  394. if (g)
  395. {
  396. md->guardian_data->emblem_id = g->emblem_id;
  397. memcpy (md->guardian_data->guild_name, g->name, NAME_LENGTH);
  398. md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
  399. } else if (md->guardian_data->guild_id)
  400. add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
  401. }
  402. return (amount>0)?md->bl.id:0;
  403. }
  404. /*==========================================
  405. * Is MOB in the state in which the present movement is possible or not?
  406. *------------------------------------------
  407. */
  408. int mob_can_move(struct mob_data *md)
  409. {
  410. nullpo_retr(0, md);
  411. if(DIFF_TICK(md->canmove_tick, gettick()) > 0 || md->skilltimer != -1 || (md->sc.opt1 > 0 && md->sc.opt1 != OPT1_STONEWAIT) || md->sc.option&OPTION_HIDE)
  412. return 0;
  413. // アンクル中で動けないとか
  414. if(md->sc.count && (
  415. md->sc.data[SC_ANKLE].timer != -1 || //アンクルスネア
  416. md->sc.data[SC_AUTOCOUNTER].timer != -1 || //オートカウンター
  417. md->sc.data[SC_BLADESTOP].timer != -1 || //白刃取り
  418. md->sc.data[SC_SPIDERWEB].timer != -1 || //スパイダーウェッブ
  419. (md->sc.data[SC_DANCING].timer !=-1 && md->sc.data[SC_DANCING].val1 == CG_HERMODE) || //cannot move while Hermod is active.
  420. (md->sc.data[SC_GOSPEL].timer !=-1 && md->sc.data[SC_GOSPEL].val4 == BCT_SELF) || // cannot move while gospel is in effect
  421. md->sc.data[SC_STOP].timer != -1 ||
  422. md->sc.data[SC_CLOSECONFINE].timer != -1 ||
  423. md->sc.data[SC_CLOSECONFINE2].timer != -1
  424. ))
  425. return 0;
  426. return 1;
  427. }
  428. /*==========================================
  429. * Time calculation concerning one step next to mob
  430. *------------------------------------------
  431. */
  432. static int calc_next_walk_step(struct mob_data *md)
  433. {
  434. nullpo_retr(0, md);
  435. if(md->walkpath.path_pos>=md->walkpath.path_len)
  436. return -1;
  437. if(md->walkpath.path[md->walkpath.path_pos]&1)
  438. return status_get_speed(&md->bl)*14/10;
  439. return status_get_speed(&md->bl);
  440. }
  441. static int mob_walktoxy_sub(struct mob_data *md);
  442. /*==========================================
  443. * Mob Walk processing
  444. *------------------------------------------
  445. */
  446. static int mob_walk(struct mob_data *md,unsigned int tick,int data)
  447. {
  448. int moveblock;
  449. int i;
  450. static int dirx[8]={0,-1,-1,-1,0,1,1,1};
  451. static int diry[8]={1,1,0,-1,-1,-1,0,1};
  452. int x,y,dx,dy;
  453. nullpo_retr(0, md);
  454. md->state.state=MS_IDLE;
  455. if(md->walkpath.path_pos>=md->walkpath.path_len || md->walkpath.path_pos!=data)
  456. return 0;
  457. md->walkpath.path_half ^= 1;
  458. if(md->walkpath.path_half==0){
  459. md->walkpath.path_pos++;
  460. if(md->state.change_walk_target){
  461. mob_walktoxy_sub(md);
  462. return 0;
  463. }
  464. }
  465. else {
  466. if(md->walkpath.path[md->walkpath.path_pos]>=8)
  467. return 1;
  468. x = md->bl.x;
  469. y = md->bl.y;
  470. #ifndef CELL_NOSTACK
  471. if(map_getcell(md->bl.m,x,y,CELL_CHKNOPASS)) {
  472. mob_stop_walking(md,1);
  473. return 0;
  474. }
  475. #endif
  476. md->dir=md->walkpath.path[md->walkpath.path_pos];
  477. dx = dirx[md->dir];
  478. dy = diry[md->dir];
  479. if (map_getcell(md->bl.m,x+dx,y+dy,CELL_CHKBASILICA) && !(status_get_mode(&md->bl)&MD_BOSS)) {
  480. mob_stop_walking(md,1);
  481. return 0;
  482. }
  483. if (map_getcell(md->bl.m,x+dx,y+dy,CELL_CHKNOPASS)) {
  484. mob_walktoxy_sub(md);
  485. return 0;
  486. }
  487. moveblock = ( x/BLOCK_SIZE != (x+dx)/BLOCK_SIZE || y/BLOCK_SIZE != (y+dy)/BLOCK_SIZE);
  488. md->state.state=MS_WALK;
  489. map_foreachinmovearea(clif_moboutsight,md->bl.m,x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,dx,dy,BL_PC,md);
  490. if ( md->min_chase > md->db->range2)
  491. md->min_chase--;
  492. x += dx;
  493. y += dy;
  494. map_moveblock(&md->bl, x, y, tick);
  495. map_foreachinmovearea(clif_mobinsight,md->bl.m,x-AREA_SIZE,y-AREA_SIZE,x+AREA_SIZE,y+AREA_SIZE,-dx,-dy,BL_PC,md);
  496. md->state.state=MS_IDLE;
  497. }
  498. if((i=calc_next_walk_step(md))>0){
  499. i = i>>1;
  500. if(i < 1 && md->walkpath.path_half == 0)
  501. i = 1;
  502. if(md->walkpath.path_pos>=md->walkpath.path_len)
  503. clif_fixmobpos(md); // とまったときに位置の再送信
  504. else {
  505. md->timer=add_timer(tick+i,mob_timer,md->bl.id,md->walkpath.path_pos);
  506. md->state.state=MS_WALK;
  507. }
  508. }
  509. return 0;
  510. }
  511. /*==========================================
  512. * Reachability to a Specification ID existence place
  513. * state indicates type of 'seek' mob should do:
  514. * - MSS_LOOT: Looking for item, path must be easy.
  515. * - MSS_RUSH: Chasing attacking player, path is determined by mob_ai&1
  516. * - MSS_FOLLOW: Initiative/support seek, path must be easy.
  517. *------------------------------------------
  518. */
  519. int mob_can_reach(struct mob_data *md,struct block_list *bl,int range, int state)
  520. {
  521. int dx,dy;
  522. struct walkpath_data wpd;
  523. int i, easy = 0;
  524. nullpo_retr(0, md);
  525. nullpo_retr(0, bl);
  526. switch (state) {
  527. case MSS_RUSH:
  528. easy = (battle_config.mob_ai&1?0:1);
  529. break;
  530. case MSS_LOOT:
  531. case MSS_FOLLOW:
  532. default:
  533. easy = 1;
  534. break;
  535. }
  536. if( md->bl.m != bl->m) // 違うャbプ
  537. return 0;
  538. if( md->bl.x==bl->x && md->bl.y==bl->y ) // 同じマス
  539. return 1;
  540. if( range>0 && !check_distance_bl(&md->bl, bl, range))
  541. return 0;
  542. // Obstacle judging
  543. wpd.path_len=0;
  544. wpd.path_pos=0;
  545. wpd.path_half=0;
  546. if(path_search(&wpd,md->bl.m,md->bl.x,md->bl.y,bl->x,bl->y,easy)!=-1)
  547. return 1;
  548. // It judges whether it can adjoin or not.
  549. dx=abs(bl->x - md->bl.x);
  550. dy=abs(bl->y - md->bl.y);
  551. dx=(dx>0)?1:((dx<0)?-1:0);
  552. dy=(dy>0)?1:((dy<0)?-1:0);
  553. if(path_search(&wpd,md->bl.m,md->bl.x,md->bl.y,bl->x-dx,bl->y-dy,easy)!=-1)
  554. return 1;
  555. for(i=0;i<9;i++){
  556. if(path_search(&wpd,md->bl.m,md->bl.x,md->bl.y,bl->x-1+i/3,bl->y-1+i%3,easy)!=-1)
  557. return 1;
  558. }
  559. return 0;
  560. }
  561. /*==========================================
  562. * Links nearby mobs (supportive mobs)
  563. *------------------------------------------
  564. */
  565. static int mob_linksearch(struct block_list *bl,va_list ap)
  566. {
  567. struct mob_data *md;
  568. int class_;
  569. struct block_list *target;
  570. unsigned int tick;
  571. nullpo_retr(0, bl);
  572. nullpo_retr(0, ap);
  573. md=(struct mob_data *)bl;
  574. class_ = va_arg(ap, int);
  575. target = va_arg(ap, struct block_list *);
  576. tick=va_arg(ap, unsigned int);
  577. if (md->class_ == class_ && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME
  578. && (!md->target_id || md->state.targettype == NONE_ATTACKABLE))
  579. {
  580. md->last_linktime = tick;
  581. if( mob_can_reach(md,target,md->db->range2, MSS_FOLLOW) ){ // Reachability judging
  582. md->target_id = target->id;
  583. md->attacked_count = 0;
  584. md->state.targettype = ATTACKABLE;
  585. md->state.aggressive = (status_get_mode(&md->bl)&MD_ANGRY)?1:0;
  586. md->min_chase=md->db->range3;
  587. return 1;
  588. }
  589. }
  590. return 0;
  591. }
  592. /*==========================================
  593. * Attack processing of mob
  594. *------------------------------------------
  595. */
  596. static int mob_attack(struct mob_data *md,unsigned int tick,int data)
  597. {
  598. struct block_list *tbl=NULL;
  599. int range;
  600. nullpo_retr(0, md);
  601. md->min_chase=md->db->range3;
  602. md->state.state=MS_IDLE;
  603. md->state.skillstate=MSS_IDLE;
  604. if( md->skilltimer!=-1 ) // スキル使用中
  605. return 0;
  606. if((tbl=map_id2bl(md->target_id))==NULL || !status_check_skilluse(&md->bl, tbl, 0, 0)){
  607. md->target_id=0;
  608. md->state.targettype = NONE_ATTACKABLE;
  609. return 0;
  610. }
  611. if (!check_distance_bl(&md->bl, tbl, md->db->range3)){
  612. mob_stopattack(md);
  613. return 0;
  614. }
  615. range = md->db->range;
  616. if (range <= 3)
  617. range++; //Melee attackers get a bonus range cell when attacking.
  618. /* It seems mobs always teleport the last two tiles when chasing players, so do not give them this bonus range tile.[Skotlex]
  619. if(battle_iswalking(tbl)) range++;
  620. */
  621. if(!check_distance_bl(&md->bl, tbl, range))
  622. return 0;
  623. if(battle_config.monster_attack_direction_change)
  624. md->dir=map_calc_dir(&md->bl, tbl->x,tbl->y ); // 向き設定
  625. if (status_get_mode(&md->bl)&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME)
  626. { // Link monsters nearby [Skotlex]
  627. md->last_linktime = tick;
  628. map_foreachinarea(mob_linksearch, md->bl.m,
  629. md->bl.x-md->db->range2, md->bl.y-md->db->range2,
  630. md->bl.x+md->db->range2, md->bl.y+md->db->range2,
  631. BL_MOB, md->class_, tbl, tick);
  632. }
  633. md->state.skillstate=md->state.aggressive?MSS_ANGRY:MSS_BERSERK;
  634. if( mobskill_use(md,tick,-1) ) // スキル使用
  635. return 0;
  636. if(md->sc.count && md->sc.data[SC_WINKCHARM].timer != -1)
  637. clif_emotion(&md->bl, 3);
  638. else
  639. md->target_lv = battle_weapon_attack(&md->bl,tbl,tick,0);
  640. if(!(battle_config.monster_cloak_check_type&2) && md->sc.data[SC_CLOAKING].timer != -1)
  641. status_change_end(&md->bl,SC_CLOAKING,-1);
  642. //Mobs can't move if they can't attack neither.
  643. //Use the attack delay for next can attack try
  644. //But use the attack motion to know when it can start moving. [Skotlex]
  645. md->attackabletime = tick + status_get_adelay(&md->bl);
  646. md->canmove_tick = tick + status_get_amotion(&md->bl);
  647. md->timer=add_timer(md->attackabletime,mob_timer,md->bl.id,0);
  648. md->state.state=MS_ATTACK;
  649. return 0;
  650. }
  651. /*==========================================
  652. * The attack of PC which is attacking id is stopped.
  653. * The callback function of clif_foreachclient
  654. *------------------------------------------
  655. */
  656. int mob_stopattacked(struct map_session_data *sd,va_list ap)
  657. {
  658. int id;
  659. nullpo_retr(0, sd);
  660. nullpo_retr(0, ap);
  661. id=va_arg(ap,int);
  662. if(sd->attacktarget==id)
  663. pc_stopattack(sd);
  664. return 0;
  665. }
  666. /*==========================================
  667. * The timer in which the mob's states changes
  668. *------------------------------------------
  669. */
  670. int mob_changestate(struct mob_data *md,int state,int type)
  671. {
  672. unsigned int tick;
  673. int i;
  674. nullpo_retr(0, md);
  675. if(md->timer != -1)
  676. delete_timer(md->timer,mob_timer);
  677. md->timer=-1;
  678. md->state.state=state;
  679. switch(state){
  680. case MS_WALK:
  681. if((i=calc_next_walk_step(md))>0){
  682. i = i>>2;
  683. md->timer=add_timer(gettick()+i,mob_timer,md->bl.id,0);
  684. }
  685. else
  686. md->state.state=MS_IDLE;
  687. break;
  688. case MS_ATTACK:
  689. tick = gettick();
  690. i=DIFF_TICK(md->attackabletime,tick);
  691. if(i>0 && i<2000)
  692. md->timer=add_timer(md->attackabletime,mob_timer,md->bl.id,0);
  693. else if(type) {
  694. md->attackabletime = tick + status_get_amotion(&md->bl);
  695. md->timer=add_timer(md->attackabletime,mob_timer,md->bl.id,0);
  696. }
  697. else {
  698. md->attackabletime = tick + 1;
  699. md->timer=add_timer(md->attackabletime,mob_timer,md->bl.id,0);
  700. }
  701. break;
  702. case MS_DELAY:
  703. md->timer=add_timer(gettick()+type,mob_timer,md->bl.id,0);
  704. break;
  705. case MS_DEAD:
  706. skill_castcancel(&md->bl,0);
  707. // mobskill_deltimer(md);
  708. md->state.skillstate=MSS_DEAD;
  709. md->last_deadtime=gettick();
  710. // Since it died, all aggressors' attack to this mob is stopped.
  711. clif_foreachclient(mob_stopattacked,md->bl.id);
  712. skill_unit_move(&md->bl,gettick(),4);
  713. status_change_clear(&md->bl,2); // ステータス異常を解除する
  714. skill_clear_unitgroup(&md->bl); // 全てのスキルユニットグループを削除する
  715. skill_cleartimerskill(&md->bl);
  716. if(md->deletetimer!=-1)
  717. delete_timer(md->deletetimer,mob_timer_delete);
  718. md->deletetimer=-1;
  719. md->hp = md->target_id = md->attacked_id = md->attacked_count = 0;
  720. md->state.targettype = NONE_ATTACKABLE;
  721. break;
  722. }
  723. return 0;
  724. }
  725. /*==========================================
  726. * timer processing of mob (timer function)
  727. * It branches to a walk and an attack.
  728. *------------------------------------------
  729. */
  730. static int mob_timer(int tid,unsigned int tick,int id,int data)
  731. {
  732. struct mob_data *md;
  733. struct block_list *bl;
  734. if( (bl=map_id2bl(id)) == NULL ){ //攻撃してきた敵がもういないのは正常のようだ
  735. return 1;
  736. }
  737. if(!bl || !bl->type || bl->type!=BL_MOB)
  738. return 1;
  739. nullpo_retr(1, md=(struct mob_data*)bl);
  740. if(md->timer != tid){
  741. if(battle_config.error_log)
  742. ShowError("mob_timer %d != %d\n",md->timer,tid);
  743. return 0;
  744. }
  745. md->timer=-1;
  746. if(md->bl.prev == NULL || md->state.state == MS_DEAD)
  747. return 1;
  748. map_freeblock_lock();
  749. switch(md->state.state){
  750. case MS_WALK:
  751. mob_walk(md,tick,data);
  752. break;
  753. case MS_ATTACK:
  754. mob_attack(md,tick,data);
  755. break;
  756. case MS_DELAY:
  757. mob_changestate(md,MS_IDLE,0);
  758. break;
  759. default:
  760. if(battle_config.error_log)
  761. ShowError("mob_timer : %d ?\n",md->state.state);
  762. break;
  763. }
  764. if (md->timer == -1)
  765. mob_changestate(md,MS_WALK,0);
  766. map_freeblock_unlock();
  767. return 0;
  768. }
  769. /*==========================================
  770. *
  771. *------------------------------------------
  772. */
  773. static int mob_walktoxy_sub(struct mob_data *md)
  774. {
  775. struct walkpath_data wpd;
  776. int x,y;
  777. static int dirx[8]={0,-1,-1,-1,0,1,1,1};
  778. static int diry[8]={1,1,0,-1,-1,-1,0,1};
  779. nullpo_retr(0, md);
  780. memset(&wpd, 0, sizeof(wpd));
  781. if(path_search(&wpd,md->bl.m,md->bl.x,md->bl.y,md->to_x,md->to_y,md->state.walk_easy))
  782. return 1;
  783. if (wpd.path[0] >= 8)
  784. return 1;
  785. x = md->bl.x+dirx[wpd.path[0]];
  786. y = md->bl.y+diry[wpd.path[0]];
  787. if (map_getcell(md->bl.m,x,y,CELL_CHKBASILICA) && !(status_get_mode(&md->bl)&MD_BOSS)) {
  788. md->state.change_walk_target=0;
  789. return 1;
  790. }
  791. memcpy(&md->walkpath,&wpd,sizeof(wpd));
  792. md->state.change_walk_target=0;
  793. mob_changestate(md,MS_WALK,0);
  794. clif_movemob(md);
  795. return 0;
  796. }
  797. /*==========================================
  798. * mob move start
  799. *------------------------------------------
  800. */
  801. int mob_walktoxy(struct mob_data *md,int x,int y,int easy)
  802. {
  803. struct walkpath_data wpd;
  804. nullpo_retr(0, md);
  805. if(md->bl.prev == NULL || md->state.state == MS_DEAD) //Just-in-case check to prevent dead mobs from moving. [Skotlex]
  806. return 1;
  807. if(md->state.state == MS_WALK && path_search(&wpd,md->bl.m,md->bl.x,md->bl.y,x,y,easy) )
  808. return 1;
  809. md->state.walk_easy = easy;
  810. md->to_x=x;
  811. md->to_y=y;
  812. if (md->sc.data[SC_CONFUSION].timer != -1) //Randomize target direction.
  813. map_random_dir(&md->bl, &md->to_x, &md->to_y);
  814. if(md->state.state == MS_WALK)
  815. md->state.change_walk_target=1;
  816. else
  817. return mob_walktoxy_sub(md);
  818. return 0;
  819. }
  820. /*==========================================
  821. * mob spawn with delay (timer function)
  822. *------------------------------------------
  823. */
  824. static int mob_delayspawn(int tid, unsigned int tick, int m, int n)
  825. {
  826. mob_spawn(m);
  827. return 0;
  828. }
  829. /*==========================================
  830. * spawn timing calculation
  831. *------------------------------------------
  832. */
  833. int mob_setdelayspawn(int id)
  834. {
  835. unsigned int spawntime, spawntime1, spawntime2, spawntime3;
  836. struct mob_data *md;
  837. struct block_list *bl;
  838. if ((bl = map_id2bl(id)) == NULL || bl->type != BL_MOB)
  839. return -1;
  840. nullpo_retr(-1, md = (struct mob_data*)bl);
  841. // Processing of MOB which is not revitalized
  842. if (md->spawndelay1 == -1 && md->spawndelay2 == -1 && md->n == 0) {
  843. if (md->lootitem) {
  844. aFree(md->lootitem);
  845. md->lootitem = NULL;
  846. }
  847. if (md->guardian_data)
  848. {
  849. if (md->guardian_data->number < MAX_GUARDIANS)
  850. md->guardian_data->castle->guardian[md->guardian_data->number].id = 0;
  851. aFree (md->guardian_data);
  852. md->guardian_data = NULL;
  853. }
  854. map_deliddb(&md->bl);
  855. map_delblock(bl); //In case it wasn't done before invoking the function.
  856. map_freeblock(bl);
  857. return 0;
  858. }
  859. spawntime1 = md->last_spawntime + md->spawndelay1;
  860. spawntime2 = md->last_deadtime + md->spawndelay2;
  861. spawntime3 = gettick() + 5000 + rand()%5000; //Lupus
  862. // spawntime = max(spawntime1,spawntime2,spawntime3);
  863. if (DIFF_TICK(spawntime1, spawntime2) > 0)
  864. spawntime = spawntime1;
  865. else
  866. spawntime = spawntime2;
  867. if (DIFF_TICK(spawntime3, spawntime) > 0)
  868. spawntime = spawntime3;
  869. add_timer(spawntime, mob_delayspawn, id, 0);
  870. return 0;
  871. }
  872. static int mob_count_sub(struct block_list *bl,va_list ap)
  873. {
  874. return 1;
  875. }
  876. /*==========================================
  877. * Mob spawning. Initialization is also variously here.
  878. *------------------------------------------
  879. */
  880. int mob_spawn (int id)
  881. {
  882. int x, y, i = 0;
  883. unsigned int c, tick = gettick();
  884. struct mob_data *md;
  885. struct block_list *bl;
  886. if ((bl = map_id2bl(id)) == NULL || bl->type != BL_MOB)
  887. return -1;
  888. nullpo_retr(-1, md = (struct mob_data*)bl);
  889. md->last_spawntime = tick;
  890. if (md->bl.prev != NULL)
  891. map_delblock(&md->bl);
  892. else {
  893. if(md->class_ != md->base_class){ // クラスチェンジしたMob
  894. md->class_ = md->base_class;
  895. md->db = mob_db(md->base_class);
  896. memcpy(md->name,md->db->jname,NAME_LENGTH);
  897. md->speed=md->db->speed;
  898. }
  899. }
  900. md->bl.m = md->m;
  901. do {
  902. if (md->x0 == 0 && md->y0 == 0) {
  903. x = rand()%(map[md->bl.m].xs-2)+1;
  904. y = rand()%(map[md->bl.m].ys-2)+1;
  905. } else {
  906. x = md->x0+rand()%(md->xs+1)-md->xs/2;
  907. y = md->y0+rand()%(md->ys+1)-md->ys/2;
  908. }
  909. i++;
  910. if (battle_config.no_spawn_on_player && i <= battle_config.no_spawn_on_player)
  911. { //Avoid spawning on the view-range of players. [Skotlex]
  912. if (map_foreachinarea(mob_count_sub, md->bl.m,
  913. x-AREA_SIZE, y-AREA_SIZE, x+AREA_SIZE, y+AREA_SIZE,
  914. BL_PC) > 0)
  915. continue;
  916. }
  917. } while(map_getcell(md->bl.m,x,y,CELL_CHKNOPASS) && i < 50);
  918. if (i >= 50) {
  919. if (md->spawndelay1 != -1 || md->spawndelay2 == -1)
  920. // retry again later
  921. add_timer(tick+5000,mob_delayspawn,id,0);
  922. return 1;
  923. }
  924. md->to_x = md->bl.x = x;
  925. md->to_y = md->bl.y = y;
  926. md->dir = 0;
  927. md->target_dir = 0;
  928. memset(&md->state, 0, sizeof(md->state));
  929. md->attacked_id = 0;
  930. md->attacked_count = 0;
  931. md->target_id = 0;
  932. md->mode = 0;
  933. md->move_fail_count = 0;
  934. if (!md->speed)
  935. md->speed = md->db->speed;
  936. md->def_ele = md->db->element;
  937. if (!md->level) // [Valaris]
  938. md->level=md->db->lv;
  939. md->master_id = 0;
  940. md->master_dist = 0;
  941. md->state.state = MS_IDLE;
  942. md->state.skillstate = MSS_IDLE;
  943. md->timer = -1;
  944. md->last_thinktime = tick;
  945. md->next_walktime = tick+rand()%50+5000;
  946. md->attackabletime = tick;
  947. md->canmove_tick = tick;
  948. md->last_linktime = tick;
  949. /* Guardians should be spawned using mob_spawn_guardian! [Skotlex]
  950. * and the Emperium is spawned using mob_once_spawn.
  951. md->guild_id = 0;
  952. if (md->class_ >= 1285 && md->class_ <= 1288) {
  953. struct guild_castle *gc=guild_mapname2gc(map[md->bl.m].name);
  954. if(gc)
  955. md->guild_id = gc->guild_id;
  956. }
  957. */
  958. md->deletetimer = -1;
  959. md->skilltimer = -1;
  960. for (i = 0, c = tick-1000*3600*10; i < MAX_MOBSKILL; i++)
  961. md->skilldelay[i] = c;
  962. md->skillid = 0;
  963. md->skilllv = 0;
  964. memset(md->dmglog, 0, sizeof(md->dmglog));
  965. md->tdmg = 0;
  966. if (md->lootitem)
  967. memset(md->lootitem, 0, sizeof(md->lootitem));
  968. md->lootitem_count = 0;
  969. for (i = 0; i < MAX_MOBSKILLTIMERSKILL; i++)
  970. md->skilltimerskill[i].timer = -1;
  971. for (i = 0; i < MAX_STATUSCHANGE; i++) {
  972. md->sc.data[i].timer = -1;
  973. md->sc.data[i].val1 = md->sc.data[i].val2 = md->sc.data[i].val3 = md->sc.data[i].val4 = 0;
  974. }
  975. md->sc.count = 0;
  976. md->sc.opt1 = md->sc.opt2 = md->sc.opt3 = md->sc.option = 0;
  977. if(md->db->option){ // Added for carts, falcons and pecos for cloned monsters. [Valaris]
  978. if(md->db->option & 0x0008)
  979. md->sc.option |= 0x0008;
  980. if(md->db->option & 0x0080)
  981. md->sc.option |= 0x0080;
  982. if(md->db->option & 0x0100)
  983. md->sc.option |= 0x0100;
  984. if(md->db->option & 0x0200)
  985. md->sc.option |= 0x0200;
  986. if(md->db->option & 0x0400)
  987. md->sc.option |= 0x0400;
  988. if(md->db->option & OPTION_FALCON)
  989. md->sc.option |= OPTION_FALCON;
  990. if(md->db->option & OPTION_RIDING)
  991. md->sc.option |= OPTION_RIDING;
  992. }
  993. memset(md->skillunit, 0, sizeof(md->skillunit));
  994. memset(md->skillunittick, 0, sizeof(md->skillunittick));
  995. md->max_hp = md->db->max_hp;
  996. if(md->special_state.size==1) // change for sized monsters [Valaris]
  997. md->max_hp/=2;
  998. else if(md->special_state.size==2)
  999. md->max_hp*=2;
  1000. md->hp = md->max_hp;
  1001. map_addblock(&md->bl);
  1002. skill_unit_move(&md->bl,tick,1);
  1003. clif_spawnmob(md);
  1004. mobskill_use(md, tick, MSC_SPAWN);
  1005. return 0;
  1006. }
  1007. /*==========================================
  1008. * The stop of MOB's attack
  1009. *------------------------------------------
  1010. */
  1011. int mob_stopattack(struct mob_data *md)
  1012. {
  1013. md->target_id = 0;
  1014. md->state.targettype = NONE_ATTACKABLE;
  1015. md->attacked_id = 0;
  1016. md->attacked_count = 0;
  1017. return 0;
  1018. }
  1019. /*==========================================
  1020. * The stop of MOB's walking
  1021. *------------------------------------------
  1022. */
  1023. int mob_stop_walking(struct mob_data *md,int type)
  1024. {
  1025. nullpo_retr(0, md);
  1026. if(md->state.state == MS_WALK || md->state.state == MS_IDLE) {
  1027. int dx=0,dy=0;
  1028. md->walkpath.path_len=0;
  1029. if(type&2 && mob_can_move(md)){
  1030. dx=md->to_x-md->bl.x;
  1031. if(dx<0)
  1032. dx=-1;
  1033. else if(dx>0)
  1034. dx=1;
  1035. dy=md->to_y-md->bl.y;
  1036. if(dy<0)
  1037. dy=-1;
  1038. else if(dy>0)
  1039. dy=1;
  1040. }
  1041. md->to_x=md->bl.x+dx;
  1042. md->to_y=md->bl.y+dy;
  1043. if(dx!=0 || dy!=0){
  1044. mob_walktoxy_sub(md);
  1045. return 0;
  1046. }
  1047. mob_changestate(md,MS_IDLE,0);
  1048. }
  1049. if(type&0x01)
  1050. clif_fixmobpos(md);
  1051. return 0;
  1052. }
  1053. /*==========================================
  1054. * Determines if the mob can change target. [Skotlex]
  1055. *------------------------------------------
  1056. */
  1057. static int mob_can_changetarget(struct mob_data* md, struct block_list* target, int mode)
  1058. {
  1059. switch (md->state.skillstate) {
  1060. case MSS_BERSERK: //Only Assist, Angry or Aggressive+CastSensor mobs can change target while attacking.
  1061. if (mode&(MD_ASSIST|MD_ANGRY) || (mode&(MD_AGGRESSIVE|MD_CASTSENSOR)) == (MD_AGGRESSIVE|MD_CASTSENSOR))
  1062. return (battle_config.mob_ai&4 || check_distance_bl(&md->bl, target, 3));
  1063. else
  1064. return 0;
  1065. case MSS_RUSH:
  1066. return (mode&MD_AGGRESSIVE);
  1067. case MSS_FOLLOW:
  1068. case MSS_ANGRY:
  1069. case MSS_IDLE:
  1070. case MSS_WALK:
  1071. case MSS_LOOT:
  1072. return 1;
  1073. default:
  1074. return 0;
  1075. }
  1076. }
  1077. /*==========================================
  1078. * Determination for an attack of a monster
  1079. *------------------------------------------
  1080. */
  1081. int mob_target(struct mob_data *md,struct block_list *bl,int dist)
  1082. {
  1083. nullpo_retr(0, md);
  1084. nullpo_retr(0, bl);
  1085. // Nothing will be carried out if there is no mind of changing TAGE by TAGE ending.
  1086. if((md->target_id > 0 && md->state.targettype == ATTACKABLE) && !mob_can_changetarget(md, bl, status_get_mode(&md->bl)) &&
  1087. // if the monster was provoked ignore the above rule [celest]
  1088. !(md->state.provoke_flag && md->state.provoke_flag == bl->id))
  1089. return 0;
  1090. if(!status_check_skilluse(&md->bl, bl, 0, 0))
  1091. return 0;
  1092. md->target_id = bl->id; // Since there was no disturbance, it locks on to target.
  1093. if(bl->type == BL_PC || bl->type == BL_MOB)
  1094. md->state.targettype = ATTACKABLE;
  1095. else
  1096. md->state.targettype = NONE_ATTACKABLE;
  1097. if (md->state.provoke_flag)
  1098. md->state.provoke_flag = 0;
  1099. md->min_chase=dist+md->db->range2;
  1100. if(md->min_chase>26)
  1101. md->min_chase=26;
  1102. return 0;
  1103. }
  1104. /*==========================================
  1105. * The ?? routine of an active monster
  1106. *------------------------------------------
  1107. */
  1108. static int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap)
  1109. {
  1110. struct mob_data *md;
  1111. struct block_list **target;
  1112. int dist;
  1113. nullpo_retr(0, bl);
  1114. nullpo_retr(0, ap);
  1115. md=va_arg(ap,struct mob_data *);
  1116. target= va_arg(ap,struct block_list**);
  1117. //If can't seek yet, not an enemy, or you can't attack it, skip.
  1118. if ((*target) == bl || battle_check_target(&md->bl,bl,BCT_ENEMY)<=0 || !status_check_skilluse(&md->bl, bl, 0, 0))
  1119. return 0;
  1120. switch (bl->type)
  1121. {
  1122. case BL_PC:
  1123. case BL_MOB:
  1124. if((dist=distance_bl(&md->bl, bl)) < md->db->range2
  1125. && (md->db->range > 6 || mob_can_reach(md,bl,dist+1, MSS_FOLLOW))
  1126. && ((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) //New target closer than previous one.
  1127. ) {
  1128. (*target) = bl;
  1129. md->target_id=bl->id;
  1130. md->state.targettype = ATTACKABLE;
  1131. md->state.aggressive = (status_get_mode(&md->bl)&MD_ANGRY)?1:0;
  1132. md->min_chase= md->db->range3;
  1133. return 1;
  1134. }
  1135. break;
  1136. }
  1137. return 0;
  1138. }
  1139. /*==========================================
  1140. * chase target-change routine.
  1141. *------------------------------------------
  1142. */
  1143. static int mob_ai_sub_hard_changechase(struct block_list *bl,va_list ap)
  1144. {
  1145. struct mob_data *md;
  1146. struct block_list **target;
  1147. nullpo_retr(0, bl);
  1148. nullpo_retr(0, ap);
  1149. md=va_arg(ap,struct mob_data *);
  1150. target= va_arg(ap,struct block_list**);
  1151. //If can't seek yet, not an enemy, or you can't attack it, skip.
  1152. if ((*target) == bl || battle_check_target(&md->bl,bl,BCT_ENEMY)<=0 || !status_check_skilluse(&md->bl, bl, 0, 0))
  1153. return 0;
  1154. switch (bl->type)
  1155. {
  1156. case BL_PC:
  1157. case BL_MOB:
  1158. if(check_distance_bl(&md->bl, bl, md->db->range) &&
  1159. battle_check_range (&md->bl, bl, md->db->range)
  1160. ) {
  1161. (*target) = bl;
  1162. md->target_id=bl->id;
  1163. md->state.targettype = ATTACKABLE;
  1164. md->state.aggressive = (status_get_mode(&md->bl)&MD_ANGRY)?1:0;
  1165. md->min_chase= md->db->range3;
  1166. return 1;
  1167. }
  1168. break;
  1169. }
  1170. return 0;
  1171. }
  1172. /*==========================================
  1173. * loot monster item search
  1174. *------------------------------------------
  1175. */
  1176. static int mob_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  1177. {
  1178. struct mob_data* md;
  1179. int dist,*itc;
  1180. nullpo_retr(0, bl);
  1181. nullpo_retr(0, ap);
  1182. nullpo_retr(0, md=va_arg(ap,struct mob_data *));
  1183. nullpo_retr(0, itc=va_arg(ap,int *));
  1184. if(!md->lootitem || (battle_config.monster_loot_type == 1 && md->lootitem_count >= LOOTITEM_SIZE))
  1185. return 0;
  1186. if((dist=distance_bl(&md->bl, bl)) < md->db->range2 &&
  1187. mob_can_reach(md,bl,dist, MSS_LOOT) && rand()%1000<1000/(++(*itc)))
  1188. { // It is made a probability, such as within the limits PC.
  1189. md->target_id=bl->id;
  1190. md->state.targettype = NONE_ATTACKABLE;
  1191. md->min_chase=md->db->range3;
  1192. md->next_walktime = gettick() + 500; //So that the mob may go after the item inmediately.
  1193. }
  1194. return 0;
  1195. }
  1196. /*==========================================
  1197. * Processing of slave monsters
  1198. *------------------------------------------
  1199. */
  1200. static int mob_ai_sub_hard_slavemob(struct mob_data *md,unsigned int tick)
  1201. {
  1202. struct block_list *bl;
  1203. int old_dist;
  1204. nullpo_retr(0, md);
  1205. bl=map_id2bl(md->master_id);
  1206. if (!bl || status_isdead(bl)) { //主が死亡しているか見つからない
  1207. if(md->special_state.ai>0)
  1208. mob_timer_delete(0, 0, md->bl.id, 0);
  1209. else
  1210. mob_damage(NULL,md,md->hp,0);
  1211. return 0;
  1212. }
  1213. if(status_get_mode(&md->bl)&MD_CANMOVE)
  1214. { //If the mob can move, follow around. [Check by Skotlex]
  1215. if(bl->m != md->bl.m || md->master_dist > 30)
  1216. { // Since it is not in the same map (or is way to far), just warp it
  1217. mob_warp(md,bl->m,bl->x,bl->y,3);
  1218. return 0;
  1219. }
  1220. // Distance with between slave and master is measured.
  1221. old_dist=md->master_dist;
  1222. md->master_dist=distance_bl(&md->bl, bl);
  1223. // Since the master was in near immediately before, teleport is carried out and it pursues.
  1224. if(old_dist<10 && md->master_dist>18){
  1225. mob_warp(md,-1,bl->x,bl->y,3);
  1226. return 0;
  1227. }
  1228. // Although there is the master, since it is somewhat far, it approaches.
  1229. if((!md->target_id || md->state.targettype == NONE_ATTACKABLE) && mob_can_move(md) &&
  1230. md->master_dist<md->db->range3 && (md->walkpath.path_pos>=md->walkpath.path_len || md->walkpath.path_len==0)){
  1231. int i=0,dx,dy,ret;
  1232. if(md->master_dist>MOB_SLAVEDISTANCE || md->master_dist == 0)
  1233. { //Chase back to Master's area also if standing on top of the master.
  1234. do {
  1235. if(i<=5){
  1236. dx=bl->x - md->bl.x;
  1237. dy=bl->y - md->bl.y;
  1238. if(dx<0) dx+=rand()%MOB_SLAVEDISTANCE +1;
  1239. else if(dx>0) dx-=rand()%MOB_SLAVEDISTANCE +1;
  1240. if(dy<0) dy+=rand()%MOB_SLAVEDISTANCE +1;
  1241. else if(dy>0) dy-=rand()%MOB_SLAVEDISTANCE +1;
  1242. }else{
  1243. ret = MOB_SLAVEDISTANCE*2+1;
  1244. dx=bl->x - md->bl.x + rand()%ret - MOB_SLAVEDISTANCE;
  1245. dy=bl->y - md->bl.y + rand()%ret - MOB_SLAVEDISTANCE;
  1246. }
  1247. ret=mob_walktoxy(md,md->bl.x+dx,md->bl.y+dy,0);
  1248. i++;
  1249. } while(ret && i<10);
  1250. md->next_walktime=tick+1000;
  1251. }
  1252. }
  1253. } else if (bl->m != md->bl.m && map_flag_gvg(md->bl.m)) {
  1254. //Delete the summoned mob if it's in a gvg ground and the master is elsewhere. [Skotlex]
  1255. if(md->special_state.ai>0)
  1256. mob_timer_delete(0, 0, md->bl.id, 0);
  1257. else
  1258. mob_damage(NULL,md,md->hp,0);
  1259. return 0;
  1260. }
  1261. //Avoid attempting to lock the master's target too often to avoid unnecessary overload. [Skotlex]
  1262. if (DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME && (!md->target_id || md->state.targettype == NONE_ATTACKABLE)) {
  1263. md->last_linktime = tick;
  1264. switch (bl->type) {
  1265. case BL_MOB:
  1266. {
  1267. struct mob_data *mmd= (struct mob_data*)bl;
  1268. struct block_list *tbl;
  1269. if(mmd->target_id>0 && mmd->state.targettype == ATTACKABLE &&
  1270. (tbl=map_id2bl(mmd->target_id)) && status_check_skilluse(&md->bl, tbl, 0, 0)
  1271. ) {
  1272. md->target_id=tbl->id;
  1273. md->state.targettype = ATTACKABLE;
  1274. md->state.aggressive = (status_get_mode(&md->bl)&MD_ANGRY)?1:0;
  1275. md->min_chase=md->db->range2+distance_bl(&md->bl, tbl);
  1276. }
  1277. }
  1278. break;
  1279. case BL_PC:
  1280. {
  1281. struct map_session_data *msd = (struct map_session_data*)bl;
  1282. struct block_list *tbl = NULL;
  1283. if(msd->attacktarget)
  1284. tbl = map_id2bl(msd->attacktarget);
  1285. else if (msd->skilltarget)
  1286. tbl = map_id2bl(msd->skilltarget);
  1287. if(tbl && status_check_skilluse(&md->bl, tbl, 0, 0)) {
  1288. md->target_id=tbl->id;
  1289. md->state.targettype = ATTACKABLE;
  1290. md->state.aggressive = (status_get_mode(&md->bl)&MD_ANGRY)?1:0;
  1291. md->min_chase=md->db->range2+distance_bl(&md->bl, tbl);
  1292. }
  1293. }
  1294. break;
  1295. }
  1296. }
  1297. return 0;
  1298. }
  1299. /*==========================================
  1300. * A lock of target is stopped and mob moves to a standby state.
  1301. *------------------------------------------
  1302. */
  1303. int mob_unlocktarget(struct mob_data *md,int tick)
  1304. {
  1305. nullpo_retr(0, md);
  1306. md->target_id=0;
  1307. md->state.targettype = NONE_ATTACKABLE;
  1308. md->state.skillstate=MSS_IDLE;
  1309. md->next_walktime=tick+rand()%3000+3000;
  1310. return 0;
  1311. }
  1312. /*==========================================
  1313. * Random walk
  1314. *------------------------------------------
  1315. */
  1316. int mob_randomwalk(struct mob_data *md,int tick)
  1317. {
  1318. const int retrycount=20;
  1319. int speed;
  1320. nullpo_retr(0, md);
  1321. speed=status_get_speed(&md->bl);
  1322. if(DIFF_TICK(md->next_walktime,tick)<0){
  1323. int i,x,y,c,d=12-md->move_fail_count;
  1324. int mask[8][2] = {{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
  1325. if(d<5) d=5;
  1326. for(i=0;i<retrycount;i++){ // Search of a movable place
  1327. int r=rand();
  1328. x=r%(d*2+1)-d;
  1329. y=r/(d*2+1)%(d*2+1)-d;
  1330. if (md->target_dir){
  1331. if (x<0) x=0-x;
  1332. if (y<0) y=0-y;
  1333. x *= mask[md->target_dir-1][0];
  1334. y *= mask[md->target_dir-1][1];
  1335. }
  1336. x+=md->bl.x;
  1337. y+=md->bl.y;
  1338. if((map_getcell(md->bl.m,x,y,CELL_CHKPASS)) && mob_walktoxy(md,x,y,1)==0){
  1339. md->move_fail_count=0;
  1340. break;
  1341. }
  1342. if(i+1>=retrycount){
  1343. md->move_fail_count++;
  1344. md->target_dir = 0;
  1345. if(md->move_fail_count>1000){
  1346. if(battle_config.error_log)
  1347. ShowWarning("MOB cant move. random spawn %d, class = %d\n",md->bl.id,md->class_);
  1348. md->move_fail_count=0;
  1349. mob_spawn(md->bl.id);
  1350. }
  1351. }
  1352. }
  1353. for(i=c=0;i<md->walkpath.path_len;i++){ // The next walk start time is calculated.
  1354. if(md->walkpath.path[i]&1)
  1355. c+=speed*14/10;
  1356. else
  1357. c+=speed;
  1358. }
  1359. md->next_walktime = tick+rand()%3000+3000+c;
  1360. md->state.skillstate=MSS_WALK;
  1361. return 1;
  1362. }
  1363. return 0;
  1364. }
  1365. /*==========================================
  1366. * AI of MOB whose is near a Player
  1367. *------------------------------------------
  1368. */
  1369. static int mob_ai_sub_hard(struct block_list *bl,va_list ap)
  1370. {
  1371. struct mob_data *md;
  1372. struct block_list *tbl = NULL, *abl = NULL;
  1373. unsigned int tick;
  1374. int i, dx, dy, dist;
  1375. int attack_type = 0;
  1376. int mode;
  1377. int search_size = AREA_SIZE*2;
  1378. int blind_flag = 0;
  1379. nullpo_retr(0, bl);
  1380. nullpo_retr(0, ap);
  1381. md = (struct mob_data*)bl;
  1382. tick = va_arg(ap, unsigned int);
  1383. if(md->bl.prev == NULL || md->state.state == MS_DEAD)
  1384. return 1;
  1385. if (DIFF_TICK(tick, md->last_thinktime) < MIN_MOBTHINKTIME)
  1386. return 0;
  1387. md->last_thinktime = tick;
  1388. if (md->skilltimer != -1){ // Casting skill, or has died
  1389. if (DIFF_TICK (tick, md->next_walktime) > MIN_MOBTHINKTIME)
  1390. md->next_walktime = tick;
  1391. return 0;
  1392. }
  1393. // Abnormalities
  1394. if((md->sc.opt1 > 0 && md->sc.opt1 != OPT1_STONEWAIT) || md->state.state == MS_DELAY || md->sc.data[SC_BLADESTOP].timer != -1)
  1395. return 0;
  1396. if (md->sc.count && md->sc.data[SC_BLIND].timer != -1)
  1397. blind_flag = 1;
  1398. mode = status_get_mode(&md->bl);
  1399. if (md->target_id)
  1400. { //Check validity of current target. [Skotlex]
  1401. tbl = map_id2bl(md->target_id);
  1402. if (!tbl || tbl->m != md->bl.m || !status_check_skilluse(&md->bl, tbl, 0, 0))
  1403. { //Unlock current target.
  1404. if (md->state.state == MS_WALK && (battle_config.mob_ai&8 || !tbl)) //Inmediately stop chasing.
  1405. mob_stop_walking(md, 2);
  1406. mob_unlocktarget(md, tick-(battle_config.mob_ai&8?3000:0)); //Imediately do random walk.
  1407. tbl = NULL;
  1408. }
  1409. }
  1410. // Check for target change.
  1411. if (md->attacked_id && mode&MD_CANATTACK && md->attacked_id != md->target_id)
  1412. {
  1413. abl = map_id2bl(md->attacked_id);
  1414. if (abl && (!tbl || mob_can_changetarget(md, abl, mode))) {
  1415. if (md->bl.m != abl->m || abl->prev == NULL ||
  1416. (dist = distance_bl(&md->bl, abl)) >= 32 ||
  1417. battle_check_target(bl, abl, BCT_ENEMY) <= 0 ||
  1418. (battle_config.mob_ai&2 && !status_check_skilluse(bl, abl, 0, 0)) ||
  1419. !mob_can_reach(md, abl, dist+2, MSS_RUSH)) //Some more cells of grace...
  1420. { //Can't attack back
  1421. if (md->attacked_count++ > 3) {
  1422. if (mobskill_use(md, tick, MSC_RUDEATTACKED) == 0 &&
  1423. mode&MD_CANMOVE && mob_can_move(md))
  1424. {
  1425. int dist = rand() % 10 + 1;//後退する距離
  1426. int dir = map_calc_dir(abl, bl->x, bl->y);
  1427. int mask[8][2] = {{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
  1428. mob_walktoxy(md, md->bl.x + dist * mask[dir][0], md->bl.y + dist * mask[dir][1], 0);
  1429. md->next_walktime = tick + 500;
  1430. }
  1431. }
  1432. } else if (!(battle_config.mob_ai&2) && !status_check_skilluse(bl, abl, 0, 0)) {
  1433. //Can't attack back, but didn't invoke a rude attacked skill...
  1434. md->attacked_id = 0; //Simply unlock, shouldn't attempt to run away when in dumb_ai mode.
  1435. } else if (blind_flag && dist > 2 && DIFF_TICK(tick,md->next_walktime) < 0) { //Blinded, but can reach
  1436. if (!md->target_id)
  1437. { //Attempt to follow new target
  1438. if (mode&MD_CANMOVE && mob_can_move(md)) { // why is it moving to the target when the mob can't see the player? o.o
  1439. dx = abl->x - md->bl.x;
  1440. dy = abl->y - md->bl.y;
  1441. md->next_walktime = tick + 1000;
  1442. mob_walktoxy(md, md->bl.x+dx, md->bl.y+dy, 0);
  1443. }
  1444. }
  1445. } else { //Attackable
  1446. if (!tbl || dist < md->db->range || !check_distance_bl(&md->bl, tbl, dist)
  1447. || battle_gettarget(tbl) != md->bl.id)
  1448. { //Change if the new target is closer than the actual one
  1449. //or if the previous target is not attacking the mob. [Skotlex]
  1450. md->target_id = md->attacked_id; // set target
  1451. md->state.targettype = ATTACKABLE;
  1452. md->state.aggressive = 0; //Retaliating.
  1453. attack_type = 1;
  1454. md->attacked_count = 0;
  1455. md->min_chase = dist + md->db->range2;
  1456. if (md->min_chase > 26)
  1457. md->min_chase = 26;
  1458. tbl = abl; //Set the new target
  1459. }
  1460. }
  1461. }
  1462. }
  1463. if (md->attacked_id) {
  1464. if (md->state.aggressive && md->attacked_id == md->target_id)
  1465. md->state.aggressive = 0; //No longer aggressive, change to retaliate AI.
  1466. md->attacked_id = 0; //Clear it since it's been checked for already.
  1467. }
  1468. // Processing of slave monster, is it needed when there's a target to deal with?
  1469. if (md->master_id > 0 && !tbl)
  1470. mob_ai_sub_hard_slavemob(md, tick);
  1471. // Scan area for targets
  1472. if ((mode&MD_AGGRESSIVE && battle_config.monster_active_enable && !tbl) ||
  1473. (mode&MD_ANGRY && md->state.skillstate == MSS_FOLLOW)
  1474. ) {
  1475. search_size = (blind_flag) ? 3 : md->db->range2;
  1476. map_foreachinarea (mob_ai_sub_hard_activesearch, md->bl.m,
  1477. md->bl.x-search_size,md->bl.y-search_size,
  1478. md->bl.x+search_size,md->bl.y+search_size,
  1479. md->special_state.ai?BL_CHAR:BL_PC,
  1480. md, &tbl);
  1481. } else if (mode&MD_CHANGECHASE && (md->state.skillstate == MSS_RUSH || md->state.skillstate == MSS_FOLLOW)) {
  1482. search_size = (blind_flag && md->db->range>3) ? 3 : md->db->range;
  1483. map_foreachinarea (mob_ai_sub_hard_changechase, md->bl.m,
  1484. md->bl.x-search_size,md->bl.y-search_size,
  1485. md->bl.x+search_size,md->bl.y+search_size,
  1486. md->special_state.ai?BL_CHAR:BL_PC,
  1487. md, &tbl);
  1488. }
  1489. // Scan area for items to loot, avoid trying to loot of the mob is full and can't consume the items.
  1490. if (!md->target_id && mode&MD_LOOTER && md->lootitem &&
  1491. (md->lootitem_count < LOOTITEM_SIZE || battle_config.monster_loot_type != 1))
  1492. {
  1493. i = 0;
  1494. search_size = (blind_flag) ? 3 : md->db->range2;
  1495. map_foreachinarea (mob_ai_sub_hard_lootsearch, md->bl.m,
  1496. md->bl.x-search_size, md->bl.y-search_size,
  1497. md->bl.x+search_size, md->bl.y+search_size,
  1498. BL_ITEM, md, &i);
  1499. }
  1500. if (tbl)
  1501. { //Target exists, attack or loot as applicable.
  1502. if (tbl->type != BL_ITEM)
  1503. { //Attempt to attack.
  1504. //At this point we know the target is attackable, we just gotta check if the range matches.
  1505. if (blind_flag && DIFF_TICK(tick,md->next_walktime) < 0 && !check_distance_bl(&md->bl, tbl, 1))
  1506. { //Run towards the enemy when out of range?
  1507. md->target_id = 0;
  1508. md->state.targettype = NONE_ATTACKABLE;
  1509. if (!(mode & MD_CANMOVE) || !mob_can_move(md))
  1510. return 0;
  1511. dx = tbl->x - md->bl.x;
  1512. dy = tbl->y - md->bl.y;
  1513. md->next_walktime = tick + 1000;
  1514. mob_walktoxy(md, md->bl.x+dx, md->bl.y+dy, 0);
  1515. return 0;
  1516. }
  1517. if (!battle_check_range (&md->bl, tbl, md->db->range))
  1518. { //Out of range...
  1519. if (!(mode & MD_CANMOVE))
  1520. { //Can't chase.
  1521. mob_unlocktarget(md,tick);
  1522. return 0;
  1523. }
  1524. if (!mob_can_move(md)) //Wait until you can move?
  1525. return 0;
  1526. //Follow up
  1527. md->state.skillstate = md->state.aggressive?MSS_FOLLOW:MSS_RUSH;
  1528. mobskill_use (md, tick, -1);
  1529. if (md->timer != -1 && md->state.state != MS_ATTACK &&
  1530. (DIFF_TICK (md->next_walktime, tick) < 0 ||
  1531. !(battle_config.mob_ai&1) ||
  1532. check_distance_blxy(tbl, md->to_x, md->to_y, md->db->range)) //Current target tile is still within attack range.
  1533. ) {
  1534. return 0; //No need to follow, already doing it?
  1535. }
  1536. search_size = (blind_flag) ? 3 : ((md->min_chase > md->db->range2) ? md->min_chase : md->db->range2);
  1537. if (!mob_can_reach(md, tbl, search_size, MSS_RUSH))
  1538. { //Can't reach
  1539. mob_unlocktarget(md,tick);
  1540. return 0;
  1541. }
  1542. //Target reachable. Locate suitable spot to move to.
  1543. i = 0;
  1544. dx = tbl->x - md->bl.x;
  1545. dy = tbl->y - md->bl.y;
  1546. if (dx < 0) dx++;
  1547. else if (dx > 0) dx--;
  1548. if (dy < 0) dy++;
  1549. else if (dy > 0) dy--;
  1550. while (i < 5 && mob_walktoxy(md, md->bl.x + dx, md->bl.y + dy, 0))
  1551. { //Attempt to chase to nearby blocks
  1552. dx = tbl->x - md->bl.x + rand()%3 - 1;
  1553. dy = tbl->y - md->bl.y + rand()%3 - 1;
  1554. i++;
  1555. }
  1556. if (i==5)
  1557. { //Failed? Try going away from the target before retrying.
  1558. if (dx < 0) dx = 2;
  1559. else if (dx > 0) dx = -2;
  1560. if (dy < 0) dy = 2;
  1561. else if (dy > 0) dy = -2;
  1562. }
  1563. md->next_walktime = tick + 500;
  1564. mob_walktoxy (md, md->bl.x+dx, md->bl.y+dy, 0);
  1565. return 0;
  1566. }
  1567. //Target within range, engage
  1568. md->state.skillstate = md->state.aggressive?MSS_ANGRY:MSS_BERSERK;
  1569. if (md->state.state == MS_WALK)
  1570. mob_stop_walking (md, 1);
  1571. else if (md->state.state == MS_ATTACK)
  1572. return 0; //Ah, we are already attacking.
  1573. mob_changestate(md, MS_ATTACK, attack_type);
  1574. return 0;
  1575. } else { //Target is BL_ITEM, attempt loot.
  1576. struct flooritem_data *fitem;
  1577. if ((dist = distance_bl(&md->bl, tbl)) >= md->min_chase || (blind_flag && dist >= 4) || md->lootitem == NULL)
  1578. { //Can't loot...
  1579. mob_unlocktarget (md, tick);
  1580. if (md->state.state == MS_WALK)
  1581. mob_stop_walking(md,0);
  1582. return 0;
  1583. }
  1584. if (dist)
  1585. { //Still not within loot range.
  1586. if (!(mode & MD_CANMOVE))
  1587. { //A looter that can't move? Real smart.
  1588. mob_unlocktarget(md,tick);
  1589. return 0;
  1590. }
  1591. if (!mob_can_move(md)) // 動けない状態にある
  1592. return 0;
  1593. md->state.skillstate = MSS_LOOT; // ルート時スキル使用
  1594. mobskill_use(md, tick, -1);
  1595. if (md->timer != -1 && md->state.state != MS_ATTACK &&
  1596. (DIFF_TICK(md->next_walktime,tick) < 0 ||
  1597. check_distance_blxy(tbl, md->to_x, md->to_y, 0)))
  1598. { //Already on the way to looting.
  1599. return 0;
  1600. }
  1601. md->next_walktime = tick + 500;
  1602. dx = tbl->x - md->bl.x;
  1603. dy = tbl->y - md->bl.y;
  1604. if (mob_walktoxy(md, md->bl.x+dx, md->bl.y+dy, 0))
  1605. mob_unlocktarget(md, tick); //Can't loot...
  1606. return 0;
  1607. }
  1608. //Within looting range.
  1609. if (md->state.state == MS_ATTACK)
  1610. return 0; //Busy attacking?
  1611. if (md->state.state == MS_WALK)
  1612. mob_stop_walking(md,0);
  1613. fitem = (struct flooritem_data *)tbl;
  1614. if (md->lootitem_count < LOOTITEM_SIZE) {
  1615. memcpy (&md->lootitem[md->lootitem_count++], &fitem->item_data, sizeof(md->lootitem[0]));
  1616. if(log_config.pick > 0) //Logs items, taken by (L)ooter Mobs [Lupus]
  1617. 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]);
  1618. } else if (battle_config.monster_loot_type == 1) { //Can't loot, stuffed!
  1619. mob_unlocktarget(md,tick);
  1620. return 0;
  1621. } else { //Destroy first looted item...
  1622. if (md->lootitem[0].card[0] == (short)0xff00)
  1623. intif_delete_petdata( MakeDWord(md->lootitem[0].card[1],md->lootitem[0].card[2]) );
  1624. for (i = 0; i < LOOTITEM_SIZE - 1; i++)
  1625. memcpy (&md->lootitem[i], &md->lootitem[i+1], sizeof(md->lootitem[0]));
  1626. memcpy (&md->lootitem[LOOTITEM_SIZE-1], &fitem->item_data, sizeof(md->lootitem[0]));
  1627. }
  1628. //Clear item.
  1629. map_clearflooritem (tbl->id);
  1630. mob_unlocktarget (md,tick);
  1631. return 0;
  1632. }
  1633. }
  1634. // When there's no target, it is idling.
  1635. if (mobskill_use(md, tick, -1))
  1636. return 0;
  1637. // Nothing else to do... except random walking.
  1638. // Slaves do not random walk! [Skotlex]
  1639. if (mode&MD_CANMOVE && mob_can_move(md) && !md->master_id)
  1640. {
  1641. if (DIFF_TICK(md->next_walktime, tick) > 7000 &&
  1642. (md->walkpath.path_len == 0 || md->walkpath.path_pos >= md->walkpath.path_len))
  1643. md->next_walktime = tick + 3000 * rand() % 2000;
  1644. // Random movement
  1645. if (mob_randomwalk(md,tick))
  1646. return 0;
  1647. }
  1648. // Since he has finished walking, it stands by.
  1649. if (md->walkpath.path_len == 0 || md->walkpath.path_pos >= md->walkpath.path_len)
  1650. md->state.skillstate = MSS_IDLE;
  1651. return 0;
  1652. }
  1653. /*==========================================
  1654. * Serious processing for mob in PC field of view (foreachclient)
  1655. *------------------------------------------
  1656. */
  1657. static int mob_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
  1658. {
  1659. unsigned int tick;
  1660. nullpo_retr(0, sd);
  1661. nullpo_retr(0, ap);
  1662. tick=va_arg(ap,unsigned int);
  1663. map_foreachinarea(mob_ai_sub_hard,sd->bl.m,
  1664. sd->bl.x-AREA_SIZE*2,sd->bl.y-AREA_SIZE*2,
  1665. sd->bl.x+AREA_SIZE*2,sd->bl.y+AREA_SIZE*2,
  1666. BL_MOB,tick);
  1667. return 0;
  1668. }
  1669. /*==========================================
  1670. * Serious processing for mob in PC field of view (interval timer function)
  1671. *------------------------------------------
  1672. */
  1673. static int mob_ai_hard(int tid,unsigned int tick,int id,int data)
  1674. {
  1675. clif_foreachclient(mob_ai_sub_foreachclient,tick);
  1676. return 0;
  1677. }
  1678. /*==========================================
  1679. * Negligent mode MOB AI (PC is not in near)
  1680. *------------------------------------------
  1681. */
  1682. static int mob_ai_sub_lazy(DBKey key,void * data,va_list app)
  1683. {
  1684. struct mob_data *md = (struct mob_data *)data;
  1685. va_list ap;
  1686. unsigned int tick;
  1687. int mode;
  1688. nullpo_retr(0, md);
  1689. nullpo_retr(0, app);
  1690. if(md->bl.type!=BL_MOB)
  1691. return 0;
  1692. ap = va_arg(app, va_list);
  1693. tick=va_arg(ap,unsigned int);
  1694. if(DIFF_TICK(tick,md->last_thinktime)<MIN_MOBTHINKTIME*10)
  1695. return 0;
  1696. md->last_thinktime=tick;
  1697. if (md->bl.prev==NULL || md->state.state == MS_DEAD)
  1698. return 1;
  1699. if(md->skilltimer!=-1){
  1700. if(DIFF_TICK(tick,md->next_walktime)>MIN_MOBTHINKTIME*10)
  1701. md->next_walktime=tick;
  1702. return 0;
  1703. }
  1704. // 取り巻きモンスターの処理(呼び戻しされた時)
  1705. if (md->master_id > 0) {
  1706. mob_ai_sub_hard_slavemob (md,tick);
  1707. return 0;
  1708. }
  1709. mode = status_get_mode(&md->bl);
  1710. if(DIFF_TICK(md->next_walktime,tick)<0 &&
  1711. (mode&MD_CANMOVE) && mob_can_move(md) ){
  1712. if( map[md->bl.m].users>0 ){
  1713. // Since PC is in the same map, somewhat better negligent processing is carried out.
  1714. // It sometimes moves.
  1715. if(rand()%1000<MOB_LAZYMOVEPERC)
  1716. mob_randomwalk(md,tick);
  1717. // MOB which is not not the summons MOB but BOSS, either sometimes reboils.
  1718. else if( rand()%1000<MOB_LAZYWARPPERC && md->x0<=0 && md->master_id!=0 &&
  1719. !(mode&MD_BOSS))
  1720. mob_spawn(md->bl.id);
  1721. else if(rand()%1000<MOB_LAZYSKILLPERC) //Chance to do a mob's idle skill.
  1722. mobskill_use(md, tick, -1);
  1723. }else{
  1724. // Since PC is not even in the same map, suitable processing is carried out even if it takes.
  1725. // MOB which is not BOSS which is not Summons MOB, either -- a case -- sometimes -- leaping
  1726. if( rand()%1000<MOB_LAZYWARPPERC && md->x0<=0 && md->master_id!=0 &&
  1727. !(mode&MD_BOSS))
  1728. mob_warp(md,-1,-1,-1,-1);
  1729. }
  1730. md->next_walktime = tick+rand()%10000+5000;
  1731. }
  1732. return 0;
  1733. }
  1734. /*==========================================
  1735. * Negligent processing for mob outside PC field of view (interval timer function)
  1736. *------------------------------------------
  1737. */
  1738. static int mob_ai_lazy(int tid,unsigned int tick,int id,int data)
  1739. {
  1740. map_foreachiddb(mob_ai_sub_lazy,tick);
  1741. return 0;
  1742. }
  1743. /*==========================================
  1744. * The structure object for item drop with delay
  1745. * Since it is only two being able to pass [ int ] a timer function
  1746. * Data is put in and passed to this structure object.
  1747. *------------------------------------------
  1748. */
  1749. struct delay_item_drop {
  1750. int m,x,y;
  1751. struct item item_data;
  1752. struct map_session_data *first_sd,*second_sd,*third_sd;
  1753. };
  1754. /*==========================================
  1755. * Initializes the delay drop structure for mob-dropped items.
  1756. *------------------------------------------
  1757. */
  1758. static struct delay_item_drop* mob_setdropitem(int nameid, int qty, int m, int x, int y,
  1759. struct map_session_data* first_sd, struct map_session_data* second_sd, struct map_session_data* third_sd)
  1760. {
  1761. struct delay_item_drop *drop = aCalloc(1, sizeof (struct delay_item_drop));
  1762. drop->item_data.nameid = nameid;
  1763. drop->item_data.amount = qty;
  1764. drop->item_data.identify = !itemdb_isequip3(nameid);
  1765. drop->m = m;
  1766. drop->x = x;
  1767. drop->y = y;
  1768. drop->first_sd = first_sd;
  1769. drop->second_sd = second_sd;
  1770. drop->third_sd = third_sd;
  1771. return drop;
  1772. };
  1773. /*==========================================
  1774. * Initializes the delay drop structure for mob-looted items.
  1775. *------------------------------------------
  1776. */
  1777. static struct delay_item_drop* mob_setlootitem(struct item* item, int m, int x, int y,
  1778. struct map_session_data* first_sd, struct map_session_data* second_sd, struct map_session_data* third_sd)
  1779. {
  1780. struct delay_item_drop *drop = aCalloc(1, sizeof (struct delay_item_drop));
  1781. memcpy(&drop->item_data, item, sizeof(struct item));
  1782. drop->m = m;
  1783. drop->x = x;
  1784. drop->y = y;
  1785. drop->first_sd = first_sd;
  1786. drop->second_sd = second_sd;
  1787. drop->third_sd = third_sd;
  1788. return drop;
  1789. };
  1790. /*==========================================
  1791. * item drop with delay (timer function)
  1792. *------------------------------------------
  1793. */
  1794. static int mob_delay_item_drop(int tid,unsigned int tick,int id,int data)
  1795. {
  1796. struct delay_item_drop *ditem;
  1797. ditem=(struct delay_item_drop *)id;
  1798. map_addflooritem(&ditem->item_data,1,ditem->m,ditem->x,ditem->y,ditem->first_sd,ditem->second_sd,ditem->third_sd,0);
  1799. aFree(ditem);
  1800. return 0;
  1801. }
  1802. /*==========================================
  1803. * Sets a timer to drop an item on the ground
  1804. * Also performs logging and autoloot if enabled.
  1805. * rate is the drop-rate of the item, required for autoloot.
  1806. *------------------------------------------
  1807. * by [Skotlex]
  1808. */
  1809. static void mob_item_drop(struct mob_data *md, unsigned int tick, struct delay_item_drop * ditem, int loot, int drop_rate)
  1810. {
  1811. if(log_config.pick > 0)
  1812. { //Logs items, dropped by mobs [Lupus]
  1813. if (loot)
  1814. log_pick((struct map_session_data*)md, "L", md->class_, ditem->item_data.nameid, -ditem->item_data.amount, &ditem->item_data);
  1815. else
  1816. log_pick((struct map_session_data*)md, "M", md->class_, ditem->item_data.nameid, -ditem->item_data.amount, NULL);
  1817. }
  1818. if (ditem->first_sd && ditem->first_sd->state.autoloot &&
  1819. (drop_rate <= ditem->first_sd->state.autoloot ||
  1820. ditem->first_sd->state.autoloot >= 10000) //Fetch 100% drops
  1821. && pc_additem(ditem->first_sd,&ditem->item_data,ditem->item_data.amount) == 0)
  1822. { //Autolooted.
  1823. if(log_config.pick > 0)
  1824. log_pick(ditem->first_sd, "P", 0, ditem->item_data.nameid, ditem->item_data.amount, &ditem->item_data);
  1825. aFree(ditem);
  1826. } else
  1827. add_timer(tick, mob_delay_item_drop, (int)ditem, 0);
  1828. }
  1829. /*==========================================
  1830. * mob data is erased.
  1831. *------------------------------------------
  1832. */
  1833. void mob_unload(struct mob_data *md)
  1834. {
  1835. nullpo_retv(md);
  1836. mob_remove_map(md, 0);
  1837. map_deliddb(&md->bl);
  1838. map_freeblock((struct block_list*)md);
  1839. }
  1840. int mob_remove_map(struct mob_data *md, int type)
  1841. {
  1842. nullpo_retr(1, md);
  1843. if(md->bl.prev == NULL)
  1844. return 1;
  1845. mob_changestate(md,MS_DEAD,0);
  1846. clif_clearchar_area(&md->bl,type);
  1847. map_delblock(&md->bl);
  1848. if (md->lootitem){
  1849. aFree(md->lootitem);
  1850. md->lootitem = NULL;
  1851. }
  1852. if (md->guardian_data)
  1853. {
  1854. aFree(md->guardian_data);
  1855. md->guardian_data = NULL;
  1856. }
  1857. return 0;
  1858. }
  1859. int mob_delete(struct mob_data *md)
  1860. {
  1861. nullpo_retr(1, md);
  1862. mob_remove_map(md, 1);
  1863. if(pcdb_checkid(mob_get_viewclass(md->class_))) //Player mobs are not removed automatically by the client.
  1864. clif_clearchar_delay(gettick()+3000,&md->bl,0);
  1865. if(mob_is_clone(md->class_))
  1866. mob_clone_delete(md->class_);
  1867. mob_deleteslave(md);
  1868. mob_setdelayspawn(md->bl.id);
  1869. return 0;
  1870. }
  1871. int mob_timer_delete(int tid, unsigned int tick, int id, int data)
  1872. {
  1873. struct mob_data *md=(struct mob_data *)map_id2bl(id);
  1874. nullpo_retr(0, md);
  1875. //for Alchemist CANNIBALIZE [Lupus]
  1876. mob_remove_map(md, 3);
  1877. mob_setdelayspawn(md->bl.id);
  1878. return 0;
  1879. }
  1880. /*==========================================
  1881. *
  1882. *------------------------------------------
  1883. */
  1884. int mob_deleteslave_sub(struct block_list *bl,va_list ap)
  1885. {
  1886. struct mob_data *md;
  1887. int id;
  1888. nullpo_retr(0, bl);
  1889. nullpo_retr(0, ap);
  1890. nullpo_retr(0, md = (struct mob_data *)bl);
  1891. id=va_arg(ap,int);
  1892. if(md->master_id > 0 && md->master_id == id )
  1893. mob_damage(NULL,md,md->hp,1);
  1894. return 0;
  1895. }
  1896. /*==========================================
  1897. *
  1898. *------------------------------------------
  1899. */
  1900. int mob_deleteslave(struct mob_data *md)
  1901. {
  1902. nullpo_retr(0, md);
  1903. map_foreachinmap(mob_deleteslave_sub, md->bl.m, BL_MOB,md->bl.id);
  1904. return 0;
  1905. }
  1906. // Mob respawning through KAIZEL or NPC_REBIRTH [Skotlex]
  1907. int mob_respawn(int tid, unsigned int tick, int id,int data )
  1908. {
  1909. struct mob_data *md = (struct mob_data*)map_id2bl(id);
  1910. if (!md || md->bl.type != BL_MOB)
  1911. return 0;
  1912. //Mob must be dead and not in a map to respawn!
  1913. if (md->bl.prev != NULL || md->state.state != MS_DEAD)
  1914. return 0;
  1915. md->state.state = MS_IDLE;
  1916. md->state.skillstate = MSS_IDLE;
  1917. md->timer = -1;
  1918. md->last_thinktime = tick;
  1919. md->next_walktime = tick+rand()%50+5000;
  1920. md->attackabletime = tick;
  1921. md->canmove_tick = tick;
  1922. md->last_linktime = tick;
  1923. map_addblock(&md->bl);
  1924. mob_heal(md,data*status_get_max_hp(&md->bl)/100);
  1925. skill_unit_move(&md->bl,tick,1);
  1926. clif_spawnmob(md);
  1927. mobskill_use(md, tick, MSC_SPAWN);
  1928. return 1;
  1929. }
  1930. /*==========================================
  1931. * It is the damage of sd to damage to md.
  1932. *------------------------------------------
  1933. */
  1934. int mob_damage(struct block_list *src,struct mob_data *md,int damage,int type)
  1935. {
  1936. int i,count,minpos,mindmg;
  1937. struct map_session_data *sd = NULL,*tmpsd[DAMAGELOG_SIZE];
  1938. struct {
  1939. struct party *p;
  1940. int id,zeny;
  1941. unsigned int base_exp,job_exp;
  1942. } pt[DAMAGELOG_SIZE];
  1943. int pnum=0;
  1944. int mvp_damage,max_hp;
  1945. unsigned int tick = gettick();
  1946. struct map_session_data *mvp_sd = NULL, *second_sd = NULL,*third_sd = NULL;
  1947. struct block_list *master = NULL;
  1948. double temp;
  1949. struct item item;
  1950. int ret, mode;
  1951. int drop_rate;
  1952. int base_drop_delay;
  1953. int race;
  1954. nullpo_retr(0, md); //srcはNULLで呼ばれる場合もあるので、他でチェック
  1955. max_hp = status_get_max_hp(&md->bl);
  1956. race = status_get_race(&md->bl);
  1957. if(src && src->type == BL_PC) {
  1958. sd = (struct map_session_data *)src;
  1959. mvp_sd = sd;
  1960. }
  1961. if(md->bl.prev==NULL){
  1962. if(battle_config.error_log==1)
  1963. ShowError("mob_damage : BlockError!!\n");
  1964. return 0;
  1965. }
  1966. if(md->state.state==MS_DEAD || md->hp<=0) {
  1967. if(md->bl.prev != NULL) {
  1968. mob_changestate(md,MS_DEAD,0);
  1969. mobskill_use(md,tick,-1); // It is skill at the time of death.
  1970. clif_clearchar_area(&md->bl,1);
  1971. map_delblock(&md->bl);
  1972. mob_setdelayspawn(md->bl.id);
  1973. }
  1974. return 0;
  1975. }
  1976. if(md->sc.count) {
  1977. if(md->sc.data[SC_CONFUSION].timer != -1)
  1978. status_change_end(&md->bl, SC_CONFUSION, -1);
  1979. if(md->sc.data[SC_HIDING].timer != -1)
  1980. status_change_end(&md->bl, SC_HIDING, -1);
  1981. if(md->sc.data[SC_CLOAKING].timer != -1)
  1982. status_change_end(&md->bl, SC_CLOAKING, -1);
  1983. }
  1984. if(damage > max_hp>>2)
  1985. skill_stop_dancing(&md->bl);
  1986. if(md->hp > max_hp)
  1987. md->hp = max_hp;
  1988. // The amount of overkill rounds to hp.
  1989. if(damage>md->hp)
  1990. damage=md->hp;
  1991. md->hp-=damage;
  1992. md->tdmg+=damage; //Store total damage...
  1993. if(!(type&2)) {
  1994. int id = 0;
  1995. if (src) {
  1996. switch (src->type) {
  1997. case BL_PC:
  1998. id = sd->status.char_id;
  1999. if(md->attacked_id <= 0)
  2000. md->attacked_id = sd->bl.id;
  2001. break;
  2002. case BL_PET:
  2003. {
  2004. struct pet_data *pd = (struct pet_data*)src;
  2005. if (battle_config.pet_attack_exp_to_master) {
  2006. id = pd->msd->status.char_id;
  2007. damage=(damage*battle_config.pet_attack_exp_rate)/100; //Modify logged damage accordingly.
  2008. }
  2009. //Let mobs retaliate against the pet's master [Skotlex]
  2010. if(md->attacked_id <= 0)
  2011. md->attacked_id = pd->msd->bl.id;
  2012. break;
  2013. }
  2014. case BL_MOB:
  2015. {
  2016. struct mob_data* md2 = (struct mob_data*)src;
  2017. if(md2->special_state.ai && md2->master_id) {
  2018. struct map_session_data* msd = map_id2sd(md2->master_id);
  2019. if (msd) id = msd->status.char_id;
  2020. }
  2021. if(md->attacked_id <= 0)
  2022. { //Let players decide whether to retaliate versus the master or the mob. [Skotlex]
  2023. if (md2->master_id && battle_config.retaliate_to_master)
  2024. md->attacked_id = md2->master_id;
  2025. else
  2026. md->attacked_id = md2->bl.id;
  2027. }
  2028. break;
  2029. }
  2030. }
  2031. }
  2032. //Log damage...
  2033. if (id && damage > 0) {
  2034. for(i=0,minpos=DAMAGELOG_SIZE-1,mindmg=0x7fffffff;i<DAMAGELOG_SIZE;i++){
  2035. if(md->dmglog[i].id==id)
  2036. break;
  2037. if(md->dmglog[i].id==0) { //Store data in first empty slot.
  2038. md->dmglog[i].id = id;
  2039. break;
  2040. }
  2041. if(md->dmglog[i].dmg<mindmg){
  2042. minpos=i;
  2043. mindmg=md->dmglog[i].dmg;
  2044. }
  2045. }
  2046. if(i<DAMAGELOG_SIZE)
  2047. md->dmglog[i].dmg+=damage;
  2048. else {
  2049. md->dmglog[minpos].id=id;
  2050. md->dmglog[minpos].dmg=damage;
  2051. }
  2052. }
  2053. }
  2054. if(md->guardian_data && md->guardian_data->number < MAX_GUARDIANS) { // guardian hp update [Valaris] (updated by [Skotlex])
  2055. if ((md->guardian_data->castle->guardian[md->guardian_data->number].hp = md->hp) <= 0)
  2056. {
  2057. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2058. guild_castledatasave(md->guardian_data->castle->castle_id, 18+md->guardian_data->number,0);
  2059. }
  2060. } // end addition
  2061. if(md->special_state.ai == 2 && //スフィアーマイン
  2062. src && md->master_id == src->id)
  2063. {
  2064. md->state.alchemist = 1;
  2065. md->target_dir = map_calc_dir(src,md->bl.x,md->bl.y)+1;
  2066. mobskill_use(md, tick, MSC_ALCHEMIST);
  2067. }
  2068. if (battle_config.show_mob_hp)
  2069. clif_charnameack (0, &md->bl);
  2070. if(md->hp > 0)
  2071. return damage;
  2072. // ----- ここから死亡処理 -----
  2073. mode = status_get_mode(&md->bl); //Mode will be used for various checks regarding exp/drops.
  2074. //changestate will clear all status effects, so we need to know if RICHMANKIM is in effect before then. [Skotlex]
  2075. //I just recycled ret because it isn't used until much later and I didn't want to add a new variable for it.
  2076. ret = (md->sc.data[SC_RICHMANKIM].timer != -1)?(25 + 11*md->sc.data[SC_RICHMANKIM].val1):0;
  2077. md->state.skillstate = MSS_DEAD;
  2078. mobskill_use(md,tick,-1); //On Dead skill.
  2079. if (md->sc.data[SC_KAIZEL].timer != -1) {
  2080. //Revive in a bit.
  2081. max_hp = 10*md->sc.data[SC_KAIZEL].val1; //% of life to rebirth with
  2082. mob_changestate(md,MS_DEAD,0);
  2083. clif_clearchar_area(&md->bl,1);
  2084. map_delblock(&md->bl);
  2085. add_timer(gettick()+3000, mob_respawn, md->bl.id, max_hp);
  2086. return damage;
  2087. }
  2088. map_freeblock_lock();
  2089. mob_changestate(md,MS_DEAD,0);
  2090. memset(tmpsd,0,sizeof(tmpsd));
  2091. memset(pt,0,sizeof(pt));
  2092. max_hp = status_get_max_hp(&md->bl);
  2093. if(src && src->type == BL_MOB)
  2094. mob_unlocktarget((struct mob_data *)src,tick);
  2095. base_drop_delay = battle_config.delay_battle_damage?0:500;
  2096. if(sd) {
  2097. int sp = 0, hp = 0;
  2098. if (sd->state.attack_type == BF_MAGIC)
  2099. base_drop_delay = 500;
  2100. if (sd->state.attack_type == BF_MAGIC && sd->skilltarget == md->bl.id && (i=pc_checkskill(sd,HW_SOULDRAIN))>0)
  2101. { //Soul Drain should only work on targetted spells [Skotlex]
  2102. if (pc_issit(sd)) pc_setstand(sd); //Character stuck in attacking animation while 'sitting' fix. [Skotlex]
  2103. clif_skill_nodamage(src,&md->bl,HW_SOULDRAIN,i,1);
  2104. sp += (status_get_lv(&md->bl))*(65+15*i)/100;
  2105. }
  2106. sp += sd->sp_gain_value;
  2107. sp += sd->sp_gain_race[race];
  2108. sp += sd->sp_gain_race[mode&MD_BOSS?10:11];
  2109. hp += sd->hp_gain_value;
  2110. if (sp > 0) {
  2111. if(sd->status.sp + sp > sd->status.max_sp)
  2112. sp = sd->status.max_sp - sd->status.sp;
  2113. sd->status.sp += sp;
  2114. if (sp > 0 && battle_config.show_hp_sp_gain)
  2115. clif_heal(sd->fd,SP_SP,sp);
  2116. }
  2117. if (hp > 0) {
  2118. if(sd->status.hp + hp > sd->status.max_hp)
  2119. hp = sd->status.max_hp - sd->status.hp;
  2120. sd->status.hp += hp;
  2121. if (hp > 0 && battle_config.show_hp_sp_gain)
  2122. clif_heal(sd->fd,SP_HP,hp);
  2123. }
  2124. if (sd->mission_mobid == md->class_) { //TK_MISSION [Skotlex]
  2125. //Recycling hp for new random target id...
  2126. if (++sd->mission_count >= 100 && (hp = mob_get_random_id(0, 0, sd->status.base_level)))
  2127. {
  2128. pc_addfame(sd, 1);
  2129. sd->mission_mobid = hp;
  2130. pc_setglobalreg(sd,"TK_MISSION_ID", hp);
  2131. sd->mission_count = 0;
  2132. clif_mission_mob(sd, hp, 0);
  2133. }
  2134. pc_setglobalreg(sd,"TK_MISSION_COUNT", sd->mission_count);
  2135. }
  2136. }
  2137. // map外に消えた人は計算から除くので
  2138. // overkill分は無いけどsumはmax_hpとは違う
  2139. for(i=0,count=0,mvp_damage=0;i<DAMAGELOG_SIZE;i++){
  2140. if(md->dmglog[i].id==0)
  2141. break; //Reached end of log.
  2142. count++; //Count an attacker even if he is dead/logged-out.
  2143. tmpsd[i] = map_charid2sd(md->dmglog[i].id);
  2144. if(tmpsd[i] == NULL)
  2145. continue;
  2146. if(tmpsd[i]->bl.m != md->bl.m || pc_isdead(tmpsd[i]))
  2147. continue;
  2148. if(mvp_damage<md->dmglog[i].dmg){
  2149. third_sd = second_sd;
  2150. second_sd = mvp_sd;
  2151. mvp_sd=tmpsd[i];
  2152. mvp_damage=md->dmglog[i].dmg;
  2153. }
  2154. }
  2155. // [MouseJstr]
  2156. if((map[md->bl.m].flag.pvp == 0) || (battle_config.pvp_exp == 1)) {
  2157. // 経験値の分配
  2158. for(i=0;i<DAMAGELOG_SIZE;i++){
  2159. int pid,flag=1,zeny=0;
  2160. unsigned int base_exp,job_exp;
  2161. double per;
  2162. struct party *p;
  2163. if(tmpsd[i]==NULL || tmpsd[i]->bl.m != md->bl.m || pc_isdead(tmpsd[i]))
  2164. continue;
  2165. if (battle_config.exp_calc_type) // eAthena's exp formula based on max hp.
  2166. per = (double)md->dmglog[i].dmg/(double)max_hp;
  2167. else //jAthena's exp formula based on total damage.
  2168. per = (double)md->dmglog[i].dmg/(double)md->tdmg;
  2169. if (count>1)
  2170. per *= (9.+(double)((count > 6)? 6:count))/10.; //attackers count bonus.
  2171. base_exp = md->db->base_exp;
  2172. job_exp = md->db->job_exp;
  2173. if (ret)
  2174. per += per*ret/100.; //SC_RICHMANKIM bonus. [Skotlex]
  2175. if(sd) {
  2176. if (sd->expaddrace[race])
  2177. per += per*sd->expaddrace[race]/100.;
  2178. per += per*sd->expaddrace[mode&MD_BOSS?10:11]/100.;
  2179. }
  2180. if (battle_config.pk_mode && (md->db->lv - tmpsd[i]->status.base_level >= 20))
  2181. per *= 1.15; // pk_mode additional exp if monster >20 levels [Valaris]
  2182. //SG additional exp from Blessings [Komurka] - probably can be optimalized ^^;;
  2183. if(md->class_ == tmpsd[i]->hate_mob[0] && (battle_config.allow_skill_without_day || is_day_of_sun()))
  2184. per += per*10*pc_checkskill(tmpsd[i],SG_SUN_BLESS)/100.;
  2185. else if(md->class_ == tmpsd[i]->hate_mob[1] && (battle_config.allow_skill_without_day || is_day_of_moon()))
  2186. per += per*10*pc_checkskill(tmpsd[i],SG_MOON_BLESS)/100.;
  2187. else if(md->class_ == tmpsd[i]->hate_mob[2] && (battle_config.allow_skill_without_day || is_day_of_star()))
  2188. per += per*20*pc_checkskill(tmpsd[i],SG_STAR_BLESS)/100.;
  2189. if(md->special_state.size==1) // change experience for different sized monsters [Valaris]
  2190. per /=2.;
  2191. else if(md->special_state.size==2)
  2192. per *=2.;
  2193. if(md->master_id) {
  2194. if(((master = map_id2bl(md->master_id)) && status_get_mode(master)&MD_BOSS) || // check if its master is a boss (MVP's and minibosses)
  2195. md->special_state.ai) { // for summoned creatures [Valaris]
  2196. per = 0;
  2197. }
  2198. } else {
  2199. if(battle_config.zeny_from_mobs) {
  2200. if(md->level > 0) zeny=(int) ((md->level+rand()%md->level)*per); // zeny calculation moblv + random moblv [Valaris]
  2201. if(md->db->mexp > 0)
  2202. zeny*=rand()%250;
  2203. if(md->special_state.size==1 && zeny >=2) // change zeny for different sized monsters [Valaris]
  2204. zeny/=2;
  2205. else if(md->special_state.size==2 && zeny >1)
  2206. zeny*=2;
  2207. }
  2208. if(battle_config.mobs_level_up && md->level > md->db->lv) // [Valaris]
  2209. per+= per*(md->level-md->db->lv)*battle_config.mobs_level_up_exp_rate/100;
  2210. }
  2211. if (per > 4) per = 4; //Limit gained exp to quadro the mob's exp. [3->4 Komurka]
  2212. if (base_exp*per > UINT_MAX)
  2213. base_exp = UINT_MAX;
  2214. else
  2215. base_exp = (unsigned int)(base_exp*per);
  2216. if (job_exp*per > UINT_MAX)
  2217. job_exp = UINT_MAX;
  2218. else
  2219. job_exp = (unsigned int)(job_exp*per);
  2220. if (base_exp < 1) base_exp = 1;
  2221. if (job_exp < 1) job_exp = 1;
  2222. //mapflags: noexp check [Lorky]
  2223. if (map[md->bl.m].flag.nobaseexp == 1) base_exp=0;
  2224. if (map[md->bl.m].flag.nojobexp == 1) job_exp=0;
  2225. //end added Lorky
  2226. if((pid=tmpsd[i]->status.party_id)>0){ // パーティに入っている
  2227. int j;
  2228. for(j=0;j<pnum;j++) // 公平パーティリストにいるかどうか
  2229. if(pt[j].id==pid)
  2230. break;
  2231. if(j==pnum){ // いないときは公平かどうか確認
  2232. if((p=party_search(pid))!=NULL && p->exp!=0){
  2233. pt[pnum].id=pid;
  2234. pt[pnum].p=p;
  2235. pt[pnum].base_exp=base_exp;
  2236. pt[pnum].job_exp=job_exp;
  2237. if(battle_config.zeny_from_mobs)
  2238. pt[pnum].zeny=zeny; // zeny share [Valaris]
  2239. pnum++;
  2240. flag=0;
  2241. }
  2242. }else{ // いるときは公平
  2243. if (pt[j].base_exp > UINT_MAX - base_exp)
  2244. pt[j].base_exp=UINT_MAX;
  2245. else
  2246. pt[j].base_exp+=base_exp;
  2247. if (pt[j].job_exp > UINT_MAX - job_exp)
  2248. pt[j].job_exp=UINT_MAX;
  2249. else
  2250. pt[j].job_exp+=job_exp;
  2251. if(battle_config.zeny_from_mobs)
  2252. pt[j].zeny+=zeny; // zeny share [Valaris]
  2253. flag=0;
  2254. }
  2255. }
  2256. if(flag) { // added zeny from mobs [Valaris]
  2257. if(base_exp > 0 || job_exp > 0)
  2258. pc_gainexp(tmpsd[i],base_exp,job_exp);
  2259. if (battle_config.zeny_from_mobs && zeny > 0) {
  2260. pc_getzeny(tmpsd[i],zeny); // zeny from mobs [Valaris]
  2261. }
  2262. }
  2263. }
  2264. // 公平分配
  2265. for(i=0;i<pnum;i++)
  2266. party_exp_share(pt[i].p,md->bl.m,pt[i].base_exp,pt[i].job_exp,pt[i].zeny);
  2267. // item drop
  2268. if (!(type&1)) {
  2269. int drop_ore = -1, drop_items = 0; //slot N for DROP LOG, number of dropped items
  2270. int log_item[10]; //8 -> 10 Lupus
  2271. memset(&log_item,0,sizeof(log_item));
  2272. for (i = 0; i < 10; i++) { // 8 -> 10 Lupus
  2273. struct delay_item_drop *ditem;
  2274. if ((master && status_get_mode(master) & MD_BOSS) || // check if its master is a boss (MVP's and minibosses)
  2275. (md->special_state.ai &&
  2276. (battle_config.alchemist_summon_reward == 0 || //Noone gives items
  2277. (md->class_ != 1142 && battle_config.alchemist_summon_reward == 1) //Non Marine spheres don't drop items
  2278. ))) // Added [Valaris]
  2279. break; // End
  2280. //mapflag: noloot check [Lorky]
  2281. if (map[md->bl.m].flag.nomobloot) break;;
  2282. //end added [Lorky]
  2283. if (md->db->dropitem[i].nameid <= 0)
  2284. continue;
  2285. drop_rate = md->db->dropitem[i].p;
  2286. if (drop_rate <= 0 && !battle_config.drop_rate0item)
  2287. drop_rate = 1;
  2288. // change drops depending on monsters size [Valaris]
  2289. if(md->special_state.size==1 && drop_rate >= 2)
  2290. drop_rate/=2;
  2291. else if(md->special_state.size==2 && drop_rate > 0)
  2292. drop_rate*=2;
  2293. //Drops affected by luk as a fixed increase [Valaris]
  2294. if (src && battle_config.drops_by_luk > 0)
  2295. drop_rate += status_get_luk(src)*battle_config.drops_by_luk/100;
  2296. //Drops affected by luk as a % increase [Skotlex]
  2297. if (src && battle_config.drops_by_luk2 > 0)
  2298. drop_rate += (int)(0.5+drop_rate*status_get_luk(src)*battle_config.drops_by_luk2/10000.0);
  2299. if (sd && battle_config.pk_mode == 1 && (md->db->lv - sd->status.base_level >= 20))
  2300. drop_rate = (int)(drop_rate*1.25); // pk_mode increase drops if 20 level difference [Valaris]
  2301. if (drop_rate < rand() % 10000 + 1) { //fixed 0.01% impossible drops bug [Lupus]
  2302. drop_ore = i; //we remember an empty slot to put there ORE DISCOVERY drop later.
  2303. continue;
  2304. }
  2305. drop_items++; //we count if there were any drops
  2306. ditem = mob_setdropitem(md->db->dropitem[i].nameid, 1, md->bl.m, md->bl.x, md->bl.y, mvp_sd, second_sd, third_sd);
  2307. log_item[i] = ditem->item_data.nameid;
  2308. //A Rare Drop Global Announce by Lupus
  2309. if(drop_rate<=battle_config.rare_drop_announce) {
  2310. struct item_data *i_data;
  2311. char message[128];
  2312. i_data = itemdb_exists(ditem->item_data.nameid);
  2313. sprintf (message, msg_txt(541), (mvp_sd?mvp_sd->status.name:"???"), md->db->jname, i_data->jname, (float)drop_rate/100);
  2314. //MSG: "'%s' won %s's %s (chance: %%%0.02f)"
  2315. intif_GMmessage(message,strlen(message)+1,0);
  2316. }
  2317. // Announce first, or else ditem will be freed. [Lance]
  2318. // By popular demand, use base drop rate for autoloot code. [Skotlex]
  2319. mob_item_drop(md, tick+base_drop_delay+i, ditem, 0, md->db->dropitem[i].p);
  2320. }
  2321. // Ore Discovery [Celest]
  2322. if (sd == mvp_sd && map[md->bl.m].flag.nomobloot==0 && pc_checkskill(sd,BS_FINDINGORE)>0 && battle_config.finding_ore_rate/10 >= rand()%10000) {
  2323. struct delay_item_drop *ditem;
  2324. ditem = mob_setdropitem(itemdb_searchrandomid(6), 1, md->bl.m, md->bl.x, md->bl.y, mvp_sd, second_sd, third_sd);
  2325. if (drop_ore<0) drop_ore=8; //we have only 10 slots in LOG, there's a check to not overflow (9th item usually a card, so we use 8th slot)
  2326. log_item[drop_ore] = ditem->item_data.nameid; //it's for logging only
  2327. drop_items++; //we count if there were any drops
  2328. mob_item_drop(md, tick+base_drop_delay+drop_ore, ditem, 0, battle_config.finding_ore_rate/10);
  2329. }
  2330. //this drop log contains ALL dropped items + ORE (if there was ORE Recovery) [Lupus]
  2331. if(sd && log_config.drop > 0 && drop_items) //we check were there any drops.. and if not - don't write the log
  2332. log_drop(sd, md->class_, log_item); //mvp_sd
  2333. if(sd/* && sd->state.attack_type == BF_WEAPON*/) { //Player reports indicate this SHOULD work with all skills. [Skotlex]
  2334. int itemid = 0;
  2335. for (i = 0; i < sd->add_drop_count; i++) {
  2336. struct delay_item_drop *ditem;
  2337. if (sd->add_drop[i].id < 0)
  2338. continue;
  2339. if (sd->add_drop[i].race & (1<<race) ||
  2340. sd->add_drop[i].race & 1<<(mode&MD_BOSS?10:11))
  2341. {
  2342. //check if the bonus item drop rate should be multiplied with mob level/10 [Lupus]
  2343. if(sd->add_drop[i].rate<0)
  2344. //it's negative, then it should be multiplied. e.g. for Mimic,Myst Case Cards, etc
  2345. // rate = base_rate * (mob_level/10) + 1
  2346. drop_rate = -sd->add_drop[i].rate*(md->level/10)+1;
  2347. else
  2348. //it's positive, then it goes as it is
  2349. drop_rate = sd->add_drop[i].rate;
  2350. if (drop_rate < rand()%10000 +1)
  2351. continue;
  2352. itemid = (sd->add_drop[i].id > 0) ? sd->add_drop[i].id :
  2353. itemdb_searchrandomgroup(sd->add_drop[i].group);
  2354. ditem = mob_setdropitem(itemid, 1, md->bl.m, md->bl.x, md->bl.y, mvp_sd, second_sd, third_sd);
  2355. mob_item_drop(md, tick+base_drop_delay+20+i, ditem, 0, drop_rate);
  2356. }
  2357. }
  2358. if(sd->get_zeny_num && rand()%100 < sd->get_zeny_rate) //Gets get_zeny_num per level +/-10% [Skotlex]
  2359. pc_getzeny(sd,md->db->lv*sd->get_zeny_num*(90+rand()%21)/100);
  2360. }
  2361. if(md->lootitem) {
  2362. for(i=0;i<md->lootitem_count;i++) {
  2363. struct delay_item_drop *ditem;
  2364. ditem = mob_setlootitem(&md->lootitem[i], md->bl.m, md->bl.x, md->bl.y, mvp_sd, second_sd, third_sd);
  2365. mob_item_drop(md, tick+base_drop_delay+40+i, ditem, 1, 10000);
  2366. }
  2367. }
  2368. }
  2369. // mvp処理
  2370. if(mvp_sd && md->db->mexp > 0 && !md->special_state.ai){
  2371. int log_mvp[2] = {0};
  2372. int j;
  2373. int mexp;
  2374. temp = ((double)md->db->mexp * (9.+(double)count)/10.); //[Gengar]
  2375. mexp = (temp > 2147483647.)? 0x7fffffff:(int)temp;
  2376. //mapflag: noexp check [Lorky]
  2377. if (map[md->bl.m].flag.nobaseexp == 1 || map[md->bl.m].flag.nojobexp == 1) mexp=1;
  2378. //end added [Lorky]
  2379. if(mexp < 1) mexp = 1;
  2380. if(use_irc && irc_announce_mvp_flag)
  2381. irc_announce_mvp(mvp_sd,md);
  2382. clif_mvp_effect(mvp_sd); // エフェクト
  2383. clif_mvp_exp(mvp_sd,mexp);
  2384. pc_gainexp(mvp_sd,mexp,0);
  2385. log_mvp[1] = mexp;
  2386. for(j=0;j<3;j++){
  2387. i = rand() % 3;
  2388. //mapflag: noloot check [Lorky]
  2389. if (map[md->bl.m].flag.nomvploot == 1) break;
  2390. //end added Lorky
  2391. if(md->db->mvpitem[i].nameid <= 0)
  2392. continue;
  2393. drop_rate = md->db->mvpitem[i].p;
  2394. if(drop_rate <= 0 && !battle_config.drop_rate0item)
  2395. drop_rate = 1;
  2396. if(drop_rate <= rand()%10000+1) //if ==0, then it doesn't drop
  2397. continue;
  2398. memset(&item,0,sizeof(item));
  2399. item.nameid=md->db->mvpitem[i].nameid;
  2400. item.identify=!itemdb_isequip3(item.nameid);
  2401. clif_mvp_item(mvp_sd,item.nameid);
  2402. log_mvp[0] = item.nameid;
  2403. if(mvp_sd->weight*2 > mvp_sd->max_weight)
  2404. map_addflooritem(&item,1,mvp_sd->bl.m,mvp_sd->bl.x,mvp_sd->bl.y,mvp_sd,second_sd,third_sd,1);
  2405. else if((ret = pc_additem(mvp_sd,&item,1))) {
  2406. clif_additem(sd,0,0,ret);
  2407. map_addflooritem(&item,1,mvp_sd->bl.m,mvp_sd->bl.x,mvp_sd->bl.y,mvp_sd,second_sd,third_sd,1);
  2408. }
  2409. //A Rare MVP Drop Global Announce by Lupus
  2410. if(drop_rate<=battle_config.rare_drop_announce) {
  2411. struct item_data *i_data;
  2412. char message[128];
  2413. i_data = itemdb_exists(item.nameid);
  2414. sprintf (message, msg_txt(541), mvp_sd?mvp_sd->status.name :"???", md->db->jname, i_data->jname, (float)drop_rate/100);
  2415. //MSG: "'%s' won %s's %s (chance: %%%0.02f)"
  2416. intif_GMmessage(message,strlen(message)+1,0);
  2417. }
  2418. if(log_config.pick > 0) {//Logs items, MVP prizes [Lupus]
  2419. log_pick((struct map_session_data*)md, "M", md->class_, item.nameid, -1, NULL);
  2420. log_pick(mvp_sd, "P", 0, item.nameid, 1, NULL);
  2421. }
  2422. break;
  2423. }
  2424. if(log_config.mvpdrop > 0)
  2425. log_mvpdrop(mvp_sd, md->class_, log_mvp);
  2426. }
  2427. } // [MouseJstr]
  2428. // <Agit> NPC Event [OnAgitBreak]
  2429. if(md->npc_event[0] && strcmp(((md->npc_event)+strlen(md->npc_event)-13),"::OnAgitBreak") == 0) {
  2430. ShowNotice("MOB.C: Run NPC_Event[OnAgitBreak].\n");
  2431. if (agit_flag == 1) //Call to Run NPC_Event[OnAgitBreak]
  2432. guild_agit_break(md);
  2433. }
  2434. // SCRIPT実行
  2435. if(md->npc_event[0]){
  2436. // if(battle_config.battle_log)
  2437. // printf("mob_damage : run event : %s\n",md->npc_event);
  2438. if(src && src->type == BL_PET)
  2439. sd = ((struct pet_data *)src)->msd;
  2440. if(sd && battle_config.mob_npc_event_type)
  2441. npc_event(sd,md->npc_event,0);
  2442. else if(mvp_sd)
  2443. npc_event(mvp_sd,md->npc_event,0);
  2444. } else if (mvp_sd) {
  2445. //lordalfa
  2446. pc_setglobalreg(mvp_sd,"killedrid",(md->class_));
  2447. if (script_config.event_script_type == 0) {
  2448. struct npc_data *npc;
  2449. if ((npc = npc_name2id("NPCKillEvent"))) {
  2450. run_script(npc->u.scr.script,0,mvp_sd->bl.id,npc->bl.id); // NPCKillNPC
  2451. ShowStatus("Event '"CL_WHITE"NPCKillEvent"CL_RESET"' executed.\n");
  2452. }
  2453. } else {
  2454. ShowStatus("%d '"CL_WHITE"%s"CL_RESET"' events executed.\n",
  2455. npc_event_doall_id("NPCKillEvent", mvp_sd->bl.id), "NPCKillEvent");
  2456. }
  2457. }
  2458. //[lordalfa]
  2459. (battle_config.mob_clear_delay) ? clif_clearchar_delay(tick+battle_config.mob_clear_delay,&md->bl,1) : clif_clearchar_area(&md->bl,1);
  2460. // clif_clearchar_area(&md->bl,1); //eh? Why send the same packet twice? [Skotlex]
  2461. if(md->level) md->level=0;
  2462. map_delblock(&md->bl);
  2463. if(pcdb_checkid(mob_get_viewclass(md->class_)))
  2464. clif_clearchar_delay(tick+3000,&md->bl,0);
  2465. if(mob_is_clone(md->class_))
  2466. mob_clone_delete(md->class_);
  2467. mob_deleteslave(md);
  2468. mob_setdelayspawn(md->bl.id);
  2469. map_freeblock_unlock();
  2470. return damage;
  2471. }
  2472. int mob_guardian_guildchange(struct block_list *bl,va_list ap)
  2473. {
  2474. struct mob_data *md;
  2475. struct guild* g;
  2476. nullpo_retr(0, bl);
  2477. nullpo_retr(0, md = (struct mob_data *)bl);
  2478. if (!md->guardian_data)
  2479. return 0;
  2480. if (md->guardian_data->castle->guild_id == 0)
  2481. { //Castle with no owner? Delete the guardians.
  2482. if (md->class_ == MOBID_EMPERIUM)
  2483. { //But don't delete the emperium, just clear it's guild-data
  2484. md->guardian_data->guild_id = 0;
  2485. md->guardian_data->emblem_id = 0;
  2486. md->guardian_data->guild_name[0] = '\0';
  2487. } else {
  2488. if (md->guardian_data->castle->guardian[md->guardian_data->number].visible)
  2489. { //Safe removal of guardian.
  2490. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  2491. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2492. guild_castledatasave(md->guardian_data->castle->castle_id, 18+md->guardian_data->number,0);
  2493. }
  2494. mob_delete(md); //Remove guardian.
  2495. }
  2496. return 0;
  2497. }
  2498. g = guild_search(md->guardian_data->castle->guild_id);
  2499. if (g == NULL)
  2500. { //Properly remove guardian info from Castle data.
  2501. ShowError("mob_guardian_guildchange: New Guild (id %d) does not exists!\n", md->guardian_data->guild_id);
  2502. md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
  2503. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2504. guild_castledatasave(md->guardian_data->castle->castle_id, 18+md->guardian_data->number,0);
  2505. mob_delete(md);
  2506. return 0;
  2507. }
  2508. md->guardian_data->guild_id = md->guardian_data->castle->guild_id;
  2509. md->guardian_data->emblem_id = g->emblem_id;
  2510. md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
  2511. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  2512. return 1;
  2513. }
  2514. /*==========================================
  2515. * Pick a random class for the mob
  2516. *------------------------------------------
  2517. */
  2518. int mob_random_class (int *value, size_t count)
  2519. {
  2520. nullpo_retr(0, value);
  2521. // no count specified, look into the array manually, but take only max 5 elements
  2522. if (count < 1) {
  2523. count = 0;
  2524. while(count < 5 && mobdb_checkid(value[count])) count++;
  2525. if(count < 1) // nothing found
  2526. return 0;
  2527. } else {
  2528. // check if at least the first value is valid
  2529. if(mobdb_checkid(value[0]) == 0)
  2530. return 0;
  2531. }
  2532. //Pick a random value, hoping it exists. [Skotlex]
  2533. return mobdb_checkid(value[rand()%count]);
  2534. }
  2535. /*==========================================
  2536. * Change mob base class
  2537. *------------------------------------------
  2538. */
  2539. int mob_class_change (struct mob_data *md, int class_)
  2540. {
  2541. unsigned int tick = gettick();
  2542. int i, c, hp_rate;
  2543. nullpo_retr(0, md);
  2544. if (md->bl.prev == NULL)
  2545. return 0;
  2546. hp_rate = md->hp*100/status_get_max_hp(&md->bl);
  2547. clif_mob_class_change(md,class_);
  2548. md->class_ = class_;
  2549. md->db = mob_db(class_);
  2550. md->max_hp = md->db->max_hp; //Update the mob's max HP
  2551. if (battle_config.monster_class_change_full_recover) {
  2552. md->hp = md->max_hp;
  2553. memset(md->dmglog, 0, sizeof(md->dmglog));
  2554. md->tdmg = 0;
  2555. } else
  2556. md->hp = md->max_hp*hp_rate/100;
  2557. if(md->hp > md->max_hp) md->hp = md->max_hp;
  2558. else if(md->hp < 1) md->hp = 1;
  2559. memcpy(md->name,md->db->jname,NAME_LENGTH-1);
  2560. memset(&md->state,0,sizeof(md->state));
  2561. md->attacked_id = 0;
  2562. md->target_id = 0;
  2563. md->move_fail_count = 0;
  2564. md->speed = md->db->speed;
  2565. md->def_ele = md->db->element;
  2566. mob_changestate(md,MS_IDLE,0);
  2567. skill_castcancel(&md->bl,0);
  2568. md->state.skillstate = MSS_IDLE;
  2569. md->last_thinktime = tick;
  2570. md->next_walktime = tick+rand()%50+5000;
  2571. md->attackabletime = tick;
  2572. md->canmove_tick = tick;
  2573. md->last_linktime = tick;
  2574. for(i=0,c=tick-1000*3600*10;i<MAX_MOBSKILL;i++)
  2575. md->skilldelay[i] = c;
  2576. md->skillid=0;
  2577. md->skilllv=0;
  2578. if(md->lootitem == NULL && md->db->mode&MD_LOOTER)
  2579. md->lootitem=(struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  2580. skill_clear_unitgroup(&md->bl);
  2581. skill_cleartimerskill(&md->bl);
  2582. clif_clearchar_area(&md->bl,0);
  2583. clif_spawnmob(md);
  2584. if (battle_config.show_mob_hp)
  2585. clif_charnameack(0, &md->bl);
  2586. return 0;
  2587. }
  2588. /*==========================================
  2589. * mob回復
  2590. *------------------------------------------
  2591. */
  2592. int mob_heal(struct mob_data *md,int heal)
  2593. {
  2594. int max_hp;
  2595. nullpo_retr(0, md);
  2596. max_hp = status_get_max_hp(&md->bl);
  2597. md->hp += heal;
  2598. if( max_hp < md->hp )
  2599. md->hp = max_hp;
  2600. if(md->guardian_data && md->guardian_data->number < MAX_GUARDIANS) { // guardian hp update [Valaris] (updated by [Skotlex])
  2601. if ((md->guardian_data->castle->guardian[md->guardian_data->number].hp = md->hp) <= 0)
  2602. {
  2603. guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2604. guild_castledatasave(md->guardian_data->castle->castle_id, 18+md->guardian_data->number,0);
  2605. }
  2606. } // end addition
  2607. if (battle_config.show_mob_hp)
  2608. clif_charnameack(0, &md->bl);
  2609. return 0;
  2610. }
  2611. /*==========================================
  2612. * Added by RoVeRT
  2613. *------------------------------------------
  2614. */
  2615. int mob_warpslave_sub(struct block_list *bl,va_list ap)
  2616. {
  2617. struct mob_data *md=(struct mob_data *)bl;
  2618. struct block_list *master;
  2619. int x,y,range,i=0;
  2620. master = va_arg(ap, struct block_list*);
  2621. range = va_arg(ap, int);
  2622. if(md->master_id!=master->id)
  2623. return 0;
  2624. do {
  2625. x = master->x - range/2 + rand()%range;
  2626. y = master->y - range/2 + rand()%range;
  2627. } while (map_getcell(master->m,x,y,CELL_CHKNOPASS) && i<25);
  2628. if (i == 100)
  2629. mob_warp(md, master->m, master->x, master->y,2);
  2630. else
  2631. mob_warp(md, master->m, x, y,2);
  2632. return 1;
  2633. }
  2634. /*==========================================
  2635. * Added by RoVeRT
  2636. * Warps slaves. Range is the area around the master that they can
  2637. * appear in randomly.
  2638. *------------------------------------------
  2639. */
  2640. int mob_warpslave(struct block_list *bl, int range)
  2641. {
  2642. if (range < 1)
  2643. range = 1; //Min range needed to avoid crashes and stuff. [Skotlex]
  2644. return map_foreachinmap(mob_warpslave_sub, bl->m, BL_MOB, bl, range);
  2645. }
  2646. /*==========================================
  2647. * mobワープ
  2648. *------------------------------------------
  2649. */
  2650. int mob_warp(struct mob_data *md,int m,int x,int y,int type)
  2651. {
  2652. int i=0,xs=0,ys=0,bx=x,by=y;
  2653. int tick = gettick();
  2654. nullpo_retr(0, md);
  2655. if( md->bl.prev==NULL )
  2656. return 0;
  2657. if( m<0 ) m=md->bl.m;
  2658. if(type >= 0) {
  2659. if(map[md->bl.m].flag.monster_noteleport)
  2660. return 0;
  2661. if(md->sc.count) { //Clear a few status changes (taken directly from pc_setpos). [Skotlex]
  2662. if(md->sc.data[SC_TRICKDEAD].timer != -1)
  2663. status_change_end(&md->bl, SC_TRICKDEAD, -1);
  2664. if(md->sc.data[SC_BLADESTOP].timer!=-1)
  2665. status_change_end(&md->bl,SC_BLADESTOP,-1);
  2666. if(md->sc.data && md->sc.data[SC_RUN].timer!=-1)
  2667. status_change_end(&md->bl,SC_RUN,-1);
  2668. if(md->sc.data[SC_DANCING].timer!=-1)
  2669. skill_stop_dancing(&md->bl);
  2670. if (md->sc.data[SC_DEVOTION].timer!=-1)
  2671. status_change_end(&md->bl,SC_DEVOTION,-1);
  2672. if (md->sc.data[SC_CLOSECONFINE].timer!=-1)
  2673. status_change_end(&md->bl,SC_CLOSECONFINE,-1);
  2674. if (md->sc.data[SC_CLOSECONFINE2].timer!=-1)
  2675. status_change_end(&md->bl,SC_CLOSECONFINE2,-1);
  2676. if (md->sc.data[SC_RUN].timer!=-1)
  2677. status_change_end(&md->bl,SC_RUN,-1);
  2678. }
  2679. clif_clearchar_area(&md->bl,type);
  2680. }
  2681. skill_unit_move(&md->bl,tick,4);
  2682. map_delblock(&md->bl);
  2683. if(bx>0 && by>0){ // 位置指定の場合周囲9セルを探索
  2684. xs=ys=9;
  2685. }
  2686. while( ( x<0 || y<0 || map_getcell(m,x,y,CELL_CHKNOPASS)) && (i++)<1000 ){
  2687. if( xs>0 && ys>0 && i<250 ){ // 指定位置付近の探索
  2688. x=bx+rand()%xs-xs/2;
  2689. y=by+rand()%ys-ys/2;
  2690. }else{ // 完全ランダム探索
  2691. x=rand()%(map[m].xs-2)+1;
  2692. y=rand()%(map[m].ys-2)+1;
  2693. }
  2694. }
  2695. md->dir=0;
  2696. if(i<1000){
  2697. md->bl.x=md->to_x=x;
  2698. md->bl.y=md->to_y=y;
  2699. md->bl.m=m;
  2700. }else {
  2701. m=md->bl.m;
  2702. if(battle_config.error_log==1)
  2703. ShowWarning("MOB %d warp failed, class = %d\n",md->bl.id,md->class_);
  2704. }
  2705. md->target_id=0; // タゲを解除する
  2706. md->state.targettype=NONE_ATTACKABLE;
  2707. md->attacked_id=0;
  2708. if (md->master_id)
  2709. md->master_dist = 0; //Assume mob warped to leader. [Skotlex]
  2710. md->state.skillstate=MSS_IDLE;
  2711. mob_changestate(md,MS_IDLE,0);
  2712. if(type>0 && i==1000) {
  2713. if(battle_config.battle_log)
  2714. ShowInfo("MOB %d warp to (%d,%d), class = %d\n",md->bl.id,x,y,md->class_);
  2715. }
  2716. map_addblock(&md->bl);
  2717. skill_unit_move(&md->bl,tick,1);
  2718. if(type>0)
  2719. {
  2720. clif_spawnmob(md);
  2721. mob_warpslave(&md->bl,AREA_SIZE);
  2722. }
  2723. return 0;
  2724. }
  2725. /*==========================================
  2726. * 画面内の取り巻きの数計算用(foreachinarea)
  2727. *------------------------------------------
  2728. */
  2729. int mob_countslave_sub(struct block_list *bl,va_list ap)
  2730. {
  2731. int id;
  2732. struct mob_data *md;
  2733. id=va_arg(ap,int);
  2734. md = (struct mob_data *)bl;
  2735. if( md->master_id==id )
  2736. return 1;
  2737. return 0;
  2738. }
  2739. /*==========================================
  2740. * 画面内の取り巻きの数計算
  2741. *------------------------------------------
  2742. */
  2743. int mob_countslave(struct block_list *bl)
  2744. {
  2745. return map_foreachinmap(mob_countslave_sub, bl->m, BL_MOB,bl->id);
  2746. }
  2747. /*==========================================
  2748. * Summons amount slaves contained in the value[5] array using round-robin. [adapted by Skotlex]
  2749. *------------------------------------------
  2750. */
  2751. int mob_summonslave(struct mob_data *md2,int *value,int amount,int skill_id)
  2752. {
  2753. struct mob_data *md;
  2754. int bx,by,m,count = 0,class_,k=0;
  2755. nullpo_retr(0, md2);
  2756. nullpo_retr(0, value);
  2757. bx=md2->bl.x;
  2758. by=md2->bl.y;
  2759. m=md2->bl.m;
  2760. if(mobdb_checkid(value[0]) == 0)
  2761. return 0;
  2762. while(count < 5 && mobdb_checkid(value[count])) count++;
  2763. if(count < 1) return 0;
  2764. if (amount > 0 && amount < count) { //Do not start on 0, pick some random sub subset [Skotlex]
  2765. k = rand()%count;
  2766. amount+=k; //Increase final value by same amount to preserve total number to summon.
  2767. }
  2768. for(;k<amount;k++) {
  2769. int x=0,y=0,i=0;
  2770. class_ = value[k%count]; //Summon slaves in round-robin fashion. [Skotlex]
  2771. if (mobdb_checkid(class_) == 0)
  2772. continue;
  2773. md=(struct mob_data *)aCalloc(1,sizeof(struct mob_data));
  2774. if(mob_db(class_)->mode&MD_LOOTER)
  2775. md->lootitem=(struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  2776. while((x<=0 || y<=0 || map_getcell(m,x,y,CELL_CHKNOPASS)) && (i++)<100){
  2777. x=rand()%9-4+bx;
  2778. y=rand()%9-4+by;
  2779. }
  2780. if(i>=100){
  2781. x=bx;
  2782. y=by;
  2783. }
  2784. mob_spawn_dataset(md,"--ja--",class_);
  2785. md->bl.m=m;
  2786. md->bl.x=x;
  2787. md->bl.y=y;
  2788. md->m =m;
  2789. md->x0=x;
  2790. md->y0=y;
  2791. md->xs=0;
  2792. md->ys=0;
  2793. if (battle_config.slaves_inherit_speed && (skill_id != NPC_METAMORPHOSIS && skill_id != NPC_TRANSFORMATION))
  2794. md->speed=md2->speed;
  2795. md->special_state.cached= battle_config.dynamic_mobs; //[Skotlex]
  2796. md->spawndelay1=-1; // 一度のみフラグ
  2797. md->spawndelay2=-1; // 一度のみフラグ
  2798. if (!battle_config.monster_class_change_full_recover &&
  2799. (skill_id == NPC_TRANSFORMATION || skill_id == NPC_METAMORPHOSIS))
  2800. { //Scale HP
  2801. md->hp = (md->max_hp*md2->hp)/md2->max_hp;
  2802. }
  2803. memset(md->npc_event,0,sizeof(md->npc_event));
  2804. md->bl.type=BL_MOB;
  2805. map_addiddb(&md->bl);
  2806. mob_spawn(md->bl.id);
  2807. clif_skill_nodamage(&md->bl,&md->bl,skill_id,amount,1);
  2808. if(skill_id == NPC_SUMMONSLAVE)
  2809. md->master_id=md2->bl.id;
  2810. }
  2811. return 0;
  2812. }
  2813. /*==========================================
  2814. *MOBskillから該当skillidのskillidxを返す
  2815. *------------------------------------------
  2816. */
  2817. int mob_skillid2skillidx(int class_,int skillid)
  2818. {
  2819. int i, max = mob_db(class_)->maxskill;
  2820. struct mob_skill *ms=mob_db(class_)->skill;
  2821. if(ms==NULL)
  2822. return -1;
  2823. for(i=0;i<max;i++){
  2824. if(ms[i].skill_id == skillid)
  2825. return i;
  2826. }
  2827. return -1;
  2828. }
  2829. //
  2830. // MOBスキル
  2831. //
  2832. /*==========================================
  2833. * スキル使用(詠唱完了、ID指定)
  2834. *------------------------------------------
  2835. */
  2836. int mobskill_castend_id( int tid, unsigned int tick, int id,int data )
  2837. {
  2838. struct mob_data* md=NULL;
  2839. struct block_list *bl;
  2840. int inf;
  2841. //Code cleanup. Insert any code that should be executed if the skill fails here.
  2842. #define skill_failed(md) { md->skillid = md->skilllv = -1; }
  2843. if((md = (struct mob_data*)map_id2bl(id)) == NULL ) //詠唱したMobがもういないというのは良くある正常処理
  2844. return 0;
  2845. if( md->bl.type!=BL_MOB || md->bl.prev==NULL )
  2846. return 0;
  2847. if( md->skilltimer != tid ) {
  2848. if (battle_config.error_log)
  2849. ShowError("mobskill_castend_id: Timer mismatch %d!=%d\n", md->skilltimer, tid);
  2850. md->skilltimer = -1;
  2851. return 0;
  2852. }
  2853. md->skilltimer=-1;
  2854. if((bl = map_id2bl(md->skilltarget)) == NULL || bl->prev==NULL || md->bl.m != bl->m) {
  2855. skill_failed(md);
  2856. return 0;
  2857. }
  2858. if(md->skillid != NPC_EMOTION)
  2859. //Set afterskill delay.
  2860. md->last_thinktime=tick + (tid==-1?status_get_adelay(&md->bl):status_get_amotion(&md->bl));
  2861. if(md->skillid == RG_BACKSTAP) {
  2862. int dir = map_calc_dir(&md->bl,bl->x,bl->y),t_dir = status_get_dir(bl);
  2863. if(bl->type != BL_SKILL && (check_distance_bl(&md->bl, bl, 0) || map_check_dir(dir,t_dir))) {
  2864. skill_failed(md);
  2865. return 0;
  2866. }
  2867. }
  2868. inf = skill_get_inf(md->skillid);
  2869. if((inf&INF_ATTACK_SKILL ||
  2870. (inf&INF_SELF_SKILL && md->bl.id != bl->id && skill_get_nk(md->skillid) != NK_NO_DAMAGE))
  2871. && battle_check_target(&md->bl,bl, BCT_ENEMY)<=0 ) {
  2872. skill_failed(md);
  2873. return 0;
  2874. }
  2875. if(tid != -1)
  2876. {
  2877. if (md->skillid == -1 ||!status_check_skilluse(&md->bl, bl, md->skillid, 1)) {
  2878. skill_failed(md);
  2879. return 0;
  2880. }
  2881. if(!check_distance_bl(&md->bl, bl, skill_get_range2(&md->bl, md->skillid,md->skilllv) + battle_config.mob_skill_add_range)) {
  2882. skill_failed(md);
  2883. return 0;
  2884. }
  2885. }
  2886. md->skilldelay[md->skillidx]=tick;
  2887. if(battle_config.mob_skill_log)
  2888. ShowInfo("MOB skill castend skill=%d, class = %d\n",md->skillid,md->class_);
  2889. // mob_stop_wShowInfo(md,0);
  2890. switch( skill_get_nk(md->skillid) )
  2891. {
  2892. case NK_NO_DAMAGE:// 支援系
  2893. skill_castend_nodamage_id(&md->bl,bl,md->skillid,md->skilllv,tick,0);
  2894. break;
  2895. // 攻撃系/吹き飛ばし系
  2896. case NK_SPLASH_DAMAGE:
  2897. default:
  2898. skill_castend_damage_id(&md->bl,bl,md->skillid,md->skilllv,tick,0);
  2899. break;
  2900. }
  2901. if (md->sc.count && md->sc.data[SC_MAGICPOWER].timer != -1 && md->skillid != HW_MAGICPOWER)
  2902. status_change_end(&md->bl, SC_MAGICPOWER, -1);
  2903. if (md->db->skill[md->skillidx].emotion >= 0)
  2904. clif_emotion(&md->bl, md->db->skill[md->skillidx].emotion);
  2905. //TODO: This value is used for the "afterskill" mob condition, what could one do to clean it other than
  2906. //to use a "previous skill" struct variable?
  2907. //md->skillid = md->skilllv = -1; //Clean up skill-data for battle_getcurrentskill references. [Skotlex]
  2908. return 0;
  2909. }
  2910. /*==========================================
  2911. * スキル使用(詠唱完了、場所指定)
  2912. *------------------------------------------
  2913. */
  2914. int mobskill_castend_pos( int tid, unsigned int tick, int id,int data )
  2915. {
  2916. struct mob_data* md=NULL;
  2917. int maxcount;
  2918. //Code cleanup. Insert any code that should be executed if the skill fails here.
  2919. #define skill_failed(md) { md->skillid = md->skilllv = -1; }
  2920. nullpo_retr(0, md=(struct mob_data *)map_id2bl(id));
  2921. if( md->bl.type!=BL_MOB || md->bl.prev==NULL )
  2922. return 0;
  2923. if( md->skilltimer != tid ) {
  2924. if (battle_config.error_log)
  2925. ShowError("mobskill_castend_pos: Timer mismatch %d!=%d\n", md->skilltimer, tid);
  2926. md->skilltimer = -1;
  2927. return 0;
  2928. }
  2929. md->skilltimer=-1;
  2930. if (tid != -1)
  2931. { //Avoid unnecessary checks for instant-cast skills. [Skotlex]
  2932. if (md->skillid == -1 || !status_check_skilluse(&md->bl, NULL, md->skillid, 1)) {
  2933. skill_failed(md);
  2934. return 0;
  2935. }
  2936. if(!check_distance_blxy(&md->bl, md->skillx, md->skilly, skill_get_range2(&md->bl, md->skillid,md->skilllv) + battle_config.mob_skill_add_range)) {
  2937. skill_failed(md);
  2938. return 0;
  2939. }
  2940. }
  2941. if (!battle_config.monster_skill_reiteration &&
  2942. skill_get_unit_flag (md->skillid) & UF_NOREITERATION &&
  2943. skill_check_unit_range (md->bl.m, md->skillx, md->skilly, md->skillid, md->skilllv)) {
  2944. skill_failed(md);
  2945. return 0;
  2946. }
  2947. if(battle_config.monster_skill_nofootset &&
  2948. skill_get_unit_flag (md->skillid) & UF_NOFOOTSET &&
  2949. skill_check_unit_range2(&md->bl, md->bl.m, md->skillx, md->skilly, md->skillid, md->skilllv)) {
  2950. skill_failed(md);
  2951. return 0;
  2952. }
  2953. if(battle_config.monster_land_skill_limit) {
  2954. maxcount = skill_get_maxcount(md->skillid);
  2955. if(maxcount > 0) {
  2956. int i,c;
  2957. for(i=c=0;i<MAX_MOBSKILLUNITGROUP;i++) {
  2958. if(md->skillunit[i].alive_count > 0 && md->skillunit[i].skill_id == md->skillid)
  2959. c++;
  2960. }
  2961. if(c >= maxcount) {
  2962. skill_failed(md);
  2963. return 0;
  2964. }
  2965. }
  2966. }
  2967. md->skilldelay[md->skillidx]=tick;
  2968. //Set afterskill delay.
  2969. md->last_thinktime=tick + (tid==-1?status_get_adelay(&md->bl):status_get_amotion(&md->bl));
  2970. if(battle_config.mob_skill_log)
  2971. ShowInfo("MOB skill castend skill=%d, class = %d\n",md->skillid,md->class_);
  2972. // mob_stop_walking(md,0);
  2973. skill_castend_pos2(&md->bl,md->skillx,md->skilly,md->skillid,md->skilllv,tick,0);
  2974. if (md->db->skill[md->skillidx].emotion >= 0)
  2975. clif_emotion(&md->bl, md->db->skill[md->skillidx].emotion);
  2976. //TODO: This value is used for the "afterskill" mob condition, what could one do to clean it other than
  2977. //to use a "previous skill" struct variable?
  2978. //md->skillid = md->skilllv = -1; //Clean up skill data for future references to battle_getcurrentskill [Skotlex]
  2979. return 0;
  2980. }
  2981. /*==========================================
  2982. * Skill use (an aria start, ID specification)
  2983. *------------------------------------------
  2984. */
  2985. int mobskill_use_id(struct mob_data *md,struct block_list *target,int skill_idx)
  2986. {
  2987. int casttime;
  2988. struct mob_skill *ms;
  2989. int skill_id, skill_lv, forcecast = 0;
  2990. int selfdestruct_flag = 0;
  2991. nullpo_retr(0, md);
  2992. nullpo_retr(0, ms=&md->db->skill[skill_idx]);
  2993. if( target==NULL && (target=map_id2bl(md->target_id))==NULL )
  2994. return 0;
  2995. if( target->prev==NULL || md->bl.prev==NULL )
  2996. return 0;
  2997. skill_id=ms->skill_id;
  2998. skill_lv=ms->skill_lv;
  2999. if(map_flag_gvg(md->bl.m) && skill_db[skill_id].nocast & 4)
  3000. return 0;
  3001. if(skill_get_inf2(skill_id)&INF2_NO_TARGET_SELF && md->bl.id == target->id)
  3002. return 0;
  3003. if(!status_check_skilluse(&md->bl, target, skill_id, 0))
  3004. return 0;
  3005. if(md->sc.count && md->sc.data[SC_WINKCHARM].timer != -1 && target->type == BL_PC) {
  3006. clif_emotion(&md->bl, 3);
  3007. return 0;
  3008. }
  3009. // 射程と障害物チェック
  3010. if(!battle_check_range(&md->bl,target,skill_get_range2(&md->bl,skill_id,skill_lv)))
  3011. return 0;
  3012. // delay=skill_delayfix(&md->bl, skill_id, skill_lv, 0);
  3013. casttime=skill_castfix(&md->bl, skill_id, skill_lv, ms->casttime);
  3014. md->state.skillcastcancel=ms->cancel;
  3015. md->skilldelay[skill_idx]=gettick();
  3016. switch(skill_id){ /* 何か特殊な処理が必要 */
  3017. case ALL_RESURRECTION: /* リザレクション */
  3018. if(target->type != BL_PC && battle_check_undead(status_get_race(target),status_get_elem_type(target))){ /* 敵がアンデッドなら */
  3019. forcecast=1; /* ターンアンデットと同じ詠唱時間 */
  3020. casttime=skill_castfix(&md->bl, PR_TURNUNDEAD,skill_lv, 0);
  3021. }
  3022. break;
  3023. case MO_EXTREMITYFIST: /*阿修羅覇鳳拳*/
  3024. case SA_MAGICROD:
  3025. case SA_SPELLBREAKER:
  3026. forcecast=1;
  3027. break;
  3028. case NPC_SUMMONSLAVE:
  3029. case NPC_SUMMONMONSTER:
  3030. if(md->master_id!=0)
  3031. return 0;
  3032. break;
  3033. case NPC_SELFDESTRUCTION:
  3034. if (casttime == 0 && md->special_state.ai == 2) {
  3035. casttime = skill_get_time(skill_id,skill_lv);
  3036. selfdestruct_flag = 1;
  3037. }
  3038. break;
  3039. }
  3040. if(battle_config.mob_skill_log)
  3041. ShowInfo("MOB skill use target_id=%d skill=%d lv=%d cast=%d, class = %d\n",target->id,skill_id,skill_lv,casttime,md->class_);
  3042. if (casttime || forcecast) { // 詠唱が必要
  3043. if (!selfdestruct_flag)
  3044. mob_stop_walking(md,1); // 歩行停止
  3045. clif_skillcasting(&md->bl, md->bl.id, target->id, 0,0, skill_id, casttime);
  3046. }
  3047. if (casttime <= 0) // 詠唱の無いものはキャンセルされない
  3048. md->state.skillcastcancel = 0;
  3049. md->skilltarget = target->id;
  3050. md->skillx = 0;
  3051. md->skilly = 0;
  3052. md->skillid = skill_id;
  3053. md->skilllv = skill_lv;
  3054. md->skillidx = skill_idx;
  3055. if(!(battle_config.monster_cloak_check_type&2) && md->sc.data[SC_CLOAKING].timer != -1 && md->skillid != AS_CLOAKING)
  3056. status_change_end(&md->bl,SC_CLOAKING,-1);
  3057. if( casttime>0 ){
  3058. md->skilltimer =
  3059. add_timer( gettick()+casttime, mobskill_castend_id, md->bl.id, 0 );
  3060. }else{
  3061. md->skilltimer = -1;
  3062. mobskill_castend_id(md->skilltimer,gettick(),md->bl.id, 0);
  3063. }
  3064. return 1;
  3065. }
  3066. /*==========================================
  3067. * スキル使用(場所指定)
  3068. *------------------------------------------
  3069. */
  3070. int mobskill_use_pos( struct mob_data *md,
  3071. int skill_x, int skill_y, int skill_idx)
  3072. {
  3073. int casttime=0;
  3074. struct mob_skill *ms;
  3075. struct block_list bl;
  3076. int skill_id, skill_lv;
  3077. nullpo_retr(0, md);
  3078. nullpo_retr(0, ms=&md->db->skill[skill_idx]);
  3079. if( md->bl.prev==NULL )
  3080. return 0;
  3081. skill_id=ms->skill_id;
  3082. skill_lv=ms->skill_lv;
  3083. if(map_flag_gvg(md->bl.m) && skill_db[skill_id].nocast & 4)
  3084. return 0;
  3085. if(!status_check_skilluse(&md->bl, NULL, skill_id, 0))
  3086. return 0;
  3087. // 射程と障害物チェック
  3088. bl.type = BL_NUL;
  3089. bl.m = md->bl.m;
  3090. bl.x = skill_x;
  3091. bl.y = skill_y;
  3092. if(!battle_check_range(&md->bl,&bl,skill_get_range2(&md->bl,skill_id,skill_lv)))
  3093. return 0;
  3094. // delay=skill_delayfix(&md->bl, skill_id, skill_lv, 0);
  3095. casttime=skill_castfix(&md->bl,skill_id, skill_lv, ms->casttime);
  3096. md->skilldelay[skill_idx]=gettick();
  3097. md->state.skillcastcancel=ms->cancel;
  3098. if(battle_config.mob_skill_log)
  3099. ShowInfo("MOB skill use target_pos=(%d,%d) skill=%d lv=%d cast=%d, class = %d\n",
  3100. skill_x,skill_y,skill_id,skill_lv,casttime,md->class_);
  3101. if( casttime>0 ) { // A cast time is required.
  3102. mob_stop_walking(md,1); // 歩行停止
  3103. clif_skillcasting( &md->bl,
  3104. md->bl.id, 0, skill_x,skill_y, skill_id,casttime);
  3105. }
  3106. if( casttime<=0 ) // A skill without a cast time wont be cancelled.
  3107. md->state.skillcastcancel=0;
  3108. md->skillx = skill_x;
  3109. md->skilly = skill_y;
  3110. md->skilltarget = 0;
  3111. md->skillid = skill_id;
  3112. md->skilllv = skill_lv;
  3113. md->skillidx = skill_idx;
  3114. if(!(battle_config.monster_cloak_check_type&2) && md->sc.data[SC_CLOAKING].timer != -1)
  3115. status_change_end(&md->bl,SC_CLOAKING,-1);
  3116. if( casttime>0 ){
  3117. md->skilltimer =
  3118. add_timer( gettick()+casttime, mobskill_castend_pos, md->bl.id, 0 );
  3119. }else{
  3120. md->skilltimer = -1;
  3121. mobskill_castend_pos(md->skilltimer,gettick(),md->bl.id, 0);
  3122. }
  3123. return 1;
  3124. }
  3125. /*==========================================
  3126. * Friendly Mob whose HP is decreasing by a nearby MOB is looked for.
  3127. *------------------------------------------
  3128. */
  3129. int mob_getfriendhpltmaxrate_sub(struct block_list *bl,va_list ap)
  3130. {
  3131. int rate;
  3132. struct block_list **fr;
  3133. struct mob_data *md;
  3134. nullpo_retr(0, bl);
  3135. nullpo_retr(0, ap);
  3136. nullpo_retr(0, md=va_arg(ap,struct mob_data *));
  3137. rate=va_arg(ap,int);
  3138. fr=va_arg(ap,struct block_list **);
  3139. if( md->bl.id == bl->id )
  3140. return 0;
  3141. if ((*fr) != NULL) //A friend was already found.
  3142. return 0;
  3143. if (battle_check_target(&md->bl,bl,BCT_ENEMY)>0)
  3144. return 0;
  3145. if (status_get_hp(bl) < status_get_max_hp(bl) * rate / 100)
  3146. (*fr) = bl;
  3147. return 0;
  3148. }
  3149. struct block_list *mob_getfriendhpltmaxrate(struct mob_data *md,int rate)
  3150. {
  3151. struct block_list *fr=NULL;
  3152. const int r=8;
  3153. int type = BL_MOB;
  3154. nullpo_retr(NULL, md);
  3155. if (md->special_state.ai) //Summoned creatures. [Skotlex]
  3156. type = BL_PC;
  3157. map_foreachinarea(mob_getfriendhpltmaxrate_sub, md->bl.m,
  3158. md->bl.x-r ,md->bl.y-r, md->bl.x+r, md->bl.y+r,
  3159. type,md,rate,&fr);
  3160. return fr;
  3161. }
  3162. /*==========================================
  3163. * Check hp rate of its master
  3164. *------------------------------------------
  3165. */
  3166. struct block_list *mob_getmasterhpltmaxrate(struct mob_data *md,int rate)
  3167. {
  3168. if (md && md->master_id > 0) {
  3169. struct block_list *bl = map_id2bl(md->master_id);
  3170. if (status_get_hp(bl) < status_get_max_hp(bl) * rate / 100)
  3171. return bl;
  3172. }
  3173. return NULL;
  3174. }
  3175. /*==========================================
  3176. * What a status state suits by nearby MOB is looked for.
  3177. *------------------------------------------
  3178. */
  3179. int mob_getfriendstatus_sub(struct block_list *bl,va_list ap)
  3180. {
  3181. int cond1,cond2;
  3182. struct mob_data **fr, *md, *mmd;
  3183. int flag=0;
  3184. nullpo_retr(0, bl);
  3185. nullpo_retr(0, ap);
  3186. nullpo_retr(0, md=(struct mob_data *)bl);
  3187. nullpo_retr(0, mmd=va_arg(ap,struct mob_data *));
  3188. if( mmd->bl.id == bl->id )
  3189. return 0;
  3190. if (battle_check_target(&mmd->bl,bl,BCT_ENEMY)>0)
  3191. return 0;
  3192. cond1=va_arg(ap,int);
  3193. cond2=va_arg(ap,int);
  3194. fr=va_arg(ap,struct mob_data **);
  3195. if( cond2==-1 ){
  3196. int j;
  3197. for(j=SC_COMMON_MIN;j<=SC_COMMON_MAX && !flag;j++){
  3198. if ((flag=(md->sc.data[j].timer!=-1))) //Once an effect was found, break out. [Skotlex]
  3199. break;
  3200. }
  3201. }else
  3202. flag=( md->sc.data[cond2].timer!=-1 );
  3203. if( flag^( cond1==MSC_FRIENDSTATUSOFF ) )
  3204. (*fr)=md;
  3205. return 0;
  3206. }
  3207. struct mob_data *mob_getfriendstatus(struct mob_data *md,int cond1,int cond2)
  3208. {
  3209. struct mob_data *fr=NULL;
  3210. const int r=8;
  3211. nullpo_retr(0, md);
  3212. map_foreachinarea(mob_getfriendstatus_sub, md->bl.m,
  3213. md->bl.x-r ,md->bl.y-r, md->bl.x+r, md->bl.y+r,
  3214. BL_MOB,md,cond1,cond2,&fr);
  3215. return fr;
  3216. }
  3217. /*==========================================
  3218. * Skill use judging
  3219. *------------------------------------------
  3220. */
  3221. int mobskill_use(struct mob_data *md, unsigned int tick, int event)
  3222. {
  3223. struct mob_skill *ms;
  3224. struct block_list *fbl = NULL; //Friend bl, which can either be a BL_PC or BL_MOB depending on the situation. [Skotlex]
  3225. struct mob_data *fmd = NULL;
  3226. int i;
  3227. nullpo_retr (0, md);
  3228. nullpo_retr (0, ms = md->db->skill);
  3229. if (battle_config.mob_skill_rate == 0 || md->skilltimer != -1)
  3230. return 0;
  3231. for (i = 0; i < md->db->maxskill; i++) {
  3232. int c2 = ms[i].cond2, flag = 0;
  3233. // ディレイ中
  3234. if (DIFF_TICK(tick, md->skilldelay[i]) < ms[i].delay)
  3235. continue;
  3236. // 状態判定
  3237. if (ms[i].state >= 0 && ms[i].state != md->state.skillstate)
  3238. continue;
  3239. if (rand() % 10000 > ms[i].permillage) //Lupus (max value = 10000)
  3240. continue;
  3241. // 条件判定
  3242. flag = (event == ms[i].cond1);
  3243. //Avoid entering on defined events to avoid "hyper-active skill use" due to the overflow of calls to this function
  3244. //in battle. The only exception is MSC_SKILLUSED which explicitly uses the event value to trigger. [Skotlex]
  3245. if (!flag && (event == -1 || event == MSC_SKILLUSED)){
  3246. switch (ms[i].cond1)
  3247. {
  3248. case MSC_ALWAYS:
  3249. flag = 1; break;
  3250. case MSC_MYHPLTMAXRATE: // HP< maxhp%
  3251. {
  3252. int max_hp = status_get_max_hp(&md->bl);
  3253. flag = (md->hp < max_hp * c2 / 100); break;
  3254. }
  3255. case MSC_MYSTATUSON: // status[num] on
  3256. case MSC_MYSTATUSOFF: // status[num] off
  3257. if (!md->sc.count) {
  3258. flag = 0;
  3259. } else if (ms[i].cond2 == -1) {
  3260. int j;
  3261. for (j = SC_COMMON_MIN; j <= SC_COMMON_MAX; j++)
  3262. if ((flag = (md->sc.data[j].timer != -1)) != 0)
  3263. break;
  3264. } else {
  3265. flag = (md->sc.data[ms[i].cond2].timer != -1);
  3266. }
  3267. flag ^= (ms[i].cond1 == MSC_MYSTATUSOFF); break;
  3268. case MSC_FRIENDHPLTMAXRATE: // friend HP < maxhp%
  3269. flag = ((fbl = mob_getfriendhpltmaxrate(md, ms[i].cond2)) != NULL); break;
  3270. case MSC_FRIENDSTATUSON: // friend status[num] on
  3271. case MSC_FRIENDSTATUSOFF: // friend status[num] off
  3272. flag = ((fmd = mob_getfriendstatus(md, ms[i].cond1, ms[i].cond2)) != NULL); break;
  3273. case MSC_SLAVELT: // slave < num
  3274. flag = (mob_countslave(&md->bl) < c2 ); break;
  3275. case MSC_ATTACKPCGT: // attack pc > num
  3276. flag = (battle_counttargeted(&md->bl, NULL, 0) > c2); break;
  3277. case MSC_SLAVELE: // slave <= num
  3278. flag = (mob_countslave(&md->bl) <= c2 ); break;
  3279. case MSC_ATTACKPCGE: // attack pc >= num
  3280. flag = (battle_counttargeted(&md->bl, NULL, 0) >= c2); break;
  3281. case MSC_AFTERSKILL:
  3282. flag = (md->skillid == c2); break;
  3283. case MSC_SKILLUSED: // specificated skill used
  3284. flag = ((event & 0xffff) == MSC_SKILLUSED && ((event >> 16) == c2 || c2 == 0)); break;
  3285. case MSC_RUDEATTACKED:
  3286. flag = (!md->attacked_id && md->attacked_count > 0);
  3287. if (flag) md->attacked_count = 0; //Rude attacked count should be reset after the skill condition is met. Thanks to Komurka [Skotlex]
  3288. break;
  3289. case MSC_MASTERHPLTMAXRATE:
  3290. flag = ((fbl = mob_getmasterhpltmaxrate(md, ms[i].cond2)) != NULL); break;
  3291. case MSC_MASTERATTACKED:
  3292. flag = (md->master_id > 0 && battle_counttargeted(map_id2bl(md->master_id), NULL, 0) > 0); break;
  3293. case MSC_ALCHEMIST:
  3294. flag = (md->state.alchemist);
  3295. break;
  3296. }
  3297. }
  3298. if (!flag)
  3299. continue; //Skill requisite failed to be fulfilled.
  3300. //Execute skill
  3301. if (skill_get_inf(ms[i].skill_id) & INF_GROUND_SKILL) {
  3302. // 場所指定
  3303. struct block_list *bl = NULL;
  3304. int x = 0, y = 0;
  3305. if (ms[i].target <= MST_AROUND) {
  3306. switch (ms[i].target) {
  3307. case MST_TARGET:
  3308. case MST_AROUND5:
  3309. case MST_AROUND6:
  3310. case MST_AROUND7:
  3311. case MST_AROUND8:
  3312. bl = map_id2bl(md->target_id);
  3313. break;
  3314. case MST_MASTER:
  3315. bl = &md->bl;
  3316. if (md->master_id)
  3317. bl = map_id2bl(md->master_id);
  3318. if (bl) //Otherwise, fall through.
  3319. break;
  3320. case MST_FRIEND:
  3321. if (fbl)
  3322. {
  3323. bl = fbl;
  3324. break;
  3325. } else if (fmd) {
  3326. bl= &fmd->bl;
  3327. break;
  3328. } // else fall through
  3329. default:
  3330. bl = &md->bl;
  3331. break;
  3332. }
  3333. if (bl != NULL) {
  3334. x = bl->x; y=bl->y;
  3335. }
  3336. }
  3337. if (x <= 0 || y <= 0)
  3338. continue;
  3339. // 自分の周囲
  3340. if (ms[i].target >= MST_AROUND1) {
  3341. int bx = x, by = y, i = 0, m = bl->m, r = (ms[i].target-MST_AROUND1) +1;
  3342. do {
  3343. bx = x + rand() % (r*2+1) - r;
  3344. by = y + rand() % (r*2+1) - r;
  3345. } while (map_getcell(m, bx, by, CELL_CHKNOPASS) && (i++) < 1000);
  3346. if (i < 1000){
  3347. x = bx; y = by;
  3348. }
  3349. }
  3350. // 相手の周囲
  3351. if (ms[i].target >= MST_AROUND5) {
  3352. int bx = x, by = y, i = 0, m = bl->m, r = (ms[i].target-MST_AROUND5) + 1;
  3353. do {
  3354. bx = x + rand() % (r*2+1) - r;
  3355. by = y + rand() % (r*2+1) - r;
  3356. } while (map_getcell(m, bx, by, CELL_CHKNOPASS) && (i++) < 1000);
  3357. if (i < 1000){
  3358. x = bx; y = by;
  3359. }
  3360. }
  3361. if (!mobskill_use_pos(md, x, y, i))
  3362. return 0;
  3363. } else {
  3364. // ID指定
  3365. if (ms[i].target <= MST_MASTER) {
  3366. struct block_list *bl;
  3367. switch (ms[i].target) {
  3368. case MST_TARGET:
  3369. bl = map_id2bl(md->target_id);
  3370. break;
  3371. case MST_MASTER:
  3372. bl = &md->bl;
  3373. if (md->master_id)
  3374. bl = map_id2bl(md->master_id);
  3375. if (bl) //Otherwise, fall through.
  3376. break;
  3377. case MST_FRIEND:
  3378. if (fbl) {
  3379. bl = fbl;
  3380. break;
  3381. } else if (fmd) {
  3382. bl = &fmd->bl;
  3383. break;
  3384. } // else fall through
  3385. default:
  3386. bl = &md->bl;
  3387. break;
  3388. }
  3389. if (bl && !mobskill_use_id(md, bl, i))
  3390. return 0;
  3391. }
  3392. }
  3393. return 1;
  3394. }
  3395. return 0;
  3396. }
  3397. /*==========================================
  3398. * Skill use event processing
  3399. *------------------------------------------
  3400. */
  3401. int mobskill_event(struct mob_data *md, int flag)
  3402. {
  3403. int tick = gettick();
  3404. nullpo_retr(0, md);
  3405. if (flag == -1 && mobskill_use(md, tick, MSC_CASTTARGETED))
  3406. return 1;
  3407. if ((flag & BF_SHORT) && mobskill_use(md, tick, MSC_CLOSEDATTACKED))
  3408. return 1;
  3409. if ((flag & BF_LONG) && mobskill_use(md, tick, MSC_LONGRANGEATTACKED))
  3410. return 1;
  3411. return 0;
  3412. }
  3413. /*==========================================
  3414. * スキル用タイマー削除
  3415. *------------------------------------------
  3416. */
  3417. int mobskill_deltimer(struct mob_data *md )
  3418. {
  3419. nullpo_retr(0, md);
  3420. if( md->skilltimer!=-1 ){
  3421. if( skill_get_inf( md->skillid )& INF_GROUND_SKILL )
  3422. delete_timer( md->skilltimer, mobskill_castend_pos );
  3423. else
  3424. delete_timer( md->skilltimer, mobskill_castend_id );
  3425. md->skilltimer=-1;
  3426. }
  3427. return 0;
  3428. }
  3429. // Player cloned mobs. [Valaris]
  3430. int mob_is_clone(int class_)
  3431. {
  3432. if(class_ >= MOB_CLONE_START && class_ <= MOB_CLONE_END)
  3433. return 1;
  3434. return 0;
  3435. }
  3436. //Flag values:
  3437. //&1: Set special ai (fight mobs, not players)
  3438. //If mode is not passed, a default aggressive mode is used.
  3439. //If master_id is passed, clone is attached to him.
  3440. //Returns: ID of newly crafted copy.
  3441. int mob_clone_spawn(struct map_session_data *sd, char *map, int x, int y, const char *event, int master_id, int mode, int flag, unsigned int duration)
  3442. {
  3443. int class_;
  3444. int i,j,inf,skill_id;
  3445. struct mob_skill *ms;
  3446. nullpo_retr(0, sd);
  3447. for(class_=MOB_CLONE_START; class_<MOB_CLONE_END; class_++){
  3448. if(mob_db_data[class_]==NULL)
  3449. break;
  3450. }
  3451. if(class_>MOB_CLONE_END)
  3452. return 0;
  3453. mob_db_data[class_]=(struct mob_db*)aCalloc(1, sizeof(struct mob_db));
  3454. mob_db_data[class_]->view_class=sd->status.class_;
  3455. sprintf(mob_db_data[class_]->name,sd->status.name);
  3456. sprintf(mob_db_data[class_]->jname,sd->status.name);
  3457. mob_db_data[class_]->lv=status_get_lv(&sd->bl);
  3458. mob_db_data[class_]->max_hp=status_get_max_hp(&sd->bl);
  3459. mob_db_data[class_]->max_sp=0;
  3460. mob_db_data[class_]->base_exp=1;
  3461. mob_db_data[class_]->job_exp=1;
  3462. mob_db_data[class_]->range=status_get_range(&sd->bl);
  3463. mob_db_data[class_]->atk1=status_get_batk(&sd->bl); //Base attack as minimum damage.
  3464. mob_db_data[class_]->atk2=mob_db_data[class_]->atk1 + status_get_atk(&sd->bl)+status_get_atk2(&sd->bl); //batk + weapon dmg
  3465. mob_db_data[class_]->def=status_get_def(&sd->bl);
  3466. mob_db_data[class_]->mdef=status_get_mdef(&sd->bl);
  3467. mob_db_data[class_]->str=status_get_str(&sd->bl);
  3468. mob_db_data[class_]->agi=status_get_agi(&sd->bl);
  3469. mob_db_data[class_]->vit=status_get_vit(&sd->bl);
  3470. mob_db_data[class_]->int_=status_get_int(&sd->bl);
  3471. mob_db_data[class_]->dex=status_get_dex(&sd->bl);
  3472. mob_db_data[class_]->luk=status_get_luk(&sd->bl);
  3473. mob_db_data[class_]->range2=AREA_SIZE*2/3; //Chase area of 2/3rds of a screen.
  3474. mob_db_data[class_]->range3=AREA_SIZE; //Let them have the same view-range as players.
  3475. mob_db_data[class_]->size=status_get_size(&sd->bl);
  3476. mob_db_data[class_]->race=status_get_race(&sd->bl);
  3477. mob_db_data[class_]->element=status_get_element(&sd->bl);
  3478. mob_db_data[class_]->mode=mode?mode:(MD_AGGRESSIVE|MD_ASSIST|MD_CANATTACK|MD_CANMOVE);
  3479. mob_db_data[class_]->speed=status_get_speed(&sd->bl);
  3480. mob_db_data[class_]->adelay=status_get_adelay(&sd->bl);
  3481. mob_db_data[class_]->amotion=status_get_amotion(&sd->bl);
  3482. mob_db_data[class_]->dmotion=status_get_dmotion(&sd->bl);
  3483. mob_db_data[class_]->sex=sd->status.sex;
  3484. mob_db_data[class_]->hair=sd->status.hair;
  3485. mob_db_data[class_]->hair_color=sd->status.hair_color;
  3486. #if PACKETVER < 4
  3487. mob_db_data[class_]->weapon = sd->status.weapon;
  3488. mob_db_data[class_]->shield = sd->status.shield;
  3489. #else
  3490. if (sd->equip_index[9] >= 0 && sd->inventory_data[sd->equip_index[9]] && sd->view_class != JOB_WEDDING && sd->view_class != JOB_XMAS) {
  3491. if (sd->inventory_data[sd->equip_index[9]]->view_id > 0)
  3492. mob_db_data[class_]->weapon=sd->inventory_data[sd->equip_index[9]]->view_id;
  3493. else
  3494. mob_db_data[class_]->weapon=sd->status.inventory[sd->equip_index[9]].nameid;
  3495. } else
  3496. mob_db_data[class_]->shield=0;
  3497. if (sd->equip_index[8] >= 0 && sd->equip_index[8] != sd->equip_index[9] && sd->inventory_data[sd->equip_index[8]] && sd->view_class != JOB_WEDDING && sd->view_class != JOB_XMAS) {
  3498. if (sd->inventory_data[sd->equip_index[8]]->view_id > 0)
  3499. mob_db_data[class_]->shield=sd->inventory_data[sd->equip_index[8]]->view_id;
  3500. else
  3501. mob_db_data[class_]->shield=sd->status.inventory[sd->equip_index[8]].nameid;
  3502. } else
  3503. mob_db_data[class_]->shield=0;
  3504. #endif
  3505. mob_db_data[class_]->head_top=sd->status.head_top;
  3506. mob_db_data[class_]->head_mid=sd->status.head_mid;
  3507. mob_db_data[class_]->head_buttom=sd->status.head_bottom;
  3508. mob_db_data[class_]->option=sd->sc.option;
  3509. mob_db_data[class_]->clothes_color=sd->status.clothes_color;
  3510. //Skill copy [Skotlex]
  3511. ms = &mob_db_data[class_]->skill[0];
  3512. //Go Backwards to give better priority to advanced skills.
  3513. for (i=0,j = MAX_SKILL_TREE-1;j>=0 && i< MAX_MOBSKILL ;j--) {
  3514. skill_id = skill_tree[sd->status.class_][j].id;
  3515. if (!skill_id || sd->status.skill[skill_id].lv < 1 || (skill_get_inf2(skill_id)&(INF2_WEDDING_SKILL|INF2_GUILD_SKILL)))
  3516. continue;
  3517. memset (&ms[i], 0, sizeof(struct mob_skill));
  3518. ms[i].skill_id = skill_id;
  3519. ms[i].skill_lv = sd->status.skill[skill_id].lv;
  3520. ms[i].state = -1;
  3521. ms[i].permillage = 500; //Default chance for moving/idle skills.
  3522. ms[i].emotion = -1;
  3523. ms[i].cancel = 0;
  3524. ms[i].delay = 5000+skill_delayfix(&sd->bl,skill_id, ms[i].skill_lv, 0);
  3525. ms[i].casttime = skill_castfix(&sd->bl,skill_id, ms[i].skill_lv, 0);
  3526. inf = skill_get_inf(skill_id);
  3527. if (inf&INF_ATTACK_SKILL) {
  3528. ms[i].target = MST_TARGET;
  3529. ms[i].cond1 = MSC_ALWAYS;
  3530. if (skill_get_range(skill_id, ms[i].skill_lv) > 3) {
  3531. ms[i].state = MSS_RUSH;
  3532. } else {
  3533. ms[i].state = MSS_BERSERK;
  3534. ms[i].permillage = 2500;
  3535. }
  3536. } else if(inf&INF_GROUND_SKILL) {
  3537. //Normal aggressive mob, disable skills that cannot help them fight
  3538. //against players (those with flags UF_NOMOB and UF_NOPC are specific
  3539. //to always aid players!) [Skotlex]
  3540. if (!(flag&1) && skill_get_unit_flag(skill_id)&(UF_NOMOB|UF_NOPC))
  3541. continue;
  3542. ms[i].permillage = 1000;
  3543. if (skill_get_inf2(skill_id)&INF2_TRAP) { //Traps!
  3544. ms[i].state = MSS_IDLE;
  3545. ms[i].target = MST_AROUND2;
  3546. ms[i].delay = 60000;
  3547. } else if (skill_get_unit_target(skill_id) == BCT_ENEMY) { //Target Enemy
  3548. ms[i].target = MST_TARGET;
  3549. ms[i].cond1 = MSC_ALWAYS;
  3550. } else { //Target allies
  3551. ms[i].target = MST_FRIEND;
  3552. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  3553. ms[i].cond2 = 95;
  3554. }
  3555. } else if (inf&INF_SELF_SKILL) {
  3556. if (skill_get_nk(skill_id) != NK_NO_DAMAGE) { //Offensive skill
  3557. ms[i].target = MST_TARGET;
  3558. ms[i].state = MSS_BERSERK;
  3559. } else //Self skill
  3560. ms[i].target = MST_SELF;
  3561. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  3562. ms[i].cond2 = 90;
  3563. ms[i].permillage = 2000;
  3564. //Delay: Remove the stock 5 secs and add half of the support time.
  3565. ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
  3566. if (ms[i].delay < 5000)
  3567. ms[i].delay = 5000; //With a minimum of 5 secs.
  3568. } else if (inf&INF_SUPPORT_SKILL) {
  3569. ms[i].target = MST_FRIEND;
  3570. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  3571. ms[i].cond2 = 90;
  3572. if (skill_id == AL_HEAL)
  3573. ms[i].permillage = 5000; //Higher skill rate usage for heal.
  3574. else if (skill_id == ALL_RESURRECTION)
  3575. ms[i].cond2 = 1;
  3576. //Delay: Remove the stock 5 secs and add half of the support time.
  3577. ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
  3578. if (ms[i].delay < 2000)
  3579. ms[i].delay = 2000; //With a minimum of 2 secs.
  3580. if (i+1 < MAX_MOBSKILL) { //duplicate this so it also triggers on self.
  3581. memcpy(&ms[i+1], &ms[i], sizeof(struct mob_skill));
  3582. mob_db_data[class_]->maxskill = ++i;
  3583. ms[i].target = MST_SELF;
  3584. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  3585. }
  3586. } else {
  3587. switch (skill_id) { //Certain Special skills that are passive, and thus, never triggered.
  3588. case MO_TRIPLEATTACK:
  3589. case TF_DOUBLE:
  3590. ms[i].state = MSS_BERSERK;
  3591. ms[i].target = MST_TARGET;
  3592. ms[i].cond1 = MSC_ALWAYS;
  3593. ms[i].permillage = skill_id==TF_DOUBLE?(ms[i].skill_lv*500):(3000-ms[i].skill_lv*100);
  3594. ms[i].delay -= 5000; //Remove the added delay as these could trigger on "all hits".
  3595. break;
  3596. default: //Untreated Skill
  3597. continue;
  3598. }
  3599. }
  3600. if (battle_config.mob_skill_rate!= 100)
  3601. ms[i].permillage = ms[i].permillage*battle_config.mob_skill_rate/100;
  3602. if (battle_config.mob_skill_delay != 100)
  3603. ms[i].delay = ms[i].delay*battle_config.mob_skill_delay/100;
  3604. mob_db_data[class_]->maxskill = ++i;
  3605. }
  3606. //Finally, spawn it.
  3607. i = mob_once_spawn(sd,(char*)map,x,y,"--en--",class_,1,event);
  3608. if ((master_id || flag || duration) && i) { //Further manipulate crafted char.
  3609. struct mob_data* md = (struct mob_data*)map_id2bl(i);
  3610. if (md && md->bl.type == BL_MOB) {
  3611. if (flag&1) //Friendly Character
  3612. md->special_state.ai = 1;
  3613. if (master_id) //Attach to Master
  3614. md->master_id = master_id;
  3615. if (duration) //Auto Delete after a while.
  3616. md->deletetimer = add_timer (gettick() + duration, mob_timer_delete, i, 0);
  3617. }
  3618. #if 0
  3619. //I am playing with this for packet-research purposes, enable it if you want, but don't remove it :X [Skotlex]
  3620. //Guardian data
  3621. if (sd->status.guild_id) {
  3622. struct guild* g = guild_search(sd->status.guild_id);
  3623. md->guardian_data = aCalloc(1, sizeof(struct guardian_data));
  3624. md->guardian_data->castle = NULL;
  3625. md->guardian_data->number = MAX_GUARDIANS;
  3626. md->guardian_data->guild_id = sd->status.guild_id;
  3627. if (g)
  3628. {
  3629. md->guardian_data->emblem_id = g->emblem_id;
  3630. memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
  3631. }
  3632. }
  3633. #endif
  3634. }
  3635. return i;
  3636. }
  3637. int mob_clone_delete(int class_)
  3638. {
  3639. int i;
  3640. for(i=MOB_CLONE_START; i<MOB_CLONE_END; i++){
  3641. if(i==class_ && mob_db_data[i]!=NULL){
  3642. aFree(mob_db_data[i]);
  3643. mob_db_data[i]=NULL;
  3644. break;
  3645. }
  3646. }
  3647. return 0;
  3648. }
  3649. //
  3650. // 初期化
  3651. //
  3652. /*==========================================
  3653. * Since un-setting [ mob ] up was used, it is an initial provisional value setup.
  3654. *------------------------------------------
  3655. */
  3656. static int mob_makedummymobdb(int class_)
  3657. {
  3658. if (mob_dummy != NULL)
  3659. {
  3660. if (mob_db(class_) == mob_dummy)
  3661. return 1; //Using the mob_dummy data already. [Skotlex]
  3662. if (class_ > 0 && class_ <= MAX_MOB_DB)
  3663. { //Remove the mob data so that it uses the dummy data instead.
  3664. aFree(mob_db_data[class_]);
  3665. mob_db_data[class_] = NULL;
  3666. }
  3667. return 0;
  3668. }
  3669. //Initialize dummy data.
  3670. mob_dummy = (struct mob_db*)aCalloc(1, sizeof(struct mob_db)); //Initializing the dummy mob.
  3671. sprintf(mob_dummy->name,"DUMMY");
  3672. sprintf(mob_dummy->jname,"Dummy");
  3673. mob_dummy->lv=1;
  3674. mob_dummy->max_hp=1000;
  3675. mob_dummy->max_sp=1;
  3676. mob_dummy->base_exp=2;
  3677. mob_dummy->job_exp=1;
  3678. mob_dummy->range=1;
  3679. mob_dummy->atk1=7;
  3680. mob_dummy->atk2=10;
  3681. mob_dummy->def=0;
  3682. mob_dummy->mdef=0;
  3683. mob_dummy->str=1;
  3684. mob_dummy->agi=1;
  3685. mob_dummy->vit=1;
  3686. mob_dummy->int_=1;
  3687. mob_dummy->dex=6;
  3688. mob_dummy->luk=2;
  3689. mob_dummy->range2=10;
  3690. mob_dummy->range3=10;
  3691. mob_dummy->race=0;
  3692. mob_dummy->element=0;
  3693. mob_dummy->mode=0;
  3694. mob_dummy->speed=300;
  3695. mob_dummy->adelay=1000;
  3696. mob_dummy->amotion=500;
  3697. mob_dummy->dmotion=500;
  3698. return 0;
  3699. }
  3700. //Adjusts the drop rate of item according to the criteria given. [Skotlex]
  3701. static int mob_drop_adjust(int rate, int rate_adjust, int rate_min, int rate_max)
  3702. {
  3703. if (battle_config.logarithmic_drops && rate_adjust > 0) //Logarithmic drops equation by Ishizu-Chan
  3704. //Equation: Droprate(x,y) = x * (5 - log(x)) ^ (ln(y) / ln(5))
  3705. //x is the normal Droprate, y is the Modificator.
  3706. rate = (int)(rate * pow((5.0 - log10(rate)), (log(rate_adjust/100.) / log(5.0))) + 0.5);
  3707. else //Classical linear rate adjustment.
  3708. rate = rate*rate_adjust/100;
  3709. return (rate>rate_max)?rate_max:((rate<rate_min)?rate_min:rate);
  3710. }
  3711. /*==========================================
  3712. * mob_db.txt reading
  3713. *------------------------------------------
  3714. */
  3715. static int mob_readdb(void)
  3716. {
  3717. FILE *fp;
  3718. char line[1024];
  3719. char *filename[]={ "mob_db.txt","mob_db2.txt" };
  3720. int class_, i, fi;
  3721. for(fi=0;fi<2;fi++){
  3722. sprintf(line, "%s/%s", db_path, filename[fi]);
  3723. fp=fopen(line,"r");
  3724. if(fp==NULL){
  3725. if(fi>0)
  3726. continue;
  3727. return -1;
  3728. }
  3729. while(fgets(line,1020,fp)){
  3730. double exp, maxhp;
  3731. char *str[60], *p, *np; // 55->60 Lupus
  3732. if(line[0] == '/' && line[1] == '/')
  3733. continue;
  3734. for(i=0,p=line;i<60;i++){
  3735. if((np=strchr(p,','))!=NULL){
  3736. str[i]=p;
  3737. *np=0;
  3738. p=np+1;
  3739. } else
  3740. str[i]=p;
  3741. }
  3742. class_ = atoi(str[0]);
  3743. if (class_ == 0)
  3744. continue; //Leave blank lines alone... [Skotlex]
  3745. if (class_ <= 1000 || class_ > MAX_MOB_DB)
  3746. {
  3747. ShowWarning("Mob with ID: %d not loaded. ID must be in range [%d-%d]\n", class_, 1000, MAX_MOB_DB);
  3748. continue;
  3749. } else if (pcdb_checkid(class_))
  3750. {
  3751. ShowWarning("Mob with ID: %d not loaded. That ID is reserved for player classes.\n");
  3752. continue;
  3753. }
  3754. if (mob_db_data[class_] == NULL)
  3755. mob_db_data[class_] = aCalloc(1, sizeof (struct mob_data));
  3756. mob_db_data[class_]->view_class = class_;
  3757. memcpy(mob_db_data[class_]->name, str[1], NAME_LENGTH-1);
  3758. memcpy(mob_db_data[class_]->jname, str[2], NAME_LENGTH-1);
  3759. mob_db_data[class_]->lv = atoi(str[3]);
  3760. mob_db_data[class_]->max_hp = atoi(str[4]);
  3761. mob_db_data[class_]->max_sp = atoi(str[5]);
  3762. exp = (double)atoi(str[6]) * (double)battle_config.base_exp_rate / 100.;
  3763. if (exp < 0)
  3764. mob_db_data[class_]->base_exp = 0;
  3765. if (exp > UINT_MAX)
  3766. mob_db_data[class_]->base_exp = UINT_MAX;
  3767. else
  3768. mob_db_data[class_]->base_exp = (unsigned int)exp;
  3769. exp = (double)atoi(str[7]) * (double)battle_config.job_exp_rate / 100.;
  3770. if (exp < 0)
  3771. mob_db_data[class_]->job_exp = 0;
  3772. else if (exp > UINT_MAX)
  3773. mob_db_data[class_]->job_exp = UINT_MAX;
  3774. else
  3775. mob_db_data[class_]->job_exp = (unsigned int)exp;
  3776. mob_db_data[class_]->range=atoi(str[8]);
  3777. mob_db_data[class_]->atk1=atoi(str[9]);
  3778. mob_db_data[class_]->atk2=atoi(str[10]);
  3779. mob_db_data[class_]->def=atoi(str[11]);
  3780. mob_db_data[class_]->mdef=atoi(str[12]);
  3781. mob_db_data[class_]->str=atoi(str[13]);
  3782. mob_db_data[class_]->agi=atoi(str[14]);
  3783. mob_db_data[class_]->vit=atoi(str[15]);
  3784. mob_db_data[class_]->int_=atoi(str[16]);
  3785. mob_db_data[class_]->dex=atoi(str[17]);
  3786. mob_db_data[class_]->luk=atoi(str[18]);
  3787. mob_db_data[class_]->range2=atoi(str[19]);
  3788. mob_db_data[class_]->range3=atoi(str[20]);
  3789. mob_db_data[class_]->size=atoi(str[21]);
  3790. mob_db_data[class_]->race=atoi(str[22]);
  3791. mob_db_data[class_]->element=atoi(str[23]);
  3792. mob_db_data[class_]->mode=atoi(str[24]);
  3793. mob_db_data[class_]->speed=atoi(str[25]);
  3794. mob_db_data[class_]->adelay=atoi(str[26]);
  3795. mob_db_data[class_]->amotion=atoi(str[27]);
  3796. mob_db_data[class_]->dmotion=atoi(str[28]);
  3797. for(i=0;i<10;i++){ // 8 -> 10 Lupus
  3798. int rate = 0,rate_adjust,type,ratemin,ratemax;
  3799. struct item_data *id;
  3800. mob_db_data[class_]->dropitem[i].nameid=atoi(str[29+i*2]);
  3801. type = itemdb_type(mob_db_data[class_]->dropitem[i].nameid);
  3802. rate = atoi(str[30+i*2]);
  3803. if (class_ >= 1324 && class_ <= 1363)
  3804. { //Treasure box drop rates [Skotlex]
  3805. rate_adjust = battle_config.item_rate_treasure;
  3806. ratemin = battle_config.item_drop_treasure_min;
  3807. ratemax = battle_config.item_drop_treasure_max;
  3808. }
  3809. else switch (type)
  3810. {
  3811. case 0:
  3812. rate_adjust = battle_config.item_rate_heal;
  3813. ratemin = battle_config.item_drop_heal_min;
  3814. ratemax = battle_config.item_drop_heal_max;
  3815. break;
  3816. case 2:
  3817. rate_adjust = battle_config.item_rate_use;
  3818. ratemin = battle_config.item_drop_use_min;
  3819. ratemax = battle_config.item_drop_use_max;
  3820. break;
  3821. case 4:
  3822. case 5:
  3823. case 8: // Changed to include Pet Equip
  3824. rate_adjust = battle_config.item_rate_equip;
  3825. ratemin = battle_config.item_drop_equip_min;
  3826. ratemax = battle_config.item_drop_equip_max;
  3827. break;
  3828. case 6:
  3829. rate_adjust = battle_config.item_rate_card;
  3830. ratemin = battle_config.item_drop_card_min;
  3831. ratemax = battle_config.item_drop_card_max;
  3832. break;
  3833. default:
  3834. rate_adjust = battle_config.item_rate_common;
  3835. ratemin = battle_config.item_drop_common_min;
  3836. ratemax = battle_config.item_drop_common_max;
  3837. break;
  3838. }
  3839. mob_db_data[class_]->dropitem[i].p = mob_drop_adjust(rate, rate_adjust, ratemin, ratemax);
  3840. //calculate and store Max available drop chance of the item
  3841. id = itemdb_search(mob_db_data[class_]->dropitem[i].nameid);
  3842. if (mob_db_data[class_]->dropitem[i].p) {
  3843. if (id->maxchance==10000 || (id->maxchance < mob_db_data[class_]->dropitem[i].p) ) {
  3844. //item has bigger drop chance or sold in shops
  3845. id->maxchance = mob_db_data[class_]->dropitem[i].p;
  3846. }
  3847. }
  3848. }
  3849. // MVP EXP Bonus, Chance: MEXP,ExpPer
  3850. mob_db_data[class_]->mexp=atoi(str[49])*battle_config.mvp_exp_rate/100;
  3851. mob_db_data[class_]->mexpper=atoi(str[50]);
  3852. //Now that we know if it is an mvp or not,
  3853. //apply battle_config modifiers [Skotlex]
  3854. maxhp = (double)mob_db_data[class_]->max_hp;
  3855. if (mob_db_data[class_]->mexp > 0)
  3856. { //Mvp
  3857. if (battle_config.mvp_hp_rate != 100)
  3858. maxhp = maxhp * (double)battle_config.mvp_hp_rate /100.;
  3859. } else if (battle_config.monster_hp_rate != 100) //Normal mob
  3860. maxhp = maxhp * (double)battle_config.monster_hp_rate /100.;
  3861. if (maxhp < 1) maxhp = 1;
  3862. else if (maxhp > 0x7fffffff) maxhp = 0x7fffffff;
  3863. mob_db_data[class_]->max_hp = (int)maxhp;
  3864. // MVP Drops: MVP1id,MVP1per,MVP2id,MVP2per,MVP3id,MVP3per
  3865. for(i=0;i<3;i++){
  3866. struct item_data *id;
  3867. int rate=atoi(str[52+i*2]);
  3868. mob_db_data[class_]->mvpitem[i].nameid=atoi(str[51+i*2]);
  3869. mob_db_data[class_]->mvpitem[i].p= mob_drop_adjust(rate, battle_config.item_rate_mvp,
  3870. battle_config.item_drop_mvp_min, battle_config.item_drop_mvp_max);
  3871. //calculate and store Max available drop chance of the MVP item
  3872. id = itemdb_search(mob_db_data[class_]->mvpitem[i].nameid);
  3873. if (mob_db_data[class_]->mvpitem[i].p) {
  3874. if (id->maxchance==10000 || (id->maxchance < mob_db_data[class_]->mvpitem[i].p/10+1) ) {
  3875. //item has bigger drop chance or sold in shops
  3876. id->maxchance = mob_db_data[class_]->mvpitem[i].p/10+1; //reduce MVP drop info to not spoil common drop rate
  3877. }
  3878. }
  3879. }
  3880. if (mob_db_data[class_]->max_hp <= 0) {
  3881. ShowWarning ("Mob %d (%s) has no HP, using poring data for it\n", class_, mob_db_data[class_]->jname);
  3882. mob_makedummymobdb(class_);
  3883. }
  3884. }
  3885. fclose(fp);
  3886. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",filename[fi]);
  3887. }
  3888. return 0;
  3889. }
  3890. /*==========================================
  3891. * MOB display graphic change data reading
  3892. *------------------------------------------
  3893. */
  3894. static int mob_readdb_mobavail(void)
  3895. {
  3896. FILE *fp;
  3897. char line[1024];
  3898. int ln=0;
  3899. int class_,j,k;
  3900. char *str[20],*p,*np;
  3901. sprintf(line, "%s/mob_avail.txt", db_path);
  3902. if( (fp=fopen(line,"r"))==NULL ){
  3903. ShowError("can't read %s\n", line);
  3904. return -1;
  3905. }
  3906. while(fgets(line,1020,fp)){
  3907. if(line[0]=='/' && line[1]=='/')
  3908. continue;
  3909. memset(str,0,sizeof(str));
  3910. for(j=0,p=line;j<12;j++){
  3911. if((np=strchr(p,','))!=NULL){
  3912. str[j]=p;
  3913. *np=0;
  3914. p=np+1;
  3915. } else
  3916. str[j]=p;
  3917. }
  3918. if(str[0]==NULL)
  3919. continue;
  3920. class_=atoi(str[0]);
  3921. if (class_ == 0)
  3922. continue; //Leave blank lines alone... [Skotlex]
  3923. if(mob_db(class_) == mob_dummy) // 値が異常なら処理しない。
  3924. continue;
  3925. k=atoi(str[1]);
  3926. if(k < 0)
  3927. continue;
  3928. if (j > 3 && k > 23 && k < 69)
  3929. k += 3977; // advanced job/baby class
  3930. mob_db_data[class_]->view_class=k;
  3931. //Player sprites
  3932. if(pcdb_checkid(k)) {
  3933. mob_db_data[class_]->sex=atoi(str[2]);
  3934. mob_db_data[class_]->hair=atoi(str[3]);
  3935. mob_db_data[class_]->hair_color=atoi(str[4]);
  3936. mob_db_data[class_]->weapon=atoi(str[5]);
  3937. mob_db_data[class_]->shield=atoi(str[6]);
  3938. mob_db_data[class_]->head_top=atoi(str[7]);
  3939. mob_db_data[class_]->head_mid=atoi(str[8]);
  3940. mob_db_data[class_]->head_buttom=atoi(str[9]);
  3941. mob_db_data[class_]->option=atoi(str[10])&~0x46;
  3942. mob_db_data[class_]->clothes_color=atoi(str[11]); // Monster player dye option - Valaris
  3943. }
  3944. else if(atoi(str[2]) > 0) mob_db_data[class_]->equip=atoi(str[2]); // mob equipment [Valaris]
  3945. ln++;
  3946. }
  3947. fclose(fp);
  3948. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,"mob_avail.txt");
  3949. return 0;
  3950. }
  3951. /*==========================================
  3952. * Reading of random monster data
  3953. *------------------------------------------
  3954. */
  3955. static int mob_read_randommonster(void)
  3956. {
  3957. FILE *fp;
  3958. char line[1024];
  3959. char *str[10],*p;
  3960. int i,j;
  3961. const char* mobfile[] = {
  3962. "mob_branch.txt",
  3963. "mob_poring.txt",
  3964. "mob_boss.txt" };
  3965. for(i=0;i<MAX_RANDOMMONSTER;i++){
  3966. mob_db_data[0]->summonper[i] = 1002; // 設定し忘れた場合はポリンが出るようにしておく
  3967. sprintf(line, "%s/%s", db_path, mobfile[i]);
  3968. fp=fopen(line,"r");
  3969. if(fp==NULL){
  3970. ShowError("can't read %s\n",line);
  3971. return -1;
  3972. }
  3973. while(fgets(line,1020,fp)){
  3974. int class_,per;
  3975. if(line[0] == '/' && line[1] == '/')
  3976. continue;
  3977. memset(str,0,sizeof(str));
  3978. for(j=0,p=line;j<3 && p;j++){
  3979. str[j]=p;
  3980. p=strchr(p,',');
  3981. if(p) *p++=0;
  3982. }
  3983. if(str[0]==NULL || str[2]==NULL)
  3984. continue;
  3985. class_ = atoi(str[0]);
  3986. per=atoi(str[2]);
  3987. if(mob_db(class_) != mob_dummy)
  3988. mob_db_data[class_]->summonper[i]=per;
  3989. }
  3990. fclose(fp);
  3991. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",mobfile[i]);
  3992. }
  3993. return 0;
  3994. }
  3995. /*==========================================
  3996. * mob_skill_db.txt reading
  3997. *------------------------------------------
  3998. */
  3999. static int mob_readskilldb(void)
  4000. {
  4001. FILE *fp;
  4002. char line[1024];
  4003. int i,tmp;
  4004. const struct {
  4005. char str[32];
  4006. int id;
  4007. } cond1[] = {
  4008. { "always", MSC_ALWAYS },
  4009. { "myhpltmaxrate", MSC_MYHPLTMAXRATE },
  4010. { "friendhpltmaxrate",MSC_FRIENDHPLTMAXRATE },
  4011. { "mystatuson", MSC_MYSTATUSON },
  4012. { "mystatusoff", MSC_MYSTATUSOFF },
  4013. { "friendstatuson", MSC_FRIENDSTATUSON },
  4014. { "friendstatusoff", MSC_FRIENDSTATUSOFF },
  4015. { "attackpcgt", MSC_ATTACKPCGT },
  4016. { "attackpcge", MSC_ATTACKPCGE },
  4017. { "slavelt", MSC_SLAVELT },
  4018. { "slavele", MSC_SLAVELE },
  4019. { "closedattacked", MSC_CLOSEDATTACKED },
  4020. { "longrangeattacked",MSC_LONGRANGEATTACKED },
  4021. { "skillused", MSC_SKILLUSED },
  4022. { "afterskill", MSC_AFTERSKILL },
  4023. { "casttargeted", MSC_CASTTARGETED },
  4024. { "rudeattacked", MSC_RUDEATTACKED },
  4025. { "masterhpltmaxrate",MSC_MASTERHPLTMAXRATE },
  4026. { "masterattacked", MSC_MASTERATTACKED },
  4027. { "alchemist", MSC_ALCHEMIST },
  4028. { "onspawn", MSC_SPAWN},
  4029. }, cond2[] ={
  4030. { "anybad", -1 },
  4031. { "stone", SC_STONE },
  4032. { "freeze", SC_FREEZE },
  4033. { "stan", SC_STAN },
  4034. { "sleep", SC_SLEEP },
  4035. { "poison", SC_POISON },
  4036. { "curse", SC_CURSE },
  4037. { "silence", SC_SILENCE },
  4038. { "confusion", SC_CONFUSION },
  4039. { "blind", SC_BLIND },
  4040. { "hiding", SC_HIDING },
  4041. { "sight", SC_SIGHT },
  4042. }, state[] = {
  4043. { "any", -1 },
  4044. { "idle", MSS_IDLE },
  4045. { "walk", MSS_WALK },
  4046. { "loot", MSS_LOOT },
  4047. { "dead", MSS_DEAD },
  4048. { "attack", MSS_BERSERK }, //Retaliating attack
  4049. { "angry", MSS_ANGRY }, //Preemptive attack (aggressive mobs)
  4050. { "chase", MSS_RUSH }, //Chase escaping target
  4051. { "follow", MSS_FOLLOW }, //Preemptive chase (aggressive mobs)
  4052. }, target[] = {
  4053. { "target", MST_TARGET },
  4054. { "self", MST_SELF },
  4055. { "friend", MST_FRIEND },
  4056. { "master", MST_MASTER },
  4057. { "around5", MST_AROUND5 },
  4058. { "around6", MST_AROUND6 },
  4059. { "around7", MST_AROUND7 },
  4060. { "around8", MST_AROUND8 },
  4061. { "around1", MST_AROUND1 },
  4062. { "around2", MST_AROUND2 },
  4063. { "around3", MST_AROUND3 },
  4064. { "around4", MST_AROUND4 },
  4065. { "around", MST_AROUND },
  4066. };
  4067. int x;
  4068. char *filename[]={ "mob_skill_db.txt","mob_skill_db2.txt" };
  4069. if (!battle_config.mob_skill_rate) {
  4070. ShowStatus("Mob skill use disabled. Not reading mob skills.\n");
  4071. return 0;
  4072. }
  4073. for(x=0;x<2;x++){
  4074. sprintf(line, "%s/%s", db_path, filename[x]);
  4075. fp=fopen(line,"r");
  4076. if(fp==NULL){
  4077. if(x==0)
  4078. ShowError("can't read %s\n",line);
  4079. continue;
  4080. }
  4081. while(fgets(line,1020,fp)){
  4082. char *sp[20],*p;
  4083. int mob_id;
  4084. struct mob_skill *ms, gms;
  4085. int j=0;
  4086. if(line[0] == '/' && line[1] == '/')
  4087. continue;
  4088. memset(sp,0,sizeof(sp));
  4089. for(i=0,p=line;i<18 && p;i++){
  4090. sp[i]=p;
  4091. if((p=strchr(p,','))!=NULL)
  4092. *p++=0;
  4093. }
  4094. if(i == 0 || (mob_id=atoi(sp[0]))== 0 || (mob_id > 0 && mob_db(mob_id) == mob_dummy))
  4095. continue;
  4096. if(i < 18) {
  4097. ShowError("Insufficient number of fields for Mob Skill (Mob ID[%s], Name[%s], Skill:[%s/Lv%s])\n", sp[0], i>1?sp[1]:"?", i>3?sp[3]:"?", i>4?sp[4]:"?");
  4098. continue;
  4099. }
  4100. if( strcmp(sp[1],"clear")==0 ){
  4101. if (mob_id < 0)
  4102. continue;
  4103. memset(mob_db_data[mob_id]->skill,0,sizeof(struct mob_skill));
  4104. mob_db_data[mob_id]->maxskill=0;
  4105. continue;
  4106. }
  4107. if (mob_id < 0)
  4108. { //Prepare global skill. [Skotlex]
  4109. memset(&gms, 0, sizeof (struct mob_skill));
  4110. ms = &gms;
  4111. } else {
  4112. for(i=0;i<MAX_MOBSKILL;i++)
  4113. if( (ms=&mob_db_data[mob_id]->skill[i])->skill_id == 0)
  4114. break;
  4115. if(i==MAX_MOBSKILL){
  4116. ShowWarning("mob_skill: readdb: too many skill ! [%s] in %d[%s]\n",
  4117. sp[1],mob_id,mob_db_data[mob_id]->jname);
  4118. continue;
  4119. }
  4120. }
  4121. ms->state=atoi(sp[2]);
  4122. for(j=0;j<sizeof(state)/sizeof(state[0]);j++){
  4123. if( strcmp(sp[2],state[j].str)==0)
  4124. ms->state=state[j].id;
  4125. }
  4126. //Skill ID
  4127. j=atoi(sp[3]);
  4128. if (j<=0 || j>MAX_SKILL_DB) //fixed Lupus
  4129. {
  4130. if (mob_id < 0)
  4131. ShowWarning("Invalid Skill ID (%d) for all mobs\n", j);
  4132. else
  4133. ShowWarning("Invalid Skill ID (%d) for mob %d (%s)\n", j, mob_id, mob_db_data[mob_id]->jname);
  4134. continue;
  4135. }
  4136. ms->skill_id=j;
  4137. //Skill lvl
  4138. j= atoi(sp[4])<=0 ? 1 : atoi(sp[4]);
  4139. ms->skill_lv= j>battle_config.mob_max_skilllvl ? battle_config.mob_max_skilllvl : j; //we strip max skill level
  4140. //Apply battle_config modifiers to rate (permillage) and delay [Skotlex]
  4141. tmp = atoi(sp[5]);
  4142. if (battle_config.mob_skill_rate != 100)
  4143. tmp = tmp*battle_config.mob_skill_rate/100;
  4144. if (tmp > 10000)
  4145. ms->permillage= 10000;
  4146. else
  4147. ms->permillage= tmp;
  4148. ms->casttime=atoi(sp[6]);
  4149. ms->delay=atoi(sp[7]);
  4150. if (battle_config.mob_skill_delay != 100)
  4151. ms->delay = ms->delay*battle_config.mob_skill_delay/100;
  4152. if (ms->delay < 0) //time overflow?
  4153. ms->delay = INT_MAX;
  4154. ms->cancel=atoi(sp[8]);
  4155. if( strcmp(sp[8],"yes")==0 ) ms->cancel=1;
  4156. ms->target=atoi(sp[9]);
  4157. for(j=0;j<sizeof(target)/sizeof(target[0]);j++){
  4158. if( strcmp(sp[9],target[j].str)==0)
  4159. ms->target=target[j].id;
  4160. }
  4161. ms->cond1=-1;
  4162. for(j=0;j<sizeof(cond1)/sizeof(cond1[0]);j++){
  4163. if( strcmp(sp[10],cond1[j].str)==0)
  4164. ms->cond1=cond1[j].id;
  4165. }
  4166. ms->cond2=atoi(sp[11]);
  4167. for(j=0;j<sizeof(cond2)/sizeof(cond2[0]);j++){
  4168. if( strcmp(sp[11],cond2[j].str)==0)
  4169. ms->cond2=cond2[j].id;
  4170. }
  4171. ms->val[0]=atoi(sp[12]);
  4172. ms->val[1]=atoi(sp[13]);
  4173. ms->val[2]=atoi(sp[14]);
  4174. ms->val[3]=atoi(sp[15]);
  4175. ms->val[4]=atoi(sp[16]);
  4176. if(sp[17] != NULL && strlen(sp[17])>2)
  4177. ms->emotion=atoi(sp[17]);
  4178. else
  4179. ms->emotion=-1;
  4180. if (mob_id < 0)
  4181. { //Set this skill to ALL mobs. [Skotlex]
  4182. mob_id *= -1;
  4183. for (i = 1; i < MAX_MOB_DB; i++)
  4184. {
  4185. if (mob_db_data[i] == NULL)
  4186. continue;
  4187. if (mob_db_data[i]->mode&MD_BOSS)
  4188. {
  4189. if (!(mob_id&2)) //Skill not for bosses
  4190. continue;
  4191. } else
  4192. if (!(mob_id&1)) //Skill not for normal enemies.
  4193. continue;
  4194. for(j=0;j<MAX_MOBSKILL;j++)
  4195. if( mob_db_data[i]->skill[j].skill_id == 0)
  4196. break;
  4197. if(j==MAX_MOBSKILL)
  4198. continue;
  4199. memcpy (&mob_db_data[i]->skill[j], ms, sizeof(struct mob_skill));
  4200. mob_db_data[i]->maxskill=j+1;
  4201. }
  4202. } else //Skill set on a single mob.
  4203. mob_db_data[mob_id]->maxskill=i+1;
  4204. }
  4205. fclose(fp);
  4206. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",filename[x]);
  4207. }
  4208. return 0;
  4209. }
  4210. /*==========================================
  4211. * mob_race_db.txt reading
  4212. *------------------------------------------
  4213. */
  4214. static int mob_readdb_race(void)
  4215. {
  4216. FILE *fp;
  4217. char line[1024];
  4218. int race,j,k;
  4219. char *str[20],*p,*np;
  4220. sprintf(line, "%s/mob_race2_db.txt", db_path);
  4221. if( (fp=fopen(line,"r"))==NULL ){
  4222. ShowError("can't read %s\n", line);
  4223. return -1;
  4224. }
  4225. while(fgets(line,1020,fp)){
  4226. if(line[0]=='/' && line[1]=='/')
  4227. continue;
  4228. memset(str,0,sizeof(str));
  4229. for(j=0,p=line;j<12;j++){
  4230. if((np=strchr(p,','))!=NULL){
  4231. str[j]=p;
  4232. *np=0;
  4233. p=np+1;
  4234. } else
  4235. str[j]=p;
  4236. }
  4237. if(str[0]==NULL)
  4238. continue;
  4239. race=atoi(str[0]);
  4240. if (race < 0 || race >= MAX_MOB_RACE_DB)
  4241. continue;
  4242. for (j=1; j<20; j++) {
  4243. if (!str[j])
  4244. break;
  4245. k=atoi(str[j]);
  4246. if (mob_db(k) == mob_dummy)
  4247. continue;
  4248. mob_db_data[k]->race2 = race;
  4249. }
  4250. }
  4251. fclose(fp);
  4252. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","mob_race2_db.txt");
  4253. return 0;
  4254. }
  4255. #ifndef TXT_ONLY
  4256. /*==========================================
  4257. * SQL reading
  4258. *------------------------------------------
  4259. */
  4260. static int mob_read_sqldb(void)
  4261. {
  4262. const char unknown_str[NAME_LENGTH] ="unknown";
  4263. int i, fi, class_;
  4264. double exp, maxhp;
  4265. long unsigned int ln = 0;
  4266. char *mob_db_name[] = { mob_db_db, mob_db2_db };
  4267. //For easier handling of converting. [Skotlex]
  4268. #define TO_INT(a) (sql_row[a]==NULL?0:atoi(sql_row[a]))
  4269. #define TO_STR(a) (sql_row[a]==NULL?unknown_str:sql_row[a])
  4270. for (fi = 0; fi < 2; fi++) {
  4271. sprintf (tmp_sql, "SELECT * FROM `%s`", mob_db_name[fi]);
  4272. if (mysql_query(&mmysql_handle, tmp_sql)) {
  4273. ShowSQL("DB error (%s) - %s\n", mob_db_name[fi], mysql_error(&mmysql_handle));
  4274. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  4275. continue;
  4276. }
  4277. sql_res = mysql_store_result(&mmysql_handle);
  4278. if (sql_res) {
  4279. while((sql_row = mysql_fetch_row(sql_res))){
  4280. class_ = TO_INT(0);
  4281. if (class_ <= 1000 || class_ > MAX_MOB_DB)
  4282. {
  4283. ShowWarning("Mob with ID: %d not loaded. ID must be in range [%d-%d]\n", class_, 1000, MAX_MOB_DB);
  4284. continue;
  4285. } else if (pcdb_checkid(class_))
  4286. {
  4287. ShowWarning("Mob with ID: %d not loaded. That ID is reserved for Upper Classes.\n");
  4288. continue;
  4289. }
  4290. if (mob_db_data[class_] == NULL)
  4291. mob_db_data[class_] = aCalloc(1, sizeof (struct mob_data));
  4292. ln++;
  4293. mob_db_data[class_]->view_class = class_;
  4294. memcpy(mob_db_data[class_]->name, TO_STR(1), NAME_LENGTH-1);
  4295. memcpy(mob_db_data[class_]->jname, TO_STR(2), NAME_LENGTH-1);
  4296. mob_db_data[class_]->lv = TO_INT(3);
  4297. mob_db_data[class_]->max_hp = TO_INT(4);
  4298. mob_db_data[class_]->max_sp = TO_INT(5);
  4299. exp = (double)TO_INT(6) * (double)battle_config.base_exp_rate / 100.;
  4300. if (exp < 0)
  4301. mob_db_data[class_]->base_exp = 0;
  4302. else if (exp > UINT_MAX)
  4303. mob_db_data[class_]->base_exp = UINT_MAX;
  4304. else
  4305. mob_db_data[class_]->base_exp = (unsigned int)exp;
  4306. exp = (double)TO_INT(7) * (double)battle_config.job_exp_rate / 100.;
  4307. if (exp < 0)
  4308. mob_db_data[class_]->job_exp = 0;
  4309. else if (exp > UINT_MAX)
  4310. mob_db_data[class_]->job_exp = UINT_MAX;
  4311. else
  4312. mob_db_data[class_]->job_exp = (unsigned int)exp;
  4313. mob_db_data[class_]->range = TO_INT(8);
  4314. mob_db_data[class_]->atk1 = TO_INT(9);
  4315. mob_db_data[class_]->atk2 = TO_INT(10);
  4316. mob_db_data[class_]->def = TO_INT(11);
  4317. mob_db_data[class_]->mdef = TO_INT(12);
  4318. mob_db_data[class_]->str = TO_INT(13);
  4319. mob_db_data[class_]->agi = TO_INT(14);
  4320. mob_db_data[class_]->vit = TO_INT(15);
  4321. mob_db_data[class_]->int_ = TO_INT(16);
  4322. mob_db_data[class_]->dex = TO_INT(17);
  4323. mob_db_data[class_]->luk = TO_INT(18);
  4324. mob_db_data[class_]->range2 = TO_INT(19);
  4325. mob_db_data[class_]->range3 = TO_INT(20);
  4326. mob_db_data[class_]->size = TO_INT(21);
  4327. mob_db_data[class_]->race = TO_INT(22);
  4328. mob_db_data[class_]->element = TO_INT(23);
  4329. mob_db_data[class_]->mode = TO_INT(24);
  4330. mob_db_data[class_]->speed = TO_INT(25);
  4331. mob_db_data[class_]->adelay = TO_INT(26);
  4332. mob_db_data[class_]->amotion = TO_INT(27);
  4333. mob_db_data[class_]->dmotion = TO_INT(28);
  4334. for (i = 0; i < 10; i++){ // 8 -> 10 Lupus
  4335. int rate = 0, rate_adjust, type, ratemin, ratemax;
  4336. struct item_data *id;
  4337. mob_db_data[class_]->dropitem[i].nameid=TO_INT(29+i*2);
  4338. type = itemdb_type(mob_db_data[class_]->dropitem[i].nameid);
  4339. rate = TO_INT(30+i*2);
  4340. if (class_ >= 1324 && class_ <= 1363)
  4341. { //Treasure box drop rates [Skotlex]
  4342. rate_adjust = battle_config.item_rate_treasure;
  4343. ratemin = battle_config.item_drop_treasure_min;
  4344. ratemax = battle_config.item_drop_treasure_max;
  4345. }
  4346. else switch(type)
  4347. {
  4348. case 0: // Added by Valaris
  4349. rate_adjust = battle_config.item_rate_heal;
  4350. ratemin = battle_config.item_drop_heal_min;
  4351. ratemax = battle_config.item_drop_heal_max;
  4352. break;
  4353. case 2:
  4354. rate_adjust = battle_config.item_rate_use;
  4355. ratemin = battle_config.item_drop_use_min;
  4356. ratemax = battle_config.item_drop_use_max; // End
  4357. break;
  4358. case 4:
  4359. case 5:
  4360. case 8: // Changed to include Pet Equip
  4361. rate_adjust = battle_config.item_rate_equip;
  4362. ratemin = battle_config.item_drop_equip_min;
  4363. ratemax = battle_config.item_drop_equip_max;
  4364. break;
  4365. case 6:
  4366. rate_adjust = battle_config.item_rate_card;
  4367. ratemin = battle_config.item_drop_card_min;
  4368. ratemax = battle_config.item_drop_card_max;
  4369. break;
  4370. default:
  4371. rate_adjust = battle_config.item_rate_common;
  4372. ratemin = battle_config.item_drop_common_min;
  4373. ratemax = battle_config.item_drop_common_max;
  4374. break;
  4375. }
  4376. mob_db_data[class_]->dropitem[i].p = mob_drop_adjust(rate, rate_adjust, ratemin, ratemax);
  4377. //calculate and store Max available drop chance of the item
  4378. id = itemdb_search(mob_db_data[class_]->dropitem[i].nameid);
  4379. if (mob_db_data[class_]->dropitem[i].p) {
  4380. if (id->maxchance==10000 || (id->maxchance < mob_db_data[class_]->dropitem[i].p) ) {
  4381. //item has bigger drop chance or sold in shops
  4382. id->maxchance = mob_db_data[class_]->dropitem[i].p;
  4383. }
  4384. }
  4385. }
  4386. // MVP EXP Bonus, Chance: MEXP,ExpPer
  4387. mob_db_data[class_]->mexp = TO_INT(49) * battle_config.mvp_exp_rate / 100;
  4388. mob_db_data[class_]->mexpper = TO_INT(50);
  4389. //Now that we know if it is an mvp or not,
  4390. //apply battle_config modifiers [Skotlex]
  4391. maxhp = (double)mob_db_data[class_]->max_hp;
  4392. if (mob_db_data[class_]->mexp > 0)
  4393. { //Mvp
  4394. if (battle_config.mvp_hp_rate != 100)
  4395. maxhp = maxhp * (double)battle_config.mvp_hp_rate /100.;
  4396. } else if (battle_config.monster_hp_rate != 100) //Normal mob
  4397. maxhp = maxhp * (double)battle_config.monster_hp_rate /100.;
  4398. if (maxhp < 0) maxhp = 1;
  4399. else if (maxhp > 0x7fffffff) maxhp = 0x7fffffff;
  4400. mob_db_data[class_]->max_hp = (int)maxhp;
  4401. // MVP Drops: MVP1id,MVP1per,MVP2id,MVP2per,MVP3id,MVP3per
  4402. for (i=0; i<3; i++) {
  4403. struct item_data *id;
  4404. mob_db_data[class_]->mvpitem[i].nameid = TO_INT(51+i*2);
  4405. mob_db_data[class_]->mvpitem[i].p = mob_drop_adjust(TO_INT(52+i*2),
  4406. battle_config.item_rate_mvp, battle_config.item_drop_mvp_min, battle_config.item_drop_mvp_max);
  4407. //calculate and store Max available drop chance of the MVP item
  4408. id = itemdb_search(mob_db_data[class_]->mvpitem[i].nameid);
  4409. if (mob_db_data[class_]->mvpitem[i].p) {
  4410. if (id->maxchance==10000 || (id->maxchance < mob_db_data[class_]->mvpitem[i].p/10+1) ) {
  4411. //item has bigger drop chance or sold in shops
  4412. id->maxchance = mob_db_data[class_]->mvpitem[i].p/10+1; //reduce MVP drop info to not spoil common drop rate
  4413. }
  4414. }
  4415. }
  4416. if (mob_db_data[class_]->max_hp <= 0) {
  4417. ShowWarning ("Mob %d (%s) has no HP, using poring data for it\n", class_, mob_db_data[class_]->jname);
  4418. mob_makedummymobdb(class_);
  4419. }
  4420. }
  4421. mysql_free_result(sql_res);
  4422. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", ln, mob_db_name[fi]);
  4423. ln = 0;
  4424. }
  4425. }
  4426. return 0;
  4427. }
  4428. #endif /* not TXT_ONLY */
  4429. void mob_reload(void)
  4430. {
  4431. int i;
  4432. #ifndef TXT_ONLY
  4433. if(db_use_sqldbs)
  4434. mob_read_sqldb();
  4435. else
  4436. #endif /* TXT_ONLY */
  4437. mob_readdb();
  4438. mob_readdb_mobavail();
  4439. mob_read_randommonster();
  4440. //Mob skills need to be cleared before re-reading them. [Skotlex]
  4441. for (i = 0; i < MAX_MOB_DB; i++)
  4442. if (mob_db_data[i])
  4443. {
  4444. memset(&mob_db_data[i]->skill,0,sizeof(mob_db_data[i]->skill));
  4445. mob_db_data[i]->maxskill=0;
  4446. }
  4447. mob_readskilldb();
  4448. mob_readdb_race();
  4449. }
  4450. /*==========================================
  4451. * Circumference initialization of mob
  4452. *------------------------------------------
  4453. */
  4454. int do_init_mob(void)
  4455. { //Initialize the mob database
  4456. memset(mob_db_data,0,sizeof(mob_db_data)); //Clear the array
  4457. mob_db_data[0] = aCalloc(1, sizeof (struct mob_data)); //This mob is used for random spawns
  4458. mob_makedummymobdb(0); //The first time this is invoked, it creates the dummy mob
  4459. #ifndef TXT_ONLY
  4460. if(db_use_sqldbs)
  4461. mob_read_sqldb();
  4462. else
  4463. #endif /* TXT_ONLY */
  4464. mob_readdb();
  4465. mob_readdb_mobavail();
  4466. mob_read_randommonster();
  4467. mob_readskilldb();
  4468. mob_readdb_race();
  4469. add_timer_func_list(mob_timer,"mob_timer");
  4470. add_timer_func_list(mob_delayspawn,"mob_delayspawn");
  4471. add_timer_func_list(mob_delay_item_drop,"mob_delay_item_drop");
  4472. add_timer_func_list(mob_ai_hard,"mob_ai_hard");
  4473. add_timer_func_list(mob_ai_lazy,"mob_ai_lazy");
  4474. add_timer_func_list(mobskill_castend_id,"mobskill_castend_id");
  4475. add_timer_func_list(mobskill_castend_pos,"mobskill_castend_pos");
  4476. add_timer_func_list(mob_timer_delete,"mob_timer_delete");
  4477. add_timer_func_list(mob_spawn_guardian_sub,"mob_spawn_guardian_sub");
  4478. add_timer_func_list(mob_respawn,"mob_respawn");
  4479. add_timer_interval(gettick()+MIN_MOBTHINKTIME,mob_ai_hard,0,0,MIN_MOBTHINKTIME);
  4480. add_timer_interval(gettick()+MIN_MOBTHINKTIME*10,mob_ai_lazy,0,0,MIN_MOBTHINKTIME*10);
  4481. return 0;
  4482. }
  4483. /*==========================================
  4484. * Clean memory usage.
  4485. *------------------------------------------
  4486. */
  4487. int do_final_mob(void)
  4488. {
  4489. int i;
  4490. if (mob_dummy)
  4491. {
  4492. aFree(mob_dummy);
  4493. mob_dummy = NULL;
  4494. }
  4495. for (i = 0; i <= MAX_MOB_DB; i++)
  4496. {
  4497. if (mob_db_data[i] != NULL)
  4498. {
  4499. aFree(mob_db_data[i]);
  4500. mob_db_data[i] = NULL;
  4501. }
  4502. }
  4503. return 0;
  4504. }