Advertisement
Guest User

suporte

a guest
Aug 31st, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 154.97 KB | None | 0 0
  1. // Copyright (c) Hercules Dev Team, licensed under GNU GPL.
  2. // See the LICENSE file
  3. // Portions Copyright (c) Athena Dev Teams
  4.  
  5. #define HERCULES_CORE
  6.  
  7. #include "../config/core.h" // AUTOLOOT_DISTANCE, DBPATH, DEFTYPE_MAX, DEFTYPE_MIN, RENEWAL_DROP, RENEWAL_EXP
  8. #include "mob.h"
  9.  
  10. #include <math.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "atcommand.h"
  17. #include "battle.h"
  18. #include "clif.h"
  19. #include "date.h"
  20. #include "elemental.h"
  21. #include "guild.h"
  22. #include "homunculus.h"
  23. #include "intif.h"
  24. #include "itemdb.h"
  25. #include "log.h"
  26. #include "map.h"
  27. #include "mercenary.h"
  28. #include "npc.h"
  29. #include "party.h"
  30. #include "path.h"
  31. #include "pc.h"
  32. #include "pet.h"
  33. #include "quest.h"
  34. #include "script.h"
  35. #include "skill.h"
  36. #include "status.h"
  37. #include "../common/HPM.h"
  38. #include "../common/cbasetypes.h"
  39. #include "../common/db.h"
  40. #include "../common/ers.h"
  41. #include "../common/malloc.h"
  42. #include "../common/nullpo.h"
  43. #include "../common/random.h"
  44. #include "../common/showmsg.h"
  45. #include "../common/socket.h"
  46. #include "../common/strlib.h"
  47. #include "../common/timer.h"
  48. #include "../common/utils.h"
  49.  
  50. struct mob_interface mob_s;
  51.  
  52. #define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode.
  53.  
  54. #define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME)
  55.  
  56. // Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
  57. // in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
  58. #define MOB_LAZYSKILLPERC(md) (md->state.spotted?1000:0)
  59. // Move probability for mobs away from players (rate of 1000 minute)
  60. // in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
  61. #define MOB_LAZYMOVEPERC(md) ((md)->state.spotted?1000:0)
  62. #define MOB_MAX_DELAY (24*3600*1000)
  63. #define MAX_MINCHASE 30 //Max minimum chase value to use for mobs.
  64. #define RUDE_ATTACKED_COUNT 2 //After how many rude-attacks should the skill be used?
  65.  
  66. //Dynamic item drop ratio database for per-item drop ratio modifiers overriding global drop ratios.
  67. #define MAX_ITEMRATIO_MOBS 10
  68. struct item_drop_ratio {
  69. int drop_ratio;
  70. int mob_id[MAX_ITEMRATIO_MOBS];
  71. };
  72. static struct item_drop_ratio *item_drop_ratio_db[MAX_ITEMDB];
  73.  
  74. static struct eri *item_drop_ers; //For loot drops delay structures.
  75. static struct eri *item_drop_list_ers;
  76.  
  77. static struct {
  78. int qty;
  79. int class_[350];
  80. } summon[MAX_RANDOMMONSTER];
  81.  
  82. struct mob_db *mob_db(int index) {
  83. if (index < 0 || index > MAX_MOB_DB || mob->db_data[index] == NULL)
  84. return mob->dummy;
  85. return mob->db_data[index];
  86. }
  87. struct mob_chat *mob_chat(short id) {
  88. if(id <= 0 || id > MAX_MOB_CHAT || mob->chat_db[id] == NULL)
  89. return NULL;
  90. return mob->chat_db[id];
  91. }
  92.  
  93. /*==========================================
  94. * Mob is searched with a name.
  95. *------------------------------------------*/
  96. int mobdb_searchname(const char *str)
  97. {
  98. int i;
  99. struct mob_db* monster;
  100. for(i=0;i<=MAX_MOB_DB;i++){
  101. monster = mob->db(i);
  102. if(monster == mob->dummy) //Skip dummy mobs.
  103. continue;
  104. if(strcmpi(monster->name,str)==0 || strcmpi(monster->jname,str)==0)
  105. return i;
  106. if(battle_config.case_sensitive_aegisnames && strcmp(monster->sprite,str)==0)
  107. return i;
  108. if(!battle_config.case_sensitive_aegisnames && strcasecmp(monster->sprite,str)==0)
  109. return i;
  110. }
  111.  
  112. return 0;
  113. }
  114. int mobdb_searchname_array_sub(struct mob_db* monster, const char *str, int flag) {
  115. if (monster == mob->dummy)
  116. return 1;
  117. if(!monster->base_exp && !monster->job_exp && monster->spawn[0].qty < 1)
  118. return 1; // Monsters with no base/job exp and no spawn point are, by this criteria, considered "slave mobs" and excluded from search results
  119. if( !flag ) {
  120. if(stristr(monster->jname,str))
  121. return 0;
  122. if(stristr(monster->name,str))
  123. return 0;
  124. } else {
  125. if(strcmpi(monster->jname,str) == 0)
  126. return 0;
  127. if(strcmpi(monster->name,str) == 0)
  128. return 0;
  129. }
  130. if (battle_config.case_sensitive_aegisnames)
  131. return strcmp(monster->sprite,str);
  132. return strcasecmp(monster->sprite,str);
  133. }
  134.  
  135. /*==========================================
  136. * MvP Tomb [GreenBox]
  137. *------------------------------------------*/
  138. void mvptomb_create(struct mob_data *md, char *killer, time_t time)
  139. {
  140. struct npc_data *nd;
  141.  
  142. if ( md->tomb_nid )
  143. mob->mvptomb_destroy(md);
  144.  
  145. nd = npc->create_npc(md->bl.m, md->bl.x, md->bl.y);
  146. md->tomb_nid = nd->bl.id;
  147. nd->dir = md->ud.dir;
  148. nd->bl.type = BL_NPC;
  149.  
  150. safestrncpy(nd->name, msg_txt(856), sizeof(nd->name)); // "Tomb"
  151.  
  152. nd->class_ = 565;
  153. nd->speed = 200;
  154. nd->subtype = TOMB;
  155.  
  156. nd->u.tomb.md = md;
  157. nd->u.tomb.kill_time = time;
  158.  
  159. if (killer)
  160. safestrncpy(nd->u.tomb.killer_name, killer, NAME_LENGTH);
  161. else
  162. nd->u.tomb.killer_name[0] = '\0';
  163.  
  164. map->addnpc(nd->bl.m, nd);
  165. map->addblock(&nd->bl);
  166. status->set_viewdata(&nd->bl, nd->class_);
  167. clif->spawn(&nd->bl);
  168.  
  169. }
  170.  
  171. void mvptomb_destroy(struct mob_data *md) {
  172. struct npc_data *nd;
  173.  
  174. if ( (nd = map->id2nd(md->tomb_nid)) ) {
  175. int16 m, i;
  176.  
  177. m = nd->bl.m;
  178.  
  179. clif->clearunit_area(&nd->bl,CLR_OUTSIGHT);
  180.  
  181. map->delblock(&nd->bl);
  182.  
  183. ARR_FIND( 0, map->list[m].npc_num, i, map->list[m].npc[i] == nd );
  184. if( !(i == map->list[m].npc_num) ) {
  185. map->list[m].npc_num--;
  186. map->list[m].npc[i] = map->list[m].npc[map->list[m].npc_num];
  187. map->list[m].npc[map->list[m].npc_num] = NULL;
  188. }
  189.  
  190. map->deliddb(&nd->bl);
  191.  
  192. aFree(nd);
  193. }
  194.  
  195. md->tomb_nid = 0;
  196. }
  197.  
  198. /*==========================================
  199. * Founds up to N matches. Returns number of matches [Skotlex]
  200. *------------------------------------------*/
  201. int mobdb_searchname_array(struct mob_db** data, int size, const char *str, int flag)
  202. {
  203. int count = 0, i;
  204. struct mob_db* monster;
  205. for(i=0;i<=MAX_MOB_DB;i++){
  206. monster = mob->db(i);
  207. if (monster == mob->dummy || mob->is_clone(i) ) //keep clones out (or you leak player stats)
  208. continue;
  209. if (!mob->db_searchname_array_sub(monster, str, flag)) {
  210. if (count < size)
  211. data[count] = monster;
  212. count++;
  213. }
  214. }
  215. return count;
  216. }
  217.  
  218. /*==========================================
  219. * Id Mob is checked.
  220. *------------------------------------------*/
  221. int mobdb_checkid(const int id)
  222. {
  223. if (mob->db(id) == mob->dummy)
  224. return 0;
  225. if (mob->is_clone(id)) //checkid is used mostly for random ID based code, therefore clone mobs are out of the question.
  226. return 0;
  227. return id;
  228. }
  229.  
  230. /*==========================================
  231. * Returns the view data associated to this mob class.
  232. *------------------------------------------*/
  233. struct view_data * mob_get_viewdata(int class_)
  234. {
  235. if (mob->db(class_) == mob->dummy)
  236. return 0;
  237. return &mob->db(class_)->vd;
  238. }
  239. /*==========================================
  240. * Cleans up mob-spawn data to make it "valid"
  241. *------------------------------------------*/
  242. int mob_parse_dataset(struct spawn_data *data)
  243. {
  244. size_t len;
  245.  
  246. if ((!mob->db_checkid(data->class_) && !mob->is_clone(data->class_)) || !data->num)
  247. return 0;
  248.  
  249. if( ( len = strlen(data->eventname) ) > 0 )
  250. {
  251. if( data->eventname[len-1] == '"' )
  252. data->eventname[len-1] = '\0'; //Remove trailing quote.
  253. if( data->eventname[0] == '"' ) //Strip leading quotes
  254. memmove(data->eventname, data->eventname+1, len-1);
  255. }
  256.  
  257. if(strcmp(data->name,"--en--")==0)
  258. safestrncpy(data->name, mob->db(data->class_)->name, sizeof(data->name));
  259. else if(strcmp(data->name,"--ja--")==0)
  260. safestrncpy(data->name, mob->db(data->class_)->jname, sizeof(data->name));
  261.  
  262. return 1;
  263. }
  264. /*==========================================
  265. * Generates the basic mob data using the spawn_data provided.
  266. *------------------------------------------*/
  267. struct mob_data* mob_spawn_dataset(struct spawn_data *data) {
  268. struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
  269. md->bl.id= npc->get_new_npc_id();
  270. md->bl.type = BL_MOB;
  271. md->bl.m = data->m;
  272. md->bl.x = data->x;
  273. md->bl.y = data->y;
  274. md->class_ = data->class_;
  275. md->state.boss = data->state.boss;
  276. md->db = mob->db(md->class_);
  277. if (data->level > 0 && data->level <= MAX_LEVEL)
  278. md->level = data->level;
  279. memcpy(md->name, data->name, NAME_LENGTH);
  280. if (data->state.ai)
  281. md->special_state.ai = data->state.ai;
  282. if (data->state.size)
  283. md->special_state.size = data->state.size;
  284. if (data->eventname[0] && strlen(data->eventname) >= 4)
  285. memcpy(md->npc_event, data->eventname, 50);
  286. if(md->db->status.mode&MD_LOOTER)
  287. md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  288. md->spawn_timer = INVALID_TIMER;
  289. md->deletetimer = INVALID_TIMER;
  290. md->skill_idx = -1;
  291. status->set_viewdata(&md->bl, md->class_);
  292. status->change_init(&md->bl);
  293. unit->dataset(&md->bl);
  294.  
  295. map->addiddb(&md->bl);
  296. return md;
  297. }
  298.  
  299. /*==========================================
  300. * Fetches a random mob_id [Skotlex]
  301. * type: Where to fetch from:
  302. * 0: dead branch list
  303. * 1: poring list
  304. * 2: bloody branch list
  305. * flag:
  306. * &1: Apply the summon success chance found in the list (otherwise get any monster from the db)
  307. * &2: Apply a monster check level.
  308. * &4: Selected monster should not be a boss type
  309. * &8: Selected monster must have normal spawn.
  310. * lv: Mob level to check against
  311. *------------------------------------------*/
  312. int mob_get_random_id(int type, int flag, int lv)
  313. {
  314. struct mob_db *monster;
  315. int i=0, class_;
  316. if(type < 0 || type >= MAX_RANDOMMONSTER) {
  317. ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
  318. return 0;
  319. }
  320. do {
  321. if (type)
  322. class_ = summon[type].class_[rnd()%summon[type].qty];
  323. else //Dead branch
  324. class_ = rnd() % MAX_MOB_DB;
  325. monster = mob->db(class_);
  326. } while ((monster == mob->dummy ||
  327. mob->is_clone(class_) ||
  328. (flag&1 && monster->summonper[type] <= rnd() % 1000000) ||
  329. (flag&2 && lv < monster->lv) ||
  330. (flag&4 && monster->status.mode&MD_BOSS) ||
  331. (flag&8 && monster->spawn[0].qty < 1)
  332. ) && (i++) < MAX_MOB_DB);
  333.  
  334. if(i >= MAX_MOB_DB) // no suitable monster found, use fallback for given list
  335. class_ = mob->db_data[0]->summonper[type];
  336. return class_;
  337. }
  338.  
  339. /*==========================================
  340. * Kill Steal Protection [Zephyrus]
  341. *------------------------------------------*/
  342. bool mob_ksprotected(struct block_list *src, struct block_list *target) {
  343. struct block_list *s_bl, *t_bl;
  344. struct map_session_data
  345. *sd, // Source
  346. *pl_sd, // Owner
  347. *t_sd; // Mob Target
  348. struct status_change_entry *sce;
  349. struct mob_data *md;
  350. int64 tick = timer->gettick();
  351. char output[128];
  352.  
  353. if( !battle_config.ksprotection )
  354. return false; // KS Protection Disabled
  355.  
  356. if( !(md = BL_CAST(BL_MOB,target)) )
  357. return false; // Target is not MOB
  358.  
  359. if( (s_bl = battle->get_master(src)) == NULL )
  360. s_bl = src;
  361.  
  362. if( !(sd = BL_CAST(BL_PC,s_bl)) )
  363. return false; // Master is not PC
  364.  
  365. t_bl = map->id2bl(md->target_id);
  366. if( !t_bl || (s_bl = battle->get_master(t_bl)) == NULL )
  367. s_bl = t_bl;
  368.  
  369. t_sd = BL_CAST(BL_PC,s_bl);
  370.  
  371. do {
  372. if( map->list[md->bl.m].flag.allowks || map_flag_ks(md->bl.m) )
  373. return false; // Ignores GVG, PVP and AllowKS map flags
  374.  
  375. if( md->db->mexp || md->master_id )
  376. return false; // MVP, Slaves mobs ignores KS
  377.  
  378. if( (sce = md->sc.data[SC_KSPROTECTED]) == NULL )
  379. break; // No KS Protected
  380.  
  381. if( sd->bl.id == sce->val1 || // Same Owner
  382. (sce->val2 == 2 && sd->status.party_id && sd->status.party_id == sce->val3) || // Party KS allowed
  383. (sce->val2 == 3 && sd->status.guild_id && sd->status.guild_id == sce->val4) ) // Guild KS allowed
  384. break;
  385.  
  386. if( t_sd && (
  387. (sce->val2 == 1 && sce->val1 != t_sd->bl.id) ||
  388. (sce->val2 == 2 && sce->val3 && sce->val3 != t_sd->status.party_id) ||
  389. (sce->val2 == 3 && sce->val4 && sce->val4 != t_sd->status.guild_id)) )
  390. break;
  391.  
  392. if( (pl_sd = map->id2sd(sce->val1)) == NULL || pl_sd->bl.m != md->bl.m )
  393. break;
  394.  
  395. if( !pl_sd->state.noks )
  396. return false; // No KS Protected, but normal players should be protected too
  397.  
  398. // Message to KS
  399. if( DIFF_TICK(sd->ks_floodprotect_tick, tick) <= 0 )
  400. {
  401. sprintf(output, "[KS Warning!! - Owner : %s]", pl_sd->status.name);
  402. clif_disp_onlyself(sd, output, strlen(output));
  403.  
  404. sd->ks_floodprotect_tick = tick + 2000;
  405. }
  406.  
  407. // Message to Owner
  408. if( DIFF_TICK(pl_sd->ks_floodprotect_tick, tick) <= 0 )
  409. {
  410. sprintf(output, "[Watch out! %s is trying to KS you!]", sd->status.name);
  411. clif_disp_onlyself(pl_sd, output, strlen(output));
  412.  
  413. pl_sd->ks_floodprotect_tick = tick + 2000;
  414. }
  415.  
  416. return true;
  417. } while(0);
  418.  
  419. status->change_start(NULL, target, SC_KSPROTECTED, 10000, sd->bl.id, sd->state.noks,
  420. sd->status.party_id, sd->status.guild_id, battle_config.ksprotection, SCFLAG_NONE);
  421.  
  422. return false;
  423. }
  424.  
  425. struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int class_, const char *event, unsigned int size, unsigned int ai)
  426. {
  427. struct spawn_data data;
  428.  
  429. memset(&data, 0, sizeof(struct spawn_data));
  430. data.m = m;
  431. data.num = 1;
  432. data.class_ = class_;
  433. data.state.size = size;
  434. data.state.ai = ai;
  435.  
  436. if (mobname)
  437. safestrncpy(data.name, mobname, sizeof(data.name));
  438. else
  439. if (battle_config.override_mob_names == 1)
  440. strcpy(data.name, "--en--");
  441. else
  442. strcpy(data.name, "--ja--");
  443.  
  444. if (event)
  445. safestrncpy(data.eventname, event, sizeof(data.eventname));
  446.  
  447. // Locate spot next to player.
  448. if (bl && (x < 0 || y < 0))
  449. map->search_freecell(bl, m, &x, &y, 1, 1, 0);
  450.  
  451. // if none found, pick random position on map
  452. if (x <= 0 || x >= map->list[m].xs || y <= 0 || y >= map->list[m].ys)
  453. map->search_freecell(NULL, m, &x, &y, -1, -1, 1);
  454.  
  455. data.x = x;
  456. data.y = y;
  457.  
  458. if (!mob->parse_dataset(&data))
  459. return NULL;
  460.  
  461. return mob->spawn_dataset(&data);
  462. }
  463.  
  464. /*==========================================
  465. * Spawn a single mob on the specified coordinates.
  466. *------------------------------------------*/
  467. int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, const char* mobname, int class_, int amount, const char* event, unsigned int size, unsigned int ai) {
  468. struct mob_data* md = NULL;
  469. int count, lv;
  470. bool no_guardian_data = false;
  471.  
  472. if( ai && ai&0x200 ) {
  473. no_guardian_data = true;
  474. ai &=~ 0x200;
  475. }
  476.  
  477. if (m < 0 || amount <= 0)
  478. return 0; // invalid input
  479.  
  480. lv = (sd) ? sd->status.base_level : 255;
  481.  
  482. for (count = 0; count < amount; count++) {
  483. int c = (class_ >= 0) ? class_ : mob->get_random_id(-class_ - 1, (battle_config.random_monster_checklv) ? 3 : 1, lv);
  484. md = mob->once_spawn_sub((sd) ? &sd->bl : NULL, m, x, y, mobname, c, event, size, ai);
  485.  
  486. if (!md)
  487. continue;
  488.  
  489. if ( class_ == MOBID_EMPERIUM && !no_guardian_data ) {
  490. struct guild_castle* gc = guild->mapindex2gc(map_id2index(m));
  491. struct guild* g = (gc) ? guild->search(gc->guild_id) : NULL;
  492. if( gc ) {
  493. md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
  494. md->guardian_data->castle = gc;
  495. md->guardian_data->number = MAX_GUARDIANS;
  496. if( g )
  497. md->guardian_data->g = g;
  498. else if( gc->guild_id ) //Guild not yet available, retry in 5.
  499. timer->add(timer->gettick()+5000,mob->spawn_guardian_sub,md->bl.id,gc->guild_id);
  500. }
  501. } // end addition [Valaris]
  502.  
  503. mob->spawn(md);
  504.  
  505. if (class_ < 0 && battle_config.dead_branch_active) {
  506. //Behold Aegis's masterful decisions yet again...
  507. //"I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" - Poki#3
  508. sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE|MD_ANGRY, 0, 60000);
  509. }
  510. }
  511.  
  512. return (md) ? md->bl.id : 0; // id of last spawned mob
  513. }
  514.  
  515. /*==========================================
  516. * Spawn mobs in the specified area.
  517. *------------------------------------------*/
  518. int mob_once_spawn_area(struct map_session_data* sd, int16 m, int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int class_, int amount, const char* event, unsigned int size, unsigned int ai)
  519. {
  520. int i, max, id = 0;
  521. int lx = -1, ly = -1;
  522.  
  523. if (m < 0 || amount <= 0)
  524. return 0; // invalid input
  525.  
  526. // normalize x/y coordinates
  527. if (x0 > x1)
  528. swap(x0, x1);
  529. if (y0 > y1)
  530. swap(y0, y1);
  531.  
  532. // choose a suitable max. number of attempts
  533. max = (y1 - y0 + 1)*(x1 - x0 + 1)*3;
  534. if (max > 1000)
  535. max = 1000;
  536.  
  537. // spawn mobs, one by one
  538. for (i = 0; i < amount; i++)
  539. {
  540. int x, y;
  541. int j = 0;
  542.  
  543. // find a suitable map cell
  544. do {
  545. x = rnd()%(x1-x0+1)+x0;
  546. y = rnd()%(y1-y0+1)+y0;
  547. j++;
  548. } while (map->getcell(m,x,y,CELL_CHKNOPASS) && j < max);
  549.  
  550. if (j == max)
  551. {// attempt to find an available cell failed
  552. if (lx == -1 && ly == -1)
  553. return 0; // total failure
  554.  
  555. // fallback to last good x/y pair
  556. x = lx;
  557. y = ly;
  558. }
  559.  
  560. // record last successful coordinates
  561. lx = x;
  562. ly = y;
  563.  
  564. id = mob->once_spawn(sd, m, x, y, mobname, class_, 1, event, size, ai);
  565. }
  566.  
  567. return id; // id of last spawned mob
  568. }
  569.  
  570. /**
  571. * Sets a guardian's guild data and liberates castle if couldn't retrieve guild data
  572. * @param data (int)guild_id
  573. * @retval Always 0
  574. * @author Skotlex
  575. **/
  576. int mob_spawn_guardian_sub(int tid, int64 tick, int id, intptr_t data) {
  577. //Needed because the guild data may not be available at guardian spawn time.
  578. struct block_list* bl = map->id2bl(id);
  579. struct mob_data* md;
  580. struct guild* g;
  581.  
  582. if( bl == NULL ) //It is possible mob was already removed from map when the castle has no owner. [Skotlex]
  583. return 0;
  584.  
  585. if( bl->type != BL_MOB ) {
  586. ShowError("mob_spawn_guardian_sub: Block error!\n");
  587. return 0;
  588. }
  589.  
  590. md = (struct mob_data*)bl;
  591. nullpo_ret(md->guardian_data);
  592. g = guild->search((int)data);
  593.  
  594. if( g == NULL ) { //Liberate castle, if the guild is not found this is an error! [Skotlex]
  595. ShowError("mob_spawn_guardian_sub: Couldn't load guild %d!\n", (int)data);
  596. //Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
  597. if( md->class_ == MOBID_EMPERIUM && md->guardian_data ) {
  598. md->guardian_data->g = NULL;
  599. if( md->guardian_data->castle->guild_id ) {//Free castle up.
  600. ShowNotice("Clearing ownership of castle %d (%s)\n", md->guardian_data->castle->castle_id, md->guardian_data->castle->castle_name);
  601. guild->castledatasave(md->guardian_data->castle->castle_id, 1, 0);
  602. }
  603. } else {
  604. if( md->guardian_data && md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS
  605. && md->guardian_data->castle->guardian[md->guardian_data->number].visible )
  606. guild->castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  607.  
  608. unit->free(&md->bl,CLR_OUTSIGHT); // Remove guardian.
  609. }
  610. return 0;
  611. }
  612.  
  613. if( guild->checkskill(g,GD_GUARDUP) )
  614. status_calc_mob(md, SCO_NONE); // Give bonuses.
  615.  
  616. return 0;
  617. }
  618.  
  619. /*==========================================
  620. * Summoning Guardians [Valaris]
  621. *------------------------------------------*/
  622. int mob_spawn_guardian(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, int guardian, bool has_index)
  623. {
  624. struct mob_data *md=NULL;
  625. struct spawn_data data;
  626. struct guild *g=NULL;
  627. struct guild_castle *gc;
  628. int16 m;
  629. memset(&data, 0, sizeof(struct spawn_data));
  630. data.num = 1;
  631.  
  632. m=map->mapname2mapid(mapname);
  633.  
  634. if(m<0)
  635. {
  636. ShowWarning("mob_spawn_guardian: Map [%s] not found.\n", mapname);
  637. return 0;
  638. }
  639. data.m = m;
  640. data.num = 1;
  641. if(class_<=0) {
  642. class_ = mob->get_random_id(-class_-1, 1, 99);
  643. if (!class_) return 0;
  644. }
  645.  
  646. data.class_ = class_;
  647.  
  648. if( !has_index ) {
  649. guardian = -1;
  650. } else if( guardian < 0 || guardian >= MAX_GUARDIANS ) {
  651. ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d (castle map %s)\n", guardian, class_, map->list[m].name);
  652. return 0;
  653. }
  654.  
  655. if((x<=0 || y<=0) && !map->search_freecell(NULL, m, &x, &y, -1,-1, 1)) {
  656. ShowWarning("mob_spawn_guardian: Couldn't locate a spawn cell for guardian class %d (index %d) at castle map %s\n",class_, guardian, map->list[m].name);
  657. return 0;
  658. }
  659. data.x = x;
  660. data.y = y;
  661. safestrncpy(data.name, mobname, sizeof(data.name));
  662. safestrncpy(data.eventname, event, sizeof(data.eventname));
  663. if (!mob->parse_dataset(&data))
  664. return 0;
  665.  
  666. gc=guild->mapname2gc(map->list[m].name);
  667. if (gc == NULL) {
  668. ShowError("mob_spawn_guardian: No castle set at map %s\n", map->list[m].name);
  669. return 0;
  670. }
  671. if (!gc->guild_id)
  672. ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle with no guild (castle map %s)\n", class_, map->list[m].name);
  673. else
  674. g = guild->search(gc->guild_id);
  675.  
  676. if( has_index && gc->guardian[guardian].id ) {
  677. //Check if guardian already exists, refuse to spawn if so.
  678. struct mob_data *md2 = (TBL_MOB*)map->id2bl(gc->guardian[guardian].id);
  679. if (md2 && md2->bl.type == BL_MOB
  680. && md2->guardian_data
  681. && md2->guardian_data->number == guardian
  682. ) {
  683. ShowError("mob_spawn_guardian: Attempted to spawn guardian in position %d which already has a guardian (castle map %s)\n", guardian, map->list[m].name);
  684. return 0;
  685. }
  686. }
  687.  
  688. md = mob->spawn_dataset(&data);
  689. md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
  690. md->guardian_data->number = guardian;
  691. md->guardian_data->castle = gc;
  692. if( has_index )
  693. {// permanent guardian
  694. gc->guardian[guardian].id = md->bl.id;
  695. }
  696. else
  697. {// temporary guardian
  698. int i;
  699. ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == 0);
  700. if( i == gc->temp_guardians_max )
  701. {
  702. ++(gc->temp_guardians_max);
  703. RECREATE(gc->temp_guardians, int, gc->temp_guardians_max);
  704. }
  705. gc->temp_guardians[i] = md->bl.id;
  706. }
  707. if( g )
  708. md->guardian_data->g = g;
  709. else if( gc->guild_id )
  710. timer->add(timer->gettick()+5000,mob->spawn_guardian_sub,md->bl.id,gc->guild_id);
  711. mob->spawn(md);
  712.  
  713. return md->bl.id;
  714. }
  715.  
  716. /*==========================================
  717. * Summoning BattleGround [Zephyrus]
  718. *------------------------------------------*/
  719. int mob_spawn_bg(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, unsigned int bg_id)
  720. {
  721. struct mob_data *md = NULL;
  722. struct spawn_data data;
  723. int16 m;
  724.  
  725. if( (m = map->mapname2mapid(mapname)) < 0 ) {
  726. ShowWarning("mob_spawn_bg: Map [%s] not found.\n", mapname);
  727. return 0;
  728. }
  729.  
  730. memset(&data, 0, sizeof(struct spawn_data));
  731. data.m = m;
  732. data.num = 1;
  733. if( class_ <= 0 )
  734. {
  735. class_ = mob->get_random_id(-class_-1,1,99);
  736. if( !class_ ) return 0;
  737. }
  738.  
  739. data.class_ = class_;
  740. if( (x <= 0 || y <= 0) && !map->search_freecell(NULL, m, &x, &y, -1,-1, 1) ) {
  741. ShowWarning("mob_spawn_bg: Couldn't locate a spawn cell for guardian class %d (bg_id %d) at map %s\n",class_, bg_id, map->list[m].name);
  742. return 0;
  743. }
  744.  
  745. data.x = x;
  746. data.y = y;
  747. safestrncpy(data.name, mobname, sizeof(data.name));
  748. safestrncpy(data.eventname, event, sizeof(data.eventname));
  749. if( !mob->parse_dataset(&data) )
  750. return 0;
  751.  
  752. md = mob->spawn_dataset(&data);
  753. mob->spawn(md);
  754. md->bg_id = bg_id; // BG Team ID
  755.  
  756. return md->bl.id;
  757. }
  758.  
  759. /*==========================================
  760. * Reachability to a Specification ID existence place
  761. * state indicates type of 'seek' mob should do:
  762. * - MSS_LOOT: Looking for item, path must be easy.
  763. * - MSS_RUSH: Chasing attacking player, path is complex
  764. * - MSS_FOLLOW: Initiative/support seek, path is complex
  765. *------------------------------------------*/
  766. int mob_can_reach(struct mob_data *md,struct block_list *bl,int range, int state)
  767. {
  768. int easy = 0;
  769.  
  770. nullpo_ret(md);
  771. nullpo_ret(bl);
  772. switch (state) {
  773. case MSS_RUSH:
  774. case MSS_FOLLOW:
  775. easy = 0; //(battle_config.mob_ai&0x1?0:1);
  776. break;
  777. case MSS_LOOT:
  778. default:
  779. easy = 1;
  780. break;
  781. }
  782. return unit->can_reach_bl(&md->bl, bl, range, easy, NULL, NULL);
  783. }
  784.  
  785. /*==========================================
  786. * Links nearby mobs (supportive mobs)
  787. *------------------------------------------*/
  788. int mob_linksearch(struct block_list *bl,va_list ap) {
  789. struct mob_data *md;
  790. int class_;
  791. struct block_list *target;
  792. int64 tick;
  793.  
  794. nullpo_ret(bl);
  795. md=(struct mob_data *)bl;
  796. class_ = va_arg(ap, int);
  797. target = va_arg(ap, struct block_list *);
  798. tick = va_arg(ap, int64);
  799.  
  800. if (md->class_ == class_ && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME
  801. && !md->target_id)
  802. {
  803. md->last_linktime = tick;
  804. if (mob->can_reach(md,target,md->db->range2, MSS_FOLLOW)) {
  805. // Reachability judging
  806. md->target_id = target->id;
  807. md->min_chase=md->db->range3;
  808. return 1;
  809. }
  810. }
  811.  
  812. return 0;
  813. }
  814.  
  815. /*==========================================
  816. * mob spawn with delay (timer function)
  817. *------------------------------------------*/
  818. int mob_delayspawn(int tid, int64 tick, int id, intptr_t data) {
  819. struct block_list* bl = map->id2bl(id);
  820. struct mob_data* md = BL_CAST(BL_MOB, bl);
  821.  
  822. if( md )
  823. {
  824. if( md->spawn_timer != tid )
  825. {
  826. ShowError("mob_delayspawn: Timer mismatch: %d != %d\n", tid, md->spawn_timer);
  827. return 0;
  828. }
  829. md->spawn_timer = INVALID_TIMER;
  830. mob->spawn(md);
  831. }
  832. return 0;
  833. }
  834.  
  835. /*==========================================
  836. * spawn timing calculation
  837. *------------------------------------------*/
  838. int mob_setdelayspawn(struct mob_data *md)
  839. {
  840. unsigned int spawntime, mode;
  841. struct mob_db *db;
  842.  
  843. if (!md->spawn) //Doesn't has respawn data!
  844. return unit->free(&md->bl,CLR_DEAD);
  845.  
  846. spawntime = md->spawn->delay1; //Base respawn time
  847. if (md->spawn->delay2) //random variance
  848. spawntime+= rnd()%md->spawn->delay2;
  849.  
  850. //Apply the spawn delay fix [Skotlex]
  851. db = mob->db(md->spawn->class_);
  852. mode = db->status.mode;
  853. if (mode & MD_BOSS) {
  854. //Bosses
  855. if (battle_config.boss_spawn_delay != 100) {
  856. // Divide by 100 first to prevent overflows
  857. //(precision loss is minimal as duration is in ms already)
  858. spawntime = spawntime/100*battle_config.boss_spawn_delay;
  859. }
  860. } else if (mode&MD_PLANT) {
  861. //Plants
  862. if (battle_config.plant_spawn_delay != 100) {
  863. spawntime = spawntime/100*battle_config.plant_spawn_delay;
  864. }
  865. } else if (battle_config.mob_spawn_delay != 100) {
  866. //Normal mobs
  867. spawntime = spawntime/100*battle_config.mob_spawn_delay;
  868. }
  869.  
  870. if (spawntime < 5000) //Monsters should never respawn faster than within 5 seconds
  871. spawntime = 5000;
  872.  
  873. if( md->spawn_timer != INVALID_TIMER )
  874. timer->delete(md->spawn_timer, mob->delayspawn);
  875. md->spawn_timer = timer->add(timer->gettick()+spawntime, mob->delayspawn, md->bl.id, 0);
  876. return 0;
  877. }
  878.  
  879. int mob_count_sub(struct block_list *bl, va_list ap) {
  880. int mobid[10] = { 0 }, i;
  881. ARR_FIND(0, 10, i, (mobid[i] = va_arg(ap, int)) == 0); //fetch till 0
  882. if (mobid[0]) { //if there one let's check it otherwise go backward
  883. TBL_MOB *md = BL_CAST(BL_MOB, bl);
  884. nullpo_ret(md);
  885. ARR_FIND(0, 10, i, md->class_ == mobid[i]);
  886. return (i < 10) ? 1 : 0;
  887. }
  888. return 1; //backward compatibility
  889. }
  890.  
  891. /*==========================================
  892. * Mob spawning. Initialization is also variously here.
  893. *------------------------------------------*/
  894. int mob_spawn (struct mob_data *md)
  895. {
  896. int i=0;
  897. int64 tick = timer->gettick();
  898. int64 c = 0;
  899.  
  900. md->last_thinktime = tick;
  901. if (md->bl.prev != NULL)
  902. unit->remove_map(&md->bl,CLR_RESPAWN,ALC_MARK);
  903. else if (md->spawn && md->class_ != md->spawn->class_) {
  904. md->class_ = md->spawn->class_;
  905. status->set_viewdata(&md->bl, md->class_);
  906. md->db = mob->db(md->class_);
  907. memcpy(md->name,md->spawn->name,NAME_LENGTH);
  908. }
  909.  
  910. if (md->spawn) { //Respawn data
  911. md->bl.m = md->spawn->m;
  912. md->bl.x = md->spawn->x;
  913. md->bl.y = md->spawn->y;
  914.  
  915. if( (md->bl.x == 0 && md->bl.y == 0) || md->spawn->xs || md->spawn->ys ) {
  916. //Monster can be spawned on an area.
  917. if( !map->search_freecell(&md->bl, -1, &md->bl.x, &md->bl.y, md->spawn->xs, md->spawn->ys, battle_config.no_spawn_on_player?4:0) ) {
  918. // retry again later
  919. if( md->spawn_timer != INVALID_TIMER )
  920. timer->delete(md->spawn_timer, mob->delayspawn);
  921. md->spawn_timer = timer->add(tick+5000,mob->delayspawn,md->bl.id,0);
  922. return 1;
  923. }
  924. } else if( battle_config.no_spawn_on_player > 99 && map->foreachinrange(mob->count_sub, &md->bl, AREA_SIZE, BL_PC) ) {
  925. // retry again later (players on sight)
  926. if( md->spawn_timer != INVALID_TIMER )
  927. timer->delete(md->spawn_timer, mob->delayspawn);
  928. md->spawn_timer = timer->add(tick+5000,mob->delayspawn,md->bl.id,0);
  929. return 1;
  930. }
  931. }
  932.  
  933. memset(&md->state, 0, sizeof(md->state));
  934. status_calc_mob(md, SCO_FIRST);
  935. md->attacked_id = 0;
  936. md->target_id = 0;
  937. md->move_fail_count = 0;
  938. md->ud.state.attack_continue = 0;
  939. md->ud.target_to = 0;
  940. md->ud.dir = 0;
  941. if( md->spawn_timer != INVALID_TIMER )
  942. {
  943. timer->delete(md->spawn_timer, mob->delayspawn);
  944. md->spawn_timer = INVALID_TIMER;
  945. }
  946.  
  947. //md->master_id = 0;
  948. md->master_dist = 0;
  949.  
  950. md->state.aggressive = md->status.mode&MD_ANGRY?1:0;
  951. md->state.skillstate = MSS_IDLE;
  952. md->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME;
  953. md->last_linktime = tick;
  954. md->dmgtick = tick - 5000;
  955. md->last_pcneartime = 0;
  956.  
  957. for (i = 0, c = tick-MOB_MAX_DELAY; i < MAX_MOBSKILL; i++)
  958. md->skilldelay[i] = c;
  959.  
  960. memset(md->dmglog, 0, sizeof(md->dmglog));
  961. md->tdmg = 0;
  962.  
  963. if (md->lootitem)
  964. memset(md->lootitem, 0, sizeof(*md->lootitem));
  965.  
  966. md->lootitem_count = 0;
  967.  
  968. if(md->db->option)
  969. // Added for carts, falcons and pecos for cloned monsters. [Valaris]
  970. md->sc.option = md->db->option;
  971.  
  972. // MvP tomb [GreenBox]
  973. if ( md->tomb_nid )
  974. mob->mvptomb_destroy(md);
  975.  
  976. map->addblock(&md->bl);
  977. if( map->list[md->bl.m].users )
  978. clif->spawn(&md->bl);
  979. skill->unit_move(&md->bl,tick,1);
  980. mob->skill_use(md, tick, MSC_SPAWN);
  981. return 0;
  982. }
  983.  
  984. /*==========================================
  985. * Determines if the mob can change target. [Skotlex]
  986. *------------------------------------------*/
  987. int mob_can_changetarget(struct mob_data* md, struct block_list* target, int mode)
  988. {
  989. // if the monster was provoked ignore the above rule [celest]
  990. if(md->state.provoke_flag)
  991. {
  992. if (md->state.provoke_flag == target->id)
  993. return 1;
  994. else if (!(battle_config.mob_ai&0x4))
  995. return 0;
  996. }
  997.  
  998. switch (md->state.skillstate) {
  999. case MSS_BERSERK:
  1000. if (!(mode&MD_CHANGETARGET_MELEE))
  1001. return 0;
  1002. return (battle_config.mob_ai&0x4 || check_distance_bl(&md->bl, target, 3));
  1003. case MSS_RUSH:
  1004. return (mode&MD_CHANGETARGET_CHASE);
  1005. case MSS_FOLLOW:
  1006. case MSS_ANGRY:
  1007. case MSS_IDLE:
  1008. case MSS_WALK:
  1009. case MSS_LOOT:
  1010. return 1;
  1011. default:
  1012. return 0;
  1013. }
  1014. }
  1015.  
  1016. /*==========================================
  1017. * Determination for an attack of a monster
  1018. *------------------------------------------*/
  1019. int mob_target(struct mob_data *md,struct block_list *bl,int dist)
  1020. {
  1021. nullpo_ret(md);
  1022. nullpo_ret(bl);
  1023.  
  1024. // Nothing will be carried out if there is no mind of changing TAGE by TAGE ending.
  1025. if(md->target_id && !mob->can_changetarget(md, bl, status_get_mode(&md->bl)))
  1026. return 0;
  1027.  
  1028. if(!status->check_skilluse(&md->bl, bl, 0, 0))
  1029. return 0;
  1030.  
  1031. md->target_id = bl->id; // Since there was no disturbance, it locks on to target.
  1032. if (md->state.provoke_flag && bl->id != md->state.provoke_flag)
  1033. md->state.provoke_flag = 0;
  1034. md->min_chase=dist+md->db->range3;
  1035. if(md->min_chase>MAX_MINCHASE)
  1036. md->min_chase=MAX_MINCHASE;
  1037. return 0;
  1038. }
  1039.  
  1040. /*==========================================
  1041. * The ?? routine of an active monster
  1042. *------------------------------------------*/
  1043. int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap)
  1044. {
  1045. struct mob_data *md;
  1046. struct block_list **target;
  1047. int mode;
  1048. int dist;
  1049.  
  1050. nullpo_ret(bl);
  1051. md=va_arg(ap,struct mob_data *);
  1052. target= va_arg(ap,struct block_list**);
  1053. mode= va_arg(ap,int);
  1054.  
  1055. //If can't seek yet, not an enemy, or you can't attack it, skip.
  1056. if (md->bl.id == bl->id || (*target) == bl || !status->check_skilluse(&md->bl, bl, 0, 0))
  1057. return 0;
  1058.  
  1059. if ((mode&MD_TARGETWEAK) && status->get_lv(bl) >= md->level-5)
  1060. return 0;
  1061.  
  1062. if(battle->check_target(&md->bl,bl,BCT_ENEMY)<=0)
  1063. return 0;
  1064.  
  1065. switch (bl->type) {
  1066. case BL_PC:
  1067. if (((TBL_PC*)bl)->state.gangsterparadise &&
  1068. !(status_get_mode(&md->bl)&MD_BOSS))
  1069. return 0; //Gangster paradise protection.
  1070. default:
  1071. if (battle_config.hom_setting&0x4 &&
  1072. (*target) && (*target)->type == BL_HOM && bl->type != BL_HOM)
  1073. return 0; //For some reason Homun targets are never overridden.
  1074.  
  1075. dist = distance_bl(&md->bl, bl);
  1076. if(
  1077. ((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) &&
  1078. battle->check_range(&md->bl,bl,md->db->range2)
  1079. ) { //Pick closest target?
  1080. #ifdef ACTIVEPATHSEARCH
  1081. struct walkpath_data wpd;
  1082. if (!path->search(&wpd, md->bl.m, md->bl.x, md->bl.y, bl->x, bl->y, 0, CELL_CHKNOPASS)) // Count walk path cells
  1083. return 0;
  1084. //Standing monsters use range2, walking monsters use range3
  1085. if ((md->ud.walktimer == INVALID_TIMER && wpd.path_len > md->db->range2)
  1086. || (md->ud.walktimer != INVALID_TIMER && wpd.path_len > md->db->range3))
  1087. return 0;
  1088. #endif
  1089. (*target) = bl;
  1090. md->target_id=bl->id;
  1091. md->min_chase= dist + md->db->range3;
  1092. if(md->min_chase>MAX_MINCHASE)
  1093. md->min_chase=MAX_MINCHASE;
  1094. return 1;
  1095. }
  1096. break;
  1097. }
  1098. return 0;
  1099. }
  1100.  
  1101. /*==========================================
  1102. * chase target-change routine.
  1103. *------------------------------------------*/
  1104. int mob_ai_sub_hard_changechase(struct block_list *bl,va_list ap) {
  1105. struct mob_data *md;
  1106. struct block_list **target;
  1107.  
  1108. nullpo_ret(bl);
  1109. md=va_arg(ap,struct mob_data *);
  1110. target= va_arg(ap,struct block_list**);
  1111.  
  1112. //If can't seek yet, not an enemy, or you can't attack it, skip.
  1113. if( md->bl.id == bl->id || *target == bl
  1114. || battle->check_target(&md->bl,bl,BCT_ENEMY) <= 0
  1115. || !status->check_skilluse(&md->bl, bl, 0, 0)
  1116. )
  1117. return 0;
  1118.  
  1119. if(battle->check_range (&md->bl, bl, md->status.rhw.range)) {
  1120. (*target) = bl;
  1121. md->target_id=bl->id;
  1122. md->min_chase= md->db->range3;
  1123. }
  1124. return 1;
  1125. }
  1126.  
  1127. /*==========================================
  1128. * finds nearby bg ally for guardians looking for users to follow.
  1129. *------------------------------------------*/
  1130. int mob_ai_sub_hard_bg_ally(struct block_list *bl,va_list ap) {
  1131. struct mob_data *md;
  1132. struct block_list **target;
  1133.  
  1134. nullpo_ret(bl);
  1135. md=va_arg(ap,struct mob_data *);
  1136. target= va_arg(ap,struct block_list**);
  1137.  
  1138. if( status->check_skilluse(&md->bl, bl, 0, 0) && battle->check_target(&md->bl,bl,BCT_ENEMY)<=0 ) {
  1139. (*target) = bl;
  1140. }
  1141. return 1;
  1142. }
  1143.  
  1144. /*==========================================
  1145. * loot monster item search
  1146. *------------------------------------------*/
  1147. int mob_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
  1148. {
  1149. struct mob_data* md;
  1150. struct block_list **target;
  1151. int dist;
  1152.  
  1153. md=va_arg(ap,struct mob_data *);
  1154. target= va_arg(ap,struct block_list**);
  1155.  
  1156. dist=distance_bl(&md->bl, bl);
  1157. if(mob->can_reach(md,bl,dist+1, MSS_LOOT) &&
  1158. ((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) //New target closer than previous one.
  1159. ) {
  1160. (*target) = bl;
  1161. md->target_id=bl->id;
  1162. md->min_chase=md->db->range3;
  1163. }
  1164. return 0;
  1165. }
  1166.  
  1167. int mob_warpchase_sub(struct block_list *bl,va_list ap) {
  1168. struct block_list *target;
  1169. struct npc_data **target_nd;
  1170. struct npc_data *nd;
  1171. int *min_distance;
  1172. int cur_distance;
  1173.  
  1174. target= va_arg(ap, struct block_list*);
  1175. target_nd= va_arg(ap, struct npc_data**);
  1176. min_distance= va_arg(ap, int*);
  1177.  
  1178. nd = (TBL_NPC*) bl;
  1179.  
  1180. if(nd->subtype != WARP)
  1181. return 0; //Not a warp
  1182.  
  1183. if(nd->u.warp.mapindex != map_id2index(target->m))
  1184. return 0; //Does not lead to the same map.
  1185.  
  1186. cur_distance = distance_blxy(target, nd->u.warp.x, nd->u.warp.y);
  1187. if (cur_distance < *min_distance) {
  1188. //Pick warp that leads closest to target.
  1189. *target_nd = nd;
  1190. *min_distance = cur_distance;
  1191. return 1;
  1192. }
  1193. return 0;
  1194. }
  1195. /*==========================================
  1196. * Processing of slave monsters
  1197. *------------------------------------------*/
  1198. int mob_ai_sub_hard_slavemob(struct mob_data *md, int64 tick) {
  1199. struct block_list *bl;
  1200.  
  1201. bl=map->id2bl(md->master_id);
  1202.  
  1203. if (!bl || status->isdead(bl)) {
  1204. status_kill(&md->bl);
  1205. return 1;
  1206. }
  1207. if (bl->prev == NULL)
  1208. return 0; //Master not on a map? Could be warping, do not process.
  1209.  
  1210. if (status_get_mode(&md->bl)&MD_CANMOVE) {
  1211. //If the mob can move, follow around. [Check by Skotlex]
  1212. int old_dist;
  1213.  
  1214. // Distance with between slave and master is measured.
  1215. old_dist=md->master_dist;
  1216. md->master_dist=distance_bl(&md->bl, bl);
  1217.  
  1218. // Since the master was in near immediately before, teleport is carried out and it pursues.
  1219. if(bl->m != md->bl.m ||
  1220. (old_dist<10 && md->master_dist>18) ||
  1221. md->master_dist > MAX_MINCHASE
  1222. ){
  1223. md->master_dist = 0;
  1224. unit->warp(&md->bl,bl->m,bl->x,bl->y,CLR_TELEPORT);
  1225. return 1;
  1226. }
  1227.  
  1228. if(md->target_id) //Slave is busy with a target.
  1229. return 0;
  1230.  
  1231. // Approach master if within view range, chase back to Master's area also if standing on top of the master.
  1232. if( (md->master_dist>MOB_SLAVEDISTANCE || md->master_dist == 0)
  1233. && unit->can_move(&md->bl)
  1234. ) {
  1235. short x = bl->x, y = bl->y;
  1236. mob_stop_attack(md);
  1237. if(map->search_freecell(&md->bl, bl->m, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 1)
  1238. && unit->walktoxy(&md->bl, x, y, 0))
  1239. return 1;
  1240. }
  1241. } else if (bl->m != md->bl.m && map_flag_gvg(md->bl.m)) {
  1242. //Delete the summoned mob if it's in a gvg ground and the master is elsewhere. [Skotlex]
  1243. status_kill(&md->bl);
  1244. return 1;
  1245. }
  1246.  
  1247. //Avoid attempting to lock the master's target too often to avoid unnecessary overload. [Skotlex]
  1248. if (DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME && !md->target_id) {
  1249. struct unit_data *ud = unit->bl2ud(bl);
  1250. md->last_linktime = tick;
  1251.  
  1252. if (ud) {
  1253. struct block_list *tbl=NULL;
  1254. if (ud->target && ud->state.attack_continue)
  1255. tbl=map->id2bl(ud->target);
  1256. else if (ud->skilltarget) {
  1257. tbl = map->id2bl(ud->skilltarget);
  1258. //Required check as skilltarget is not always an enemy. [Skotlex]
  1259. if (tbl && battle->check_target(&md->bl, tbl, BCT_ENEMY) <= 0)
  1260. tbl = NULL;
  1261. }
  1262. if (tbl && status->check_skilluse(&md->bl, tbl, 0, 0)) {
  1263. md->target_id=tbl->id;
  1264. md->min_chase=md->db->range3+distance_bl(&md->bl, tbl);
  1265. if(md->min_chase>MAX_MINCHASE)
  1266. md->min_chase=MAX_MINCHASE;
  1267. return 1;
  1268. }
  1269. }
  1270. }
  1271. return 0;
  1272. }
  1273.  
  1274. /*==========================================
  1275. * A lock of target is stopped and mob moves to a standby state.
  1276. * This also triggers idle skill/movement since the AI can get stuck
  1277. * when trying to pick new targets when the current chosen target is
  1278. * unreachable.
  1279. *------------------------------------------*/
  1280. int mob_unlocktarget(struct mob_data *md, int64 tick) {
  1281. nullpo_ret(md);
  1282.  
  1283. switch (md->state.skillstate) {
  1284. case MSS_WALK:
  1285. if (md->ud.walktimer != INVALID_TIMER)
  1286. break;
  1287. //Because it is not unset when the mob finishes walking.
  1288. md->state.skillstate = MSS_IDLE;
  1289. case MSS_IDLE:
  1290. // Idle skill.
  1291. if (!(++md->ud.walk_count%IDLE_SKILL_INTERVAL) && mob->skill_use(md, tick, -1))
  1292. break;
  1293. //Random walk.
  1294. if (!md->master_id &&
  1295. DIFF_TICK(md->next_walktime, tick) <= 0 &&
  1296. !mob->randomwalk(md,tick))
  1297. //Delay next random walk when this one failed.
  1298. md->next_walktime = tick+rnd()%1000;
  1299. break;
  1300. default:
  1301. mob_stop_attack(md);
  1302. mob_stop_walking(md,1); //Stop chasing.
  1303. md->state.skillstate = MSS_IDLE;
  1304. if(battle_config.mob_ai&0x8) //Walk instantly after dropping target
  1305. md->next_walktime = tick+rnd()%1000;
  1306. else
  1307. md->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME;
  1308. break;
  1309. }
  1310. if (md->target_id) {
  1311. md->target_id=0;
  1312. md->ud.target_to = 0;
  1313. unit->set_target(&md->ud, 0);
  1314. }
  1315. if(map->count_oncell(md->bl.m, md->bl.x, md->bl.y, BL_CHAR|BL_NPC, 1) > battle_config.official_cell_stack_limit) {
  1316. unit->walktoxy(&md->bl, md->bl.x, md->bl.y, 8);
  1317. }
  1318.  
  1319. return 0;
  1320. }
  1321. /*==========================================
  1322. * Random walk
  1323. *------------------------------------------*/
  1324. int mob_randomwalk(struct mob_data *md, int64 tick) {
  1325. const int retrycount=20;
  1326. int i,x,y,c,d;
  1327. int speed;
  1328.  
  1329. nullpo_ret(md);
  1330.  
  1331. if(DIFF_TICK(md->next_walktime,tick)>0 ||
  1332. !unit->can_move(&md->bl) ||
  1333. !(status_get_mode(&md->bl)&MD_CANMOVE))
  1334. return 0;
  1335.  
  1336. d =12-md->move_fail_count;
  1337. if(d<5) d=5;
  1338. if(d>7) d=7;
  1339. for (i = 0; i < retrycount; i++) {
  1340. // Search of a movable place
  1341. int r=rnd();
  1342. x=r%(d*2+1)-d;
  1343. y=r/(d*2+1)%(d*2+1)-d;
  1344. x+=md->bl.x;
  1345. y+=md->bl.y;
  1346.  
  1347. if(((x != md->bl.x) || (y != md->bl.y)) && map->getcell(md->bl.m,x,y,CELL_CHKPASS) && unit->walktoxy(&md->bl,x,y,8)){
  1348. break;
  1349. }
  1350. }
  1351. if(i==retrycount){
  1352. md->move_fail_count++;
  1353. if(md->move_fail_count>1000){
  1354. ShowWarning("MOB can't move. random spawn %d, class = %d, at %s (%d,%d)\n",md->bl.id,md->class_,map->list[md->bl.m].name, md->bl.x, md->bl.y);
  1355. md->move_fail_count=0;
  1356. mob->spawn(md);
  1357. }
  1358. return 0;
  1359. }
  1360. speed=status->get_speed(&md->bl);
  1361. for(i=c=0;i<md->ud.walkpath.path_len;i++) {
  1362. // The next walk start time is calculated.
  1363. if(md->ud.walkpath.path[i]&1)
  1364. c+=speed*MOVE_DIAGONAL_COST/MOVE_COST;
  1365. else
  1366. c+=speed;
  1367. }
  1368. md->state.skillstate=MSS_WALK;
  1369. md->move_fail_count=0;
  1370. md->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME+c;
  1371. return 1;
  1372. }
  1373.  
  1374. int mob_warpchase(struct mob_data *md, struct block_list *target)
  1375. {
  1376. struct npc_data *warp = NULL;
  1377. int distance = AREA_SIZE;
  1378. if (!(target && battle_config.mob_ai&0x40 && battle_config.mob_warp&1))
  1379. return 0; //Can't warp chase.
  1380.  
  1381. if (target->m == md->bl.m && check_distance_bl(&md->bl, target, AREA_SIZE))
  1382. return 0; //No need to do a warp chase.
  1383.  
  1384. if (md->ud.walktimer != INVALID_TIMER &&
  1385. map->getcell(md->bl.m,md->ud.to_x,md->ud.to_y,CELL_CHKNPC))
  1386. return 1; //Already walking to a warp.
  1387.  
  1388. //Search for warps within mob's viewing range.
  1389. map->foreachinrange(mob->warpchase_sub, &md->bl,
  1390. md->db->range2, BL_NPC, target, &warp, &distance);
  1391.  
  1392. if (warp && unit->walktobl(&md->bl, &warp->bl, 1, 1))
  1393. return 1;
  1394. return 0;
  1395. }
  1396.  
  1397. /*==========================================
  1398. * AI of MOB whose is near a Player
  1399. *------------------------------------------*/
  1400. bool mob_ai_sub_hard(struct mob_data *md, int64 tick) {
  1401. struct block_list *tbl = NULL, *abl = NULL;
  1402. int mode;
  1403. int view_range, can_move;
  1404.  
  1405. if(md->bl.prev == NULL || md->status.hp <= 0)
  1406. return false;
  1407.  
  1408. if (DIFF_TICK(tick, md->last_thinktime) < MIN_MOBTHINKTIME)
  1409. return false;
  1410.  
  1411. md->last_thinktime = tick;
  1412.  
  1413. if (md->ud.skilltimer != INVALID_TIMER)
  1414. return false;
  1415.  
  1416. // Abnormalities
  1417. if(( md->sc.opt1 > 0 && md->sc.opt1 != OPT1_STONEWAIT && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE )
  1418. || md->sc.data[SC_DEEP_SLEEP] || md->sc.data[SC_BLADESTOP] || md->sc.data[SC__MANHOLE] || md->sc.data[SC_CURSEDCIRCLE_TARGET]) {
  1419. //Should reset targets.
  1420. md->target_id = md->attacked_id = 0;
  1421. return false;
  1422. }
  1423.  
  1424. if (md->sc.count && md->sc.data[SC_BLIND])
  1425. view_range = 3;
  1426. else
  1427. view_range = md->db->range2;
  1428. mode = status_get_mode(&md->bl);
  1429.  
  1430. can_move = (mode&MD_CANMOVE)&&unit->can_move(&md->bl);
  1431.  
  1432. if (md->target_id) {
  1433. //Check validity of current target. [Skotlex]
  1434. tbl = map->id2bl(md->target_id);
  1435. if (!tbl || tbl->m != md->bl.m
  1436. || (md->ud.attacktimer == INVALID_TIMER && !status->check_skilluse(&md->bl, tbl, 0, 0))
  1437. || (md->ud.walktimer != INVALID_TIMER && !(battle_config.mob_ai&0x1) && !check_distance_bl(&md->bl, tbl, md->min_chase))
  1438. || ( tbl->type == BL_PC
  1439. && ((((TBL_PC*)tbl)->state.gangsterparadise && !(mode&MD_BOSS))
  1440. || ((TBL_PC*)tbl)->invincible_timer != INVALID_TIMER)
  1441. )
  1442. ) {
  1443. //No valid target
  1444. if (mob->warpchase(md, tbl))
  1445. return true; //Chasing this target.
  1446. if(md->ud.walktimer != INVALID_TIMER && (!can_move || md->ud.walkpath.path_pos <= battle_config.mob_chase_refresh)
  1447. && (tbl || md->ud.walkpath.path_pos == 0))
  1448. return true; //Walk at least "mob_chase_refresh" cells before dropping the target unless target is non-existent
  1449. mob->unlocktarget(md, tick); //Unlock target
  1450. tbl = NULL;
  1451. }
  1452. }
  1453.  
  1454. // Check for target change.
  1455. if (md->attacked_id && mode&MD_CANATTACK) {
  1456. if (md->attacked_id == md->target_id) {
  1457. //Rude attacked check.
  1458. if (!battle->check_range(&md->bl, tbl, md->status.rhw.range)
  1459. && ( //Can't attack back and can't reach back.
  1460. (!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 && (battle_config.mob_ai&0x2 || (md->sc.data[SC_SPIDERWEB] && md->sc.data[SC_SPIDERWEB]->val1)
  1461. || md->sc.data[SC_WUGBITE] || md->sc.data[SC_VACUUM_EXTREME] || md->sc.data[SC_THORNS_TRAP]
  1462. || md->sc.data[SC__MANHOLE] // Not yet confirmed if boss will teleport once it can't reach target.
  1463. || md->walktoxy_fail_count > 0)
  1464. )
  1465. || !mob->can_reach(md, tbl, md->min_chase, MSS_RUSH)
  1466. )
  1467. && md->state.attacked_count++ >= RUDE_ATTACKED_COUNT
  1468. && !mob->skill_use(md, tick, MSC_RUDEATTACKED) // If can't rude Attack
  1469. && can_move && unit->escape(&md->bl, tbl, rnd()%10 +1) // Attempt escape
  1470. ) {
  1471. //Escaped
  1472. md->attacked_id = 0;
  1473. return true;
  1474. }
  1475. }
  1476. else
  1477. if( (abl = map->id2bl(md->attacked_id)) && (!tbl || mob->can_changetarget(md, abl, mode) || (md->sc.count && md->sc.data[SC__CHAOS]))) {
  1478. int dist;
  1479. if( md->bl.m != abl->m || abl->prev == NULL
  1480. || (dist = distance_bl(&md->bl, abl)) >= MAX_MINCHASE // Attacker longer than visual area
  1481. || battle->check_target(&md->bl, abl, BCT_ENEMY) <= 0 // Attacker is not enemy of mob
  1482. || (battle_config.mob_ai&0x2 && !status->check_skilluse(&md->bl, abl, 0, 0)) // Cannot normal attack back to Attacker
  1483. || (!battle->check_range(&md->bl, abl, md->status.rhw.range) // Not on Melee Range and ...
  1484. && ( // Reach check
  1485. (!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 && (battle_config.mob_ai&0x2 || (md->sc.data[SC_SPIDERWEB] && md->sc.data[SC_SPIDERWEB]->val1)
  1486. || md->sc.data[SC_WUGBITE] || md->sc.data[SC_VACUUM_EXTREME] || md->sc.data[SC_THORNS_TRAP]
  1487. || md->sc.data[SC__MANHOLE] // Not yet confirmed if boss will teleport once it can't reach target.
  1488. || md->walktoxy_fail_count > 0)
  1489. )
  1490. || !mob->can_reach(md, abl, dist+md->db->range3, MSS_RUSH)
  1491. )
  1492. )
  1493. ) {
  1494. // Rude attacked
  1495. if (md->state.attacked_count++ >= RUDE_ATTACKED_COUNT
  1496. && !mob->skill_use(md, tick, MSC_RUDEATTACKED) && can_move
  1497. && !tbl && unit->escape(&md->bl, abl, rnd()%10 +1)
  1498. ) {
  1499. //Escaped.
  1500. //TODO: Maybe it shouldn't attempt to run if it has another, valid target?
  1501. md->attacked_id = 0;
  1502. return true;
  1503. }
  1504. }
  1505. else
  1506. if (!(battle_config.mob_ai&0x2) && !status->check_skilluse(&md->bl, abl, 0, 0)) {
  1507. //Can't attack back, but didn't invoke a rude attacked skill...
  1508. } else {
  1509. //Attackable
  1510. if (!tbl || dist < md->status.rhw.range
  1511. || !check_distance_bl(&md->bl, tbl, dist)
  1512. || battle->get_target(tbl) != md->bl.id
  1513. ) {
  1514. //Change if the new target is closer than the actual one
  1515. //or if the previous target is not attacking the mob. [Skotlex]
  1516. md->target_id = md->attacked_id; // set target
  1517. if (md->state.attacked_count)
  1518. md->state.attacked_count--; //Should we reset rude attack count?
  1519. md->min_chase = dist+md->db->range3;
  1520. if(md->min_chase>MAX_MINCHASE)
  1521. md->min_chase=MAX_MINCHASE;
  1522. tbl = abl; //Set the new target
  1523. }
  1524. }
  1525. }
  1526.  
  1527. //Clear it since it's been checked for already.
  1528. md->attacked_id = 0;
  1529. }
  1530.  
  1531. // Processing of slave monster
  1532. if (md->master_id > 0 && mob->ai_sub_hard_slavemob(md, tick))
  1533. return true;
  1534.  
  1535. // Scan area for targets
  1536. if (!tbl && mode&MD_LOOTER && md->lootitem && DIFF_TICK(tick, md->ud.canact_tick) > 0
  1537. && (md->lootitem_count < LOOTITEM_SIZE || battle_config.monster_loot_type != 1)
  1538. ) {
  1539. // Scan area for items to loot, avoid trying to loot if the mob is full and can't consume the items.
  1540. map->foreachinrange (mob->ai_sub_hard_lootsearch, &md->bl, view_range, BL_ITEM, md, &tbl);
  1541. }
  1542.  
  1543. if ((!tbl && mode&MD_AGGRESSIVE) || md->state.skillstate == MSS_FOLLOW) {
  1544. map->foreachinrange (mob->ai_sub_hard_activesearch, &md->bl, view_range, DEFAULT_ENEMY_TYPE(md), md, &tbl, mode);
  1545. } else if ((mode&MD_CHANGECHASE && (md->state.skillstate == MSS_RUSH || md->state.skillstate == MSS_FOLLOW)) || (md->sc.count && md->sc.data[SC__CHAOS])) {
  1546. int search_size;
  1547. search_size = view_range<md->status.rhw.range ? view_range:md->status.rhw.range;
  1548. map->foreachinrange (mob->ai_sub_hard_changechase, &md->bl, search_size, DEFAULT_ENEMY_TYPE(md), md, &tbl);
  1549. }
  1550.  
  1551. if (!tbl) { //No targets available.
  1552. if (mode&MD_ANGRY && !md->state.aggressive)
  1553. md->state.aggressive = 1; //Restore angry state when no targets are available.
  1554.  
  1555. /* bg guardians follow allies when no targets nearby */
  1556. if( md->bg_id && mode&MD_CANATTACK ) {
  1557. if( md->ud.walktimer != INVALID_TIMER )
  1558. return true;/* we are already moving */
  1559. map->foreachinrange (mob->ai_sub_hard_bg_ally, &md->bl, view_range, BL_PC, md, &tbl, mode);
  1560. if( tbl ) {
  1561. if( distance_blxy(&md->bl, tbl->x, tbl->y) <= 3 || unit->walktobl(&md->bl, tbl, 1, 1) )
  1562. return true;/* we're moving or close enough don't unlock the target. */
  1563. }
  1564. }
  1565.  
  1566. //This handles triggering idle/walk skill.
  1567. mob->unlocktarget(md, tick);
  1568. return true;
  1569. }
  1570.  
  1571. //Target exists, attack or loot as applicable.
  1572. if (tbl->type == BL_ITEM) {
  1573. //Loot time.
  1574. struct flooritem_data *fitem;
  1575. if (md->ud.target == tbl->id && md->ud.walktimer != INVALID_TIMER)
  1576. return true; //Already locked.
  1577. if (md->lootitem == NULL) {
  1578. //Can't loot...
  1579. mob->unlocktarget (md, tick);
  1580. return true;
  1581. }
  1582. if (!check_distance_bl(&md->bl, tbl, 1)) {
  1583. //Still not within loot range.
  1584. if (!(mode&MD_CANMOVE)) {
  1585. //A looter that can't move? Real smart.
  1586. mob->unlocktarget(md,tick);
  1587. return true;
  1588. }
  1589. if (!can_move) //Stuck. Wait before walking.
  1590. return true;
  1591. md->state.skillstate = MSS_LOOT;
  1592. if (!unit->walktobl(&md->bl, tbl, 1, 1))
  1593. mob->unlocktarget(md, tick); //Can't loot...
  1594. return true;
  1595. }
  1596. //Within looting range.
  1597. if (md->ud.attacktimer != INVALID_TIMER)
  1598. return true; //Busy attacking?
  1599.  
  1600. fitem = (struct flooritem_data *)tbl;
  1601. //Logs items, taken by (L)ooter Mobs [Lupus]
  1602. logs->pick_mob(md, LOG_TYPE_LOOT, fitem->item_data.amount, &fitem->item_data, NULL);
  1603.  
  1604. if (md->lootitem_count < LOOTITEM_SIZE) {
  1605. memcpy (&md->lootitem[md->lootitem_count++], &fitem->item_data, sizeof(md->lootitem[0]));
  1606. } else {
  1607. //Destroy first looted item...
  1608. if (md->lootitem[0].card[0] == CARD0_PET)
  1609. intif->delete_petdata( MakeDWord(md->lootitem[0].card[1],md->lootitem[0].card[2]) );
  1610. memmove(&md->lootitem[0], &md->lootitem[1], (LOOTITEM_SIZE-1)*sizeof(md->lootitem[0]));
  1611. memcpy (&md->lootitem[LOOTITEM_SIZE-1], &fitem->item_data, sizeof(md->lootitem[0]));
  1612. }
  1613. if (pcdb_checkid(md->vd->class_)) {
  1614. //Give them walk act/delay to properly mimic players. [Skotlex]
  1615. clif->takeitem(&md->bl,tbl);
  1616. md->ud.canact_tick = tick + md->status.amotion;
  1617. unit->set_walkdelay(&md->bl, tick, md->status.amotion, 1);
  1618. }
  1619. //Clear item.
  1620. map->clearflooritem (tbl);
  1621. mob->unlocktarget (md,tick);
  1622. return true;
  1623. }
  1624.  
  1625. //Attempt to attack.
  1626. //At this point we know the target is attackable, we just gotta check if the range matches.
  1627. if (battle->check_range(&md->bl, tbl, md->status.rhw.range) && !(md->sc.option&OPTION_HIDE)) {
  1628. //Target within range and able to use normal attack, engage
  1629. if (md->ud.target != tbl->id || md->ud.attacktimer == INVALID_TIMER)
  1630. { //Only attack if no more attack delay left
  1631. if(tbl->type == BL_PC)
  1632. mob->log_damage(md, tbl, 0); //Log interaction (counts as 'attacker' for the exp bonus)
  1633. unit->attack(&md->bl,tbl->id,1);
  1634. }
  1635. return true;
  1636. }
  1637.  
  1638. //Monsters in berserk state, unable to use normal attacks, will always attempt a skill
  1639. if(md->ud.walktimer == INVALID_TIMER && (md->state.skillstate == MSS_BERSERK || md->state.skillstate == MSS_ANGRY)) {
  1640. if (DIFF_TICK(md->ud.canmove_tick, tick) <= MIN_MOBTHINKTIME && DIFF_TICK(md->ud.canact_tick, tick) < -MIN_MOBTHINKTIME*IDLE_SKILL_INTERVAL)
  1641. { //Only use skill if able to walk on next tick and not used a skill the last second
  1642. mob->skill_use(md, tick, -1);
  1643. }
  1644. }
  1645.  
  1646. //Target still in attack range, no need to chase the target
  1647. if(battle->check_range(&md->bl, tbl, md->status.rhw.range))
  1648. return true;
  1649.  
  1650. //Only update target cell / drop target after having moved at least "mob_chase_refresh" cells
  1651. if(md->ud.walktimer != INVALID_TIMER && (!can_move || md->ud.walkpath.path_pos <= battle_config.mob_chase_refresh))
  1652. return true;
  1653.  
  1654. //Out of range...
  1655. if (!(mode&MD_CANMOVE) || (!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0)) {
  1656. //Can't chase. Immobile and trapped mobs should unlock target and use an idle skill.
  1657. if (md->ud.attacktimer == INVALID_TIMER)
  1658. { //Only unlock target if no more attack delay left
  1659. //This handles triggering idle/walk skill.
  1660. mob->unlocktarget(md,tick);
  1661. }
  1662. return true;
  1663. }
  1664.  
  1665. if (md->ud.walktimer != INVALID_TIMER && md->ud.target == tbl->id &&
  1666. (
  1667. !(battle_config.mob_ai&0x1) ||
  1668. check_distance_blxy(tbl, md->ud.to_x, md->ud.to_y, md->status.rhw.range)
  1669. )) //Current target tile is still within attack range.
  1670. return true;
  1671.  
  1672. //Follow up if possible.
  1673. //Hint: Chase skills are handled in the walktobl routine
  1674. if(!mob->can_reach(md, tbl, md->min_chase, MSS_RUSH) ||
  1675. !unit->walktobl(&md->bl, tbl, md->status.rhw.range, 2))
  1676. mob->unlocktarget(md,tick);
  1677.  
  1678. return true;
  1679. }
  1680.  
  1681. int mob_ai_sub_hard_timer(struct block_list *bl, va_list ap) {
  1682. struct mob_data *md = (struct mob_data*)bl;
  1683. int64 tick = va_arg(ap, int64);
  1684. if (mob->ai_sub_hard(md, tick)) {
  1685. //Hard AI triggered.
  1686. if(!md->state.spotted)
  1687. md->state.spotted = 1;
  1688. md->last_pcneartime = tick;
  1689. }
  1690. return 0;
  1691. }
  1692.  
  1693. /*==========================================
  1694. * Serious processing for mob in PC field of view (foreachclient)
  1695. *------------------------------------------*/
  1696. int mob_ai_sub_foreachclient(struct map_session_data *sd, va_list ap) {
  1697. int64 tick;
  1698. tick=va_arg(ap, int64);
  1699. map->foreachinrange(mob->ai_sub_hard_timer,&sd->bl, AREA_SIZE+ACTIVE_AI_RANGE, BL_MOB,tick);
  1700.  
  1701. return 0;
  1702. }
  1703.  
  1704. /*==========================================
  1705. * Negligent mode MOB AI (PC is not in near)
  1706. *------------------------------------------*/
  1707. int mob_ai_sub_lazy(struct mob_data *md, va_list args) {
  1708. int64 tick;
  1709.  
  1710. nullpo_ret(md);
  1711.  
  1712. if(md->bl.prev == NULL)
  1713. return 0;
  1714.  
  1715. tick = va_arg(args, int64);
  1716.  
  1717. if (battle_config.mob_ai&0x20 && map->list[md->bl.m].users>0)
  1718. return (int)mob->ai_sub_hard(md, tick);
  1719.  
  1720. if (md->bl.prev==NULL || md->status.hp == 0)
  1721. return 1;
  1722.  
  1723. if (battle_config.mob_active_time
  1724. && md->last_pcneartime
  1725. && !(md->status.mode&MD_BOSS)
  1726. && DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME
  1727. ) {
  1728. if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.mob_active_time)
  1729. return (int)mob->ai_sub_hard(md, tick);
  1730. md->last_pcneartime = 0;
  1731. }
  1732.  
  1733. if(battle_config.boss_active_time &&
  1734. md->last_pcneartime &&
  1735. (md->status.mode&MD_BOSS) &&
  1736. DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME)
  1737. {
  1738. if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.boss_active_time)
  1739. return (int)mob->ai_sub_hard(md, tick);
  1740. md->last_pcneartime = 0;
  1741. }
  1742.  
  1743. if(DIFF_TICK(tick,md->last_thinktime)< 10*MIN_MOBTHINKTIME)
  1744. return 0;
  1745.  
  1746. md->last_thinktime=tick;
  1747.  
  1748. if (md->master_id) {
  1749. mob->ai_sub_hard_slavemob(md,tick);
  1750. return 0;
  1751. }
  1752.  
  1753. if( DIFF_TICK(md->next_walktime,tick) < 0 && (status_get_mode(&md->bl)&MD_CANMOVE) && unit->can_move(&md->bl) ) {
  1754. if( rnd()%1000 < MOB_LAZYMOVEPERC(md) )
  1755. mob->randomwalk(md, tick);
  1756. }
  1757. else if( md->ud.walktimer == INVALID_TIMER )
  1758. {
  1759. //Because it is not unset when the mob finishes walking.
  1760. md->state.skillstate = MSS_IDLE;
  1761. if( rnd()%1000 < MOB_LAZYSKILLPERC(md) ) //Chance to do a mob's idle skill.
  1762. mob->skill_use(md, tick, -1);
  1763. }
  1764.  
  1765. return 0;
  1766. }
  1767.  
  1768. /*==========================================
  1769. * Negligent processing for mob outside PC field of view (interval timer function)
  1770. *------------------------------------------*/
  1771. int mob_ai_lazy(int tid, int64 tick, int id, intptr_t data) {
  1772. map->foreachmob(mob->ai_sub_lazy,tick);
  1773. return 0;
  1774. }
  1775.  
  1776. /*==========================================
  1777. * Serious processing for mob in PC field of view (interval timer function)
  1778. *------------------------------------------*/
  1779. int mob_ai_hard(int tid, int64 tick, int id, intptr_t data) {
  1780.  
  1781. if (battle_config.mob_ai&0x20)
  1782. map->foreachmob(mob->ai_sub_lazy,tick);
  1783. else
  1784. map->foreachpc(mob->ai_sub_foreachclient,tick);
  1785.  
  1786. return 0;
  1787. }
  1788.  
  1789. /*==========================================
  1790. * Initializes the delay drop structure for mob-dropped items.
  1791. *------------------------------------------*/
  1792. struct item_drop* mob_setdropitem(int nameid, int qty, struct item_data *data) {
  1793. struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
  1794. drop->item_data.nameid = nameid;
  1795. drop->item_data.amount = qty;
  1796. drop->item_data.identify = data ? itemdb->isidentified2(data) : itemdb->isidentified(nameid);
  1797. drop->next = NULL;
  1798. return drop;
  1799. }
  1800.  
  1801. /*==========================================
  1802. * Initializes the delay drop structure for mob-looted items.
  1803. *------------------------------------------*/
  1804. struct item_drop* mob_setlootitem(struct item* item)
  1805. {
  1806. struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
  1807. memcpy(&drop->item_data, item, sizeof(struct item));
  1808. drop->next = NULL;
  1809. return drop;
  1810. }
  1811.  
  1812. /*==========================================
  1813. * item drop with delay (timer function)
  1814. *------------------------------------------*/
  1815. int mob_delay_item_drop(int tid, int64 tick, int id, intptr_t data) {
  1816. struct item_drop_list *list;
  1817. struct item_drop *ditem, *ditem_prev;
  1818. list=(struct item_drop_list *)data;
  1819. ditem = list->item;
  1820. while (ditem) {
  1821. map->addflooritem(&ditem->item_data,ditem->item_data.amount,
  1822. list->m,list->x,list->y,
  1823. list->first_charid,list->second_charid,list->third_charid,0);
  1824. ditem_prev = ditem;
  1825. ditem = ditem->next;
  1826. ers_free(item_drop_ers, ditem_prev);
  1827. }
  1828. ers_free(item_drop_list_ers, list);
  1829. return 0;
  1830. }
  1831.  
  1832. /*==========================================
  1833. * Sets the item_drop into the item_drop_list.
  1834. * Also performs logging and autoloot if enabled.
  1835. * rate is the drop-rate of the item, required for autoloot.
  1836. * flag : Killed only by homunculus?
  1837. *------------------------------------------*/
  1838. void mob_item_drop(struct mob_data *md, struct item_drop_list *dlist, struct item_drop *ditem, int loot, int drop_rate, unsigned short flag)
  1839. {
  1840. TBL_PC* sd;
  1841.  
  1842. //Logs items, dropped by mobs [Lupus]
  1843. logs->pick_mob(md, loot?LOG_TYPE_LOOT:LOG_TYPE_PICKDROP_MONSTER, -ditem->item_data.amount, &ditem->item_data, NULL);
  1844.  
  1845. sd = map->charid2sd(dlist->first_charid);
  1846. if( sd == NULL ) sd = map->charid2sd(dlist->second_charid);
  1847. if( sd == NULL ) sd = map->charid2sd(dlist->third_charid);
  1848.  
  1849. if( sd
  1850. && (drop_rate <= sd->state.autoloot || pc->isautolooting(sd, ditem->item_data.nameid))
  1851. && (battle_config.idle_no_autoloot == 0 || DIFF_TICK(sockt->last_tick, sd->idletime) < battle_config.idle_no_autoloot)
  1852. && (battle_config.homunculus_autoloot?1:!flag)
  1853. #ifdef AUTOLOOT_DISTANCE
  1854. && sd->bl.m == md->bl.m
  1855. && check_distance_blxy(&sd->bl, dlist->x, dlist->y, AUTOLOOT_DISTANCE)
  1856. #endif
  1857. ) {
  1858. //Autoloot.
  1859. if (party->share_loot(party->search(sd->status.party_id),
  1860. sd, &ditem->item_data, sd->status.char_id) == 0
  1861. ) {
  1862. ers_free(item_drop_ers, ditem);
  1863. return;
  1864. }
  1865. }
  1866. ditem->next = dlist->item;
  1867. dlist->item = ditem;
  1868. }
  1869.  
  1870. int mob_timer_delete(int tid, int64 tick, int id, intptr_t data) {
  1871. struct block_list* bl = map->id2bl(id);
  1872. struct mob_data* md = BL_CAST(BL_MOB, bl);
  1873.  
  1874. if( md )
  1875. {
  1876. if( md->deletetimer != tid )
  1877. {
  1878. ShowError("mob_timer_delete: Timer mismatch: %d != %d\n", tid, md->deletetimer);
  1879. return 0;
  1880. }
  1881. //for Alchemist CANNIBALIZE [Lupus]
  1882. md->deletetimer = INVALID_TIMER;
  1883. unit->free(bl, CLR_TELEPORT);
  1884. }
  1885. return 0;
  1886. }
  1887.  
  1888. /*==========================================
  1889. *
  1890. *------------------------------------------*/
  1891. int mob_deleteslave_sub(struct block_list *bl,va_list ap)
  1892. {
  1893. struct mob_data *md;
  1894. int id;
  1895.  
  1896. nullpo_ret(bl);
  1897. nullpo_ret(md = (struct mob_data *)bl);
  1898.  
  1899. id=va_arg(ap,int);
  1900. if(md->master_id > 0 && md->master_id == id )
  1901. status_kill(bl);
  1902. return 0;
  1903. }
  1904.  
  1905. /*==========================================
  1906. *
  1907. *------------------------------------------*/
  1908. int mob_deleteslave(struct mob_data *md) {
  1909. nullpo_ret(md);
  1910.  
  1911. map->foreachinmap(mob->deleteslave_sub, md->bl.m, BL_MOB,md->bl.id);
  1912. return 0;
  1913. }
  1914. // Mob respawning through KAIZEL or NPC_REBIRTH [Skotlex]
  1915. int mob_respawn(int tid, int64 tick, int id, intptr_t data) {
  1916. struct block_list *bl = map->id2bl(id);
  1917.  
  1918. if(!bl) return 0;
  1919. status->revive(bl, (uint8)data, 0);
  1920. return 1;
  1921. }
  1922.  
  1923. void mob_log_damage(struct mob_data *md, struct block_list *src, int damage)
  1924. {
  1925. int char_id = 0, flag = MDLF_NORMAL;
  1926.  
  1927. if( damage < 0 )
  1928. return; //Do nothing for absorbed damage.
  1929. if( !damage && !(src->type&DEFAULT_ENEMY_TYPE(md)) )
  1930. return; //Do not log non-damaging effects from non-enemies.
  1931. if( src->id == md->bl.id )
  1932. return; //Do not log self-damage.
  1933.  
  1934. switch( src->type )
  1935. {
  1936. case BL_PC:
  1937. {
  1938. struct map_session_data *sd = (TBL_PC*)src;
  1939. char_id = sd->status.char_id;
  1940. if( damage )
  1941. md->attacked_id = src->id;
  1942. break;
  1943. }
  1944. case BL_HOM:
  1945. {
  1946. struct homun_data *hd = (TBL_HOM*)src;
  1947. flag = MDLF_HOMUN;
  1948. if( hd->master )
  1949. char_id = hd->master->status.char_id;
  1950. if( damage )
  1951. md->attacked_id = src->id;
  1952. break;
  1953. }
  1954. case BL_MER:
  1955. {
  1956. struct mercenary_data *mer = (TBL_MER*)src;
  1957. if( mer->master )
  1958. char_id = mer->master->status.char_id;
  1959. if( damage )
  1960. md->attacked_id = src->id;
  1961. break;
  1962. }
  1963. case BL_PET:
  1964. {
  1965. struct pet_data *pd = (TBL_PET*)src;
  1966. flag = MDLF_PET;
  1967. if( pd->msd )
  1968. {
  1969. char_id = pd->msd->status.char_id;
  1970. if( damage ) //Let mobs retaliate against the pet's master [Skotlex]
  1971. md->attacked_id = pd->msd->bl.id;
  1972. }
  1973. break;
  1974. }
  1975. case BL_MOB:
  1976. {
  1977. struct mob_data* md2 = (TBL_MOB*)src;
  1978. if( md2->special_state.ai && md2->master_id ) {
  1979. struct map_session_data* msd = map->id2sd(md2->master_id);
  1980. if( msd )
  1981. char_id = msd->status.char_id;
  1982. }
  1983. if( !damage )
  1984. break;
  1985. //Let players decide whether to retaliate versus the master or the mob. [Skotlex]
  1986. if( md2->master_id && battle_config.retaliate_to_master )
  1987. md->attacked_id = md2->master_id;
  1988. else
  1989. md->attacked_id = src->id;
  1990. break;
  1991. }
  1992. case BL_ELEM:
  1993. {
  1994. struct elemental_data *ele = (TBL_ELEM*)src;
  1995. if( ele->master )
  1996. char_id = ele->master->status.char_id;
  1997. if( damage )
  1998. md->attacked_id = src->id;
  1999. break;
  2000. }
  2001. default: //For all unhandled types.
  2002. md->attacked_id = src->id;
  2003. }
  2004.  
  2005. if( char_id )
  2006. { //Log damage...
  2007. int i,minpos;
  2008. unsigned int mindmg;
  2009. for(i=0,minpos=DAMAGELOG_SIZE-1,mindmg=UINT_MAX;i<DAMAGELOG_SIZE;i++){
  2010. if(md->dmglog[i].id==char_id &&
  2011. md->dmglog[i].flag==flag)
  2012. break;
  2013. if(md->dmglog[i].id==0) {
  2014. //Store data in first empty slot.
  2015. md->dmglog[i].id = char_id;
  2016. md->dmglog[i].flag= flag;
  2017. break;
  2018. }
  2019. if (md->dmglog[i].dmg<mindmg && i) {
  2020. //Never overwrite first hit slot (he gets double exp bonus)
  2021. minpos=i;
  2022. mindmg=md->dmglog[i].dmg;
  2023. }
  2024. }
  2025. if(i<DAMAGELOG_SIZE)
  2026. md->dmglog[i].dmg+=damage;
  2027. else {
  2028. md->dmglog[minpos].id = char_id;
  2029. md->dmglog[minpos].flag= flag;
  2030. md->dmglog[minpos].dmg = damage;
  2031. }
  2032. }
  2033. return;
  2034. }
  2035. //Call when a mob has received damage.
  2036. void mob_damage(struct mob_data *md, struct block_list *src, int damage) {
  2037. if (damage > 0) { //Store total damage...
  2038. if (UINT_MAX - (unsigned int)damage > md->tdmg)
  2039. md->tdmg+=damage;
  2040. else if (md->tdmg == UINT_MAX)
  2041. damage = 0; //Stop recording damage once the cap has been reached.
  2042. else { //Cap damage log...
  2043. damage = (int)(UINT_MAX - md->tdmg);
  2044. md->tdmg = UINT_MAX;
  2045. }
  2046. if (md->state.aggressive) { //No longer aggressive, change to retaliate AI.
  2047. md->state.aggressive = 0;
  2048. if(md->state.skillstate== MSS_ANGRY)
  2049. md->state.skillstate = MSS_BERSERK;
  2050. if(md->state.skillstate== MSS_FOLLOW)
  2051. md->state.skillstate = MSS_RUSH;
  2052. }
  2053. //Log damage
  2054. if (src)
  2055. mob->log_damage(md, src, damage);
  2056. md->dmgtick = timer->gettick();
  2057. }
  2058.  
  2059. if (battle_config.show_mob_info&3)
  2060. clif->charnameack (0, &md->bl);
  2061.  
  2062. if (!src)
  2063. return;
  2064.  
  2065. #if PACKETVER >= 20120404
  2066. if( !(md->status.mode&MD_BOSS) ){
  2067. int i;
  2068. for(i = 0; i < DAMAGELOG_SIZE; i++){ // must show hp bar to all char who already hit the mob.
  2069. if( md->dmglog[i].id ) {
  2070. struct map_session_data *sd = map->charid2sd(md->dmglog[i].id);
  2071. if( sd && check_distance_bl(&md->bl, &sd->bl, AREA_SIZE) ) // check if in range
  2072. clif->monster_hp_bar(md,sd);
  2073. }
  2074. }
  2075. }
  2076. #endif
  2077.  
  2078. if( md->special_state.ai == 2 ) {//LOne WOlf explained that ANYONE can trigger the marine countdown skill. [Skotlex]
  2079. md->state.alchemist = 1;
  2080. mob->skill_use(md, timer->gettick(), MSC_ALCHEMIST);
  2081. }
  2082. }
  2083.  
  2084. /*==========================================
  2085. * Signals death of mob.
  2086. * type&1 -> no drops, type&2 -> no exp
  2087. *------------------------------------------*/
  2088. int mob_dead(struct mob_data *md, struct block_list *src, int type) {
  2089. struct status_data *mstatus;
  2090. struct map_session_data *sd = NULL, *tmpsd[DAMAGELOG_SIZE];
  2091. struct map_session_data *mvp_sd = NULL, *second_sd = NULL, *third_sd = NULL;
  2092.  
  2093. struct {
  2094. struct party_data *p;
  2095. int id,zeny;
  2096. unsigned int base_exp,job_exp;
  2097. } pt[DAMAGELOG_SIZE];
  2098. int i, temp, count, m = md->bl.m, pnum = 0;
  2099. int dmgbltypes = 0; // bitfield of all bl types, that caused damage to the mob and are eligible for exp distribution
  2100. unsigned int mvp_damage;
  2101. int64 tick = timer->gettick();
  2102. bool rebirth, homkillonly;
  2103.  
  2104. mstatus = &md->status;
  2105.  
  2106. if( src && src->type == BL_PC )
  2107. {
  2108. sd = (struct map_session_data *)src;
  2109. mvp_sd = sd;
  2110. }
  2111.  
  2112. if( md->guardian_data && md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
  2113. guild->castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
  2114.  
  2115. if( src )
  2116. { // Use Dead skill only if not killed by Script or Command
  2117. md->state.skillstate = MSS_DEAD;
  2118. mob->skill_use(md,tick,-1);
  2119. }
  2120.  
  2121. map->freeblock_lock();
  2122.  
  2123. memset(pt,0,sizeof(pt));
  2124.  
  2125. if(src && src->type == BL_MOB)
  2126. mob->unlocktarget((struct mob_data *)src,tick);
  2127.  
  2128. // filter out entries not eligible for exp distribution
  2129. memset(tmpsd,0,sizeof(tmpsd));
  2130. for(i = 0, count = 0, mvp_damage = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++) {
  2131. struct map_session_data* tsd = map->charid2sd(md->dmglog[i].id);
  2132.  
  2133. if(tsd == NULL)
  2134. continue; // skip empty entries
  2135. if(tsd->bl.m != m)
  2136. continue; // skip players not on this map
  2137. count++; //Only logged into same map chars are counted for the total.
  2138. if (pc_isdead(tsd))
  2139. continue; // skip dead players
  2140. if(md->dmglog[i].flag == MDLF_HOMUN && !homun_alive(tsd->hd))
  2141. continue; // skip homunc's share if inactive
  2142. if( md->dmglog[i].flag == MDLF_PET && (!tsd->status.pet_id || !tsd->pd) )
  2143. continue; // skip pet's share if inactive
  2144.  
  2145. if(md->dmglog[i].dmg > mvp_damage) {
  2146. third_sd = second_sd;
  2147. second_sd = mvp_sd;
  2148. mvp_sd = tsd;
  2149. mvp_damage = md->dmglog[i].dmg;
  2150. }
  2151.  
  2152. tmpsd[i] = tsd; // record as valid damage-log entry
  2153.  
  2154. switch( md->dmglog[i].flag ) {
  2155. case MDLF_NORMAL: dmgbltypes|= BL_PC; break;
  2156. case MDLF_HOMUN: dmgbltypes|= BL_HOM; break;
  2157. case MDLF_PET: dmgbltypes|= BL_PET; break;
  2158. }
  2159. }
  2160.  
  2161. // determines, if the monster was killed by homunculus' damage only
  2162. homkillonly = (bool)( ( dmgbltypes&BL_HOM ) && !( dmgbltypes&~BL_HOM ) );
  2163.  
  2164. if (!battle_config.exp_calc_type && count > 1) {
  2165. //Apply first-attacker 200% exp share bonus
  2166. //TODO: Determine if this should go before calculating the MVP player instead of after.
  2167. if (UINT_MAX - md->dmglog[0].dmg > md->tdmg) {
  2168. md->tdmg += md->dmglog[0].dmg;
  2169. md->dmglog[0].dmg<<=1;
  2170. } else {
  2171. md->dmglog[0].dmg+= UINT_MAX - md->tdmg;
  2172. md->tdmg = UINT_MAX;
  2173. }
  2174. }
  2175.  
  2176. if( !(type&2) //No exp
  2177. && (!map->list[m].flag.pvp || battle_config.pvp_exp) //Pvp no exp rule [MouseJstr]
  2178. && (!md->master_id || !md->special_state.ai) //Only player-summoned mobs do not give exp. [Skotlex]
  2179. && (!map->list[m].flag.nobaseexp || !map->list[m].flag.nojobexp) //Gives Exp
  2180. ) { //Experience calculation.
  2181. int bonus = 100; //Bonus on top of your share (common to all attackers).
  2182. if (md->sc.data[SC_RICHMANKIM])
  2183. bonus += md->sc.data[SC_RICHMANKIM]->val2;
  2184. if(sd) {
  2185. temp = status->get_class(&md->bl);
  2186. if(sd->sc.data[SC_MIRACLE]) i = 2; //All mobs are Star Targets
  2187. else
  2188. ARR_FIND(0, MAX_PC_FEELHATE, i, temp == sd->hate_mob[i] &&
  2189. (battle_config.allow_skill_without_day || pc->sg_info[i].day_func()));
  2190. if(i<MAX_PC_FEELHATE && (temp=pc->checkskill(sd,pc->sg_info[i].bless_id)))
  2191. bonus += (i==2?20:10)*temp;
  2192. }
  2193. if(battle_config.mobs_level_up && md->level > md->db->lv) // [Valaris]
  2194. bonus += (md->level-md->db->lv)*battle_config.mobs_level_up_exp_rate;
  2195.  
  2196. for(i = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++) {
  2197. int flag=1,zeny=0;
  2198. unsigned int base_exp, job_exp;
  2199. double per; //Your share of the mob's exp
  2200.  
  2201. if (!tmpsd[i]) continue;
  2202.  
  2203. if (!battle_config.exp_calc_type && md->tdmg)
  2204. //jAthena's exp formula based on total damage.
  2205. per = (double)md->dmglog[i].dmg/(double)md->tdmg;
  2206. else {
  2207. //eAthena's exp formula based on max hp.
  2208. per = (double)md->dmglog[i].dmg/(double)mstatus->max_hp;
  2209. if (per > 2) per = 2; // prevents unlimited exp gain
  2210. }
  2211.  
  2212. if (count>1 && battle_config.exp_bonus_attacker) {
  2213. //Exp bonus per additional attacker.
  2214. if (count > battle_config.exp_bonus_max_attacker)
  2215. count = battle_config.exp_bonus_max_attacker;
  2216. per += per*((count-1)*battle_config.exp_bonus_attacker)/100.;
  2217. }
  2218.  
  2219. // change experience for different sized monsters [Valaris]
  2220. if (battle_config.mob_size_influence) {
  2221. switch( md->special_state.size ) {
  2222. case SZ_MEDIUM:
  2223. per /= 2.;
  2224. break;
  2225. case SZ_BIG:
  2226. per *= 2.;
  2227. break;
  2228. }
  2229. }
  2230.  
  2231. if( md->dmglog[i].flag == MDLF_PET )
  2232. per *= battle_config.pet_attack_exp_rate/100.;
  2233.  
  2234. if(battle_config.zeny_from_mobs && md->level) {
  2235. // zeny calculation moblv + random moblv [Valaris]
  2236. zeny=(int) ((md->level+rnd()%md->level)*per*bonus/100.);
  2237. if(md->db->mexp > 0)
  2238. zeny*=rnd()%250;
  2239. }
  2240.  
  2241. if (map->list[m].flag.nobaseexp || !md->db->base_exp)
  2242. base_exp = 0;
  2243. else
  2244. base_exp = (unsigned int)cap_value(md->db->base_exp * per * bonus/100. * map->list[m].bexp/100., 1, UINT_MAX);
  2245.  
  2246. if (map->list[m].flag.nojobexp || !md->db->job_exp || md->dmglog[i].flag == MDLF_HOMUN) //Homun earned job-exp is always lost.
  2247. job_exp = 0;
  2248. else
  2249. job_exp = (unsigned int)cap_value(md->db->job_exp * per * bonus/100. * map->list[m].jexp/100., 1, UINT_MAX);
  2250.  
  2251. if ( (temp = tmpsd[i]->status.party_id) > 0 ) {
  2252. int j;
  2253. for( j = 0; j < pnum && pt[j].id != temp; j++ ); //Locate party.
  2254.  
  2255. if( j == pnum ){ //Possibly add party.
  2256. pt[pnum].p = party->search(temp);
  2257. if(pt[pnum].p && pt[pnum].p->party.exp) {
  2258. pt[pnum].id = temp;
  2259. pt[pnum].base_exp = base_exp;
  2260. pt[pnum].job_exp = job_exp;
  2261. pt[pnum].zeny = zeny; // zeny share [Valaris]
  2262. pnum++;
  2263. flag=0;
  2264. }
  2265. } else {
  2266. //Add to total
  2267. if (pt[j].base_exp > UINT_MAX - base_exp)
  2268. pt[j].base_exp = UINT_MAX;
  2269. else
  2270. pt[j].base_exp += base_exp;
  2271.  
  2272. if (pt[j].job_exp > UINT_MAX - job_exp)
  2273. pt[j].job_exp = UINT_MAX;
  2274. else
  2275. pt[j].job_exp += job_exp;
  2276.  
  2277. pt[j].zeny+=zeny; // zeny share [Valaris]
  2278. flag=0;
  2279. }
  2280. }
  2281. if(base_exp && md->dmglog[i].flag == MDLF_HOMUN) //tmpsd[i] is null if it has no homunc.
  2282. homun->gainexp(tmpsd[i]->hd, base_exp);
  2283. if(flag) {
  2284. if(base_exp || job_exp) {
  2285. if( md->dmglog[i].flag != MDLF_PET || battle_config.pet_attack_exp_to_master ) {
  2286. #ifdef RENEWAL_EXP
  2287. int rate = pc->level_penalty_mod(md->level - (tmpsd[i])->status.base_level, md->status.race, md->status.mode, 1);
  2288. base_exp = (unsigned int)cap_value(base_exp * rate / 100, 1, UINT_MAX);
  2289. job_exp = (unsigned int)cap_value(job_exp * rate / 100, 1, UINT_MAX);
  2290. #endif
  2291. pc->gainexp(tmpsd[i], &md->bl, base_exp, job_exp, false);
  2292. }
  2293. }
  2294. if(zeny) // zeny from mobs [Valaris]
  2295. pc->getzeny(tmpsd[i], zeny, LOG_TYPE_PICKDROP_MONSTER, NULL);
  2296. }
  2297. }
  2298.  
  2299. for( i = 0; i < pnum; i++ ) //Party share.
  2300. party->exp_share(pt[i].p, &md->bl, pt[i].base_exp,pt[i].job_exp,pt[i].zeny);
  2301.  
  2302. } //End EXP giving.
  2303.  
  2304. if( !(type&1) && !map->list[m].flag.nomobloot && !md->state.rebirth && (
  2305. !md->special_state.ai || //Non special mob
  2306. battle_config.alchemist_summon_reward == 2 || //All summoned give drops
  2307. (md->special_state.ai==2 && battle_config.alchemist_summon_reward == 1) //Marine Sphere Drops items.
  2308. ) )
  2309. { // Item Drop
  2310. struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  2311. struct item_drop *ditem;
  2312. struct item_data* it = NULL;
  2313. int drop_rate;
  2314. #ifdef RENEWAL_DROP
  2315. int drop_modifier = mvp_sd ? pc->level_penalty_mod( md->level - mvp_sd->status.base_level, md->status.race, md->status.mode, 2) :
  2316. second_sd ? pc->level_penalty_mod( md->level - second_sd->status.base_level, md->status.race, md->status.mode, 2):
  2317. third_sd ? pc->level_penalty_mod( md->level - third_sd->status.base_level, md->status.race, md->status.mode, 2) :
  2318. 100;/* no player was attached, we don't use any modifier (100 = rates are not touched) */
  2319. #endif
  2320. dlist->m = md->bl.m;
  2321. dlist->x = md->bl.x;
  2322. dlist->y = md->bl.y;
  2323. dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
  2324. dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
  2325. dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
  2326. dlist->item = NULL;
  2327.  
  2328. for (i = 0; i < MAX_MOB_DROP; i++)
  2329. {
  2330. if (md->db->dropitem[i].nameid <= 0)
  2331. continue;
  2332. if ( !(it = itemdb->exists(md->db->dropitem[i].nameid)) )
  2333. continue;
  2334. drop_rate = md->db->dropitem[i].p;
  2335. if (drop_rate <= 0) {
  2336. if (battle_config.drop_rate0item)
  2337. continue;
  2338. drop_rate = 1;
  2339. }
  2340.  
  2341. // change drops depending on monsters size [Valaris]
  2342. if (battle_config.mob_size_influence)
  2343. {
  2344. if (md->special_state.size == SZ_MEDIUM && drop_rate >= 2)
  2345. drop_rate /= 2;
  2346. else if( md->special_state.size == SZ_BIG)
  2347. drop_rate *= 2;
  2348. }
  2349.  
  2350. if (src) {
  2351. //Drops affected by luk as a fixed increase [Valaris]
  2352. if (battle_config.drops_by_luk)
  2353. drop_rate += status_get_luk(src)*battle_config.drops_by_luk/100;
  2354. //Drops affected by luk as a % increase [Skotlex]
  2355. if (battle_config.drops_by_luk2)
  2356. drop_rate += (int)(0.5+drop_rate*status_get_luk(src)*battle_config.drops_by_luk2/10000.);
  2357. }
  2358. if (sd && battle_config.pk_mode &&
  2359. (int)(md->level - sd->status.base_level) >= 20)
  2360. drop_rate = (int)(drop_rate*1.25); // pk_mode increase drops if 20 level difference [Valaris]
  2361.  
  2362. // Increase drop rate if user has SC_CASH_RECEIVEITEM
  2363. if (sd && sd->sc.data[SC_CASH_RECEIVEITEM]) // now rig the drop rate to never be over 90% unless it is originally >90%.
  2364. drop_rate = max(drop_rate,cap_value((int)(0.5+drop_rate*(sd->sc.data[SC_CASH_RECEIVEITEM]->val1)/100.),0,9000));
  2365. #ifdef RENEWAL_DROP
  2366. if( drop_modifier != 100 ) {
  2367. drop_rate = drop_rate * drop_modifier / 100;
  2368. if( drop_rate < 1 )
  2369. drop_rate = 1;
  2370. }
  2371. #endif
  2372. if( sd && sd->status.mod_drop != 100 ) {
  2373. drop_rate = drop_rate * sd->status.mod_drop / 100;
  2374. if( drop_rate < 1 )
  2375. drop_rate = 1;
  2376. }
  2377.  
  2378. // attempt to drop the item
  2379. if (rnd() % 10000 >= drop_rate)
  2380. continue;
  2381.  
  2382. if( mvp_sd && it->type == IT_PETEGG ) {
  2383. pet->create_egg(mvp_sd, md->db->dropitem[i].nameid);
  2384. continue;
  2385. }
  2386.  
  2387. ditem = mob->setdropitem(md->db->dropitem[i].nameid, 1, it);
  2388.  
  2389. //A Rare Drop Global Announce by Lupus
  2390. if( mvp_sd && drop_rate <= battle_config.rare_drop_announce ) {
  2391. char message[128];
  2392. sprintf (message, msg_txt(541), mvp_sd->status.name, md->name, it->jname, (float)drop_rate/100);
  2393. //MSG: "'%s' won %s's %s (chance: %0.02f%%)"
  2394. intif->broadcast(message, strlen(message)+1, BC_DEFAULT);
  2395. }
  2396.  
  2397. /* heres the thing we got the feature set up however we're still discussing how to best define the ids,
  2398. * so while we discuss, for a small period of time, the list is hardcoded (yes officially only those 2 use it,
  2399. * thus why we're unsure on how to best place the setting) */
  2400. /* temp, will not be hardcoded for long thudu. */
  2401. if( it->nameid == 7782 || it->nameid == 7783 ) /* for when not hardcoded: add a check on mvp bonus drop as well */
  2402. clif->item_drop_announce(mvp_sd, it->nameid, md->name);
  2403.  
  2404. // Announce first, or else ditem will be freed. [Lance]
  2405. // By popular demand, use base drop rate for autoloot code. [Skotlex]
  2406. mob->item_drop(md, dlist, ditem, 0, md->db->dropitem[i].p, homkillonly);
  2407. }
  2408.  
  2409. // Ore Discovery [Celest]
  2410. if (sd == mvp_sd && pc->checkskill(sd,BS_FINDINGORE) > 0) {
  2411. if( (temp = itemdb->chain_item(itemdb->chain_cache[ECC_ORE],&i)) ) {
  2412. ditem = mob->setdropitem(temp, 1, NULL);
  2413. mob->item_drop(md, dlist, ditem, 0, i, homkillonly);
  2414. }
  2415. }
  2416.  
  2417. if(sd) {
  2418. // process script-granted extra drop bonuses
  2419. int itemid = 0;
  2420. for (i = 0; i < ARRAYLENGTH(sd->add_drop) && (sd->add_drop[i].id || sd->add_drop[i].group); i++)
  2421. {
  2422. if ( sd->add_drop[i].race == -md->class_ ||
  2423. ( sd->add_drop[i].race > 0 && (
  2424. sd->add_drop[i].race & (1<<mstatus->race) ||
  2425. sd->add_drop[i].race & (1<<(mstatus->mode&MD_BOSS?RC_BOSS:RC_NONBOSS))
  2426. )))
  2427. {
  2428. //check if the bonus item drop rate should be multiplied with mob level/10 [Lupus]
  2429. if(sd->add_drop[i].rate < 0) {
  2430. //it's negative, then it should be multiplied. e.g. for Mimic,Myst Case Cards, etc
  2431. // rate = base_rate * (mob_level/10) + 1
  2432. drop_rate = -sd->add_drop[i].rate*(md->level/10)+1;
  2433. drop_rate = cap_value(drop_rate, battle_config.item_drop_adddrop_min, battle_config.item_drop_adddrop_max);
  2434. if (drop_rate > 10000) drop_rate = 10000;
  2435. }
  2436. else
  2437. //it's positive, then it goes as it is
  2438. drop_rate = sd->add_drop[i].rate;
  2439.  
  2440. if (rnd()%10000 >= drop_rate)
  2441. continue;
  2442. itemid = (sd->add_drop[i].id > 0) ? sd->add_drop[i].id : itemdb->chain_item(sd->add_drop[i].group,&drop_rate);
  2443. if( itemid )
  2444. mob->item_drop(md, dlist, mob->setdropitem(itemid,1,NULL), 0, drop_rate, homkillonly);
  2445. }
  2446. }
  2447.  
  2448. // process script-granted zeny bonus (get_zeny_num) [Skotlex]
  2449. if( sd->bonus.get_zeny_num && rnd()%100 < sd->bonus.get_zeny_rate ) {
  2450. i = sd->bonus.get_zeny_num > 0 ? sd->bonus.get_zeny_num : -md->level * sd->bonus.get_zeny_num;
  2451. if (!i) i = 1;
  2452. pc->getzeny(sd, 1+rnd()%i, LOG_TYPE_PICKDROP_MONSTER, NULL);
  2453. }
  2454. }
  2455.  
  2456. // process items looted by the mob
  2457. if(md->lootitem) {
  2458. for(i = 0; i < md->lootitem_count; i++)
  2459. mob->item_drop(md, dlist, mob->setlootitem(&md->lootitem[i]), 1, 10000, homkillonly);
  2460. }
  2461. if (dlist->item) //There are drop items.
  2462. timer->add(tick + (!battle_config.delay_battle_damage?500:0), mob->delay_item_drop, 0, (intptr_t)dlist);
  2463. else //No drops
  2464. ers_free(item_drop_list_ers, dlist);
  2465. } else if (md->lootitem && md->lootitem_count) {
  2466. //Loot MUST drop!
  2467. struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
  2468. dlist->m = md->bl.m;
  2469. dlist->x = md->bl.x;
  2470. dlist->y = md->bl.y;
  2471. dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
  2472. dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
  2473. dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
  2474. dlist->item = NULL;
  2475. for(i = 0; i < md->lootitem_count; i++)
  2476. mob->item_drop(md, dlist, mob->setlootitem(&md->lootitem[i]), 1, 10000, homkillonly);
  2477. timer->add(tick + (!battle_config.delay_battle_damage?500:0), mob->delay_item_drop, 0, (intptr_t)dlist);
  2478. }
  2479.  
  2480. if(mvp_sd && md->db->mexp > 0 && !md->special_state.ai) {
  2481. int log_mvp[2] = {0};
  2482. unsigned int mexp;
  2483. double exp;
  2484.  
  2485. //mapflag: noexp check [Lorky]
  2486. if (map->list[m].flag.nobaseexp || type&2)
  2487. exp =1;
  2488. else {
  2489. exp = md->db->mexp;
  2490. if (count > 1)
  2491. exp += exp*(battle_config.exp_bonus_attacker*(count-1))/100.; //[Gengar]
  2492. }
  2493.  
  2494. mexp = (unsigned int)cap_value(exp, 1, UINT_MAX);
  2495.  
  2496. clif->mvp_effect(mvp_sd);
  2497. clif->mvp_exp(mvp_sd,mexp);
  2498. pc->gainexp(mvp_sd, &md->bl, mexp,0, false);
  2499. log_mvp[1] = mexp;
  2500.  
  2501. if( !(map->list[m].flag.nomvploot || type&1) ) {
  2502. /* pose them randomly in the list -- so on 100% drop servers it wont always drop the same item */
  2503. int mdrop_id[MAX_MVP_DROP];
  2504. int mdrop_p[MAX_MVP_DROP];
  2505. struct item item;
  2506.  
  2507. memset(&mdrop_id,0,MAX_MVP_DROP*sizeof(int));
  2508.  
  2509. for(i = 0; i < MAX_MVP_DROP; i++) {
  2510. while( 1 ) {
  2511. int va = rand()%MAX_MVP_DROP;
  2512. if( !mdrop_id[va] || !md->db->mvpitem[i].nameid ) {
  2513. mdrop_id[va] = md->db->mvpitem[i].nameid;
  2514. mdrop_p[va] = md->db->mvpitem[i].p;
  2515. break;
  2516. }
  2517. }
  2518. }
  2519.  
  2520. for(i = 0; i < MAX_MVP_DROP; i++) {
  2521. struct item_data *data;
  2522. if(mdrop_id[i] <= 0)
  2523. continue;
  2524. if(! (data = itemdb->exists(mdrop_id[i])) )
  2525. continue;
  2526.  
  2527. temp = mdrop_p[i];
  2528. if(temp <= 0 && !battle_config.drop_rate0item)
  2529. temp = 1;
  2530. if(temp <= rnd()%10000+1) //if ==0, then it doesn't drop
  2531. continue;
  2532.  
  2533. memset(&item,0,sizeof(item));
  2534. item.nameid=mdrop_id[i];
  2535. item.identify= itemdb->isidentified2(data);
  2536. clif->mvp_item(mvp_sd,item.nameid);
  2537. log_mvp[0] = item.nameid;
  2538.  
  2539. //A Rare MVP Drop Global Announce by Lupus
  2540. if(temp<=battle_config.rare_drop_announce) {
  2541. char message[128];
  2542. sprintf (message, msg_txt(541), mvp_sd->status.name, md->name, data->jname, temp/100.);
  2543. //MSG: "'%s' won %s's %s (chance: %0.02f%%)"
  2544. intif->broadcast(message, strlen(message)+1, BC_DEFAULT);
  2545. }
  2546.  
  2547. if((temp = pc->additem(mvp_sd,&item,1,LOG_TYPE_PICKDROP_PLAYER)) != 0) {
  2548. clif->additem(mvp_sd,0,0,temp);
  2549. map->addflooritem(&item,1,mvp_sd->bl.m,mvp_sd->bl.x,mvp_sd->bl.y,mvp_sd->status.char_id,(second_sd?second_sd->status.char_id:0),(third_sd?third_sd->status.char_id:0),1);
  2550. }
  2551.  
  2552. //Logs items, MVP prizes [Lupus]
  2553. logs->pick_mob(md, LOG_TYPE_MVP, -1, &item, data);
  2554. break;
  2555. }
  2556. }
  2557.  
  2558. logs->mvpdrop(mvp_sd, md->class_, log_mvp);
  2559. }
  2560.  
  2561. if (type&2 && !sd && md->class_ == MOBID_EMPERIUM && md->guardian_data) {
  2562. //Emperium destroyed by script. Discard mvp character. [Skotlex]
  2563. mvp_sd = NULL;
  2564. }
  2565.  
  2566. rebirth = ( md->sc.data[SC_KAIZEL] || (md->sc.data[SC_REBIRTH] && !md->state.rebirth) );
  2567. if( !rebirth ) { // Only trigger event on final kill
  2568. md->status.hp = 0; //So that npc_event invoked functions KNOW that mob is dead
  2569. if( src ) {
  2570. switch( src->type ) {
  2571. case BL_PET: sd = ((TBL_PET*)src)->msd; break;
  2572. case BL_HOM: sd = ((TBL_HOM*)src)->master; break;
  2573. case BL_MER: sd = ((TBL_MER*)src)->master; break;
  2574. case BL_ELEM: sd = ((TBL_ELEM*)src)->master; break;
  2575. }
  2576. }
  2577.  
  2578. if( sd ) {
  2579. if( sd->mission_mobid == md->class_) { //TK_MISSION [Skotlex]
  2580. if( ++sd->mission_count >= 100 && (temp = mob->get_random_id(0, 0xE, sd->status.base_level)) ) {
  2581. pc->addfame(sd, 1);
  2582. sd->mission_mobid = temp;
  2583. pc_setglobalreg(sd,script->add_str("TK_MISSION_ID"), temp);
  2584. sd->mission_count = 0;
  2585. clif->mission_info(sd, temp, 0);
  2586. }
  2587. pc_setglobalreg(sd,script->add_str("TK_MISSION_COUNT"), sd->mission_count);
  2588. }
  2589.  
  2590. if( sd->status.party_id )
  2591. map->foreachinrange(quest->update_objective_sub,&md->bl,AREA_SIZE,BL_PC,sd->status.party_id,md->class_);
  2592. else if( sd->avail_quests )
  2593. quest->update_objective(sd, md->class_);
  2594.  
  2595. if( sd->md && src && src->type != BL_HOM && mob->db(md->class_)->lv > sd->status.base_level/2 )
  2596. mercenary->kills(sd->md);
  2597. }
  2598.  
  2599. if( md->npc_event[0] && !md->state.npc_killmonster ) {
  2600. if( sd && battle_config.mob_npc_event_type ) {
  2601. pc->setparam(sd, SP_KILLERRID, sd->bl.id);
  2602. npc->event(sd,md->npc_event,0);
  2603. } else if( mvp_sd ) {
  2604. pc->setparam(mvp_sd, SP_KILLERRID, sd?sd->bl.id:0);
  2605. npc->event(mvp_sd,md->npc_event,0);
  2606. } else
  2607. npc->event_do(md->npc_event);
  2608. } else if( mvp_sd && !md->state.npc_killmonster ) {
  2609. pc->setparam(mvp_sd, SP_KILLEDRID, md->class_);
  2610. npc->script_event(mvp_sd, NPCE_KILLNPC); // PCKillNPC [Lance]
  2611. }
  2612.  
  2613. md->status.hp = 1;
  2614. }
  2615.  
  2616. if(md->deletetimer != INVALID_TIMER) {
  2617. timer->delete(md->deletetimer,mob->timer_delete);
  2618. md->deletetimer = INVALID_TIMER;
  2619. }
  2620. /**
  2621. * Only loops if necessary (e.g. a poring would never need to loop)
  2622. **/
  2623. if( md->can_summon )
  2624. mob->deleteslave(md);
  2625.  
  2626. map->freeblock_unlock();
  2627.  
  2628. if( !rebirth ) {
  2629.  
  2630. if( pcdb_checkid(md->vd->class_) ) {//Player mobs are not removed automatically by the client.
  2631. /* first we set them dead, then we delay the out sight effect */
  2632. clif->clearunit_area(&md->bl,CLR_DEAD);
  2633. clif->clearunit_delayed(&md->bl, CLR_OUTSIGHT,tick+3000);
  2634. } else
  2635. /**
  2636. * We give the client some time to breath and this allows it to display anything it'd like with the dead corpose
  2637. * For example, this delay allows it to display soul drain effect
  2638. **/
  2639. clif->clearunit_delayed(&md->bl, CLR_DEAD, tick+250);
  2640.  
  2641. }
  2642.  
  2643. if(!md->spawn) //Tell status->damage to remove it from memory.
  2644. return 5; // Note: Actually, it's 4. Oh well...
  2645.  
  2646. // MvP tomb [GreenBox]
  2647. if (battle_config.mvp_tomb_enabled && md->spawn->state.boss && map->list[md->bl.m].flag.notomb != 1)
  2648. mob->mvptomb_create(md, mvp_sd ? mvp_sd->status.name : NULL, time(NULL));
  2649.  
  2650. if( !rebirth ) {
  2651. status->change_clear(&md->bl,1);
  2652. mob->setdelayspawn(md); //Set respawning.
  2653. }
  2654. return 3; //Remove from map.
  2655. }
  2656.  
  2657. void mob_revive(struct mob_data *md, unsigned int hp)
  2658. {
  2659. int64 tick = timer->gettick();
  2660. md->state.skillstate = MSS_IDLE;
  2661. md->last_thinktime = tick;
  2662. md->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME;
  2663. md->last_linktime = tick;
  2664. md->last_pcneartime = 0;
  2665. memset(md->dmglog, 0, sizeof(md->dmglog)); // Reset the damage done on the rebirthed monster, otherwise will grant full exp + damage done. [Valaris]
  2666. md->tdmg = 0;
  2667. if (!md->bl.prev)
  2668. map->addblock(&md->bl);
  2669. clif->spawn(&md->bl);
  2670. skill->unit_move(&md->bl,tick,1);
  2671. mob->skill_use(md, tick, MSC_SPAWN);
  2672. if (battle_config.show_mob_info&3)
  2673. clif->charnameack (0, &md->bl);
  2674. }
  2675.  
  2676. int mob_guardian_guildchange(struct mob_data *md)
  2677. {
  2678. struct guild *g;
  2679. nullpo_ret(md);
  2680.  
  2681. if (!md->guardian_data)
  2682. return 0;
  2683.  
  2684. if (md->guardian_data->castle->guild_id == 0) {
  2685. //Castle with no owner? Delete the guardians.
  2686. if( md->class_ == MOBID_EMPERIUM ) //But don't delete the emperium, just clear it's guild-data
  2687. md->guardian_data->g = NULL;
  2688. else {
  2689. if (md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS && md->guardian_data->castle->guardian[md->guardian_data->number].visible)
  2690. guild->castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number, 0);
  2691. unit->free(&md->bl,CLR_OUTSIGHT); //Remove guardian.
  2692. }
  2693. return 0;
  2694. }
  2695.  
  2696. g = guild->search(md->guardian_data->castle->guild_id);
  2697. if (g == NULL) {
  2698. //Properly remove guardian info from Castle data.
  2699. ShowError("mob_guardian_guildchange: New Guild (id %d) does not exists!\n", md->guardian_data->castle->guild_id);
  2700. if (md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS)
  2701. guild->castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number, 0);
  2702. unit->free(&md->bl,CLR_OUTSIGHT);
  2703. return 0;
  2704. }
  2705.  
  2706. md->guardian_data->g = g;
  2707.  
  2708. return 1;
  2709. }
  2710.  
  2711. /*==========================================
  2712. * Pick a random class for the mob
  2713. *------------------------------------------*/
  2714. int mob_random_class (int *value, size_t count)
  2715. {
  2716. nullpo_ret(value);
  2717.  
  2718. // no count specified, look into the array manually, but take only max 5 elements
  2719. if (count < 1) {
  2720. count = 0;
  2721. while(count < 5 && mob->db_checkid(value[count])) count++;
  2722. if(count < 1) // nothing found
  2723. return 0;
  2724. } else {
  2725. // check if at least the first value is valid
  2726. if(mob->db_checkid(value[0]) == 0)
  2727. return 0;
  2728. }
  2729. //Pick a random value, hoping it exists. [Skotlex]
  2730. return mob->db_checkid(value[rnd()%count]);
  2731. }
  2732.  
  2733. /*==========================================
  2734. * Change mob base class
  2735. *------------------------------------------*/
  2736. int mob_class_change (struct mob_data *md, int class_)
  2737. {
  2738. int64 tick = timer->gettick(), c = 0;
  2739. int i, hp_rate;
  2740.  
  2741. nullpo_ret(md);
  2742.  
  2743. if( md->bl.prev == NULL )
  2744. return 0;
  2745.  
  2746. //Disable class changing for some targets...
  2747. if (md->guardian_data)
  2748. return 0; //Guardians/Emperium
  2749.  
  2750. if( mob_is_treasure(md) )
  2751. return 0; //Treasure Boxes
  2752.  
  2753. if( md->special_state.ai > 1 )
  2754. return 0; //Marine Spheres and Floras.
  2755.  
  2756. if( mob->is_clone(md->class_) )
  2757. return 0; //Clones
  2758.  
  2759. if( md->class_ == class_ )
  2760. return 0; //Nothing to change.
  2761.  
  2762. hp_rate = get_percentage(md->status.hp, md->status.max_hp);
  2763. md->class_ = class_;
  2764. md->db = mob->db(class_);
  2765. if (battle_config.override_mob_names==1)
  2766. memcpy(md->name,md->db->name,NAME_LENGTH);
  2767. else
  2768. memcpy(md->name,md->db->jname,NAME_LENGTH);
  2769.  
  2770. mob_stop_attack(md);
  2771. mob_stop_walking(md, 0);
  2772. unit->skillcastcancel(&md->bl, 0);
  2773. status->set_viewdata(&md->bl, class_);
  2774. clif->class_change(&md->bl, md->vd->class_, 1);
  2775. status_calc_mob(md, SCO_FIRST);
  2776. md->ud.state.speed_changed = 1; //Speed change update.
  2777.  
  2778. if (battle_config.monster_class_change_recover) {
  2779. memset(md->dmglog, 0, sizeof(md->dmglog));
  2780. md->tdmg = 0;
  2781. } else {
  2782. md->status.hp = md->status.max_hp*hp_rate/100;
  2783. if(md->status.hp < 1) md->status.hp = 1;
  2784. }
  2785.  
  2786. for(i=0,c=tick-MOB_MAX_DELAY;i<MAX_MOBSKILL;i++)
  2787. md->skilldelay[i] = c;
  2788.  
  2789. if(md->lootitem == NULL && md->db->status.mode&MD_LOOTER)
  2790. md->lootitem=(struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
  2791.  
  2792. //Targets should be cleared no morph
  2793. md->target_id = md->attacked_id = 0;
  2794.  
  2795. //Need to update name display.
  2796. clif->charnameack(0, &md->bl);
  2797. status_change_end(&md->bl,SC_KEEPING,INVALID_TIMER);
  2798. return 0;
  2799. }
  2800.  
  2801. /*==========================================
  2802. * mob heal, update display hp info of mob for players
  2803. *------------------------------------------*/
  2804. void mob_heal(struct mob_data *md,unsigned int heal)
  2805. {
  2806. if (battle_config.show_mob_info&3)
  2807. clif->charnameack (0, &md->bl);
  2808.  
  2809. #if PACKETVER >= 20120404
  2810. if( !(md->status.mode&MD_BOSS) ){
  2811. int i;
  2812. for(i = 0; i < DAMAGELOG_SIZE; i++){ // must show hp bar to all char who already hit the mob.
  2813. if( md->dmglog[i].id ) {
  2814. struct map_session_data *sd = map->charid2sd(md->dmglog[i].id);
  2815. if( sd && check_distance_bl(&md->bl, &sd->bl, AREA_SIZE) ) // check if in range
  2816. clif->monster_hp_bar(md,sd);
  2817. }
  2818. }
  2819. }
  2820. #endif
  2821. }
  2822.  
  2823. /*==========================================
  2824. * Added by RoVeRT
  2825. *------------------------------------------*/
  2826. int mob_warpslave_sub(struct block_list *bl,va_list ap) {
  2827. struct mob_data *md=(struct mob_data *)bl;
  2828. struct block_list *master;
  2829. short x,y,range=0;
  2830. master = va_arg(ap, struct block_list*);
  2831. range = va_arg(ap, int);
  2832.  
  2833. if(md->master_id!=master->id)
  2834. return 0;
  2835.  
  2836. map->search_freecell(master, 0, &x, &y, range, range, 0);
  2837. unit->warp(&md->bl, master->m, x, y,CLR_TELEPORT);
  2838. return 1;
  2839. }
  2840.  
  2841. /*==========================================
  2842. * Added by RoVeRT
  2843. * Warps slaves. Range is the area around the master that they can
  2844. * appear in randomly.
  2845. *------------------------------------------*/
  2846. int mob_warpslave(struct block_list *bl, int range) {
  2847. if (range < 1)
  2848. range = 1; //Min range needed to avoid crashes and stuff. [Skotlex]
  2849.  
  2850. return map->foreachinmap(mob->warpslave_sub, bl->m, BL_MOB, bl, range);
  2851. }
  2852.  
  2853. /*==========================================
  2854. * Counts slave sub, currently checking if mob master is the given ID.
  2855. *------------------------------------------*/
  2856. int mob_countslave_sub(struct block_list *bl,va_list ap)
  2857. {
  2858. int id;
  2859. struct mob_data *md;
  2860. id=va_arg(ap,int);
  2861.  
  2862. md = (struct mob_data *)bl;
  2863. if( md->master_id==id )
  2864. return 1;
  2865. return 0;
  2866. }
  2867.  
  2868. /*==========================================
  2869. * Counts the number of slaves a mob has on the map.
  2870. *------------------------------------------*/
  2871. int mob_countslave(struct block_list *bl) {
  2872. return map->foreachinmap(mob->countslave_sub, bl->m, BL_MOB,bl->id);
  2873. }
  2874.  
  2875. /*==========================================
  2876. * Summons amount slaves contained in the value[5] array using round-robin. [adapted by Skotlex]
  2877. *------------------------------------------*/
  2878. int mob_summonslave(struct mob_data *md2,int *value,int amount,uint16 skill_id)
  2879. {
  2880. struct mob_data *md;
  2881. struct spawn_data data;
  2882. int count = 0,k=0,hp_rate=0;
  2883.  
  2884. nullpo_ret(md2);
  2885. nullpo_ret(value);
  2886.  
  2887. memset(&data, 0, sizeof(struct spawn_data));
  2888. data.m = md2->bl.m;
  2889. data.x = md2->bl.x;
  2890. data.y = md2->bl.y;
  2891. data.num = 1;
  2892. data.state.size = md2->special_state.size;
  2893. data.state.ai = md2->special_state.ai;
  2894.  
  2895. if(mob->db_checkid(value[0]) == 0)
  2896. return 0;
  2897. /**
  2898. * Flags this monster is able to summon; saves a worth amount of memory upon deletion
  2899. **/
  2900. md2->can_summon = 1;
  2901.  
  2902. while(count < 5 && mob->db_checkid(value[count])) count++;
  2903. if(count < 1) return 0;
  2904. if (amount > 0 && amount < count) { //Do not start on 0, pick some random sub subset [Skotlex]
  2905. k = rnd()%count;
  2906. amount+=k; //Increase final value by same amount to preserve total number to summon.
  2907. }
  2908.  
  2909. if (!battle_config.monster_class_change_recover &&
  2910. (skill_id == NPC_TRANSFORMATION || skill_id == NPC_METAMORPHOSIS))
  2911. hp_rate = get_percentage(md2->status.hp, md2->status.max_hp);
  2912.  
  2913. for(;k<amount;k++) {
  2914. short x,y;
  2915. data.class_ = value[k%count]; //Summon slaves in round-robin fashion. [Skotlex]
  2916. if (mob->db_checkid(data.class_) == 0)
  2917. continue;
  2918.  
  2919. if (map->search_freecell(&md2->bl, 0, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 0)) {
  2920. data.x = x;
  2921. data.y = y;
  2922. } else {
  2923. data.x = md2->bl.x;
  2924. data.y = md2->bl.y;
  2925. }
  2926.  
  2927. //These two need to be loaded from the db for each slave.
  2928. if(battle_config.override_mob_names==1)
  2929. strcpy(data.name,"--en--");
  2930. else
  2931. strcpy(data.name,"--ja--");
  2932.  
  2933. if (!mob->parse_dataset(&data))
  2934. continue;
  2935.  
  2936. md= mob->spawn_dataset(&data);
  2937. if(skill_id == NPC_SUMMONSLAVE){
  2938. md->master_id=md2->bl.id;
  2939. md->special_state.ai = md2->special_state.ai;
  2940. }
  2941. mob->spawn(md);
  2942.  
  2943. if (hp_rate) //Scale HP
  2944. md->status.hp = md->status.max_hp*hp_rate/100;
  2945.  
  2946. //Inherit the aggressive mode of the master.
  2947. if (battle_config.slaves_inherit_mode && md->master_id) {
  2948. switch (battle_config.slaves_inherit_mode) {
  2949. case 1: //Always aggressive
  2950. if (!(md->status.mode&MD_AGGRESSIVE))
  2951. sc_start4(NULL, &md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
  2952. break;
  2953. case 2: //Always passive
  2954. if (md->status.mode&MD_AGGRESSIVE)
  2955. sc_start4(NULL, &md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
  2956. break;
  2957. default: //Copy master.
  2958. if (md2->status.mode&MD_AGGRESSIVE)
  2959. sc_start4(NULL, &md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
  2960. else
  2961. sc_start4(NULL, &md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
  2962. break;
  2963. }
  2964. }
  2965.  
  2966. clif->skill_nodamage(&md->bl,&md->bl,skill_id,amount,1);
  2967. }
  2968.  
  2969. return 0;
  2970. }
  2971.  
  2972. /*==========================================
  2973. * MOBskill lookup (get skillindex through skill_id)
  2974. * Returns -1 if not found.
  2975. *------------------------------------------*/
  2976. int mob_skill_id2skill_idx(int class_,uint16 skill_id)
  2977. {
  2978. int i, max = mob->db(class_)->maxskill;
  2979. struct mob_skill *ms=mob->db(class_)->skill;
  2980.  
  2981. if(ms==NULL)
  2982. return -1;
  2983.  
  2984. ARR_FIND( 0, max, i, ms[i].skill_id == skill_id );
  2985. return ( i < max ) ? i : -1;
  2986. }
  2987.  
  2988. /*==========================================
  2989. * Friendly Mob whose HP is decreasing by a nearby MOB is looked for.
  2990. *------------------------------------------*/
  2991. int mob_getfriendhprate_sub(struct block_list *bl,va_list ap)
  2992. {
  2993. int min_rate, max_rate,rate;
  2994. struct block_list **fr;
  2995. struct mob_data *md;
  2996.  
  2997. md = va_arg(ap,struct mob_data *);
  2998. min_rate=va_arg(ap,int);
  2999. max_rate=va_arg(ap,int);
  3000. fr=va_arg(ap,struct block_list **);
  3001.  
  3002. if( md->bl.id == bl->id && !(battle_config.mob_ai&0x10))
  3003. return 0;
  3004.  
  3005. if ((*fr) != NULL) //A friend was already found.
  3006. return 0;
  3007.  
  3008. if (battle->check_target(&md->bl,bl,BCT_ENEMY)>0)
  3009. return 0;
  3010.  
  3011. rate = get_percentage(status_get_hp(bl), status_get_max_hp(bl));
  3012.  
  3013. if (rate >= min_rate && rate <= max_rate)
  3014. (*fr) = bl;
  3015. return 1;
  3016. }
  3017. struct block_list *mob_getfriendhprate(struct mob_data *md,int min_rate,int max_rate) {
  3018. struct block_list *fr=NULL;
  3019. int type = BL_MOB;
  3020.  
  3021. nullpo_retr(NULL, md);
  3022.  
  3023. if (md->special_state.ai) //Summoned creatures. [Skotlex]
  3024. type = BL_PC;
  3025.  
  3026. map->foreachinrange(mob->getfriendhprate_sub, &md->bl, 8, type,md,min_rate,max_rate,&fr);
  3027. return fr;
  3028. }
  3029. /*==========================================
  3030. * Check hp rate of its master
  3031. *------------------------------------------*/
  3032. struct block_list *mob_getmasterhpltmaxrate(struct mob_data *md,int rate) {
  3033. if( md && md->master_id > 0 ) {
  3034. struct block_list *bl = map->id2bl(md->master_id);
  3035. if( bl && get_percentage(status_get_hp(bl), status_get_max_hp(bl)) < rate )
  3036. return bl;
  3037. }
  3038.  
  3039. return NULL;
  3040. }
  3041. /*==========================================
  3042. * What a status state suits by nearby MOB is looked for.
  3043. *------------------------------------------*/
  3044. int mob_getfriendstatus_sub(struct block_list *bl,va_list ap)
  3045. {
  3046. int cond1,cond2;
  3047. struct mob_data **fr, *md, *mmd;
  3048. int flag=0;
  3049.  
  3050. nullpo_ret(bl);
  3051. nullpo_ret(md=(struct mob_data *)bl);
  3052. nullpo_ret(mmd=va_arg(ap,struct mob_data *));
  3053.  
  3054. if( mmd->bl.id == bl->id && !(battle_config.mob_ai&0x10) )
  3055. return 0;
  3056.  
  3057. if (battle->check_target(&mmd->bl,bl,BCT_ENEMY)>0)
  3058. return 0;
  3059. cond1=va_arg(ap,int);
  3060. cond2=va_arg(ap,int);
  3061. fr=va_arg(ap,struct mob_data **);
  3062. if( cond2==-1 ){
  3063. int j;
  3064. for(j=SC_COMMON_MIN;j<=SC_COMMON_MAX && !flag;j++){
  3065. if ((flag=(md->sc.data[j] != NULL))) //Once an effect was found, break out. [Skotlex]
  3066. break;
  3067. }
  3068. }else
  3069. flag=( md->sc.data[cond2] != NULL );
  3070. if( flag^( cond1==MSC_FRIENDSTATUSOFF ) )
  3071. (*fr)=md;
  3072.  
  3073. return 0;
  3074. }
  3075.  
  3076. struct mob_data *mob_getfriendstatus(struct mob_data *md,int cond1,int cond2) {
  3077. struct mob_data* fr = NULL;
  3078. nullpo_ret(md);
  3079.  
  3080. map->foreachinrange(mob->getfriendstatus_sub, &md->bl, 8,BL_MOB, md,cond1,cond2,&fr);
  3081. return fr;
  3082. }
  3083.  
  3084. /*==========================================
  3085. * Skill use judging
  3086. *------------------------------------------*/
  3087. int mobskill_use(struct mob_data *md, int64 tick, int event) {
  3088. struct mob_skill *ms;
  3089. struct block_list *fbl = NULL; //Friend bl, which can either be a BL_PC or BL_MOB depending on the situation. [Skotlex]
  3090. struct block_list *bl;
  3091. struct mob_data *fmd = NULL;
  3092. int i,j,n;
  3093.  
  3094. nullpo_ret(md);
  3095. nullpo_ret(ms = md->db->skill);
  3096.  
  3097. if (!battle_config.mob_skill_rate || md->ud.skilltimer != INVALID_TIMER || !md->db->maxskill)
  3098. return 0;
  3099.  
  3100. if (event == -1 && DIFF_TICK(md->ud.canact_tick, tick) > 0)
  3101. return 0; //Skill act delay only affects non-event skills.
  3102.  
  3103. //Pick a starting position and loop from that.
  3104. i = battle_config.mob_ai&0x100?rnd()%md->db->maxskill:0;
  3105. for (n = 0; n < md->db->maxskill; i++, n++) {
  3106. int c2, flag = 0;
  3107.  
  3108. if (i == md->db->maxskill)
  3109. i = 0;
  3110.  
  3111. if (DIFF_TICK(tick, md->skilldelay[i]) < ms[i].delay)
  3112. continue;
  3113.  
  3114. c2 = ms[i].cond2;
  3115.  
  3116. if (ms[i].state != md->state.skillstate) {
  3117. if (md->state.skillstate != MSS_DEAD && (ms[i].state == MSS_ANY ||
  3118. (ms[i].state == MSS_ANYTARGET && md->target_id && md->state.skillstate != MSS_LOOT)
  3119. )) //ANYTARGET works with any state as long as there's a target. [Skotlex]
  3120. ;
  3121. else
  3122. continue;
  3123. }
  3124. if (rnd() % 10000 > ms[i].permillage) //Lupus (max value = 10000)
  3125. continue;
  3126.  
  3127. if (ms[i].cond1 == event)
  3128. flag = 1; //Trigger skill.
  3129. else if (ms[i].cond1 == MSC_SKILLUSED)
  3130. flag = ((event & 0xffff) == MSC_SKILLUSED && ((event >> 16) == c2 || c2 == 0));
  3131. else if(event == -1){
  3132. //Avoid entering on defined events to avoid "hyper-active skill use" due to the overflow of calls to this function in battle.
  3133. switch (ms[i].cond1)
  3134. {
  3135. case MSC_ALWAYS:
  3136. flag = 1; break;
  3137. case MSC_MYHPLTMAXRATE: // HP< maxhp%
  3138. flag = get_percentage(md->status.hp, md->status.max_hp);
  3139. flag = (flag <= c2);
  3140. break;
  3141. case MSC_MYHPINRATE:
  3142. flag = get_percentage(md->status.hp, md->status.max_hp);
  3143. flag = (flag >= c2 && flag <= ms[i].val[0]);
  3144. break;
  3145. case MSC_MYSTATUSON: // status[num] on
  3146. case MSC_MYSTATUSOFF: // status[num] off
  3147. if (!md->sc.count) {
  3148. flag = 0;
  3149. } else if (ms[i].cond2 == -1) {
  3150. for (j = SC_COMMON_MIN; j <= SC_COMMON_MAX; j++)
  3151. if ((flag = (md->sc.data[j]!=NULL)) != 0)
  3152. break;
  3153. } else {
  3154. flag = (md->sc.data[ms[i].cond2]!=NULL);
  3155. }
  3156. flag ^= (ms[i].cond1 == MSC_MYSTATUSOFF); break;
  3157. case MSC_FRIENDHPLTMAXRATE: // friend HP < maxhp%
  3158. flag = ((fbl = mob->getfriendhprate(md, 0, ms[i].cond2)) != NULL); break;
  3159. case MSC_FRIENDHPINRATE:
  3160. flag = ((fbl = mob->getfriendhprate(md, ms[i].cond2, ms[i].val[0])) != NULL); break;
  3161. case MSC_FRIENDSTATUSON: // friend status[num] on
  3162. case MSC_FRIENDSTATUSOFF: // friend status[num] off
  3163. flag = ((fmd = mob->getfriendstatus(md, ms[i].cond1, ms[i].cond2)) != NULL); break;
  3164. case MSC_SLAVELT: // slave < num
  3165. flag = (mob->countslave(&md->bl) < c2 ); break;
  3166. case MSC_ATTACKPCGT: // attack pc > num
  3167. flag = (unit->counttargeted(&md->bl) > c2); break;
  3168. case MSC_SLAVELE: // slave <= num
  3169. flag = (mob->countslave(&md->bl) <= c2 ); break;
  3170. case MSC_ATTACKPCGE: // attack pc >= num
  3171. flag = (unit->counttargeted(&md->bl) >= c2); break;
  3172. case MSC_AFTERSKILL:
  3173. flag = (md->ud.skill_id == c2); break;
  3174. case MSC_RUDEATTACKED:
  3175. flag = (md->state.attacked_count >= RUDE_ATTACKED_COUNT);
  3176. if (flag) md->state.attacked_count = 0; //Rude attacked count should be reset after the skill condition is met. Thanks to Komurka [Skotlex]
  3177. break;
  3178. case MSC_MASTERHPLTMAXRATE:
  3179. flag = ((fbl = mob->getmasterhpltmaxrate(md, ms[i].cond2)) != NULL); break;
  3180. case MSC_MASTERATTACKED:
  3181. flag = (md->master_id > 0 && (fbl=map->id2bl(md->master_id)) && unit->counttargeted(fbl) > 0); break;
  3182. case MSC_ALCHEMIST:
  3183. flag = (md->state.alchemist);
  3184. break;
  3185. }
  3186. }
  3187.  
  3188. if (!flag)
  3189. continue; //Skill requisite failed to be fulfilled.
  3190.  
  3191. //Execute skill
  3192. if (skill->get_casttype(ms[i].skill_id) == CAST_GROUND) {//Ground skill.
  3193. short x, y;
  3194. switch (ms[i].target) {
  3195. case MST_RANDOM: //Pick a random enemy within skill range.
  3196. bl = battle->get_enemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
  3197. skill->get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
  3198. break;
  3199. case MST_TARGET:
  3200. case MST_AROUND5:
  3201. case MST_AROUND6:
  3202. case MST_AROUND7:
  3203. case MST_AROUND8:
  3204. bl = map->id2bl(md->target_id);
  3205. break;
  3206. case MST_MASTER:
  3207. bl = &md->bl;
  3208. if (md->master_id)
  3209. bl = map->id2bl(md->master_id);
  3210. if (bl) //Otherwise, fall through.
  3211. break;
  3212. case MST_FRIEND:
  3213. bl = fbl?fbl:(fmd?&fmd->bl:&md->bl);
  3214. break;
  3215. default:
  3216. bl = &md->bl;
  3217. break;
  3218. }
  3219. if (!bl) continue;
  3220.  
  3221. x = bl->x;
  3222. y = bl->y;
  3223. // Look for an area to cast the spell around...
  3224. if (ms[i].target >= MST_AROUND1 || ms[i].target >= MST_AROUND5) {
  3225. j = ms[i].target >= MST_AROUND1?
  3226. (ms[i].target-MST_AROUND1) +1:
  3227. (ms[i].target-MST_AROUND5) +1;
  3228. map->search_freecell(&md->bl, md->bl.m, &x, &y, j, j, 3);
  3229. }
  3230. md->skill_idx = i;
  3231. map->freeblock_lock();
  3232. if( !battle->check_range(&md->bl,bl,skill->get_range2(&md->bl, ms[i].skill_id,ms[i].skill_lv))
  3233. || !unit->skilluse_pos2(&md->bl, x, y,ms[i].skill_id, ms[i].skill_lv,ms[i].casttime, ms[i].cancel)
  3234. ) {
  3235. map->freeblock_unlock();
  3236. continue;
  3237. }
  3238. } else {
  3239. //Targeted skill
  3240. switch (ms[i].target) {
  3241. case MST_RANDOM: //Pick a random enemy within skill range.
  3242. bl = battle->get_enemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
  3243. skill->get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
  3244. break;
  3245. case MST_TARGET:
  3246. bl = map->id2bl(md->target_id);
  3247. break;
  3248. case MST_MASTER:
  3249. bl = &md->bl;
  3250. if (md->master_id)
  3251. bl = map->id2bl(md->master_id);
  3252. if (bl) //Otherwise, fall through.
  3253. break;
  3254. case MST_FRIEND:
  3255. if (fbl) {
  3256. bl = fbl;
  3257. break;
  3258. } else if (fmd) {
  3259. bl = &fmd->bl;
  3260. break;
  3261. } // else fall through
  3262. default:
  3263. bl = &md->bl;
  3264. break;
  3265. }
  3266. if (!bl) continue;
  3267.  
  3268. md->skill_idx = i;
  3269. map->freeblock_lock();
  3270. if( !battle->check_range(&md->bl,bl,skill->get_range2(&md->bl, ms[i].skill_id,ms[i].skill_lv))
  3271. || !unit->skilluse_id2(&md->bl, bl->id,ms[i].skill_id, ms[i].skill_lv,ms[i].casttime, ms[i].cancel)
  3272. ) {
  3273. map->freeblock_unlock();
  3274. continue;
  3275. }
  3276. }
  3277. //Skill used. Post-setups...
  3278. if ( ms[ i ].msg_id ){ //Display color message [SnakeDrak]
  3279. struct mob_chat *mc = mob->chat(ms[i].msg_id);
  3280. char temp[CHAT_SIZE_MAX];
  3281. char name[NAME_LENGTH];
  3282. snprintf(name, sizeof name,"%s", md->name);
  3283. strtok(name, "#"); // discard extra name identifier if present [Daegaladh]
  3284. snprintf(temp, sizeof temp,"%s : %s", name, mc->msg);
  3285. clif->messagecolor(&md->bl, mc->color, temp);
  3286. }
  3287. if(!(battle_config.mob_ai&0x200)) { //pass on delay to same skill.
  3288. for (j = 0; j < md->db->maxskill; j++)
  3289. if (md->db->skill[j].skill_id == ms[i].skill_id)
  3290. md->skilldelay[j]=tick;
  3291. } else
  3292. md->skilldelay[i]=tick;
  3293. map->freeblock_unlock();
  3294. return 1;
  3295. }
  3296. //No skill was used.
  3297. md->skill_idx = -1;
  3298. return 0;
  3299. }
  3300. /*==========================================
  3301. * Skill use event processing
  3302. *------------------------------------------*/
  3303. int mobskill_event(struct mob_data *md, struct block_list *src, int64 tick, int flag) {
  3304. int target_id, res = 0;
  3305.  
  3306. if(md->bl.prev == NULL || md->status.hp <= 0)
  3307. return 0;
  3308.  
  3309. target_id = md->target_id;
  3310. if (!target_id || battle_config.mob_changetarget_byskill)
  3311. md->target_id = src->id;
  3312.  
  3313. if (flag == -1)
  3314. res = mob->skill_use(md, tick, MSC_CASTTARGETED);
  3315. else if ((flag&0xffff) == MSC_SKILLUSED)
  3316. res = mob->skill_use(md, tick, flag);
  3317. else if (flag&BF_SHORT)
  3318. res = mob->skill_use(md, tick, MSC_CLOSEDATTACKED);
  3319. else if (flag&BF_LONG && !(flag&BF_MAGIC)) //Long-attacked should not include magic.
  3320. res = mob->skill_use(md, tick, MSC_LONGRANGEATTACKED);
  3321.  
  3322. if (!res)
  3323. //Restore previous target only if skill condition failed to trigger. [Skotlex]
  3324. md->target_id = target_id;
  3325. //Otherwise check if the target is an enemy, and unlock if needed.
  3326. else if (battle->check_target(&md->bl, src, BCT_ENEMY) <= 0)
  3327. md->target_id = target_id;
  3328.  
  3329. return res;
  3330. }
  3331.  
  3332. // Player cloned mobs. [Valaris]
  3333. int mob_is_clone(int class_)
  3334. {
  3335. if(class_ < MOB_CLONE_START || class_ > MOB_CLONE_END)
  3336. return 0;
  3337. if (mob->db(class_) == mob->dummy)
  3338. return 0;
  3339. return class_;
  3340. }
  3341.  
  3342. //Flag values:
  3343. //&1: Set special AI (fight mobs, not players)
  3344. //If mode is not passed, a default aggressive mode is used.
  3345. //If master_id is passed, clone is attached to him.
  3346. //Returns: ID of newly crafted copy.
  3347. int mob_clone_spawn(struct map_session_data *sd, int16 m, int16 x, int16 y, const char *event, int master_id, int mode, int flag, unsigned int duration) {
  3348. int class_;
  3349. int i,j,h,inf,skill_id, fd;
  3350. struct mob_data *md;
  3351. struct mob_skill *ms;
  3352. struct mob_db* db;
  3353. struct status_data *mstatus;
  3354.  
  3355. nullpo_ret(sd);
  3356.  
  3357. if(pc_isdead(sd) && master_id && flag&1)
  3358. return 0;
  3359.  
  3360. ARR_FIND( MOB_CLONE_START, MOB_CLONE_END, class_, mob->db_data[class_] == NULL );
  3361. if(class_ >= MOB_CLONE_END)
  3362. return 0;
  3363.  
  3364. db = mob->db_data[class_]=(struct mob_db*)aCalloc(1, sizeof(struct mob_db));
  3365. mstatus = &db->status;
  3366. strcpy(db->sprite,sd->status.name);
  3367. strcpy(db->name,sd->status.name);
  3368. strcpy(db->jname,sd->status.name);
  3369. db->lv=status->get_lv(&sd->bl);
  3370. memcpy(mstatus, &sd->base_status, sizeof(struct status_data));
  3371. mstatus->rhw.atk2= mstatus->dex + mstatus->rhw.atk + mstatus->rhw.atk2; //Max ATK
  3372. mstatus->rhw.atk = mstatus->dex; //Min ATK
  3373. if (mstatus->lhw.atk) {
  3374. mstatus->lhw.atk2= mstatus->dex + mstatus->lhw.atk + mstatus->lhw.atk2; //Max ATK
  3375. mstatus->lhw.atk = mstatus->dex; //Min ATK
  3376. }
  3377. if (mode) //User provided mode.
  3378. mstatus->mode = mode;
  3379. else if (flag&1) //Friendly Character, remove looting.
  3380. mstatus->mode &= ~MD_LOOTER;
  3381. mstatus->hp = mstatus->max_hp;
  3382. mstatus->sp = mstatus->max_sp;
  3383. memcpy(&db->vd, &sd->vd, sizeof(struct view_data));
  3384. db->base_exp=1;
  3385. db->job_exp=1;
  3386. db->range2=AREA_SIZE; //Let them have the same view-range as players.
  3387. db->range3=AREA_SIZE; //Min chase of a screen.
  3388. db->option=sd->sc.option;
  3389.  
  3390. //Skill copy [Skotlex]
  3391. ms = &db->skill[0];
  3392.  
  3393. /**
  3394. * We temporarily disable sd's fd so it doesn't receive the messages from skill_check_condition_castbegin
  3395. **/
  3396. fd = sd->fd;
  3397. sd->fd = 0;
  3398.  
  3399. //Go Backwards to give better priority to advanced skills.
  3400. for (i=0,j = MAX_SKILL_TREE-1;j>=0 && i< MAX_MOBSKILL ;j--) {
  3401. int idx = pc->skill_tree[pc->class2idx(sd->status.class_)][j].idx;
  3402. skill_id = pc->skill_tree[pc->class2idx(sd->status.class_)][j].id;
  3403. if (!skill_id || sd->status.skill[idx].lv < 1 ||
  3404. (skill->db[idx].inf2&(INF2_WEDDING_SKILL|INF2_GUILD_SKILL))
  3405. )
  3406. continue;
  3407. for(h = 0; h < map->list[sd->bl.m].zone->disabled_skills_count; h++) {
  3408. if( skill_id == map->list[sd->bl.m].zone->disabled_skills[h]->nameid && map->list[sd->bl.m].zone->disabled_skills[h]->subtype == MZS_CLONE ) {
  3409. break;
  3410. }
  3411. }
  3412. if( h < map->list[sd->bl.m].zone->disabled_skills_count )
  3413. continue;
  3414. //Normal aggressive mob, disable skills that cannot help them fight
  3415. //against players (those with flags UF_NOMOB and UF_NOPC are specific
  3416. //to always aid players!) [Skotlex]
  3417. if (!(flag&1) &&
  3418. skill->get_unit_id(skill_id, 0) &&
  3419. skill->get_unit_flag(skill_id)&(UF_NOMOB|UF_NOPC))
  3420. continue;
  3421. /**
  3422. * The clone should be able to cast the skill (e.g. have the required weapon) bugreport:5299)
  3423. **/
  3424. if( !skill->check_condition_castbegin(sd,skill_id,sd->status.skill[idx].lv) )
  3425. continue;
  3426.  
  3427. memset (&ms[i], 0, sizeof(struct mob_skill));
  3428. ms[i].skill_id = skill_id;
  3429. ms[i].skill_lv = sd->status.skill[idx].lv;
  3430. ms[i].state = MSS_ANY;
  3431. ms[i].permillage = 500*battle_config.mob_skill_rate/100; //Default chance of all skills: 5%
  3432. ms[i].emotion = -1;
  3433. ms[i].cancel = 0;
  3434. ms[i].casttime = skill->cast_fix(&sd->bl,skill_id, ms[i].skill_lv);
  3435. ms[i].delay = 5000+skill->delay_fix(&sd->bl,skill_id, ms[i].skill_lv);
  3436.  
  3437. inf = skill->db[idx].inf;
  3438. if (inf&INF_ATTACK_SKILL) {
  3439. ms[i].target = MST_TARGET;
  3440. ms[i].cond1 = MSC_ALWAYS;
  3441. if (skill->get_range(skill_id, ms[i].skill_lv) > 3)
  3442. ms[i].state = MSS_ANYTARGET;
  3443. else
  3444. ms[i].state = MSS_BERSERK;
  3445. } else if(inf&INF_GROUND_SKILL) {
  3446. if (skill->get_inf2(skill_id)&INF2_TRAP) { //Traps!
  3447. ms[i].state = MSS_IDLE;
  3448. ms[i].target = MST_AROUND2;
  3449. ms[i].delay = 60000;
  3450. } else if (skill->get_unit_target(skill_id) == BCT_ENEMY) { //Target Enemy
  3451. ms[i].state = MSS_ANYTARGET;
  3452. ms[i].target = MST_TARGET;
  3453. ms[i].cond1 = MSC_ALWAYS;
  3454. } else { //Target allies
  3455. ms[i].target = MST_FRIEND;
  3456. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  3457. ms[i].cond2 = 95;
  3458. }
  3459. } else if (inf&INF_SELF_SKILL) {
  3460. if (skill->get_inf2(skill_id)&INF2_NO_TARGET_SELF) { //auto-select target skill.
  3461. ms[i].target = MST_TARGET;
  3462. ms[i].cond1 = MSC_ALWAYS;
  3463. if (skill->get_range(skill_id, ms[i].skill_lv) > 3) {
  3464. ms[i].state = MSS_ANYTARGET;
  3465. } else {
  3466. ms[i].state = MSS_BERSERK;
  3467. }
  3468. } else { //Self skill
  3469. ms[i].target = MST_SELF;
  3470. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  3471. ms[i].cond2 = 90;
  3472. ms[i].permillage = 2000;
  3473. //Delay: Remove the stock 5 secs and add half of the support time.
  3474. ms[i].delay += -5000 +(skill->get_time(skill_id, ms[i].skill_lv) + skill->get_time2(skill_id, ms[i].skill_lv))/2;
  3475. if (ms[i].delay < 5000)
  3476. ms[i].delay = 5000; //With a minimum of 5 secs.
  3477. }
  3478. } else if (inf&INF_SUPPORT_SKILL) {
  3479. ms[i].target = MST_FRIEND;
  3480. ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
  3481. ms[i].cond2 = 90;
  3482. if (skill_id == AL_HEAL)
  3483. ms[i].permillage = 5000; //Higher skill rate usage for heal.
  3484. else if (skill_id == ALL_RESURRECTION)
  3485. ms[i].cond2 = 1;
  3486. //Delay: Remove the stock 5 secs and add half of the support time.
  3487. ms[i].delay += -5000 +(skill->get_time(skill_id, ms[i].skill_lv) + skill->get_time2(skill_id, ms[i].skill_lv))/2;
  3488. if (ms[i].delay < 2000)
  3489. ms[i].delay = 2000; //With a minimum of 2 secs.
  3490.  
  3491. if (i+1 < MAX_MOBSKILL) { //duplicate this so it also triggers on self.
  3492. memcpy(&ms[i+1], &ms[i], sizeof(struct mob_skill));
  3493. db->maxskill = ++i;
  3494. ms[i].target = MST_SELF;
  3495. ms[i].cond1 = MSC_MYHPLTMAXRATE;
  3496. }
  3497. } else {
  3498. switch (skill_id) { //Certain Special skills that are passive, and thus, never triggered.
  3499. case MO_TRIPLEATTACK:
  3500. case TF_DOUBLE:
  3501. case GS_CHAINACTION:
  3502. ms[i].state = MSS_BERSERK;
  3503. ms[i].target = MST_TARGET;
  3504. ms[i].cond1 = MSC_ALWAYS;
  3505. ms[i].permillage = skill_id==MO_TRIPLEATTACK?(3000-ms[i].skill_lv*100):(ms[i].skill_lv*500);
  3506. ms[i].delay -= 5000; //Remove the added delay as these could trigger on "all hits".
  3507. break;
  3508. default: //Untreated Skill
  3509. continue;
  3510. }
  3511. }
  3512. if (battle_config.mob_skill_rate!= 100)
  3513. ms[i].permillage = ms[i].permillage*battle_config.mob_skill_rate/100;
  3514. if (battle_config.mob_skill_delay != 100)
  3515. ms[i].delay = ms[i].delay*battle_config.mob_skill_delay/100;
  3516.  
  3517. db->maxskill = ++i;
  3518. }
  3519.  
  3520. /**
  3521. * We grant the session it's fd value back.
  3522. **/
  3523. sd->fd = fd;
  3524.  
  3525. //Finally, spawn it.
  3526. md = mob->once_spawn_sub(&sd->bl, m, x, y, "--en--", class_, event, SZ_SMALL, AI_NONE);
  3527. if (!md) return 0; //Failed?
  3528.  
  3529. md->special_state.clone = 1;
  3530.  
  3531. if (master_id || flag || duration) { //Further manipulate crafted char.
  3532. if (flag&1) //Friendly Character
  3533. md->special_state.ai = AI_ATTACK;
  3534. if (master_id) //Attach to Master
  3535. md->master_id = master_id;
  3536. if (duration) //Auto Delete after a while.
  3537. {
  3538. if( md->deletetimer != INVALID_TIMER )
  3539. timer->delete(md->deletetimer, mob->timer_delete);
  3540. md->deletetimer = timer->add(timer->gettick() + duration, mob->timer_delete, md->bl.id, 0);
  3541. }
  3542. }
  3543.  
  3544. mob->spawn(md);
  3545.  
  3546. return md->bl.id;
  3547. }
  3548.  
  3549. int mob_clone_delete(struct mob_data *md)
  3550. {
  3551. const int class_ = md->class_;
  3552. if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END
  3553. && mob->db_data[class_]!=NULL) {
  3554. mob->destroy_mob_db(class_);
  3555. //Clear references to the db
  3556. md->db = mob->dummy;
  3557. md->vd = NULL;
  3558. return 1;
  3559. }
  3560. return 0;
  3561. }
  3562.  
  3563. //
  3564. // Initialization
  3565. //
  3566. /*==========================================
  3567. * Since un-setting [ mob ] up was used, it is an initial provisional value setup.
  3568. *------------------------------------------*/
  3569. int mob_makedummymobdb(int class_)
  3570. {
  3571. if (mob->dummy != NULL)
  3572. {
  3573. if (mob->db(class_) == mob->dummy)
  3574. return 1; //Using the mob->dummy data already. [Skotlex]
  3575. if (class_ > 0 && class_ <= MAX_MOB_DB) {
  3576. //Remove the mob data so that it uses the dummy data instead.
  3577. mob->destroy_mob_db(class_);
  3578. }
  3579. return 0;
  3580. }
  3581. //Initialize dummy data.
  3582. mob->dummy = (struct mob_db*)aCalloc(1, sizeof(struct mob_db)); //Initializing the dummy mob.
  3583. sprintf(mob->dummy->sprite,"DUMMY");
  3584. sprintf(mob->dummy->name,"Dummy");
  3585. sprintf(mob->dummy->jname,"Dummy");
  3586. mob->dummy->lv=1;
  3587. mob->dummy->status.max_hp=1000;
  3588. mob->dummy->status.max_sp=1;
  3589. mob->dummy->status.rhw.range=1;
  3590. mob->dummy->status.rhw.atk=7;
  3591. mob->dummy->status.rhw.atk2=10;
  3592. mob->dummy->status.str=1;
  3593. mob->dummy->status.agi=1;
  3594. mob->dummy->status.vit=1;
  3595. mob->dummy->status.int_=1;
  3596. mob->dummy->status.dex=6;
  3597. mob->dummy->status.luk=2;
  3598. mob->dummy->status.speed=300;
  3599. mob->dummy->status.adelay=1000;
  3600. mob->dummy->status.amotion=500;
  3601. mob->dummy->status.dmotion=500;
  3602. mob->dummy->base_exp=2;
  3603. mob->dummy->job_exp=1;
  3604. mob->dummy->range2=10;
  3605. mob->dummy->range3=10;
  3606.  
  3607. return 0;
  3608. }
  3609.  
  3610. //Adjusts the drop rate of item according to the criteria given. [Skotlex]
  3611. unsigned int mob_drop_adjust(int baserate, int rate_adjust, unsigned short rate_min, unsigned short rate_max)
  3612. {
  3613. double rate = baserate;
  3614.  
  3615. if (battle_config.logarithmic_drops && rate_adjust > 0 && rate_adjust != 100 && baserate > 0) //Logarithmic drops equation by Ishizu-Chan
  3616. //Equation: Droprate(x,y) = x * (5 - log(x)) ^ (ln(y) / ln(5))
  3617. //x is the normal Droprate, y is the Modificator.
  3618. rate = rate * pow((5.0 - log10(rate)), (log(rate_adjust/100.) / log(5.0))) + 0.5;
  3619. else
  3620. //Classical linear rate adjustment.
  3621. rate = rate * rate_adjust/100;
  3622.  
  3623. return (unsigned int)cap_value(rate,rate_min,rate_max);
  3624. }
  3625.  
  3626. /**
  3627. * Check if global item drop rate is overridden for given item
  3628. * in db/mob_item_ratio.txt
  3629. * @param nameid ID of the item
  3630. * @param mob_id ID of the monster
  3631. * @param rate_adjust pointer to store ratio if found
  3632. */
  3633. void item_dropratio_adjust(int nameid, int mob_id, int *rate_adjust)
  3634. {
  3635. if( item_drop_ratio_db[nameid] ) {
  3636. if( item_drop_ratio_db[nameid]->mob_id[0] ) { // only for listed mobs
  3637. int i;
  3638. ARR_FIND(0, MAX_ITEMRATIO_MOBS, i, item_drop_ratio_db[nameid]->mob_id[i] == mob_id);
  3639. if(i < MAX_ITEMRATIO_MOBS) // found
  3640. *rate_adjust = item_drop_ratio_db[nameid]->drop_ratio;
  3641. }
  3642. else // for all mobs
  3643. *rate_adjust = item_drop_ratio_db[nameid]->drop_ratio;
  3644. }
  3645. }
  3646. /* (mob_parse_dbrow)_cap_value */
  3647. static inline int mob_parse_dbrow_cap_value(int class_, int min, int max, int value) {
  3648. if( value > max ) {
  3649. ShowError("mob_parse_dbrow: for class '%d', field value '%d' is higher than the maximum '%d'! capping...\n", class_, value, max);
  3650. return max;
  3651. } else if ( value < min ) {
  3652. ShowError("mob_parse_dbrow: for class '%d', field value '%d' is lower than the minimum '%d'! capping...\n", class_, value, min);
  3653. return min;
  3654. }
  3655. return value;
  3656. }
  3657. /*==========================================
  3658. * processes one mobdb entry
  3659. *------------------------------------------*/
  3660. bool mob_parse_dbrow(char** str) {
  3661. struct mob_db *db, entry;
  3662. struct status_data *mstatus;
  3663. int class_, i, k;
  3664. double exp, maxhp;
  3665. struct mob_data data;
  3666.  
  3667. class_ = atoi(str[0]);
  3668.  
  3669. if (class_ <= 1000 || class_ > MAX_MOB_DB) {
  3670. ShowError("mob_parse_dbrow: Invalid monster ID %d, must be in range %d-%d.\n", class_, 1000, MAX_MOB_DB);
  3671. return false;
  3672. }
  3673. if (pcdb_checkid(class_)) {
  3674. ShowError("mob_parse_dbrow: Invalid monster ID %d, reserved for player classes.\n", class_);
  3675. return false;
  3676. }
  3677.  
  3678. if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END) {
  3679. ShowError("mob_parse_dbrow: Invalid monster ID %d. Range %d-%d is reserved for player clones. Please increase MAX_MOB_DB (%d).\n", class_, MOB_CLONE_START, MOB_CLONE_END-1, MAX_MOB_DB);
  3680. return false;
  3681. }
  3682.  
  3683. memset(&entry, 0, sizeof(entry));
  3684.  
  3685. db = &entry;
  3686. mstatus = &db->status;
  3687.  
  3688. db->vd.class_ = class_;
  3689. safestrncpy(db->sprite, str[1], sizeof(db->sprite));
  3690. safestrncpy(db->jname, str[2], sizeof(db->jname));
  3691. safestrncpy(db->name, str[3], sizeof(db->name));
  3692. db->lv = atoi(str[4]);
  3693. db->lv = cap_value(db->lv, 1, USHRT_MAX);
  3694. mstatus->max_hp = atoi(str[5]);
  3695. mstatus->max_sp = atoi(str[6]);
  3696.  
  3697. exp = (double)atoi(str[7]) * (double)battle_config.base_exp_rate / 100.;
  3698. db->base_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  3699.  
  3700. exp = (double)atoi(str[8]) * (double)battle_config.job_exp_rate / 100.;
  3701. db->job_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  3702.  
  3703. mstatus->rhw.range = atoi(str[9]);
  3704.  
  3705. mstatus->rhw.atk = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[10]));
  3706. mstatus->rhw.atk2 = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[11]));
  3707.  
  3708. mstatus->def = mob_parse_dbrow_cap_value(class_,DEFTYPE_MIN,DEFTYPE_MAX,atoi(str[12]));
  3709. mstatus->mdef = mob_parse_dbrow_cap_value(class_,DEFTYPE_MIN,DEFTYPE_MAX,atoi(str[13]));
  3710.  
  3711. mstatus->str = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[14]));
  3712. mstatus->agi = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[15]));
  3713. mstatus->vit = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[16]));
  3714. mstatus->int_ = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[17]));
  3715. mstatus->dex = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[18]));
  3716. mstatus->luk = mob_parse_dbrow_cap_value(class_,UINT16_MIN,UINT16_MAX,atoi(str[19]));
  3717.  
  3718. /*
  3719. * Disabled for renewal since difference of 0 and 1 still has an impact in the formulas
  3720. * Just in case there is a mishandled division by zero please let us know. [malufett]
  3721. */
  3722. #ifndef RENEWAL
  3723. //All status should be min 1 to prevent divisions by zero from some skills. [Skotlex]
  3724. if (mstatus->str < 1) mstatus->str = 1;
  3725. if (mstatus->agi < 1) mstatus->agi = 1;
  3726. if (mstatus->vit < 1) mstatus->vit = 1;
  3727. if (mstatus->int_< 1) mstatus->int_= 1;
  3728. if (mstatus->dex < 1) mstatus->dex = 1;
  3729. if (mstatus->luk < 1) mstatus->luk = 1;
  3730. #endif
  3731.  
  3732. //Tests showed that chase range is effectively 2 cells larger than expected [Playtester]
  3733. if (db->range3 > 0)
  3734. db->range3 += 2;
  3735.  
  3736. db->range2 = atoi(str[20]);
  3737. db->range3 = atoi(str[21]);
  3738. if (battle_config.view_range_rate != 100) {
  3739. db->range2 = db->range2 * battle_config.view_range_rate / 100;
  3740. if (db->range2 < 1)
  3741. db->range2 = 1;
  3742. }
  3743. if (battle_config.chase_range_rate != 100) {
  3744. db->range3 = db->range3 * battle_config.chase_range_rate / 100;
  3745. if (db->range3 < db->range2)
  3746. db->range3 = db->range2;
  3747. }
  3748.  
  3749. mstatus->size = atoi(str[22]);
  3750. mstatus->race = atoi(str[23]);
  3751.  
  3752. i = atoi(str[24]); //Element
  3753. mstatus->def_ele = i%10;
  3754. mstatus->ele_lv = i/20;
  3755. if (mstatus->def_ele >= ELE_MAX) {
  3756. ShowError("mob_parse_dbrow: Invalid element type %d for monster ID %d (max=%d).\n", mstatus->def_ele, class_, ELE_MAX-1);
  3757. return false;
  3758. }
  3759. if (mstatus->ele_lv < 1 || mstatus->ele_lv > 4) {
  3760. ShowError("mob_parse_dbrow: Invalid element level %d for monster ID %d, must be in range 1-4.\n", mstatus->ele_lv, class_);
  3761. return false;
  3762. }
  3763.  
  3764. mstatus->mode = (int)strtol(str[25], NULL, 0);
  3765. if (!battle_config.monster_active_enable)
  3766. mstatus->mode &= ~MD_AGGRESSIVE;
  3767.  
  3768. mstatus->speed = atoi(str[26]);
  3769. mstatus->aspd_rate = 1000;
  3770. i = atoi(str[27]);
  3771. mstatus->adelay = cap_value(i, battle_config.monster_max_aspd*2, 4000);
  3772. i = atoi(str[28]);
  3773. mstatus->amotion = cap_value(i, battle_config.monster_max_aspd, 2000);
  3774. //If the attack animation is longer than the delay, the client crops the attack animation!
  3775. //On aegis there is no real visible effect of having a recharge-time less than amotion anyway.
  3776. if (mstatus->adelay < mstatus->amotion)
  3777. mstatus->adelay = mstatus->amotion;
  3778. mstatus->dmotion = atoi(str[29]);
  3779. if(battle_config.monster_damage_delay_rate != 100)
  3780. mstatus->dmotion = mstatus->dmotion * battle_config.monster_damage_delay_rate / 100;
  3781.  
  3782. // Fill in remaining status data by using a dummy monster.
  3783. data.bl.type = BL_MOB;
  3784. data.level = db->lv;
  3785. memcpy(&data.status, mstatus, sizeof(struct status_data));
  3786. status->calc_misc(&data.bl, mstatus, db->lv);
  3787.  
  3788. // MVP EXP Bonus: MEXP
  3789. // Some new MVP's MEXP multiple by high exp-rate cause overflow. [LuzZza]
  3790. exp = (double)atoi(str[30]) * (double)battle_config.mvp_exp_rate / 100.;
  3791. db->mexp = (unsigned int)cap_value(exp, 0, UINT_MAX);
  3792.  
  3793. //Now that we know if it is an mvp or not, apply battle_config modifiers [Skotlex]
  3794. maxhp = (double)mstatus->max_hp;
  3795. if (db->mexp > 0) { //Mvp
  3796. if (battle_config.mvp_hp_rate != 100)
  3797. maxhp = maxhp * (double)battle_config.mvp_hp_rate / 100.;
  3798. } else //Normal mob
  3799. if (battle_config.monster_hp_rate != 100)
  3800. maxhp = maxhp * (double)battle_config.monster_hp_rate / 100.;
  3801.  
  3802. mstatus->max_hp = (unsigned int)cap_value(maxhp, 1, UINT_MAX);
  3803. if(mstatus->max_sp < 1) mstatus->max_sp = 1;
  3804.  
  3805. //Since mobs always respawn with full life...
  3806. mstatus->hp = mstatus->max_hp;
  3807. mstatus->sp = mstatus->max_sp;
  3808.  
  3809. // MVP Drops: MVP1id,MVP1per,MVP2id,MVP2per,MVP3id,MVP3per
  3810. for(i = 0; i < MAX_MVP_DROP; i++) {
  3811. int rate_adjust = battle_config.item_rate_mvp;;
  3812. db->mvpitem[i].nameid = atoi(str[31+i*2]);
  3813. if (!db->mvpitem[i].nameid) {
  3814. db->mvpitem[i].p = 0; //No item....
  3815. continue;
  3816. }
  3817. mob->item_dropratio_adjust(db->mvpitem[i].nameid, class_, &rate_adjust);
  3818. db->mvpitem[i].p = mob->drop_adjust(atoi(str[32+i*2]), rate_adjust, battle_config.item_drop_mvp_min, battle_config.item_drop_mvp_max);
  3819.  
  3820. //calculate and store Max available drop chance of the MVP item
  3821. if (db->mvpitem[i].p) {
  3822. struct item_data *id;
  3823. id = itemdb->search(db->mvpitem[i].nameid);
  3824. if (id->maxchance == -1 || (id->maxchance < db->mvpitem[i].p/10 + 1) ) {
  3825. //item has bigger drop chance or sold in shops
  3826. id->maxchance = db->mvpitem[i].p/10 + 1; //reduce MVP drop info to not spoil common drop rate
  3827. }
  3828. }
  3829. }
  3830.  
  3831. for(i = 0; i < MAX_MOB_DROP; i++) {
  3832. int rate = 0, rate_adjust, type;
  3833. unsigned short ratemin, ratemax;
  3834. struct item_data *id;
  3835. k = 31 + MAX_MVP_DROP*2 + i*2;
  3836. db->dropitem[i].nameid = atoi(str[k]);
  3837. if (!db->dropitem[i].nameid) {
  3838. db->dropitem[i].p = 0; //No drop.
  3839. continue;
  3840. }
  3841. id = itemdb->search(db->dropitem[i].nameid);
  3842. type = id->type;
  3843. rate = atoi(str[k+1]);
  3844. if( (class_ >= 1324 && class_ <= 1363) || (class_ >= 1938 && class_ <= 1946) ) {
  3845. //Treasure box drop rates [Skotlex]
  3846. rate_adjust = battle_config.item_rate_treasure;
  3847. ratemin = battle_config.item_drop_treasure_min;
  3848. ratemax = battle_config.item_drop_treasure_max;
  3849. }
  3850. else switch (type)
  3851. { // Added support to restrict normal drops of MVP's [Reddozen]
  3852. case IT_HEALING:
  3853. rate_adjust = (mstatus->mode&MD_BOSS) ? battle_config.item_rate_heal_boss : battle_config.item_rate_heal;
  3854. ratemin = battle_config.item_drop_heal_min;
  3855. ratemax = battle_config.item_drop_heal_max;
  3856. break;
  3857. case IT_USABLE:
  3858. case IT_CASH:
  3859. rate_adjust = (mstatus->mode&MD_BOSS) ? battle_config.item_rate_use_boss : battle_config.item_rate_use;
  3860. ratemin = battle_config.item_drop_use_min;
  3861. ratemax = battle_config.item_drop_use_max;
  3862. break;
  3863. case IT_WEAPON:
  3864. case IT_ARMOR:
  3865. case IT_PETARMOR:
  3866. rate_adjust = (mstatus->mode&MD_BOSS) ? battle_config.item_rate_equip_boss : battle_config.item_rate_equip;
  3867. ratemin = battle_config.item_drop_equip_min;
  3868. ratemax = battle_config.item_drop_equip_max;
  3869. break;
  3870. case IT_CARD:
  3871. rate_adjust = (mstatus->mode&MD_BOSS) ? battle_config.item_rate_card_boss : battle_config.item_rate_card;
  3872. ratemin = battle_config.item_drop_card_min;
  3873. ratemax = battle_config.item_drop_card_max;
  3874. break;
  3875. default:
  3876. rate_adjust = (mstatus->mode&MD_BOSS) ? battle_config.item_rate_common_boss : battle_config.item_rate_common;
  3877. ratemin = battle_config.item_drop_common_min;
  3878. ratemax = battle_config.item_drop_common_max;
  3879. break;
  3880. }
  3881. mob->item_dropratio_adjust(id->nameid, class_, &rate_adjust);
  3882. db->dropitem[i].p = mob->drop_adjust(rate, rate_adjust, ratemin, ratemax);
  3883.  
  3884. //calculate and store Max available drop chance of the item
  3885. if( db->dropitem[i].p && (class_ < 1324 || class_ > 1363) && (class_ < 1938 || class_ > 1946) )
  3886. { //Skip treasure chests.
  3887. if (id->maxchance == -1 || (id->maxchance < db->dropitem[i].p) ) {
  3888. id->maxchance = db->dropitem[i].p; //item has bigger drop chance or sold in shops
  3889. }
  3890. for (k = 0; k< MAX_SEARCH; k++) {
  3891. if (id->mob[k].chance <= db->dropitem[i].p)
  3892. break;
  3893. }
  3894. if (k == MAX_SEARCH)
  3895. continue;
  3896.  
  3897. if (id->mob[k].id != class_)
  3898. memmove(&id->mob[k+1], &id->mob[k], (MAX_SEARCH-k-1)*sizeof(id->mob[0]));
  3899. id->mob[k].chance = db->dropitem[i].p;
  3900. id->mob[k].id = class_;
  3901. }
  3902. }
  3903. // Finally insert monster's data into the database.
  3904. if (mob->db_data[class_] == NULL)
  3905. mob->db_data[class_] = (struct mob_db*)aMalloc(sizeof(struct mob_db));
  3906. else
  3907. //Copy over spawn data
  3908. memcpy(&db->spawn, mob->db_data[class_]->spawn, sizeof(db->spawn));
  3909.  
  3910. memcpy(mob->db_data[class_], db, sizeof(struct mob_db));
  3911. return true;
  3912. }
  3913.  
  3914. /*==========================================
  3915. * mob_db.txt reading
  3916. *------------------------------------------*/
  3917. bool mob_readdb_sub(char* fields[], int columns, int current) {
  3918. return mob->parse_dbrow(fields);
  3919. }
  3920.  
  3921. void mob_readdb(void) {
  3922. const char* filename[] = {
  3923. DBPATH"mob_db.txt",
  3924. "mob_db2.txt" };
  3925. int fi;
  3926.  
  3927. for( fi = 0; fi < ARRAYLENGTH(filename); ++fi ) {
  3928. if(fi > 0) {
  3929. char filepath[256];
  3930. sprintf(filepath, "%s/%s", map->db_path, filename[fi]);
  3931. if(!exists(filepath)) {
  3932. continue;
  3933. }
  3934. }
  3935.  
  3936. sv->readdb(map->db_path, filename[fi], ',', 31+2*MAX_MVP_DROP+2*MAX_MOB_DROP, 31+2*MAX_MVP_DROP+2*MAX_MOB_DROP, -1, mob->readdb_sub);
  3937. }
  3938. mob->name_constants();
  3939. }
  3940.  
  3941. /*==========================================
  3942. * mob_db table reading
  3943. *------------------------------------------*/
  3944. int mob_read_sqldb(void) {
  3945. const char* mob_db_name[] = { map->mob_db_db, map->mob_db2_db };
  3946. int fi;
  3947.  
  3948. for( fi = 0; fi < ARRAYLENGTH(mob_db_name); ++fi ) {
  3949. uint32 lines = 0, count = 0;
  3950.  
  3951. // retrieve all rows from the mob database
  3952. if( SQL_ERROR == SQL->Query(map->mysql_handle, "SELECT * FROM `%s`", mob_db_name[fi]) ) {
  3953. Sql_ShowDebug(map->mysql_handle);
  3954. continue;
  3955. }
  3956.  
  3957. // process rows one by one
  3958. while( SQL_SUCCESS == SQL->NextRow(map->mysql_handle) ) {
  3959. // wrap the result into a TXT-compatible format
  3960. char line[1024];
  3961. char* str[31+2*MAX_MVP_DROP+2*MAX_MOB_DROP];
  3962. char* p;
  3963. int i;
  3964.  
  3965. lines++;
  3966. for(i = 0, p = line; i < 31+2*MAX_MVP_DROP+2*MAX_MOB_DROP; i++)
  3967. {
  3968. char* data;
  3969. size_t len;
  3970. SQL->GetData(map->mysql_handle, i, &data, &len);
  3971.  
  3972. strcpy(p, data);
  3973. str[i] = p;
  3974. p+= len + 1;
  3975. }
  3976.  
  3977. if (!mob->parse_dbrow(str))
  3978. continue;
  3979.  
  3980. count++;
  3981. }
  3982.  
  3983. // free the query result
  3984. SQL->FreeResult(map->mysql_handle);
  3985.  
  3986. ShowStatus("Done reading '"CL_WHITE"%"PRIu32""CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_db_name[fi]);
  3987. }
  3988. mob->name_constants();
  3989. return 0;
  3990. }
  3991.  
  3992. void mob_name_constants(void) {
  3993. int i;
  3994. #ifdef ENABLE_CASE_CHECK
  3995. script->parser_current_file = "Mob Database (Likely an invalid or conflicting SpriteName)";
  3996. #endif // ENABLE_CASE_CHECK
  3997. for (i = 0; i < MAX_MOB_DB; i++) {
  3998. if (mob->db_data[i] && !mob->is_clone(i))
  3999. script->set_constant2(mob->db_data[i]->sprite, i, 0);
  4000. }
  4001. #ifdef ENABLE_CASE_CHECK
  4002. script->parser_current_file = NULL;
  4003. #endif // ENABLE_CASE_CHECK
  4004. }
  4005.  
  4006. /*==========================================
  4007. * MOB display graphic change data reading
  4008. *------------------------------------------*/
  4009. bool mob_readdb_mobavail(char* str[], int columns, int current)
  4010. {
  4011. int class_, k;
  4012.  
  4013. class_=atoi(str[0]);
  4014.  
  4015. if(mob->db(class_) == mob->dummy) {
  4016. // invalid class (probably undefined in db)
  4017. ShowWarning("mob_readdb_mobavail: Unknown mob id %d.\n", class_);
  4018. return false;
  4019. }
  4020.  
  4021. k=atoi(str[1]);
  4022.  
  4023. memset(&mob->db_data[class_]->vd, 0, sizeof(struct view_data));
  4024. mob->db_data[class_]->vd.class_=k;
  4025.  
  4026. //Player sprites
  4027. if(pcdb_checkid(k) && columns==12) {
  4028. mob->db_data[class_]->vd.sex=atoi(str[2]);
  4029. mob->db_data[class_]->vd.hair_style=atoi(str[3]);
  4030. mob->db_data[class_]->vd.hair_color=atoi(str[4]);
  4031. mob->db_data[class_]->vd.weapon=atoi(str[5]);
  4032. mob->db_data[class_]->vd.shield=atoi(str[6]);
  4033. mob->db_data[class_]->vd.head_top=atoi(str[7]);
  4034. mob->db_data[class_]->vd.head_mid=atoi(str[8]);
  4035. mob->db_data[class_]->vd.head_bottom=atoi(str[9]);
  4036. mob->db_data[class_]->option=atoi(str[10])&~(OPTION_HIDE|OPTION_CLOAK|OPTION_INVISIBLE);
  4037. mob->db_data[class_]->vd.cloth_color=atoi(str[11]); // Monster player dye option - Valaris
  4038. }
  4039. else if(columns==3)
  4040. mob->db_data[class_]->vd.head_bottom=atoi(str[2]); // mob equipment [Valaris]
  4041. else if( columns != 2 )
  4042. return false;
  4043.  
  4044. return true;
  4045. }
  4046.  
  4047. /*==========================================
  4048. * Reading of random monster data
  4049. *------------------------------------------*/
  4050. int mob_read_randommonster(void)
  4051. {
  4052. FILE *fp;
  4053. char line[1024];
  4054. char *str[10],*p;
  4055. int i,j;
  4056. const char* mobfile[] = {
  4057. DBPATH"mob_branch.txt",
  4058. DBPATH"mob_poring.txt",
  4059. DBPATH"mob_boss.txt",
  4060. "mob_pouch.txt",
  4061. "mob_classchange.txt"};
  4062.  
  4063. memset(&summon, 0, sizeof(summon));
  4064.  
  4065. for( i = 0; i < ARRAYLENGTH(mobfile) && i < MAX_RANDOMMONSTER; i++ ) {
  4066. unsigned int count = 0;
  4067. mob->db_data[0]->summonper[i] = 1002; // Default fallback value, in case the database does not provide one
  4068. sprintf(line, "%s/%s", map->db_path, mobfile[i]);
  4069. fp=fopen(line,"r");
  4070. if(fp==NULL){
  4071. ShowError("can't read %s\n",line);
  4072. return -1;
  4073. }
  4074. while(fgets(line, sizeof(line), fp))
  4075. {
  4076. int class_;
  4077. if(line[0] == '/' && line[1] == '/')
  4078. continue;
  4079. memset(str,0,sizeof(str));
  4080. for(j=0,p=line;j<3 && p;j++){
  4081. str[j]=p;
  4082. p=strchr(p,',');
  4083. if(p) *p++=0;
  4084. }
  4085.  
  4086. if(str[0]==NULL || str[2]==NULL)
  4087. continue;
  4088.  
  4089. class_ = atoi(str[0]);
  4090. if(mob->db(class_) == mob->dummy)
  4091. continue;
  4092. count++;
  4093. mob->db_data[class_]->summonper[i]=atoi(str[2]);
  4094. if (i) {
  4095. if( summon[i].qty < ARRAYLENGTH(summon[i].class_) ) //MvPs
  4096. summon[i].class_[summon[i].qty++] = class_;
  4097. else {
  4098. ShowDebug("Can't store more random mobs from %s, increase size of mob.c:summon variable!\n", mobfile[i]);
  4099. break;
  4100. }
  4101. }
  4102. }
  4103. if (i && !summon[i].qty) { //At least have the default here.
  4104. summon[i].class_[0] = mob->db_data[0]->summonper[i];
  4105. summon[i].qty = 1;
  4106. }
  4107. fclose(fp);
  4108. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",count,mobfile[i]);
  4109. }
  4110. return 0;
  4111. }
  4112.  
  4113. /*==========================================
  4114. * processes one mob_chat_db entry [SnakeDrak]
  4115. * @param last_msg_id ensures that only one error message per mob id is printed
  4116. *------------------------------------------*/
  4117. bool mob_parse_row_chatdb(char** str, const char* source, int line, int* last_msg_id)
  4118. {
  4119. char* msg;
  4120. struct mob_chat *ms;
  4121. int msg_id;
  4122. size_t len;
  4123.  
  4124. msg_id = atoi(str[0]);
  4125.  
  4126. if (msg_id <= 0 || msg_id > MAX_MOB_CHAT)
  4127. {
  4128. if (msg_id != *last_msg_id) {
  4129. ShowError("mob_chat: Invalid chat ID: %d at %s, line %d\n", msg_id, source, line);
  4130. *last_msg_id = msg_id;
  4131. }
  4132. return false;
  4133. }
  4134.  
  4135. if (mob->chat_db[msg_id] == NULL)
  4136. mob->chat_db[msg_id] = (struct mob_chat*)aCalloc(1, sizeof (struct mob_chat));
  4137.  
  4138. ms = mob->chat_db[msg_id];
  4139. //MSG ID
  4140. ms->msg_id=msg_id;
  4141. //Color
  4142. ms->color=(unsigned int)strtoul(str[1],NULL,0);
  4143. //Message
  4144. msg = str[2];
  4145. len = strlen(msg);
  4146.  
  4147. while( len && ( msg[len-1]=='\r' || msg[len-1]=='\n' ) )
  4148. {// find EOL to strip
  4149. len--;
  4150. }
  4151.  
  4152. if(len>(CHAT_SIZE_MAX-1))
  4153. {
  4154. if (msg_id != *last_msg_id) {
  4155. ShowError("mob_chat: readdb: Message too long! Line %d, id: %d\n", line, msg_id);
  4156. *last_msg_id = msg_id;
  4157. }
  4158. return false;
  4159. }
  4160. else if( !len )
  4161. {
  4162. ShowWarning("mob_parse_row_chatdb: Empty message for id %d.\n", msg_id);
  4163. return false;
  4164. }
  4165.  
  4166. msg[len] = 0; // strip previously found EOL
  4167. safestrncpy(ms->msg, str[2], CHAT_SIZE_MAX);
  4168.  
  4169. return true;
  4170. }
  4171.  
  4172. /*==========================================
  4173. * mob_chat_db.txt reading [SnakeDrak]
  4174. *-------------------------------------------------------------------------*/
  4175. void mob_readchatdb(void) {
  4176. char arc[]="mob_chat_db.txt";
  4177. uint32 lines=0, count=0;
  4178. char line[1024], filepath[256];
  4179. int i, tmp=0;
  4180. FILE *fp;
  4181. sprintf(filepath, "%s/%s", map->db_path, arc);
  4182. fp=fopen(filepath, "r");
  4183. if(fp == NULL) {
  4184. ShowWarning("mob_readchatdb: File not found \"%s\", skipping.\n", filepath);
  4185. return;
  4186. }
  4187.  
  4188. while(fgets(line, sizeof(line), fp)) {
  4189. char *str[3], *p, *np;
  4190. int j=0;
  4191.  
  4192. lines++;
  4193. if(line[0] == '/' && line[1] == '/')
  4194. continue;
  4195. memset(str, 0, sizeof(str));
  4196.  
  4197. p=line;
  4198. while(ISSPACE(*p))
  4199. ++p;
  4200. if(*p == '\0')
  4201. continue;// empty line
  4202. for(i = 0; i <= 2; i++)
  4203. {
  4204. str[i] = p;
  4205. if(i<2 && (np = strchr(p, ',')) != NULL) {
  4206. *np = '\0'; p = np + 1; j++;
  4207. }
  4208. }
  4209.  
  4210. if( j < 2 || str[2]==NULL)
  4211. {
  4212. ShowError("mob_readchatdb: Insufficient number of fields for skill at %s, line %d\n", arc, lines);
  4213. continue;
  4214. }
  4215.  
  4216. if( !mob->parse_row_chatdb(str, filepath, lines, &tmp) )
  4217. continue;
  4218.  
  4219. count++;
  4220. }
  4221. fclose(fp);
  4222. ShowStatus("Done reading '"CL_WHITE"%"PRIu32""CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, arc);
  4223. }
  4224.  
  4225. /*==========================================
  4226. * processes one mob_skill_db entry
  4227. *------------------------------------------*/
  4228. bool mob_parse_row_mobskilldb(char** str, int columns, int current)
  4229. {
  4230. static const struct {
  4231. char str[32];
  4232. enum MobSkillState id;
  4233. } state[] = {
  4234. { "any", MSS_ANY }, //All states except Dead
  4235. { "idle", MSS_IDLE },
  4236. { "walk", MSS_WALK },
  4237. { "loot", MSS_LOOT },
  4238. { "dead", MSS_DEAD },
  4239. { "attack", MSS_BERSERK }, //Retaliating attack
  4240. { "angry", MSS_ANGRY }, //Preemptive attack (aggressive mobs)
  4241. { "chase", MSS_RUSH }, //Chase escaping target
  4242. { "follow", MSS_FOLLOW }, //Preemptive chase (aggressive mobs)
  4243. { "anytarget", MSS_ANYTARGET }, //Berserk+Angry+Rush+Follow
  4244. };
  4245. static const struct {
  4246. char str[32];
  4247. int id;
  4248. } cond1[] = {
  4249. { "always", MSC_ALWAYS },
  4250. { "myhpltmaxrate", MSC_MYHPLTMAXRATE },
  4251. { "myhpinrate", MSC_MYHPINRATE },
  4252. { "friendhpltmaxrate", MSC_FRIENDHPLTMAXRATE },
  4253. { "friendhpinrate", MSC_FRIENDHPINRATE },
  4254. { "mystatuson", MSC_MYSTATUSON },
  4255. { "mystatusoff", MSC_MYSTATUSOFF },
  4256. { "friendstatuson", MSC_FRIENDSTATUSON },
  4257. { "friendstatusoff", MSC_FRIENDSTATUSOFF },
  4258. { "attackpcgt", MSC_ATTACKPCGT },
  4259. { "attackpcge", MSC_ATTACKPCGE },
  4260. { "slavelt", MSC_SLAVELT },
  4261. { "slavele", MSC_SLAVELE },
  4262. { "closedattacked", MSC_CLOSEDATTACKED },
  4263. { "longrangeattacked", MSC_LONGRANGEATTACKED },
  4264. { "skillused", MSC_SKILLUSED },
  4265. { "afterskill", MSC_AFTERSKILL },
  4266. { "casttargeted", MSC_CASTTARGETED },
  4267. { "rudeattacked", MSC_RUDEATTACKED },
  4268. { "masterhpltmaxrate", MSC_MASTERHPLTMAXRATE },
  4269. { "masterattacked", MSC_MASTERATTACKED },
  4270. { "alchemist", MSC_ALCHEMIST },
  4271. { "onspawn", MSC_SPAWN },
  4272. }, cond2[] ={
  4273. { "anybad", -1 },
  4274. { "stone", SC_STONE },
  4275. { "freeze", SC_FREEZE },
  4276. { "stun", SC_STUN },
  4277. { "sleep", SC_SLEEP },
  4278. { "poison", SC_POISON },
  4279. { "curse", SC_CURSE },
  4280. { "silence", SC_SILENCE },
  4281. { "confusion", SC_CONFUSION },
  4282. { "blind", SC_BLIND },
  4283. { "hiding", SC_HIDING },
  4284. { "sight", SC_SIGHT },
  4285. }, target[] = {
  4286. { "target", MST_TARGET },
  4287. { "randomtarget", MST_RANDOM },
  4288. { "self", MST_SELF },
  4289. { "friend", MST_FRIEND },
  4290. { "master", MST_MASTER },
  4291. { "around5", MST_AROUND5 },
  4292. { "around6", MST_AROUND6 },
  4293. { "around7", MST_AROUND7 },
  4294. { "around8", MST_AROUND8 },
  4295. { "around1", MST_AROUND1 },
  4296. { "around2", MST_AROUND2 },
  4297. { "around3", MST_AROUND3 },
  4298. { "around4", MST_AROUND4 },
  4299. { "around", MST_AROUND },
  4300. };
  4301. static int last_mob_id = 0; // ensures that only one error message per mob id is printed
  4302.  
  4303. struct mob_skill *ms, gms;
  4304. int mob_id;
  4305. int i =0, j, tmp;
  4306. uint16 sidx = 0;
  4307.  
  4308. mob_id = atoi(str[0]);
  4309.  
  4310. if (mob_id > 0 && mob->db(mob_id) == mob->dummy)
  4311. {
  4312. if (mob_id != last_mob_id) {
  4313. ShowError("mob_parse_row_mobskilldb: Non existant Mob id %d\n", mob_id);
  4314. last_mob_id = mob_id;
  4315. }
  4316. return false;
  4317. }
  4318. if( strcmp(str[1],"clear")==0 ){
  4319. if (mob_id < 0)
  4320. return false;
  4321. memset(mob->db_data[mob_id]->skill,0,sizeof(struct mob_skill));
  4322. mob->db_data[mob_id]->maxskill=0;
  4323. return true;
  4324. }
  4325.  
  4326. if (mob_id < 0) {
  4327. //Prepare global skill. [Skotlex]
  4328. memset(&gms, 0, sizeof (struct mob_skill));
  4329. ms = &gms;
  4330. } else {
  4331. ARR_FIND( 0, MAX_MOBSKILL, i, (ms = &mob->db_data[mob_id]->skill[i])->skill_id == 0 );
  4332. if( i == MAX_MOBSKILL )
  4333. {
  4334. if (mob_id != last_mob_id) {
  4335. ShowError("mob_parse_row_mobskilldb: Too many skills for monster %d[%s]\n", mob_id, mob->db_data[mob_id]->sprite);
  4336. last_mob_id = mob_id;
  4337. }
  4338. return false;
  4339. }
  4340. }
  4341.  
  4342. //State
  4343. ARR_FIND( 0, ARRAYLENGTH(state), j, strcmp(str[2],state[j].str) == 0 );
  4344. if( j < ARRAYLENGTH(state) )
  4345. ms->state = state[j].id;
  4346. else {
  4347. ShowWarning("mob_parse_row_mobskilldb: Unrecognized state %s\n", str[2]);
  4348. ms->state = MSS_ANY;
  4349. }
  4350.  
  4351. //Skill ID
  4352. j=atoi(str[3]);
  4353. if ( !(sidx = skill->get_index(j) ) ) {
  4354. if (mob_id < 0)
  4355. ShowError("mob_parse_row_mobskilldb: Invalid Skill ID (%d) for all mobs\n", j);
  4356. else
  4357. ShowError("mob_parse_row_mobskilldb: Invalid Skill ID (%d) for mob %d (%s)\n", j, mob_id, mob->db_data[mob_id]->sprite);
  4358. return false;
  4359. }
  4360. ms->skill_id=j;
  4361.  
  4362. //Skill lvl
  4363. j= atoi(str[4])<=0 ? 1 : atoi(str[4]);
  4364. ms->skill_lv= j>battle_config.mob_max_skilllvl ? battle_config.mob_max_skilllvl : j; //we strip max skill level
  4365.  
  4366. //Apply battle_config modifiers to rate (permillage) and delay [Skotlex]
  4367. tmp = atoi(str[5]);
  4368. if (battle_config.mob_skill_rate != 100)
  4369. tmp = tmp*battle_config.mob_skill_rate/100;
  4370. if (tmp > 10000)
  4371. ms->permillage= 10000;
  4372. else if (!tmp && battle_config.mob_skill_rate)
  4373. ms->permillage= 1;
  4374. else
  4375. ms->permillage= tmp;
  4376. ms->casttime=atoi(str[6]);
  4377. ms->delay=atoi(str[7]);
  4378. if (battle_config.mob_skill_delay != 100)
  4379. ms->delay = ms->delay*battle_config.mob_skill_delay/100;
  4380. if (ms->delay < 0 || ms->delay > MOB_MAX_DELAY) //time overflow?
  4381. ms->delay = MOB_MAX_DELAY;
  4382. ms->cancel=atoi(str[8]);
  4383. if( strcmp(str[8],"yes")==0 ) ms->cancel=1;
  4384.  
  4385. //Target
  4386. ARR_FIND( 0, ARRAYLENGTH(target), j, strcmp(str[9],target[j].str) == 0 );
  4387. if( j < ARRAYLENGTH(target) )
  4388. ms->target = target[j].id;
  4389. else {
  4390. ShowWarning("mob_parse_row_mobskilldb: Unrecognized target %s for %d\n", str[9], mob_id);
  4391. ms->target = MST_TARGET;
  4392. }
  4393.  
  4394. //Check that the target condition is right for the skill type. [Skotlex]
  4395. if ( skill->get_casttype2(sidx) == CAST_GROUND) {//Ground skill.
  4396. if (ms->target > MST_AROUND) {
  4397. ShowWarning("mob_parse_row_mobskilldb: Wrong mob skill target for ground skill %d (%s) for %s.\n",
  4398. ms->skill_id, skill->db[sidx].name,
  4399. mob_id < 0?"all mobs":mob->db_data[mob_id]->sprite);
  4400. ms->target = MST_TARGET;
  4401. }
  4402. } else if (ms->target > MST_MASTER) {
  4403. ShowWarning("mob_parse_row_mobskilldb: Wrong mob skill target 'around' for non-ground skill %d (%s) for %s.\n",
  4404. ms->skill_id, skill->db[sidx].name,
  4405. mob_id < 0?"all mobs":mob->db_data[mob_id]->sprite);
  4406. ms->target = MST_TARGET;
  4407. }
  4408.  
  4409. //Cond1
  4410. ARR_FIND( 0, ARRAYLENGTH(cond1), j, strcmp(str[10],cond1[j].str) == 0 );
  4411. if( j < ARRAYLENGTH(cond1) )
  4412. ms->cond1 = cond1[j].id;
  4413. else {
  4414. ShowWarning("mob_parse_row_mobskilldb: Unrecognized condition 1 %s for %d\n", str[10], mob_id);
  4415. ms->cond1 = -1;
  4416. }
  4417.  
  4418. //Cond2
  4419. // numeric value
  4420. ms->cond2 = atoi(str[11]);
  4421. // or special constant
  4422. ARR_FIND( 0, ARRAYLENGTH(cond2), j, strcmp(str[11],cond2[j].str) == 0 );
  4423. if( j < ARRAYLENGTH(cond2) )
  4424. ms->cond2 = cond2[j].id;
  4425.  
  4426. ms->val[0]=(int)strtol(str[12],NULL,0);
  4427. ms->val[1]=(int)strtol(str[13],NULL,0);
  4428. ms->val[2]=(int)strtol(str[14],NULL,0);
  4429. ms->val[3]=(int)strtol(str[15],NULL,0);
  4430. ms->val[4]=(int)strtol(str[16],NULL,0);
  4431.  
  4432. if(ms->skill_id == NPC_EMOTION && mob_id>0 &&
  4433. ms->val[1] == mob->db(mob_id)->status.mode)
  4434. {
  4435. ms->val[1] = 0;
  4436. ms->val[4] = 1; //request to return mode to normal.
  4437. }
  4438. if (ms->skill_id == NPC_EMOTION_ON && mob_id>0 && ms->val[1]) {
  4439. //Adds a mode to the mob.
  4440. //Remove aggressive mode when the new mob type is passive.
  4441. if (!(ms->val[1]&MD_AGGRESSIVE))
  4442. ms->val[3]|=MD_AGGRESSIVE;
  4443. ms->val[2]|= ms->val[1]; //Add the new mode.
  4444. ms->val[1] = 0; //Do not "set" it.
  4445. }
  4446.  
  4447. if(*str[17])
  4448. ms->emotion=atoi(str[17]);
  4449. else
  4450. ms->emotion=-1;
  4451.  
  4452. if(str[18]!=NULL && mob->chat_db[atoi(str[18])]!=NULL)
  4453. ms->msg_id=atoi(str[18]);
  4454. else
  4455. ms->msg_id=0;
  4456.  
  4457. if (mob_id < 0) {
  4458. //Set this skill to ALL mobs. [Skotlex]
  4459. mob_id *= -1;
  4460. for (i = 1; i < MAX_MOB_DB; i++)
  4461. {
  4462. if (mob->db_data[i] == NULL)
  4463. continue;
  4464. if (mob->db_data[i]->status.mode&MD_BOSS)
  4465. {
  4466. if (!(mob_id&2)) //Skill not for bosses
  4467. continue;
  4468. } else
  4469. if (!(mob_id&1)) //Skill not for normal enemies.
  4470. continue;
  4471.  
  4472. ARR_FIND( 0, MAX_MOBSKILL, j, mob->db_data[i]->skill[j].skill_id == 0 );
  4473. if(j==MAX_MOBSKILL)
  4474. continue;
  4475.  
  4476. memcpy (&mob->db_data[i]->skill[j], ms, sizeof(struct mob_skill));
  4477. mob->db_data[i]->maxskill=j+1;
  4478. }
  4479. } else //Skill set on a single mob.
  4480. mob->db_data[mob_id]->maxskill=i+1;
  4481.  
  4482. return true;
  4483. }
  4484.  
  4485. /*==========================================
  4486. * mob_skill_db.txt reading
  4487. *------------------------------------------*/
  4488. void mob_readskilldb(void) {
  4489. const char* filename[] = {
  4490. DBPATH"mob_skill_db.txt",
  4491. "mob_skill_db2.txt" };
  4492. int fi;
  4493.  
  4494. if( battle_config.mob_skill_rate == 0 ) {
  4495. ShowStatus("Mob skill use disabled. Not reading mob skills.\n");
  4496. return;
  4497. }
  4498.  
  4499. for( fi = 0; fi < ARRAYLENGTH(filename); ++fi ) {
  4500. if(fi > 0) {
  4501. char filepath[256];
  4502. sprintf(filepath, "%s/%s", map->db_path, filename[fi]);
  4503. if(!exists(filepath)) {
  4504. continue;
  4505. }
  4506. }
  4507.  
  4508. sv->readdb(map->db_path, filename[fi], ',', 19, 19, -1, mob->parse_row_mobskilldb);
  4509. }
  4510. }
  4511.  
  4512. /**
  4513. * mob_skill_db table reading [CalciumKid]
  4514. * not overly sure if this is all correct
  4515. * seems to work though...
  4516. */
  4517. int mob_read_sqlskilldb(void) {
  4518. const char* mob_skill_db_name[] = { map->mob_skill_db_db, map->mob_skill_db2_db };
  4519. int fi;
  4520.  
  4521. if( battle_config.mob_skill_rate == 0 ) {
  4522. ShowStatus("Mob skill use disabled. Not reading mob skills.\n");
  4523. return 0;
  4524. }
  4525.  
  4526.  
  4527. for( fi = 0; fi < ARRAYLENGTH(mob_skill_db_name); ++fi ) {
  4528. uint32 lines = 0, count = 0;
  4529.  
  4530. // retrieve all rows from the mob skill database
  4531. if( SQL_ERROR == SQL->Query(map->mysql_handle, "SELECT * FROM `%s`", mob_skill_db_name[fi]) ) {
  4532. Sql_ShowDebug(map->mysql_handle);
  4533. continue;
  4534. }
  4535.  
  4536. // process rows one by one
  4537. while( SQL_SUCCESS == SQL->NextRow(map->mysql_handle) ) {
  4538. // wrap the result into a TXT-compatible format
  4539. char* str[19];
  4540. char* dummy = "";
  4541. int i;
  4542. ++lines;
  4543. for( i = 0; i < 19; ++i )
  4544. {
  4545. SQL->GetData(map->mysql_handle, i, &str[i], NULL);
  4546. if( str[i] == NULL ) str[i] = dummy; // get rid of NULL columns
  4547. }
  4548.  
  4549. if (!mob->parse_row_mobskilldb(str, 19, count))
  4550. continue;
  4551.  
  4552. count++;
  4553. }
  4554.  
  4555. // free the query result
  4556. SQL->FreeResult(map->mysql_handle);
  4557.  
  4558. ShowStatus("Done reading '"CL_WHITE"%"PRIu32""CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_skill_db_name[fi]);
  4559. }
  4560. return 0;
  4561. }
  4562.  
  4563. /*==========================================
  4564. * mob_race2_db.txt reading
  4565. *------------------------------------------*/
  4566. bool mob_readdb_race2(char* fields[], int columns, int current)
  4567. {
  4568. int race, mobid, i;
  4569.  
  4570. race = atoi(fields[0]);
  4571.  
  4572. if (race < RC2_NONE || race >= RC2_MAX)
  4573. {
  4574. ShowWarning("mob_readdb_race2: Unknown race2 %d.\n", race);
  4575. return false;
  4576. }
  4577.  
  4578. for(i = 1; i<columns; i++)
  4579. {
  4580. mobid = atoi(fields[i]);
  4581. if (mob->db(mobid) == mob->dummy)
  4582. {
  4583. ShowWarning("mob_readdb_race2: Unknown mob id %d for race2 %d.\n", mobid, race);
  4584. continue;
  4585. }
  4586. mob->db_data[mobid]->race2 = race;
  4587. }
  4588. return true;
  4589. }
  4590.  
  4591. /**
  4592. * Read mob_item_ratio.txt
  4593. */
  4594. bool mob_readdb_itemratio(char* str[], int columns, int current)
  4595. {
  4596. int nameid, ratio, i;
  4597. nameid = atoi(str[0]);
  4598.  
  4599. if( itemdb->exists(nameid) == NULL )
  4600. {
  4601. ShowWarning("itemdb_read_itemratio: Invalid item id %d.\n", nameid);
  4602. return false;
  4603. }
  4604.  
  4605. ratio = atoi(str[1]);
  4606.  
  4607. if(item_drop_ratio_db[nameid] == NULL)
  4608. item_drop_ratio_db[nameid] = (struct item_drop_ratio*)aCalloc(1, sizeof(struct item_drop_ratio));
  4609.  
  4610. item_drop_ratio_db[nameid]->drop_ratio = ratio;
  4611. for(i = 0; i < columns-2; i++)
  4612. item_drop_ratio_db[nameid]->mob_id[i] = atoi(str[i+2]);
  4613.  
  4614. return true;
  4615. }
  4616.  
  4617. /**
  4618. * read all mob-related databases
  4619. */
  4620. void mob_load(bool minimal) {
  4621. if (minimal) {
  4622. // Only read the mob db in minimal mode
  4623. mob->readdb();
  4624. return;
  4625. }
  4626. sv->readdb(map->db_path, "mob_item_ratio.txt", ',', 2, 2+MAX_ITEMRATIO_MOBS, -1, mob->readdb_itemratio); // must be read before mobdb
  4627. mob->readchatdb();
  4628. if (map->db_use_sql_mob_db) {
  4629. mob->read_sqldb();
  4630. }
  4631. if (map->db_use_sql_mob_skill_db) {
  4632. mob->read_sqlskilldb();
  4633. } else {
  4634. mob->readdb();
  4635. mob->readskilldb();
  4636. }
  4637. sv->readdb(map->db_path, "mob_avail.txt", ',', 2, 12, -1, mob->readdb_mobavail);
  4638. mob->read_randommonster();
  4639. sv->readdb(map->db_path, DBPATH"mob_race2_db.txt", ',', 2, 20, -1, mob->readdb_race2);
  4640. }
  4641.  
  4642. void mob_reload(void) {
  4643. int i;
  4644.  
  4645. //Mob skills need to be cleared before re-reading them. [Skotlex]
  4646. for (i = 0; i < MAX_MOB_DB; i++)
  4647. if (mob->db_data[i] && !mob->is_clone(i)) {
  4648. memset(&mob->db_data[i]->skill,0,sizeof(mob->db_data[i]->skill));
  4649. mob->db_data[i]->maxskill=0;
  4650. }
  4651.  
  4652. // Clear item_drop_ratio_db
  4653. for (i = 0; i < MAX_ITEMDB; i++) {
  4654. if (item_drop_ratio_db[i]) {
  4655. aFree(item_drop_ratio_db[i]);
  4656. item_drop_ratio_db[i] = NULL;
  4657. }
  4658. }
  4659.  
  4660. mob->load(false);
  4661. }
  4662.  
  4663. /**
  4664. * Clears spawn related information for a script reload.
  4665. */
  4666. void mob_clear_spawninfo()
  4667. {
  4668. int i;
  4669. for (i = 0; i < MAX_MOB_DB; i++)
  4670. if (mob->db_data[i])
  4671. memset(&mob->db_data[i]->spawn,0,sizeof(mob->db_data[i]->spawn));
  4672. }
  4673.  
  4674. /*==========================================
  4675. * Circumference initialization of mob
  4676. *------------------------------------------*/
  4677. int do_init_mob(bool minimal) {
  4678. // Initialize the mob database
  4679. memset(mob->db_data,0,sizeof(mob->db_data)); //Clear the array
  4680. mob->db_data[0] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db)); //This mob is used for random spawns
  4681. mob->makedummymobdb(0); //The first time this is invoked, it creates the dummy mob
  4682. item_drop_ers = ers_new(sizeof(struct item_drop),"mob.c::item_drop_ers",ERS_OPT_CLEAN);
  4683. item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"mob.c::item_drop_list_ers",ERS_OPT_NONE);
  4684.  
  4685. mob->load(minimal);
  4686.  
  4687. if (minimal)
  4688. return 0;
  4689.  
  4690. timer->add_func_list(mob->delayspawn,"mob_delayspawn");
  4691. timer->add_func_list(mob->delay_item_drop,"mob_delay_item_drop");
  4692. timer->add_func_list(mob->ai_hard,"mob_ai_hard");
  4693. timer->add_func_list(mob->ai_lazy,"mob_ai_lazy");
  4694. timer->add_func_list(mob->timer_delete,"mob_timer_delete");
  4695. timer->add_func_list(mob->spawn_guardian_sub,"mob_spawn_guardian_sub");
  4696. timer->add_func_list(mob->respawn,"mob_respawn");
  4697. timer->add_interval(timer->gettick()+MIN_MOBTHINKTIME,mob->ai_hard,0,0,MIN_MOBTHINKTIME);
  4698. timer->add_interval(timer->gettick()+MIN_MOBTHINKTIME*10,mob->ai_lazy,0,0,MIN_MOBTHINKTIME*10);
  4699.  
  4700. return 0;
  4701. }
  4702.  
  4703. void mob_destroy_mob_db(int index)
  4704. {
  4705. struct mob_db *data = mob->db_data[index];
  4706. int v;
  4707. for (v = 0; v < data->hdatac; v++ ) {
  4708. if (data->hdata[v]->flag.free ) {
  4709. aFree(data->hdata[v]->data);
  4710. }
  4711. aFree(data->hdata[v]);
  4712. }
  4713. if (data->hdata)
  4714. aFree(data->hdata);
  4715. aFree(data);
  4716. mob->db_data[index] = NULL;
  4717. }
  4718.  
  4719. /*==========================================
  4720. * Clean memory usage.
  4721. *------------------------------------------*/
  4722. int do_final_mob(void)
  4723. {
  4724. int i;
  4725. if (mob->dummy)
  4726. {
  4727. aFree(mob->dummy);
  4728. mob->dummy = NULL;
  4729. }
  4730. for (i = 0; i <= MAX_MOB_DB; i++)
  4731. {
  4732. if (mob->db_data[i] != NULL)
  4733. {
  4734. mob->destroy_mob_db(i);
  4735. }
  4736. }
  4737. for (i = 0; i <= MAX_MOB_CHAT; i++)
  4738. {
  4739. if (mob->chat_db[i] != NULL)
  4740. {
  4741. aFree(mob->chat_db[i]);
  4742. mob->chat_db[i] = NULL;
  4743. }
  4744. }
  4745. for (i = 0; i < MAX_ITEMDB; i++)
  4746. {
  4747. if (item_drop_ratio_db[i] != NULL)
  4748. {
  4749. aFree(item_drop_ratio_db[i]);
  4750. item_drop_ratio_db[i] = NULL;
  4751. }
  4752. }
  4753. ers_destroy(item_drop_ers);
  4754. ers_destroy(item_drop_list_ers);
  4755. return 0;
  4756. }
  4757.  
  4758. void mob_defaults(void) {
  4759. //Defines the Manuk/Splendide mob groups for the status reductions [Epoque]
  4760. const int mob_manuk[8] = { 1986, 1987, 1988, 1989, 1990, 1997, 1998, 1999 };
  4761. const int mob_splendide[5] = { 1991, 1992, 1993, 1994, 1995 };
  4762.  
  4763. mob = &mob_s;
  4764.  
  4765. memset(mob->db_data, 0, sizeof(mob->db_data));
  4766. mob->dummy = NULL;
  4767. memset(mob->chat_db, 0, sizeof(mob->chat_db));
  4768.  
  4769. memcpy(mob->manuk, mob_manuk, sizeof(mob->manuk));
  4770. memcpy(mob->splendide, mob_splendide, sizeof(mob->splendide));
  4771. /* */
  4772. mob->reload = mob_reload;
  4773. mob->init = do_init_mob;
  4774. mob->final = do_final_mob;
  4775. /* */
  4776. mob->db = mob_db;
  4777. mob->chat = mob_chat;
  4778. mob->makedummymobdb = mob_makedummymobdb;
  4779. mob->spawn_guardian_sub = mob_spawn_guardian_sub;
  4780. mob->skill_id2skill_idx = mob_skill_id2skill_idx;
  4781. mob->db_searchname = mobdb_searchname;
  4782. mob->db_searchname_array_sub = mobdb_searchname_array_sub;
  4783. mob->mvptomb_create = mvptomb_create;
  4784. mob->mvptomb_destroy = mvptomb_destroy;
  4785. mob->db_searchname_array = mobdb_searchname_array;
  4786. mob->db_checkid = mobdb_checkid;
  4787. mob->get_viewdata = mob_get_viewdata;
  4788. mob->parse_dataset = mob_parse_dataset;
  4789. mob->spawn_dataset = mob_spawn_dataset;
  4790. mob->get_random_id = mob_get_random_id;
  4791. mob->ksprotected = mob_ksprotected;
  4792. mob->once_spawn_sub = mob_once_spawn_sub;
  4793. mob->once_spawn = mob_once_spawn;
  4794. mob->once_spawn_area = mob_once_spawn_area;
  4795. mob->spawn_guardian = mob_spawn_guardian;
  4796. mob->spawn_bg = mob_spawn_bg;
  4797. mob->can_reach = mob_can_reach;
  4798. mob->linksearch = mob_linksearch;
  4799. mob->delayspawn = mob_delayspawn;
  4800. mob->setdelayspawn = mob_setdelayspawn;
  4801. mob->count_sub = mob_count_sub;
  4802. mob->spawn = mob_spawn;
  4803. mob->can_changetarget = mob_can_changetarget;
  4804. mob->target = mob_target;
  4805. mob->ai_sub_hard_activesearch = mob_ai_sub_hard_activesearch;
  4806. mob->ai_sub_hard_changechase = mob_ai_sub_hard_changechase;
  4807. mob->ai_sub_hard_bg_ally = mob_ai_sub_hard_bg_ally;
  4808. mob->ai_sub_hard_lootsearch = mob_ai_sub_hard_lootsearch;
  4809. mob->warpchase_sub = mob_warpchase_sub;
  4810. mob->ai_sub_hard_slavemob = mob_ai_sub_hard_slavemob;
  4811. mob->unlocktarget = mob_unlocktarget;
  4812. mob->randomwalk = mob_randomwalk;
  4813. mob->warpchase = mob_warpchase;
  4814. mob->ai_sub_hard = mob_ai_sub_hard;
  4815. mob->ai_sub_hard_timer = mob_ai_sub_hard_timer;
  4816. mob->ai_sub_foreachclient = mob_ai_sub_foreachclient;
  4817. mob->ai_sub_lazy = mob_ai_sub_lazy;
  4818. mob->ai_lazy = mob_ai_lazy;
  4819. mob->ai_hard = mob_ai_hard;
  4820. mob->setdropitem = mob_setdropitem;
  4821. mob->setlootitem = mob_setlootitem;
  4822. mob->delay_item_drop = mob_delay_item_drop;
  4823. mob->item_drop = mob_item_drop;
  4824. mob->timer_delete = mob_timer_delete;
  4825. mob->deleteslave_sub = mob_deleteslave_sub;
  4826. mob->deleteslave = mob_deleteslave;
  4827. mob->respawn = mob_respawn;
  4828. mob->log_damage = mob_log_damage;
  4829. mob->damage = mob_damage;
  4830. mob->dead = mob_dead;
  4831. mob->revive = mob_revive;
  4832. mob->guardian_guildchange = mob_guardian_guildchange;
  4833. mob->random_class = mob_random_class;
  4834. mob->class_change = mob_class_change;
  4835. mob->heal = mob_heal;
  4836. mob->warpslave_sub = mob_warpslave_sub;
  4837. mob->warpslave = mob_warpslave;
  4838. mob->countslave_sub = mob_countslave_sub;
  4839. mob->countslave = mob_countslave;
  4840. mob->summonslave = mob_summonslave;
  4841. mob->getfriendhprate_sub = mob_getfriendhprate_sub;
  4842. mob->getfriendhprate = mob_getfriendhprate;
  4843. mob->getmasterhpltmaxrate = mob_getmasterhpltmaxrate;
  4844. mob->getfriendstatus_sub = mob_getfriendstatus_sub;
  4845. mob->getfriendstatus = mob_getfriendstatus;
  4846. mob->skill_use = mobskill_use;
  4847. mob->skill_event = mobskill_event;
  4848. mob->is_clone = mob_is_clone;
  4849. mob->clone_spawn = mob_clone_spawn;
  4850. mob->clone_delete = mob_clone_delete;
  4851. mob->drop_adjust = mob_drop_adjust;
  4852. mob->item_dropratio_adjust = item_dropratio_adjust;
  4853. mob->parse_dbrow = mob_parse_dbrow;
  4854. mob->readdb_sub = mob_readdb_sub;
  4855. mob->readdb = mob_readdb;
  4856. mob->read_sqldb = mob_read_sqldb;
  4857. mob->name_constants = mob_name_constants;
  4858. mob->readdb_mobavail = mob_readdb_mobavail;
  4859. mob->read_randommonster = mob_read_randommonster;
  4860. mob->parse_row_chatdb = mob_parse_row_chatdb;
  4861. mob->readchatdb = mob_readchatdb;
  4862. mob->parse_row_mobskilldb = mob_parse_row_mobskilldb;
  4863. mob->readskilldb = mob_readskilldb;
  4864. mob->read_sqlskilldb = mob_read_sqlskilldb;
  4865. mob->readdb_race2 = mob_readdb_race2;
  4866. mob->readdb_itemratio = mob_readdb_itemratio;
  4867. mob->load = mob_load;
  4868. mob->clear_spawninfo = mob_clear_spawninfo;
  4869. mob->destroy_mob_db = mob_destroy_mob_db;
  4870. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement