mob.c 130 KB

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