From 3f4ee61f6a7bd199c2ca8724d87017410b3d3319 Mon Sep 17 00:00:00 2001 From: Neerti Date: Fri, 7 Dec 2018 12:43:59 -0500 Subject: [PATCH] Fixes a few AI bugs --- code/modules/ai/ai_holder_combat.dm | 308 ++++++++++++++++++ .../subtypes/animal/giant_spider/nurse.dm | 235 +++++++++++++ 2 files changed, 543 insertions(+) create mode 100644 code/modules/ai/ai_holder_combat.dm create mode 100644 code/modules/mob/living/simple_mob/subtypes/animal/giant_spider/nurse.dm diff --git a/code/modules/ai/ai_holder_combat.dm b/code/modules/ai/ai_holder_combat.dm new file mode 100644 index 0000000000..3d5e5c10aa --- /dev/null +++ b/code/modules/ai/ai_holder_combat.dm @@ -0,0 +1,308 @@ +// This file is for actual fighting. Targeting is in a seperate file. + +/datum/ai_holder + var/firing_lanes = TRUE // If ture, tries to refrain from shooting allies or the wall. + var/conserve_ammo = FALSE // If true, the mob will avoid shooting anything that does not have a chance to hit a mob. Requires firing_lanes to be true. + var/pointblank = FALSE // If ranged is true, and this is true, people adjacent to the mob will suffer the ranged instead of using a melee attack. + + var/can_breakthrough = TRUE // If false, the AI will not try to open a path to its goal, like opening doors. + var/violent_breakthrough = TRUE // If false, the AI is not allowed to destroy things like windows or other structures in the way. Requires above var to be true. + + var/stand_ground = FALSE // If true, the AI won't try to get closer to an enemy if out of range. + + +// This does the actual attacking. +/datum/ai_holder/proc/engage_target() + ai_log("engage_target() : Entering.", AI_LOG_DEBUG) + + // Can we still see them? +// if(!target || !can_attack(target) || (!(target in list_targets())) ) + if(!target || !can_attack(target)) + ai_log("engage_target() : Lost sight of target.", AI_LOG_TRACE) + lose_target() // We lost them. + + if(!find_target()) // If we can't get a new one, then wait for a bit and then time out. + set_stance(STANCE_IDLE) + lost_target() + ai_log("engage_target() : No more targets. Exiting.", AI_LOG_DEBUG) + return + // if(lose_target_time + lose_target_timeout < world.time) + // ai_log("engage_target() : Unseen enemy timed out.", AI_LOG_TRACE) + // set_stance(STANCE_IDLE) // It must've been the wind. + // lost_target() + // ai_log("engage_target() : Exiting.", AI_LOG_DEBUG) + // return + + // // But maybe we do one last ditch effort. + // if(!target_last_seen_turf || intelligence_level < AI_SMART) + // ai_log("engage_target() : No last known position or is too dumb to fight unseen enemies.", AI_LOG_TRACE) + // set_stance(STANCE_IDLE) + // else + // ai_log("engage_target() : Fighting unseen enemy.", AI_LOG_TRACE) + // engage_unseen_enemy() + else + ai_log("engage_target() : Got new target ([target]).", AI_LOG_TRACE) + + var/distance = get_dist(holder, target) + ai_log("engage_target() : Distance to target ([target]) is [distance].", AI_LOG_TRACE) + holder.face_atom(target) + last_conflict_time = world.time + + request_help() // Call our allies. + + // Do a 'special' attack, if one is allowed. +// if(prob(special_attack_prob) && (distance >= special_attack_min_range) && (distance <= special_attack_max_range)) + if(holder.ICheckSpecialAttack(target)) + ai_log("engage_target() : Attempting a special attack.", AI_LOG_TRACE) + on_engagement(target) + if(special_attack(target)) // If this fails, then we try a regular melee/ranged attack. + ai_log("engage_target() : Successful special attack. Exiting.", AI_LOG_DEBUG) + return + + // Stab them. + else if(distance <= 1 && !pointblank) + ai_log("engage_target() : Attempting a melee attack.", AI_LOG_TRACE) + on_engagement(target) + melee_attack(target) + + // Shoot them. + else if(holder.ICheckRangedAttack(target) && (distance <= max_range(target)) ) + on_engagement(target) + if(firing_lanes && !test_projectile_safety(target)) + // Nudge them a bit, maybe they can shoot next time. + step_rand(holder) + holder.face_atom(target) + ai_log("engage_target() : Could not safely fire at target. Exiting.", AI_LOG_DEBUG) + return + + ai_log("engage_target() : Attempting a ranged attack.", AI_LOG_TRACE) + ranged_attack(target) + + // Run after them. + else if(!stand_ground) + ai_log("engage_target() : Target ([target]) too far away. Exiting.", AI_LOG_DEBUG) + set_stance(STANCE_APPROACH) + +// We're not entirely sure how holder will do melee attacks since any /mob/living could be holder, but we don't have to care because Interfaces. +/datum/ai_holder/proc/melee_attack(atom/A) + pre_melee_attack(A) + . = holder.IAttack(A) + if(.) + post_melee_attack(A) + +// Ditto. +/datum/ai_holder/proc/ranged_attack(atom/A) + pre_ranged_attack(A) + . = holder.IRangedAttack(A) + if(.) + post_ranged_attack(A) + +// Most mobs probably won't have this defined but we don't care. +/datum/ai_holder/proc/special_attack(atom/movable/AM) + pre_special_attack(AM) + . = holder.ISpecialAttack(AM) + if(.) + post_special_attack(AM) + +// Called when within striking/shooting distance, however cooldown is not considered. +// Override to do things like move in a random step for evasiveness. +// Note that this is called BEFORE the attack. +/datum/ai_holder/proc/on_engagement(atom/A) + +// Called before a ranged attack is attempted. +/datum/ai_holder/proc/pre_ranged_attack(atom/A) + +// Called before a melee attack is attempted. +/datum/ai_holder/proc/pre_melee_attack(atom/A) + +// Called before a 'special' attack is attempted. +/datum/ai_holder/proc/pre_special_attack(atom/A) + +// Called after a successful (IE not on cooldown) ranged attack. +// Note that this is not whether the projectile actually hit, just that one was launched. +/datum/ai_holder/proc/post_ranged_attack(atom/A) + +// Ditto but for melee. +/datum/ai_holder/proc/post_melee_attack(atom/A) + +// And one more for special snowflake attacks. +/datum/ai_holder/proc/post_special_attack(atom/A) + +// Used to make sure projectiles will probably hit the target and not the wall or a friend. +/datum/ai_holder/proc/test_projectile_safety(atom/movable/AM) + var/mob/living/L = check_trajectory(AM, holder) // This isn't always reliable but its better than the previous method. +// world << "Checked trajectory, would hit [L]." + + if(istype(L)) // Did we hit a mob? +// world << "Hit [L]." + if(holder.IIsAlly(L)) +// world << "Would hit ally, canceling." + return FALSE // We would hit a friend! +// world << "Won't threaten ally, firing." + return TRUE // Otherwise we don't care, even if its not the intended target. + else + if(!isliving(AM)) // If the original target was an object, then let it happen if it doesn't threaten an ally. +// world << "Targeting object, ignoring and firing." + return TRUE +// world << "Not sure." + + return !conserve_ammo // If we have infinite ammo than shooting the wall isn't so bad, but otherwise lets not. + +// Test if we are within range to attempt an attack, melee or ranged. +/datum/ai_holder/proc/within_range(atom/movable/AM) + var/distance = get_dist(holder, AM) + if(distance <= 1) + return TRUE // Can melee. + else if(holder.ICheckRangedAttack(AM) && distance <= max_range(AM)) + return TRUE // Can shoot. + return FALSE + +// Determines how close the AI will move to its target. +/datum/ai_holder/proc/closest_distance(atom/movable/AM) + return max(max_range(AM) - 1, 1) // Max range -1 just because we don't want to constantly get kited + +// Can be used to conditionally do a ranged or melee attack. +/datum/ai_holder/proc/max_range(atom/movable/AM) + return holder.ICheckRangedAttack(AM) ? 7 : 1 + +// Goes to the target, to attack them. +// Called when in STANCE_APPROACH. +/datum/ai_holder/proc/walk_to_target() + ai_log("walk_to_target() : Entering.", AI_LOG_DEBUG) + // Make sure we can still chase/attack them. + if(!target || !can_attack(target)) + ai_log("walk_to_target() : Lost target.", AI_LOG_INFO) + if(!find_target()) + lost_target() + ai_log("walk_to_target() : Exiting.", AI_LOG_DEBUG) + return + else + ai_log("walk_to_target() : Found new target ([target]).", AI_LOG_INFO) + + // Find out where we're going. + var/get_to = closest_distance(target) + var/distance = get_dist(holder, target) + ai_log("walk_to_target() : get_to is [get_to].", AI_LOG_TRACE) + + // We're here! + // Special case: Our holder has a special attack that is ranged, but normally the holder uses melee. + // If that happens, we'll switch to STANCE_FIGHT so they can use it. If the special attack is limited, they'll likely switch back next tick. + if(distance <= get_to || holder.ICheckSpecialAttack(target)) + ai_log("walk_to_target() : Within range.", AI_LOG_INFO) + forget_path() + set_stance(STANCE_FIGHT) + ai_log("walk_to_target() : Exiting.", AI_LOG_DEBUG) + return + + + // Otherwise keep walking. + if(!stand_ground) + walk_path(target, get_to) + + ai_log("walk_to_target() : Exiting.", AI_LOG_DEBUG) + +// Resists out of things. +// Sometimes there are times you want your mob to be buckled to something, so override this for when that is needed. +/datum/ai_holder/proc/handle_resist() + holder.resist() + +// Used to break through windows and barriers to a target on the other side. +// This does two passes, so that if its just a public access door, the windows nearby don't need to be smashed. +/datum/ai_holder/proc/breakthrough(atom/target_atom) + ai_log("breakthrough() : Entering", AI_LOG_TRACE) + + if(!can_breakthrough) + ai_log("breakthrough() : Not allowed to breakthrough. Exiting.", AI_LOG_TRACE) + return FALSE + + if(!isturf(holder.loc)) + ai_log("breakthrough() : Trapped inside \the [holder.loc]. Exiting.", AI_LOG_TRACE) + return FALSE + + var/dir_to_target = get_dir(holder, target_atom) + holder.face_atom(target_atom) + ai_log("breakthrough() : Exiting", AI_LOG_DEBUG) + + // Sometimes the mob will try to hit something diagonally, and generally this fails. + // So instead we will try two more times with some adjustments if the attack fails. + var/list/directions_to_try = list( + dir_to_target, + turn(dir_to_target, 45), + turn(dir_to_target, -45) + ) + + ai_log("breakthrough() : Starting peaceful pass.", AI_LOG_DEBUG) + + var/result = FALSE + + // First, we will try to peacefully make a path, I.E opening a door we have access to. + for(var/direction in directions_to_try) + result = destroy_surroundings(direction, violent = FALSE) + if(result) + break + + // Alright, lets smash some shit instead, if it didn't work and we're allowed to be violent. + if(!result && can_violently_breakthrough()) + ai_log("breakthrough() : Starting violent pass.", AI_LOG_DEBUG) + for(var/direction in directions_to_try) + result = destroy_surroundings(direction, violent = TRUE) + if(result) + break + + ai_log("breakthrough() : Exiting with [result].", AI_LOG_TRACE) + return result + +// Despite the name, this can also be used to help clear a path without any destruction. +/datum/ai_holder/proc/destroy_surroundings(direction, violent = TRUE) + ai_log("destroy_surroundings() : Entering.", AI_LOG_TRACE) + if(!direction) + direction = pick(cardinal) // FLAIL WILDLY + ai_log("destroy_surroundings() : No direction given, picked [direction] randomly.", AI_LOG_DEBUG) + + var/turf/problem_turf = get_step(holder, direction) + + // First, give peace a chance. + if(!violent) + ai_log("destroy_surroundings() : Going to try to peacefully clear [problem_turf].", AI_LOG_DEBUG) + for(var/obj/machinery/door/D in problem_turf) + if(D.density && holder.Adjacent(D) && D.allowed(holder) && D.operable()) + // First, try to open the door if possible without smashing it. We might have access. + ai_log("destroy_surroundings() : Opening closed door.", AI_LOG_INFO) + return D.open() + + // Peace has failed us, can we just smash the things in the way? + else + ai_log("destroy_surroundings() : Going to try to violently clear [problem_turf].", AI_LOG_DEBUG) + // First, kill windows in the way. + for(var/obj/structure/window/W in problem_turf) + if(W.dir == reverse_dir[holder.dir]) // So that windows get smashed in the right order + ai_log("destroy_surroundings() : Attacking side window.", AI_LOG_INFO) + return holder.IAttack(W) + + else if(W.is_fulltile()) + ai_log("destroy_surroundings() : Attacking full tile window.", AI_LOG_INFO) + return holder.IAttack(W) + + // Kill hull shields in the way. + for(var/obj/effect/energy_field/shield in problem_turf) + if(shield.density) // Don't attack shields that are already down. + ai_log("destroy_surroundings() : Attacking hull shield.", AI_LOG_INFO) + return holder.IAttack(shield) + + // Kill common obstacle in the way like tables. + var/obj/structure/obstacle = locate(/obj/structure, problem_turf) + if(istype(obstacle, /obj/structure/window) || istype(obstacle, /obj/structure/closet) || istype(obstacle, /obj/structure/table) || istype(obstacle, /obj/structure/grille)) + ai_log("destroy_surroundings() : Attacking generic structure.", AI_LOG_INFO) + return holder.IAttack(obstacle) + + for(var/obj/machinery/door/D in problem_turf) // Required since firelocks take up the same turf. + if(D.density) + ai_log("destroy_surroundings() : Attacking closed door.", AI_LOG_INFO) + return holder.IAttack(D) + + ai_log("destroy_surroundings() : Exiting due to nothing to attack.", AI_LOG_INFO) + return FALSE // Nothing to attack. + +// Override for special behaviour. +/datum/ai_holder/proc/can_violently_breakthrough() + return violent_breakthrough \ No newline at end of file diff --git a/code/modules/mob/living/simple_mob/subtypes/animal/giant_spider/nurse.dm b/code/modules/mob/living/simple_mob/subtypes/animal/giant_spider/nurse.dm new file mode 100644 index 0000000000..1a4e9699fb --- /dev/null +++ b/code/modules/mob/living/simple_mob/subtypes/animal/giant_spider/nurse.dm @@ -0,0 +1,235 @@ +// Nurses, they create webs and eggs. +// They're fragile but their attacks can cause horrifying consequences. +/mob/living/simple_mob/animal/giant_spider/nurse + desc = "Furry and beige, it makes you shudder to look at it. This one has brilliant green eyes." + icon_state = "nurse" + icon_living = "nurse" + icon_dead = "nurse_dead" + + maxHealth = 40 + health = 40 + + movement_cooldown = 5 // A bit faster so that they can inject the eggs easier. + + melee_damage_lower = 5 // Doesn't do a lot of damage, since the goal is to make more spiders with egg attacks. + melee_damage_upper = 10 + poison_per_bite = 5 + poison_type = "stoxin" + + player_msg = "You can spin webs on an adjacent tile, or cocoon an object by clicking on it.
\ + You can also cocoon a dying or dead entity by clicking on them, and you will gain charges for egg-laying.
\ + To lay eggs, click a nearby tile. Laying eggs will deplete a charge." + ai_holder_type = /datum/ai_holder/simple_mob/melee/nurse_spider + + var/fed = 0 // Counter for how many egg laying 'charges' the spider has. + var/laying_eggs = FALSE // Only allow one set of eggs to be laid at once. + var/egg_inject_chance = 25 // One in four chance to get eggs. + var/egg_type = /obj/effect/spider/eggcluster/small + var/web_type = /obj/effect/spider/stickyweb/dark + + +/mob/living/simple_mob/animal/giant_spider/nurse/inject_poison(mob/living/L, target_zone) + ..() // Inject the stoxin here. + if(ishuman(L) && prob(egg_inject_chance)) + var/mob/living/carbon/human/H = L + var/obj/item/organ/external/O = H.get_organ(target_zone) + if(O) + var/eggcount = 0 + for(var/obj/effect/spider/eggcluster/E in O.implants) + eggcount++ + if(!eggcount) + var/eggs = new egg_type(O, src) + O.implants += eggs + to_chat(H, span("critical", "\The [src] injects something into your [O.name]!") ) // Oh god its laying eggs in me! + +// Webs target in a web if able to. +/mob/living/simple_mob/animal/giant_spider/nurse/attack_target(atom/A) + if(isturf(A)) + if(fed) + if(!laying_eggs) + return lay_eggs(A) + return web_tile(A) + + if(isliving(A)) + var/mob/living/L = A + if(!L.stat) + return ..() + + if(!istype(A, /atom/movable)) + return + var/atom/movable/AM = A + + if(AM.anchored) + return ..() + + return spin_cocoon(AM) + +/mob/living/simple_mob/animal/giant_spider/nurse/proc/spin_cocoon(atom/movable/AM) + if(!istype(AM)) + return FALSE // We can't cocoon walls sadly. + visible_message(span("notice", "\The [src] begins to secrete a sticky substance around \the [AM].") ) + + // Get our AI to stay still. + set_AI_busy(TRUE) + + if(!do_mob(src, AM, 5 SECONDS)) + set_AI_busy(FALSE) + to_chat(src, span("warning", "You need to stay still to spin a web around \the [AM].")) + return FALSE + + set_AI_busy(FALSE) + + if(!AM) // Make sure it didn't get deleted for whatever reason. + to_chat(src, span("warning", "Whatever you were spinning a web for, its no longer there...")) + return FALSE + + if(!isturf(AM.loc)) + to_chat(src, span("warning", "You can't spin \the [AM] in a web while it is inside \the [AM.loc].")) + return FALSE + + if(!Adjacent(AM)) + to_chat(src, span("warning", "You need to be next to \the [AM] to spin it into a web.")) + return FALSE + + // Finally done with the checks. + var/obj/effect/spider/cocoon/C = new(AM.loc) + var/large_cocoon = FALSE + for(var/mob/living/L in C.loc) + if(istype(L, /mob/living/simple_mob/animal/giant_spider)) // Cannibalism is bad. + continue + fed++ + visible_message(span("warning","\The [src] sticks a proboscis into \the [L], and sucks a viscous substance out.")) + to_chat(src, span("notice", "You've fed upon \the [L], and can now lay [fed] cluster\s of eggs.")) + L.forceMove(C) + large_cocoon = TRUE + break + + // This part's pretty stupid. + for(var/obj/O in C.loc) + if(!O.anchored) + O.forceMove(C) + + // Todo: Put this code on the cocoon object itself? + if(large_cocoon) + C.icon_state = pick("cocoon_large1","cocoon_large2","cocoon_large3") + + return TRUE + +/mob/living/simple_mob/animal/giant_spider/nurse/handle_special() + set waitfor = FALSE + if(get_AI_stance() == STANCE_IDLE && !is_AI_busy() && isturf(loc)) + if(fed) + lay_eggs(loc) + else + web_tile(loc) + +/mob/living/simple_mob/animal/giant_spider/nurse/proc/web_tile(turf/T) + if(!istype(T)) + return FALSE + + var/obj/effect/spider/stickyweb/W = locate() in T + if(W) + return FALSE // Already got webs here. + + visible_message(span("notice", "\The [src] begins to secrete a sticky substance.") ) + // Get our AI to stay still. + set_AI_busy(TRUE) + + if(!do_mob(src, T, 5 SECONDS)) + set_AI_busy(FALSE) + to_chat(src, span("warning", "You need to stay still to spin a web on \the [T].")) + return FALSE + + W = locate() in T + if(W) + return FALSE // Spamclick protection. + + set_AI_busy(FALSE) + new web_type(T) + return TRUE + + +/mob/living/simple_mob/animal/giant_spider/nurse/proc/lay_eggs(turf/T) + if(!istype(T)) + return FALSE + + if(!fed) + return FALSE + + var/obj/effect/spider/eggcluster/E = locate() in T + if(E) + return FALSE // Already got eggs here. + + visible_message(span("notice", "\The [src] begins to lay a cluster of eggs.") ) + // Get our AI to stay still. + set_AI_busy(TRUE) + // Stop players from spamming eggs. + laying_eggs = TRUE + + if(!do_mob(src, T, 5 SECONDS)) + set_AI_busy(FALSE) + to_chat(src, span("warning", "You need to stay still to lay eggs on \the [T].")) + return FALSE + + E = locate() in T + if(E) + return FALSE // Spamclick protection. + + set_AI_busy(FALSE) + new egg_type(T) + fed-- + laying_eggs = FALSE + return TRUE + + +// Variant that 'blocks' light (by being a negative light source). +// This is done to make webbed rooms scary and allow for spiders on the other side of webs to see prey. +/obj/effect/spider/stickyweb/dark + name = "dense web" + desc = "It's sticky, and blocks a lot of light." + light_color = "#FFFFFF" + light_range = 2 + light_power = -3 + +// This is still stupid, but whatever. +/mob/living/simple_mob/animal/giant_spider/nurse/hat + desc = "Furry and beige, it makes you shudder to look at it. This one has brilliant green eyes and a tiny nurse hat." + icon_state = "nursemed" + icon_living = "nursemed" + icon_dead = "nursemed_dead" + + +// The AI for nurse spiders. Wraps things in webs by 'attacking' them. +/datum/ai_holder/simple_mob/melee/nurse_spider + wander = TRUE + base_wander_delay = 8 + cooperative = FALSE // So we don't ask our spider friends to attack things we're webbing. This might also make them stay at the base if their friends find tasty explorers. + +// Get us unachored objects as an option as well. +/datum/ai_holder/simple_mob/melee/nurse_spider/list_targets() + . = ..() + + var/static/alternative_targets = typecacheof(list(/obj/item, /obj/structure)) + + for(var/AT in typecache_filter_list(range(vision_range, holder), alternative_targets)) + var/obj/O = AT + if(can_see(holder, O, vision_range) && !O.anchored) + . += O + +// Select an obj if no mobs are around. +/datum/ai_holder/melee/nurse_spider/pick_target(list/targets) + var/mobs_only = locate(/mob/living) in targets // If a mob is in the list of targets, then ignore objects. + if(mobs_only) + for(var/A in targets) + if(!isliving(A)) + targets -= A + + return ..(targets) + +/datum/ai_holder/simple_mob/melee/nurse_spider/can_attack(atom/movable/the_target) + . = ..() + if(!.) // Parent returned FALSE. + if(istype(the_target, /obj)) + var/obj/O = the_target + if(!O.anchored) + return TRUE \ No newline at end of file