static int elemental_ai_sub_timer_activesearch(struct block_list *bl, va_list ap) { struct elemental_data *ed; struct block_list **target; int dist; nullpo_ret(bl); ed = va_arg(ap,struct elemental_data *); target = va_arg(ap,struct block_list**); //If can't seek yet, not an enemy, or you can't attack it, skip. if( (*target) == bl || !status_check_skilluse(&ed->bl, bl, 0, 0) ) return 0; if( battle_check_target(&ed->bl,bl,BCT_ENEMY) <= 0 ) return 0; switch( bl->type ) { case BL_PC: if( !map_flag_vs(ed->bl.m) ) return 0; default: dist = distance_bl(&ed->bl, bl); if( ((*target) == NULL || !check_distance_bl(&ed->bl, *target, dist)) && battle_check_range(&ed->bl,bl,ed->db->range2) ) { //Pick closest target? (*target) = bl; ed->target_id = bl->id; ed->min_chase = dist + ed->db->range3; if( ed->min_chase > AREA_SIZE ) ed->min_chase = AREA_SIZE; return 1; } break; } return 0; }
static int elemental_ai_sub_timer(struct elemental_data *ed, struct map_session_data *sd, unsigned int tick) { struct block_list *target = NULL; int master_dist, view_range, mode; nullpo_ret(ed); nullpo_ret(sd); if( ed->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL ) return 0; // Check if caster can sustain the summoned elemental if( DIFF_TICK(tick,ed->last_spdrain_time) >= 10000 ){// Drain SP every 10 seconds int sp = 5; switch(ed->vd->class_){ case ELEMENTALID_AGNI_M: case ELEMENTALID_AQUA_M: case ELEMENTALID_VENTUS_M: case ELEMENTALID_TERA_M: sp = 8; break; case ELEMENTALID_AGNI_L: case ELEMENTALID_AQUA_L: case ELEMENTALID_VENTUS_L: case ELEMENTALID_TERA_L: sp = 11; break; } if( status_get_sp(&sd->bl) < sp ){ // Can't sustain delete it. elemental_delete(sd->ed,0); return 0; } status_zap(&sd->bl,0,sp); ed->last_spdrain_time = tick; } if( DIFF_TICK(tick,ed->last_thinktime) < MIN_ELETHINKTIME ) return 0; ed->last_thinktime = tick; if( ed->ud.skilltimer != INVALID_TIMER ) return 0; if( ed->ud.walktimer != INVALID_TIMER && ed->ud.walkpath.path_pos <= 2 ) return 0; //No thinking when you just started to walk. if(ed->ud.walkpath.path_pos < ed->ud.walkpath.path_len && ed->ud.target == sd->bl.id) return 0; //No thinking until be near the master. if( ed->sc.count && ed->sc.data[SC_BLIND] ) view_range = 3; else view_range = ed->db->range2; mode = status_get_mode(&ed->bl); master_dist = distance_bl(&sd->bl, &ed->bl); if( master_dist > AREA_SIZE ) { // Master out of vision range. elemental_unlocktarget(ed); unit_warp(&ed->bl,sd->bl.m,sd->bl.x,sd->bl.y,CLR_TELEPORT); clif_elemental_updatestatus(sd,SP_HP); clif_elemental_updatestatus(sd,SP_SP); return 0; } else if( master_dist > MAX_ELEDISTANCE ) { // Master too far, chase. short x = sd->bl.x, y = sd->bl.y; if( ed->target_id ) elemental_unlocktarget(ed); if( ed->ud.walktimer != INVALID_TIMER && ed->ud.target == sd->bl.id ) return 0; //Already walking to him if( DIFF_TICK(tick, ed->ud.canmove_tick) < 0 ) return 0; //Can't move yet. if( map_search_freecell(&ed->bl, sd->bl.m, &x, &y, MIN_ELEDISTANCE, MIN_ELEDISTANCE, 1) && unit_walktoxy(&ed->bl, x, y, 0) ) return 0; } if( mode == EL_MODE_AGGRESSIVE ) { target = map_id2bl(ed->ud.target); if( !target ) map_foreachinrange(elemental_ai_sub_timer_activesearch, &ed->bl, view_range, BL_CHAR, ed, &target, status_get_mode(&ed->bl)); if( !target ) { //No targets available. elemental_unlocktarget(ed); return 1; } if( battle_check_range(&ed->bl,target,view_range) && rnd()%100 < 2 ) { // 2% chance to cast attack skill. if( elemental_action(ed,target,tick) ) return 1; } //Attempt to attack. //At this point we know the target is attackable, we just gotta check if the range matches. if( ed->ud.target == target->id && ed->ud.attacktimer != INVALID_TIMER ) //Already locked. return 1; if( battle_check_range(&ed->bl, target, ed->base_status.rhw.range) ) {//Target within range, engage unit_attack(&ed->bl,target->id,1); return 1; } //Follow up if possible. if( !unit_walktobl(&ed->bl, target, ed->base_status.rhw.range, 2) ) elemental_unlocktarget(ed); } return 0; }
static int elemental_ai_sub_timer(struct elemental_data *ed, struct map_session_data *sd, unsigned int tick) { struct block_list *target = NULL; int master_dist, view_range, mode; nullpo_ret(ed); nullpo_ret(sd); if( ed->bl.prev == NULL || sd == NULL || sd->bl.prev == NULL ) return 0; if( DIFF_TICK(tick,ed->last_thinktime) < MIN_ELETHINKTIME ) return 0; ed->last_thinktime = tick; if( ed->ud.skilltimer != -1 ) return 0; if( ed->ud.walktimer != -1 && ed->ud.walkpath.path_pos <= 2 ) return 0; //No thinking when you just started to walk. if(ed->ud.walkpath.path_pos < ed->ud.walkpath.path_len && ed->ud.target == sd->bl.id) return 0; //No thinking until be near the master. if( ed->sc.count && ed->sc.data[SC_BLIND] ) view_range = 3; else view_range = ed->db->range2; mode = status_get_mode(&ed->bl); master_dist = distance_bl(&sd->bl, &ed->bl); if( master_dist > AREA_SIZE ) { // Master out of vision range. elemental_unlocktarget(ed); unit_warp(&ed->bl,sd->bl.m,sd->bl.x,sd->bl.y,3); return 0; } else if( master_dist > MAX_ELEDISTANCE ) { // Master too far, chase. short x = sd->bl.x, y = sd->bl.y; if( ed->target_id ) elemental_unlocktarget(ed); if( ed->ud.walktimer != -1 && ed->ud.target == sd->bl.id ) return 0; //Already walking to him if( DIFF_TICK(tick, ed->ud.canmove_tick) < 0 ) return 0; //Can't move yet. if( map_search_freecell(&ed->bl, sd->bl.m, &x, &y, MIN_ELEDISTANCE, MIN_ELEDISTANCE, 1) && unit_walktoxy(&ed->bl, x, y, 0) ) return 0; } if( mode == EL_MODE_AGGRESSIVE ) { target = map_id2bl(ed->ud.target); if( !target ) map_foreachinrange(elemental_ai_sub_timer_activesearch, &ed->bl, ed->db->range2, BL_CHAR, ed, &target, status_get_mode(&ed->bl)); if( !target ) { //No targets available. elemental_unlocktarget(ed); return 1; } if( battle_check_range(&ed->bl,target,ed->db->range2) && rand()%100 < 2 ) // 2% chance to cast attack skill. { if( elemental_action(ed,target,tick) ) return 1; } //Attempt to attack. //At this point we know the target is attackable, we just gotta check if the range matches. if( ed->ud.target == target->id && ed->ud.attacktimer != -1 ) //Already locked. return 1; if( battle_check_range(&ed->bl, target, ed->base_status.rhw.range) ) { //Target within range, engage unit_attack(&ed->bl,target->id,1); return 1; } //Follow up if possible. if( !unit_walktobl(&ed->bl, target, ed->base_status.rhw.range, 2) ) elemental_unlocktarget(ed); } return 0; }
int elemental_action(struct elemental_data *ed, struct block_list *bl, unsigned int tick) { struct skill_condition req; uint16 skill_id, skill_lv; int i; nullpo_ret(ed); nullpo_ret(bl); if( !ed->master ) return 0; if( ed->target_id ) elemental_unlocktarget(ed); // Remove previous target. ARR_FIND(0, MAX_ELESKILLTREE, i, ed->db->skill[i].id && (ed->db->skill[i].mode&EL_SKILLMODE_AGGRESSIVE)); if( i == MAX_ELESKILLTREE ) return 0; skill_id = ed->db->skill[i].id; skill_lv = ed->db->skill[i].lv; if( elemental_skillnotok(skill_id, ed) ) return 0; if( ed->ud.skilltimer != INVALID_TIMER ) return 0; else if( DIFF_TICK(tick, ed->ud.canact_tick) < 0 ) return 0; ed->target_id = ed->ud.skilltarget = bl->id; // Set new target ed->last_thinktime = tick; // Not in skill range. if( !battle_check_range(&ed->bl,bl,skill_get_range(skill_id,skill_lv)) ) { // Try to walk to the target. if( !unit_walktobl(&ed->bl, bl, skill_get_range(skill_id,skill_lv), 2) ) elemental_unlocktarget(ed); else { // Walking, waiting to be in range. Client don't handle it, then we must handle it here. int walk_dist = distance_bl(&ed->bl,bl) - skill_get_range(skill_id,skill_lv); ed->ud.skill_id = skill_id; ed->ud.skill_lv = skill_lv; if( skill_get_inf(skill_id) & INF_GROUND_SKILL ) ed->ud.skilltimer = add_timer( tick+status_get_speed(&ed->bl)*walk_dist, skill_castend_pos, ed->bl.id, 0 ); else ed->ud.skilltimer = add_timer( tick+status_get_speed(&ed->bl)*walk_dist, skill_castend_id, ed->bl.id, 0 ); } return 1; } req = elemental_skill_get_requirements(skill_id, skill_lv); if(req.hp || req.sp){ struct map_session_data *sd = BL_CAST(BL_PC, battle_get_master(&ed->bl)); if( sd ){ if( sd->skill_id_old != SO_EL_ACTION && //regardless of remaining HP/SP it can be cast (status_get_hp(&ed->bl) < req.hp || status_get_sp(&ed->bl) < req.sp) ) return 1; else status_zap(&ed->bl, req.hp, req.sp); } } //Otherwise, just cast the skill. if( skill_get_inf(skill_id) & INF_GROUND_SKILL ) unit_skilluse_pos(&ed->bl, bl->x, bl->y, skill_id, skill_lv); else unit_skilluse_id(&ed->bl, bl->id, skill_id, skill_lv); // Reset target. ed->target_id = 0; return 1; }
int elemental_action(struct elemental_data *ed, struct block_list *bl, unsigned int tick) { short skillnum, skilllv; int i; nullpo_ret(ed); nullpo_ret(bl); if( !ed->master ) return 0; if( ed->target_id ) elemental_unlocktarget(ed); // Remove previous target. ARR_FIND(0, MAX_ELESKILLTREE, i, ed->db->skill[i].id && (ed->db->skill[i].mode&EL_SKILLMODE_AGGRESSIVE)); if( i == MAX_ELESKILLTREE ) return 0; skillnum = ed->db->skill[i].id; skilllv = ed->db->skill[i].lv; if( elemental_skillnotok(skillnum, ed) ) return 0; if( ed->ud.skilltimer != -1 ) return 0; else if( DIFF_TICK(tick, ed->ud.canact_tick) < 0 ) return 0; ed->target_id = ed->ud.skilltarget = bl->id; // Set new target ed->last_thinktime = tick; // Not in skill range. if( !battle_check_range(&ed->bl,bl,skill_get_range(skillnum,skilllv)) ) { // Try to walk to the target. if( !unit_walktobl(&ed->bl, bl, skill_get_range(skillnum,skilllv), 2) ) elemental_unlocktarget(ed); else { // Walking, waiting to be in range. Client don't handle it, then we must handle it here. int walk_dist = distance_bl(&ed->bl,bl) - skill_get_range(skillnum,skilllv); ed->ud.skillid = skillnum; ed->ud.skilllv = skilllv; if( skill_get_inf(skillnum) & INF_GROUND_SKILL ) ed->ud.skilltimer = add_timer( tick+status_get_speed(&ed->bl)*walk_dist, skill_castend_pos, ed->bl.id, 0 ); else ed->ud.skilltimer = add_timer( tick+status_get_speed(&ed->bl)*walk_dist, skill_castend_id, ed->bl.id, 0 ); } return 1; } //Otherwise, just cast the skill. if( skill_get_inf(skillnum) & INF_GROUND_SKILL ) unit_skilluse_pos(&ed->bl, bl->x, bl->y, skillnum, skilllv); else unit_skilluse_id(&ed->bl, bl->id, skillnum, skilllv); // Reset target. ed->target_id = 0; return 1; }