internal static Output Unequip(EcsRegistrar rgs, long unequipperId, long gearId) { var output = new Output { Category = "Text" }; var unequipperEntityName = rgs.GetPartSingle <Parts.EntityName>(unequipperId); var unequipperAnatomy = rgs.GetPartSingle <Parts.Anatomy>(unequipperId); var gearEntityName = rgs.GetPartSingle <Parts.EntityName>(gearId); bool unequipped = false; foreach (var slot in unequipperAnatomy.SlotsEquipped) { if (slot.Value == gearId) { unequipperAnatomy.SlotsEquipped[slot.Key] = 0L; unequipped = true; } } output.Data = unequipped ? output.Data = $"{unequipperEntityName.ProperName} removes the {gearEntityName.GeneralName}." : output.Data = $"{unequipperEntityName.ProperName} doesn't have the {gearEntityName.GeneralName} equipped and can't remove it."; return(output); }
private static ActionChosen AICombatActionMeleeOnly(EcsRegistrar rgs, long globalId, long attackerId, List <long> battlefieldEntityIds) { var action = new ActionChosen { Action = Vals.CombatAction.DoNothing, AgentId = attackerId }; var naturalWeaponsMap = rgs.GetPartSingle <Parts.NaturalWeaponsMap>(globalId); var targetIdsToWeights = battlefieldEntityIds .Select(id => new { Id = id, Agent = rgs.GetPartSingleOrDefault <Parts.Agent>(id) }) .Where(a => a.Id != attackerId && a.Agent != null && !Vals.CombatStatusTag.CombatTerminalStatuses.Intersect(a.Agent.CombatStatusTags).Any()) .ToDictionary(a => a.Id, a => 0); var attackerFaction = rgs.GetPartSingle <Parts.Faction>(attackerId); var factionInteractions = rgs.GetPartSingle <Parts.FactionInteractionSet>(globalId); //eventually we have to figure out how to weight faction relative to everything else. var possibleTargetIds = targetIdsToWeights.Keys.ToArray(); foreach (var possibleTargetId in possibleTargetIds) { var targetFaction = rgs.GetPartSingle <Parts.Faction>(possibleTargetId); targetIdsToWeights[possibleTargetId] = factionInteractions.Interactions .SingleOrDefault(i => i.SourceFaction == attackerFaction.CurrentPublicFaction && i.TargetFaction == targetFaction.CurrentPublicFaction) ?.Disposition ?? 0; } var minWeight = targetIdsToWeights.Values.Any() ? targetIdsToWeights.Values.Min() : 0; if (minWeight < 0) { var targetId = targetIdsToWeights.First(kvp => kvp.Value == minWeight).Key; var attackerAnatomy = rgs.GetPartSingle <Parts.Anatomy>(attackerId); //MWCTODO++: yaaaah this is way too human centric. var weaponId = attackerAnatomy.SlotsEquipped.GetValueOrDefault(Vals.BodySlots.WieldHandRight, 0); if (weaponId == 0) { weaponId = attackerAnatomy.SlotsEquipped.GetValueOrDefault(Vals.BodySlots.WieldHandLeft, 0); } if (weaponId == 0) { var nwmap = naturalWeaponsMap.NaturalWeaponSets.GetValueOrDefault(attackerAnatomy.NaturalWeaponsCategory, null); if (nwmap != null && nwmap.Keys.Any()) { //MWCTODO: and this isn't great. weaponId = nwmap.First().Value; } } action.Action = Vals.CombatAction.AttackWeaponMelee; action.TargetEntityId = targetId; action.WeaponEntityId = weaponId; } return(action); }
internal static WeaponBundle GetWeaponBundle(EcsRegistrar rgs, long weaponId) { return(new WeaponBundle { EntityName = rgs.GetPartSingle <Parts.EntityName>(weaponId), PhysicalObject = rgs.GetPartSingle <Parts.PhysicalObject>(weaponId), Damagers = rgs.GetParts <Parts.Damager>(weaponId).ToList() }); }
internal static AgentBundle GetAgentBundle(EcsRegistrar rgs, long agentId) { return(new AgentBundle { Agent = rgs.GetPartSingle <Parts.Agent>(agentId), Anatomy = rgs.GetPartSingle <Parts.Anatomy>(agentId), EntityName = rgs.GetPartSingle <Parts.EntityName>(agentId), PhysicalObject = rgs.GetPartSingle <Parts.PhysicalObject>(agentId) }); }
internal static long Agent(EcsRegistrar rgs, string agentBlueprintName, string armorBlueprintName, string weaponBlueprintName) { long agentId = Blueprinter.GetEntityFromBlueprint(rgs, agentBlueprintName); long?armorId = string.IsNullOrEmpty(armorBlueprintName) ? (long?)null : Blueprinter.GetEntityFromBlueprint(rgs, armorBlueprintName); long?weaponId = string.IsNullOrEmpty(weaponBlueprintName) ? (long?)null : Blueprinter.GetEntityFromBlueprint(rgs, weaponBlueprintName); var anatomy = rgs.GetPartSingle <Parts.Anatomy>(agentId); if (!anatomy.SlotsEquipped.Any()) { var bodySlotsAsEnum = Vals.BodyPlan.BodyPlanToSlots[anatomy.BodyPlan]; anatomy.SlotsEquipped = bodySlotsAsEnum.GetUniqueFlags().ToDictionary(s => (Vals.BodySlots)s, s => 0L); } if (armorId.HasValue) { var armorResults = Sys.Anatomy.Equip(rgs, agentId, armorId.Value); } if (weaponId.HasValue) { var wieldResults = Sys.Anatomy.Equip(rgs, agentId, weaponId.Value); } return(agentId); }
//internal static List<Messages.Combat> ProcessAgentAction(EcsRegistrar rgs, long agentId, long? targetId, string action) internal static List <Messages.Combat> ProcessAgentAction(EcsRegistrar rgs, long agentId, ActionChosen agentAction) { if (Stances.Contains(agentAction.Action)) { return(ApplyStance(rgs, agentId, agentAction.Action)); } else if (agentAction.Action == Vals.CombatAction.SwitchToAI) { return(SwitchToAI(rgs, agentId)); } else if (agentAction.Action == Vals.CombatAction.AttackWeaponMelee) { return(ResolveSingleTargetMelee(rgs, agentId, agentAction.WeaponEntityId.Value, agentAction.TargetEntityId.Value)); } else if (agentAction.Action == Vals.CombatAction.DoNothing) { var agentNames = rgs.GetPartSingle <Parts.EntityName>(agentId); return(new List <Messages.Combat> { new Messages.Combat { Tick = rgs.NewId(), ActorId = agentId, ActorAction = $"{agentNames.ProperName} stands still and looks around in confusion. Probably some programmer failed to give him a good action in this scenario." } }); } else { throw new ArgumentException($"Action '{agentAction.Action}' not supported."); } }
private static List <Messages.Combat> SwitchToAI(EcsRegistrar rgs, long agentId) { var ai = rgs.GetPartSingle <Parts.Agent>(agentId); ai.ActiveCombatAI = Vals.AI.MeleeOnly; return(new List <Messages.Combat> { new Messages.Combat { Tick = rgs.NewId(), ActorId = agentId, ActorAction = "Switched to AI." } }); }
internal static IEnumerable <List <Output> > RunBattlefield(EcsRegistrar rgs, long globalId, long battlefieldId, Func <CombatChoicesAndTargets, ActionChosen> receivePlayerInput) { while (true) { var battlefieldContainer = rgs.GetPartSingle <Parts.Container>(battlefieldId); var battlefieldEntityIds = battlefieldContainer.EntityIds.ToList(); foreach (var agentId in battlefieldEntityIds) { var battlefieldAgent = rgs.GetPartSingleOrDefault <Parts.Agent>(agentId); if (battlefieldAgent == null) { continue; } if (battlefieldAgent.CombatStatusTags.Intersect(Vals.CombatStatusTag.CombatTerminalStatuses).Any()) { continue; } ActionChosen agentActionChosen; if (battlefieldAgent.ActiveCombatAI == Vals.AI.PlayerChoice) { var possibleActions = Agent.GetPossibleActions(rgs, globalId, agentId, battlefieldEntityIds); agentActionChosen = receivePlayerInput(possibleActions); } else { agentActionChosen = Agent.GetAgentAICombatAction(rgs, globalId, battlefieldAgent.ActiveCombatAI, agentId, battlefieldEntityIds); } var results = Combat.ProcessAgentAction(rgs, agentId, agentActionChosen); var output = Narrator.OutputForCombatMessages(rgs, results); yield return(output); } var endOfBattleOutputs = GetEndOfBattleOutputs(rgs, battlefieldEntityIds); if (endOfBattleOutputs.Any()) { yield return(endOfBattleOutputs); yield break; } } }
public static CombatChoicesAndTargets GetPossibleActions(EcsRegistrar rgs, long globalId, long agentId, List <long> battlefieldEntityIds) { var possibleCombatActions = new CombatChoicesAndTargets(); var naturalWeaponsMap = rgs.GetPartSingle <Parts.NaturalWeaponsMap>(globalId); var agentAnatomy = rgs.GetPartSingle <Parts.Anatomy>(agentId); //var agentWieldingDisabled = agentAnatomyModifers // .Where(m => m.EquipmentSlotDisabled == Vals.BodyEquipmentSlots.WieldObjectAppendage) // .Select(m => m.EquipmentSlotDisabled) // .ToList(); var agentWieldedIds = agentAnatomy.SlotsEquipped .Where(s => s.Key == Vals.BodySlots.WieldHandLeft || s.Key == Vals.BodySlots.WieldHandRight || s.Key == Vals.BodySlots.WieldTwoHanded) .Select(s => s.Value) .ToList(); var agentWieldedIdToWeaponEntityName = agentWieldedIds .Where(id => id != 0) .Distinct() .Select(id => new { Id = id, Name = rgs.GetPartSingle <Parts.EntityName>(id) }) .ToDictionary(n => n.Id, n => n.Name); var agentNaturalWeaponSlots = naturalWeaponsMap.NaturalWeaponSets[agentAnatomy.NaturalWeaponsCategory]; //MWCTODO: some magic happens here and we find out a wristblade or a laser eye has been equipped, and add that to the slots. // also any quickslots that might have attack items in them. we want quickslots. maybe a wristblade or a laser eye automatically adds a new, fixed quickslot for itself? i like this idea upon first think! var recordedIds = new HashSet <long>(); foreach (var slot in agentNaturalWeaponSlots.Keys) { var weaponId = agentAnatomy.SlotsEquipped[slot]; if (weaponId == 0L) { weaponId = agentNaturalWeaponSlots[slot]; } if (recordedIds.Contains(weaponId)) { continue; } var weapon = Bundle.GetWeaponBundle(rgs, weaponId); var choice = new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.TopLevelAction, Description = $"Melee {weapon.EntityName.GeneralName}", Action = Vals.CombatAction.AttackWeaponMelee, WeaponBodySlot = slot, WeaponEntityId = weaponId, NextCategory = Vals.CombatCategory.MeleeTarget }; possibleCombatActions.Choices.Add(choice); recordedIds.Add(weaponId); } foreach (long id in battlefieldEntityIds) { if (id == agentId) { continue; } var entityName = rgs.GetPartSingleOrDefault <Parts.EntityName>(id); var entityAgent = rgs.GetPartSingleOrDefault <Parts.Agent>(id); if (entityName == null || entityAgent == null) { continue; } if (entityAgent.CombatStatusTags.Intersect(Vals.CombatStatusTag.CombatTerminalStatuses).Any()) { continue; } possibleCombatActions.MeleeTargets.Add(id, entityName.ProperName); } //add stances possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.AllStances, Description = "Stance (Defensive)", Action = Vals.CombatAction.StanceDefensive }); possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.AllStances, Description = "Stance (Stand Ground)", Action = Vals.CombatAction.StanceStandGround }); possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.AllStances, Description = "Stance (Aggressive)", Action = Vals.CombatAction.StanceAggressive }); //add top-level choices that we know are present: note that we already did NextCategory = primary-melee because the description there is context dependent. possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.TopLevelAction, Description = "All Melee Options", NextCategory = Vals.CombatCategory.AllMelee }); possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.TopLevelAction, Description = "Change Stance", NextCategory = Vals.CombatCategory.AllStances }); possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.TopLevelAction, Description = "Run Away", Action = Vals.CombatAction.RunAway }); possibleCombatActions.Choices.Add(new AgentActionChoice { AgentId = agentId, Category = Vals.CombatCategory.TopLevelAction, Description = "Do Nothing", Action = Vals.CombatAction.DoNothing }); return(possibleCombatActions); }
internal static List <Output> OutputForCombatMessages(EcsRegistrar rgs, List <Messages.Combat> messages) { var results = new List <Output>(); foreach (var msg in messages) { //MWCTODO: add a debug switch for this info results.Add(new Output { Category = OutputCategory.Text, Data = $"(DEBUG: attempted damage {msg.AttemptedDamage} net damage {msg.NetDamage} critical {msg.ActorCritical} target condition {msg.TargetCondition})" }); var result = new Output { Category = OutputCategory.Text }; var sb = new StringBuilder(); var actorNames = rgs.GetPartSingle <Parts.EntityName>(msg.ActorId); if (Vals.CombatAction.AllStances.Contains(msg.ActorAction)) { result.Data = $"{actorNames.ProperName} assumes a {msg.ActorAction} stance."; results.Add(result); } else if (msg.ActorAction == Vals.CombatAction.DoNothing) { result.Data = $"{actorNames.ProperName} stands still and looks around dumbly. What do?"; results.Add(result); } else if (msg.ActorAction == Vals.CombatAction.RunAway) { result.Data = $"{actorNames.ProperName} tries to run away, and {actorNames.Pronoun} feet spin on the floor in a cartoon whirlwind, but nothing actually happens. Uh oh."; results.Add(result); } else if (msg.TargetId == 0) { result.Data = $"MWCTODO: we don't know what {msg.ActorAction} is."; results.Add(result); } //if the result has data, we're done with this iteration. if (!string.IsNullOrEmpty(result.Data)) { continue; } var targetNames = rgs.GetPartSingle <Parts.EntityName>(msg.TargetId); sb.Append($"{actorNames.ProperName} {PresentTenseVerb(msg.AttackVerb)} with a {msg.ActorAdjustedRoll} attack."); if (msg.ActorAdjustedRoll <= 0) { sb.Append($", presenting no real threat to {targetNames.ProperName}, who steps back from it casually."); result.Data = sb.ToString(); AppendNoDamageButNearlyDead(sb, targetNames.ProperName, targetNames.Pronoun, msg.TargetCondition); results.Add(result); continue; } if (msg.AttemptedDamage <= 0) { sb.Append($", but {targetNames.ProperName} sees it coming and ducks out of the way, avoiding it entirely."); result.Data = sb.ToString(); AppendNoDamageButNearlyDead(sb, targetNames.ProperName, targetNames.Pronoun, msg.TargetCondition); results.Add(result); continue; } if (msg.NetDamage <= 0) { sb.Append($" and connects! Unfortunately for {actorNames.ProperName}, the attack isn't solid, and {targetNames.ProperName}'s armor protects {targetNames.Pronoun} entirely."); result.Data = sb.ToString(); AppendNoDamageButNearlyDead(sb, targetNames.ProperName, targetNames.Pronoun, msg.TargetCondition); results.Add(result); continue; } var targetConditionString = GetLivingThingConditionDesc(msg.TargetCondition); //any fatality or any crit ought to get a special fatality/crit message. if (msg.NetDamage > 7000) { sb.Append($". The attack is nearly perfect, and it is devastating. {targetNames.ProperName} is {targetConditionString}."); } else if (msg.NetDamage > 4000) { sb.Append($". It's a fine strike, penetrating the armor and inflicting a nasty wound. {targetNames.ProperName} is {targetConditionString}. "); } else if (msg.NetDamage > 1900) { sb.Append($". A hit . . . hard enough to notice. {targetNames.ProperName} is {targetConditionString}. "); } else if (msg.NetDamage > 0) { if (msg.TargetCondition > 2000) { sb.Append($". It doesn't amount to much, though. Barely a scratch. It will take a lot of these to wear down {targetNames.ProperName}."); } else if (msg.TargetCondition > 1) { sb.Append($". Earlier, {targetNames.ProperName} would have laughed at a wound like this. It's not much more than a scratch. {targetNames.ProperName} isn't laughing now. When you're already gravely wounded, any attack that lands might be your last, and {targetNames.ProperName} is already looking death in the eyes."); } else { sb.Append($". It's not much of an attack, but it didn't need to be. {targetNames.ProperName} watches in horror as the point slides home. {targetNames.Pronoun} lets go of {targetNames.Pronoun} weapon and reaches up for a moment before falling to the ground, dead."); } } result.Data = sb.ToString(); results.Add(result); } return(results); }
internal static Output Equip(EcsRegistrar rgs, long equipperId, long gearId) { var output = new Output { Category = "Text" }; //MWCTODO: bundleize this var equipperAnatomy = rgs.GetPartSingle <Parts.Anatomy>(equipperId); var equipperPhysicalObject = rgs.GetPartSingle <Parts.PhysicalObject>(equipperId); var equipperEntityName = rgs.GetPartSingle <Parts.EntityName>(equipperId); var gearPhysicalObject = rgs.GetPartSingle <Parts.PhysicalObject>(gearId); var gearEntityName = rgs.GetPartSingle <Parts.EntityName>(gearId); //someday we'll support gear that occupies multiple slots or using complex slot logic. if (gearPhysicalObject.EquipmentSlots == Vals.BodySlots.NoSlot || gearPhysicalObject.EquipmentSlots == Vals.BodySlots.Special) { throw new NotImplementedException(); } //MWCTODO++: rework this to use new DisabledSlot entity concept. //var equipperDisabledSlots = equipperAnatomyModifications.Select(m => m.NewEquipmentSlotsDisabled).ToList(); var equipperSlots = equipperAnatomy .SlotsEquipped //.Where(kvp => !equipperDisabledSlots.Contains(kvp.Key)) .ToDictionary(d => d.Key, d => d.Value); var matchingEquipperSlots = equipperSlots.Where(slot => (slot.Key & gearPhysicalObject.EquipmentSlots) == slot.Key).ToList(); if (!matchingEquipperSlots.Any()) { output.Data = $"{equipperEntityName.ProperName} doesn't have the right body parts to equip the {gearEntityName.GeneralName}."; return(output); } if (matchingEquipperSlots.All(s => s.Value != 0)) { output.Data = $"There's no open place to equip the {gearEntityName.GeneralName}. Unequip something else first."; return(output); } //we will special case 2h weapons here, though. and body armor! bool gearIs2Handed = (gearPhysicalObject.EquipmentSlots & Vals.BodySlots.WieldTwoHanded) == Vals.BodySlots.WieldTwoHanded; bool gearIsHumanBodyArmor = (gearPhysicalObject.EquipmentSlots & Vals.BodySlots.HumanBodyArmor) == Vals.BodySlots.HumanBodyArmor; if (gearIs2Handed && Vals.BodyPlan.BodySlotsEncompassedByOtherSlot[Vals.BodySlots.WieldTwoHanded].Any(s => equipperAnatomy.SlotsEquipped[s] != 0)) { output.Data = $"Two free hands are needed for the {gearEntityName.GeneralName}. Unequip something else first."; return(output); } if (gearIsHumanBodyArmor && Vals.BodyPlan.BodySlotsEncompassedByOtherSlot[Vals.BodySlots.HumanBodyArmor].Any(s => equipperAnatomy.SlotsEquipped[s] != 0)) { output.Data = $"Other equipment is in the way of equipping the {gearEntityName.GeneralName}. Unequip other gear first."; return(output); } //by equipping 2h-weapons and body armor in ALL the related slots, it makes checking equipment/weapons easy elsewhere. it will complicate unequip, though. var firstOpenSlot = matchingEquipperSlots.First(s => s.Value == 0); equipperAnatomy.SlotsEquipped[firstOpenSlot.Key] = gearId; if (gearIs2Handed) { foreach (var slot in Vals.BodyPlan.BodySlotsEncompassedByOtherSlot[Vals.BodySlots.WieldTwoHanded]) { equipperAnatomy.SlotsEquipped[slot] = gearId; } } if (gearIsHumanBodyArmor) { foreach (var slot in Vals.BodyPlan.BodySlotsEncompassedByOtherSlot[Vals.BodySlots.HumanBodyArmor]) { equipperAnatomy.SlotsEquipped[slot] = gearId; } } output.Data = $"{equipperEntityName.ProperName} equips the {gearEntityName.GeneralName}."; return(output); }