public virtual int MoveTimeTotal(ITroopStub stub, int distance, bool isAttacking) { var moveTime = MoveTime(stub.Speed); decimal doubleTimeBonus = 0; decimal rushBonus = 0; int doubleTimeDistance = 500; foreach (var effect in stub.City.Technologies.GetEffects(EffectCode.TroopSpeedMod)) { // Getting rush attack/defense bonus; if ((((string)effect.Value[1]).ToUpper() == "ATTACK" && isAttacking) || (((string)effect.Value[1]).ToUpper() == "DEFENSE" && !isAttacking)) { rushBonus += (int)effect.Value[0]; } // Getting double time bonus else if (((string)effect.Value[1]).ToUpper() == "DISTANCE") { doubleTimeBonus += (int)effect.Value[0]; } } var rushBonusPercentage = rushBonus / 100; var doubleTimeBonusPercentage = doubleTimeBonus / 100; if (distance <= doubleTimeDistance) { return((int)(moveTime * distance / (1 + rushBonusPercentage) * (decimal)Config.seconds_per_unit)); } var shortDistance = moveTime * doubleTimeDistance / (1 + rushBonusPercentage); var longDistance = (distance - doubleTimeDistance) * moveTime / (1 + rushBonusPercentage + doubleTimeBonusPercentage); return((int)((shortDistance + longDistance) * (decimal)Config.seconds_per_unit)); }
public void RetreatUnits_RetreatsStationedUnitsThatAreNotInTribeThatOwnsStronghold( IStronghold stronghold, [FrozenMock] IActionFactory actionFactory, [FrozenMock] ITroopObjectInitializerFactory troopInitializerFactory, ITroopObjectInitializer troopInitializer, ITroopStub stub, ITribe tribe, ITribe shTribe, RetreatChainAction retreatAction, StrongholdManager strongholdManager) { stub.State.Returns(TroopState.Stationed); stub.City.Owner.IsInTribe.Returns(true); stub.City.Id.Returns<uint>(1234); stub.TroopId.Returns<ushort>(2); stub.City.Owner.Tribesman.Tribe.Returns(tribe); stronghold.MainBattle.Returns((IBattleManager)null); stronghold.Tribe.Returns(shTribe); stronghold.Troops.StationedHere().Returns(new[] { stub }); troopInitializerFactory.CreateStationedTroopObjectInitializer(stub).Returns(troopInitializer); actionFactory.CreateRetreatChainAction(stub.City.Id, troopInitializer).Returns(retreatAction); strongholdManager.RetreatUnits(stronghold); stub.City.Worker.Received(1).DoPassive(stub.City, retreatAction, true); }
public StationedTroopObjectInitializer(ITroopStub stub, Procedure procedure, Formula formula, IWorld world) { this.stub = stub; this.procedure = procedure; this.formula = formula; this.world = world; }
public Error RemoveFromAssignment(int assignmentId, IPlayer player, ITroopStub stub) { if (stub.City.Owner != player) { return(Error.TribesmanNotAuthorized); } Assignment assignment = player.Tribesman.Tribe.Assignments.FirstOrDefault(x => x.Id == assignmentId); if (assignment == null) { return(Error.AssignmentDone); } if (stub.State != TroopState.WaitingInDefensiveAssignment && stub.State != TroopState.WaitingInOffensiveAssignment) { return(Error.AssignmentTroopCannotBeRemovedAfterDispatch); } if (!assignment.RemoveStub(stub)) { return(Error.AssignmentDone); } procedure.TroopStubDelete(stub.City, stub); SendUpdate(); return(Error.Ok); }
public void DeleteTroopObject_WhenTroopObjectCreated_AddsUnitsBackToOriginalTroop( [Frozen] ITroopStub stub, [Frozen] ISimpleStub unitsToRetreat, StationedPartialTroopObjectInitializer troopInitializer) { unitsToRetreat.TotalCount.Returns <ushort>(1); stub.State.Returns(TroopState.Stationed); stub.RemoveFromFormation(FormationType.Defense, Arg.Any <ISimpleStub>()).Returns(true); stub.City.Troops.Remove(Arg.Any <ushort>()).Returns(true); var newStub = Substitute.For <ITroopStub>(); stub.City.CreateTroopStub().Returns(newStub); ITroopObject troopObject = Substitute.For <ITroopObject>(); troopObject.Stub.Returns(newStub); stub.City.CreateTroopObject(newStub, Arg.Any <uint>(), Arg.Any <uint>()).Returns(troopObject); troopInitializer.GetTroopObject(out troopObject); troopInitializer.DeleteTroopObject(); stub.Received(1).BeginUpdate(); stub.Received(1).AddAllToFormation(FormationType.Defense, newStub); stub.Received(1).EndUpdate(); }
public DefenseCombatUnit(uint id, uint battleId, ITroopStub stub, FormationType formation, ushort type, byte lvl, ushort count, IBattleFormulas battleFormulas, Formula formula, UnitFactory unitFactory, IDbManager dbManager) : base(id, battleId, battleFormulas, dbManager) { troopStub = stub; this.formation = formation; this.type = type; this.count = count; this.formula = formula; this.lvl = lvl; stats = stub.Template[type]; leftOverHp = stats.MaxHp; eachUnitUpkeep = unitFactory.GetUnitStats(type, lvl).Upkeep; }
public virtual decimal GetTroopSpeed(ITroopStub stub) { if (stub.TotalCount == 0) { return(0); } int count = 0; decimal totalSpeed = 0; int machineSpeed = int.MaxValue; foreach (var formation in stub) { foreach (var kvp in formation) { IBaseUnitStats stats = stub.City.Template[kvp.Key]; // Use the slowest machine speed if available. if (stats.Battle.Armor == ArmorType.Machine) { machineSpeed = Math.Min(stats.Battle.Speed, machineSpeed); } else { count += (kvp.Value * stats.Upkeep); totalSpeed += (kvp.Value * stats.Upkeep * stats.Battle.Speed); } } } return(Math.Round(machineSpeed == int.MaxValue ? totalSpeed / count : machineSpeed, 1)); }
public string ToNiceString() { StringBuilder stringBuilder = new StringBuilder(); lock (assignmentLock) { stringBuilder.Append( string.Format( "Id[{0}] Time[{1}] x[{2}] y[{3}] mode[{4}] # of stubs[{5}] pispatched[{6}]\n", Id, TargetTime, X, Y, Enum.GetName(typeof(AttackMode), AttackMode), assignmentTroops.Count, DispatchCount)); foreach (var obj in assignmentTroops) { ITroopStub stub = obj.Stub; stringBuilder.Append(string.Format("\tTime[{0}] Player[{1}] City[{2}] Stub[{3}] Upkeep[{4}]\n", obj.DepartureTime, stub.City.Owner.Name, stub.City.Name, stub.TroopId, stub.Upkeep)); } } return(stringBuilder.ToString()); }
public void GetInstantTrainCount_WhenCurrentCountIsUnderCapacity([Frozen] IObjectTypeFactory objectTypeFactory, Formula formula, IStructure structure, ITroopStub troopStub) { var effects = new List <Effect> { new Effect { Id = EffectCode.UnitTrainInstantTime, IsPrivate = false, Location = EffectLocation.City, Value = new object[] { 1, "BarrackUnits", 50, 50 } } }; structure.City.Technologies.GetEffects(EffectCode.UnitTrainInstantTime).Returns(effects); objectTypeFactory.IsObjectType("BarrackUnits", 101).Returns(true); structure.Type.Returns((ushort)1); troopStub.ToUnitList().ReturnsForAnyArgs(new List <Unit> { new Unit(101, 49) }); structure.City.Troops.MyStubs().Returns(new List <ITroopStub> { troopStub }); formula.GetInstantTrainCount(structure).Should().Be(1); }
private Error RemoveForeignTroop(ITroopStub stub) { var troopInitializer = troopObjectInitializerFactory.CreateStationedTroopObjectInitializer(stub); var ra = actionFactory.CreateRetreatChainAction(stub.City.Id, troopInitializer); return(stub.City.Worker.DoPassive(stub.City, ra, true)); }
public AttackModeMonitor(IBattleManager battleManager, ICombatGroup combatGroup, ITroopStub troopStub) { this.troopStub = troopStub; this.combatGroup = combatGroup; battleManager.ActionAttacked += BattleActionAttacked; battleManager.WithdrawAttacker += BattleWithdrawAttacker; }
private ITroopObject MockTroopObject(ITroopStub troopStub) { var troopObject = new Mock <ITroopObject>(); troopObject.SetupGet(p => p.Stub).Returns(troopStub); troopObject.SetupGet(p => p.City).Returns(troopStub.City); return(troopObject.Object); }
public virtual void TroopObjectCreate(ICity city, ITroopStub stub, out ITroopObject troopObject) { troopObject = city.CreateTroopObject(stub, city.PrimaryPosition.X, city.PrimaryPosition.Y); troopObject.BeginUpdate(); troopObject.Stats = new TroopStats(formula.GetTroopRadius(stub, null), formula.GetTroopSpeed(stub)); world.Regions.Add(troopObject); troopObject.EndUpdate(); }
private void AddToNormal(ITroopStub source, ITroopStub target) { target.BeginUpdate(); foreach (var unit in source.SelectMany(formation => formation)) { target.AddUnit(FormationType.Normal, unit.Key, unit.Value); } target.EndUpdate(); }
/// <summary> /// Removes a stub from the assignment /// </summary> /// <param name="stub"></param> public bool RemoveStub(ITroopStub stub) { if (!RemoveStubWithoutRescheduling(stub)) { return(false); } Reschedule(); return(true); }
public StationedPartialTroopObjectInitializer(ITroopStub stub, ISimpleStub unitsToRetreat, Formula formula, IWorld world) { this.stub = stub; this.unitsToRetreat = unitsToRetreat; this.formula = formula; this.world = world; }
public void GetTroopObject_WhenStubIsNotStationed_ReturnsError( [Frozen] ITroopStub stub, StationedTroopObjectInitializer troopInitializer) { stub.State.Returns(TroopState.BattleStationed); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.TroopNotStationed); troopObject.Should().BeNull(); }
private void RegisterStub(ushort id, ITroopStub stub) { if (dict.ContainsKey(id)) { return; } dict.Add(id, stub); stub.Update += StubUpdateEvent; stub.UnitUpdate += StubUnitUpdateEvent; FireAdded(stub); }
public virtual Error TroopStubDelete(ICity city, ITroopStub stub) { if (city.TroopObjects.Any(t => t.Stub == stub)) { return(Error.TroopObjectExistsForStub); } AddToNormal(stub, city.DefaultTroop); city.Troops.Remove(stub.TroopId); return(Error.Ok); }
/// <summary> /// Handler when an event switches state. This is how we know to remove a stub from the assignment once it joins the battle. /// </summary> /// <param name="stub"></param> /// <param name="newState"></param> private void StubOnStateSwitched(ITroopStub stub, TroopState newState) { lock (assignmentLock) { switch (newState) { case TroopState.Battle: RemoveStub(stub); break; } } }
private void DeregisterStub(ushort id, ITroopStub stub) { if (!dict.ContainsKey(id)) { return; } idGen.Release(id); dict.Remove(id); stub.Update -= StubUpdateEvent; stub.UnitUpdate -= StubUnitUpdateEvent; FireRemoved(stub); }
public void GetTroopObject_WhenAbleToRetreat_AddsNewTroopObjectToWorld( [Frozen] ITroopStub stub, [Frozen] IWorld world, StationedTroopObjectInitializer troopInitializer) { stub.State.Returns(TroopState.Stationed); stub.Station.Troops.RemoveStationed(Arg.Any <ushort>()).Returns(true); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.Ok); world.Regions.Received(1).Add(troopObject); }
public void GetTroopObject_WhenCannotRemoveFromStationedCity_ReturnsError( [Frozen] ITroopStub stub, StationedTroopObjectInitializer troopInitializer) { stub.StationTroopId = 100; stub.State.Returns(TroopState.Stationed); stub.Station.Troops.RemoveStationed(100).Returns(false); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.TroopNotStationed); troopObject.Should().BeNull(); }
public void GetTroopObject_WhenStubIsNotStationed_ReturnsError( [Frozen] ITroopStub stub, [Frozen] ISimpleStub unitsToRetreat, StationedPartialTroopObjectInitializer troopInitializer) { unitsToRetreat.TotalCount.Returns <ushort>(1); stub.State.Returns(TroopState.BattleStationed); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.TroopNotStationed); troopObject.Should().BeNull(); }
public void GetTroopObject_WhenUnitsToRetreatIsEmpty_ReturnsError( [Frozen] ITroopStub stub, [Frozen] ISimpleStub unitsToRetreat, StationedPartialTroopObjectInitializer troopInitializer) { stub.State.Returns(TroopState.Stationed); unitsToRetreat.TotalCount.Returns <ushort>(0); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.TroopEmpty); troopObject.Should().BeNull(); }
public void GetTroopObject_WhenCannotRemoveFromExistingTroop_ReturnsError( [Frozen] ITroopStub stub, [Frozen] ISimpleStub unitsToRetreat, StationedPartialTroopObjectInitializer troopInitializer) { unitsToRetreat.TotalCount.Returns <ushort>(1); stub.State.Returns(TroopState.Stationed); stub.RemoveFromFormation(FormationType.Defense, unitsToRetreat).Returns(false); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.TroopChanged); troopObject.Should().BeNull(); }
public void GetTroopObject_WhenAbleToRetreat_CreatesTroopObjectFromCity( [Frozen] ITroopStub stub, StationedTroopObjectInitializer troopInitializer) { stub.State.Returns(TroopState.Stationed); stub.Station.Troops.RemoveStationed(Arg.Any <ushort>()).Returns(true); stub.Station.PrimaryPosition.Returns(new Position(10, 20)); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.Ok); troopObject.Should().NotBeNull(); stub.City.Received(1).CreateTroopObject(stub, 10, 21); }
public void GetTroopObject_WhenCalledTwice_ReturnsSameInstance( [Frozen] ITroopStub stub, StationedTroopObjectInitializer troopInitializer) { stub.State.Returns(TroopState.Stationed); stub.Station.Troops.RemoveStationed(Arg.Any <ushort>()).Returns(true); ITroopObject troopObject; troopInitializer.GetTroopObject(out troopObject).Should().Be(Error.Ok); ITroopObject troopObject2; troopInitializer.GetTroopObject(out troopObject2).Should().Be(Error.Ok); troopObject.Should().BeSameAs(troopObject2); }
public DefenseCombatUnit(uint id, uint battleId, ITroopStub stub, FormationType formation, ushort type, byte lvl, ushort count, decimal leftOverHp, IBattleFormulas battleFormulas, Formula formula, UnitFactory unitFactory, IDbManager dbManager) : this(id, battleId, stub, formation, type, lvl, count, battleFormulas, formula, unitFactory, dbManager) { this.leftOverHp = leftOverHp; }
public void RetreatUnits_WhenStubIsNotStationedState_ShouldNotRetreatUnits( IStronghold stronghold, [Frozen] IActionFactory actionFactory, ITroopStub stub, StrongholdManager strongholdManager) { stronghold.MainBattle.Returns((IBattleManager)null); stronghold.Troops.StationedHere().Returns(new[] { stub }); stub.State.Returns(TroopState.BattleStationed); stub.City.Owner.IsInTribe.Returns(false); strongholdManager.RetreatUnits(stronghold); stub.City.Worker.DidNotReceiveWithAnyArgs().DoPassive(null, null, false); }