mob.c 126 KB

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