mob.c 123 KB

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