public void RemoveLoot(IBattleManager battleManager, int attackIndex, ICombatObject attacker, ICombatObject defender, out Resource actualLoot) { if (attackIndex != 0 || battleManager.Round < Config.battle_loot_begin_round) { actualLoot = new Resource(); return; } var loot = battleFormulas.GetRewardResource(attacker, defender); city.BeginUpdate(); city.Resource.Subtract(loot, formula.HiddenResource(city, true), out actualLoot); city.EndUpdate(); }
private void TroopManagerTroopRemoved(ICity city, TroopStubEventArgs args) { if (!ShouldUpdate(city)) { return; } bool doUpdate = city.IsUpdating; if (!doUpdate) { city.BeginUpdate(); } city.Resource.Crop.Upkeep = procedure.UpkeepForCity(city, battleFormulas); if (!doUpdate) { city.EndUpdate(); } channel.Post(GetChannelName(city), () => { var packet = new Packet(Command.TroopRemoved); packet.AddUInt32(city.Id); packet.AddUInt32(args.Stub.City.Id); packet.AddUInt16(args.Stub.TroopId); return(packet); }); }
public override void Callback(object custom) { Dictionary <uint, ICity> cities; locker.Lock(out cities, cityId, targetCityId).Do(() => { if (!IsValid()) { return; } // what if city is not there anymore? ICity target = cities[targetCityId]; target.BeginUpdate(); target.Resource.Add(resource); target.EndUpdate(); ICity sender = cities[cityId]; if (sender.Owner != target.Owner) { target.Owner.SendSystemMessage(sender.Owner, string.Format("{0}'s {1} has sent resources to {2}", sender.Owner.Name, sender.Name, target.Name), string.Format("{0}'s {1} has sent you {2} to {3}.", sender.Owner.Name, sender.Name, resource.ToNiceString(), target.Name)); } StateChange(ActionState.Completed); }); }
public void AddToLocal(ushort type, byte lvl, ushort count, FormationType formation) { using (Concurrency.Current.Lock(city)) { city.BeginUpdate(); city.Template[type] = Ioc.Kernel.Get <UnitFactory>().GetUnitStats(type, lvl); if (city.Template[type] == null) { throw new Exception("Unit type not found!"); } city.EndUpdate(); city.DefaultTroop.BeginUpdate(); city.DefaultTroop.AddUnit(formation, type, count); city.DefaultTroop.EndUpdate(); } }
public Error SetDefaultTheme(ICity city, string id) { if (!HasTheme(city.Owner, id)) { return(Error.ThemeNotPurchased); } city.BeginUpdate(); city.DefaultTheme = id; city.EndUpdate(); return(Error.Ok); }
private bool TroopIsDead(ITroopObject troopObject, ICity city) { if (troopObject.Stub.TotalCount != 0) { return(false); } // Remove troop since he's dead city.BeginUpdate(); procedure.TroopObjectDelete(troopObject, false); city.EndUpdate(); return(true); }
private void RecalculateUpkeep(ICity city) { bool doUpdate = city.IsUpdating; if (!doUpdate) { city.BeginUpdate(); } city.Resource.Crop.Upkeep = procedure.UpkeepForCity(city, battleFormulas); if (!doUpdate) { city.EndUpdate(); } }
public Error SetWallTheme(ICity city, string id) { if (!HasTheme(city.Owner, id)) { return(Error.ThemeNotPurchased); } city.BeginUpdate(); city.WallTheme = id; city.EndUpdate(); // Due to how the client gets wall updates we need to do force the main structure to update city.MainBuilding.BeginUpdate(); city.MainBuilding.EndUpdate(); return(Error.Ok); }
/// <summary> /// Creates a city under the specified player with initial troop and main building /// </summary> public virtual void CreateCity(ICityFactory cityFactory, IPlayer player, string cityName, byte level, Position cityPosition, IBarbarianTribeManager barbarianTribeManager, out ICity city) { city = cityFactory.CreateCity(world.Cities.GetNextCityId(), player, cityName, cityPosition, formula.GetInitialCityResources(), formula.GetInitialCityRadius(), formula.GetInitialAp(), Theme.DEFAULT_THEME_ID, Theme.DEFAULT_THEME_ID, Theme.DEFAULT_THEME_ID, Theme.DEFAULT_THEME_ID); var mainBuildingPosition = cityPosition.Left(); IStructure mainBuilding = city.CreateStructure(2000, level, mainBuildingPosition.X, mainBuildingPosition.Y); player.Add(city); world.Cities.Add(city); city.BeginUpdate(); mainBuilding.BeginUpdate(); world.Regions.Add(mainBuilding); mainBuilding.EndUpdate(); var defaultTroop = city.CreateTroopStub(); defaultTroop.BeginUpdate(); defaultTroop.AddFormation(FormationType.Normal); defaultTroop.AddFormation(FormationType.Garrison); defaultTroop.AddFormation(FormationType.InBattle); defaultTroop.EndUpdate(); RecalculateCityResourceRates(city); SetResourceCap(city); city.EndUpdate(); if (player.GetCityCount() == 1) { barbarianTribeManager.CreateBarbarianTribeNear(1, 10, city.PrimaryPosition.X, city.PrimaryPosition.Y, 10); } }
public void Remove(ICity city) { lock (cities) { if (!cities.TryRemove(city.Id, out city)) { return; } city.BeginUpdate(); dbManager.DeleteDependencies(city); city.Deleted = City.DeletedState.Deleted; city.EndUpdate(); DeregisterEvents(city); } CityRemoved(city, new EventArgs()); }
public Error SetRoadTheme(ICity city, string id) { if (!HasTheme(city.Owner, id)) { return(Error.ThemeNotPurchased); } var previousTheme = city.RoadTheme; city.BeginUpdate(); city.RoadTheme = id; city.EndUpdate(); var lockedRegions = regionManager.LockRegions(tileLocator.ForeachTile(city.PrimaryPosition.X, city.PrimaryPosition.Y, city.Radius)); roadManager.ChangeRoadTheme(city, previousTheme, id); regionManager.UnlockRegions(lockedRegions); return(Error.Ok); }
private void RecalculateValue(ICity city, IGameObject gameObject) { if (!(gameObject is IStructure)) { return; } bool doUpdate = city.IsUpdating; if (!doUpdate) { city.BeginUpdate(); } city.Value = formula.CalculateCityValue(city); if (!doUpdate) { city.EndUpdate(); } }
public Error ApplyToAll(ICity city, string id, bool applyWall) { if (!HasTheme(city.Owner, id)) { return(Error.ThemeNotPurchased); } if (applyWall) { city.BeginUpdate(); city.WallTheme = id; city.EndUpdate(); } foreach (var structure in city) { structure.BeginUpdate(); structure.Theme = id; structure.EndUpdate(); } return(Error.Ok); }
private void LocalTroopSet(Session session, Packet packet) { uint cityId; bool hideNewUnits; ISimpleStub stub; try { cityId = packet.GetUInt32(); hideNewUnits = packet.GetByte() == 1; stub = PacketHelper.ReadStub(packet, FormationType.Normal, FormationType.Garrison); } catch (Exception) { ReplyError(session, packet, Error.Unexpected); return; } locker.Lock(session.Player).Do(() => { ICity city = session.Player.GetCity(cityId); if (city == null) { ReplyError(session, packet, Error.Unexpected); return; } // Set where new units should be sent to city.BeginUpdate(); city.HideNewUnits = hideNewUnits; city.EndUpdate(); // Move units if (stub.TotalCount > 0) { var currentUnits = city.DefaultTroop.ToUnitList(FormationType.Normal, FormationType.Garrison); var newUnits = stub.ToUnitList(); if (currentUnits.Count != newUnits.Count) { ReplyError(session, packet, Error.TroopChanged); return; } // Units are ordered by their type so we can compare the array by indexes if ( currentUnits.Where( (currentUnit, i) => currentUnit.Type != newUnits[i].Type || currentUnit.Count != newUnits[i].Count).Any()) { ReplyError(session, packet, Error.TroopChanged); return; } city.DefaultTroop.BeginUpdate(); city.DefaultTroop.RemoveAllUnits(FormationType.Normal, FormationType.Garrison); city.DefaultTroop.Add(stub); city.DefaultTroop.EndUpdate(); } ReplySuccess(session, packet); }); }
private void AfterBattle(ActionState state) { if (state != ActionState.Completed) { return; } Dictionary <uint, ICity> cities; locker.Lock(out cities, cityId, targetCityId).Do(() => { if (cities == null) { throw new Exception("City not found"); } ICity city = cities[cityId]; ICity targetCity = cities[targetCityId]; //Remove notification to target city once battle is over city.Notifications.Remove(this); //Remove Incoming Icon from the target's tribe if (targetCity.Owner.Tribesman != null) { targetCity.Owner.Tribesman.Tribe.SendUpdate(); } ITroopObject troopObject; if (!city.TryGetTroop(troopObjectId, out troopObject)) { throw new Exception("Troop object should still exist"); } // Calculate how many attack points to give to the city city.BeginUpdate(); city.AttackPoint += troopObject.Stats.AttackPoint; city.EndUpdate(); // Check if troop is still alive if (troopObject.Stub.TotalCount > 0) { // Add notification for walking back city.Notifications.Add(troopObject, this); // Send troop back home var tma = actionFactory.CreateTroopMovePassiveAction(city.Id, troopObject.ObjectId, city.PrimaryPosition.X, city.PrimaryPosition.Y, true, true); ExecuteChainAndWait(tma, AfterTroopMovedHome); } else { //Remove notification to target city once battle is over city.References.Remove(troopObject, this); targetCity.BeginUpdate(); city.BeginUpdate(); // Give back the loot to the target city targetCity.Resource.Add(troopObject.Stats.Loot); // Remove troop since he's dead procedure.TroopObjectDelete(troopObject, false); targetCity.EndUpdate(); city.EndUpdate(); StateChange(ActionState.Completed); } }); }
private void RefundResource(ICity city) { city.BeginUpdate(); city.Resource.Add(formula.GetActionCancelResource(BeginTime, resource)); city.EndUpdate(); }