mob.c 117 KB

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