public CropsView(WaterWarsController controller, Scene scene, AbstractGameAsset asset, AbstractView itemStoreView) : base(controller, scene, asset, itemStoreView) { m_veSceneObjectNames = new string[4, 1]; m_veSceneObjectNames[1, 0] = string.Format("{0}_Alfalfa", IN_WORLD_NAME); m_veSceneObjectNames[2, 0] = string.Format("{0}_Chillis", IN_WORLD_NAME); m_veSceneObjectNames[3, 0] = string.Format("{0}_Grapes", IN_WORLD_NAME); }
public FactoryView(WaterWarsController controller, Scene scene, AbstractGameAsset asset, AbstractView itemStoreView) : base(controller, scene, asset, itemStoreView) { m_veSceneObjectNames = new string[4, 2]; m_veSceneObjectNames[1, 0] = string.Format("{0}_Level-1-under-construction", IN_WORLD_NAME); m_veSceneObjectNames[2, 0] = string.Format("{0}_Level-2-under-construction", IN_WORLD_NAME); m_veSceneObjectNames[3, 0] = string.Format("{0}_Level-3-under-construction", IN_WORLD_NAME); m_veSceneObjectNames[1, 1] = string.Format("{0}_Level-1", IN_WORLD_NAME); m_veSceneObjectNames[2, 1] = string.Format("{0}_Level-2", IN_WORLD_NAME); m_veSceneObjectNames[3, 1] = string.Format("{0}_Level-3", IN_WORLD_NAME); }
// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public void ChangeAllocation(AbstractGameAsset a, Player p, int allocation) { int adjustment = allocation - a.WaterAllocated; if (adjustment > p.Water) throw new WaterWarsGameLogicException( string.Format( "Player {0} only has {1} water, not enough to satisfy allocation adjustment of {2}", p.Name, p.Water, adjustment)); a.WaterAllocated += adjustment; p.Water -= adjustment; }
public virtual void RemoveGameAsset(AbstractGameAsset asset) { if (asset.Type != ChosenGameAssetTemplate.Type) { throw new Exception( string.Format( "Tried to remove game asset {0} from a buy point which has chosen {1}", asset, ChosenGameAssetTemplate.Type)); } lock (GameAssets) { if (GameAssets.ContainsKey(asset.Uuid)) { GameAssets.Remove(asset.Uuid); switch (asset.Type) { case AbstractGameAssetType.Crops: Cropss.Remove((Crops)asset); break; case AbstractGameAssetType.Houses: Housess.Remove((Houses)asset); break; case AbstractGameAssetType.Factory: Factories.Remove((Factory)asset); break; default: throw new Exception(string.Format("Unrecognized asset {0}", asset)); } } else { m_log.WarnFormat( "[WATER WARS]: Request to remove asset {0} but it wasn't in buy point {1}", asset, this); } if (GameAssets.Count == 0) { ChosenGameAssetTemplate = AbstractGameAsset.None; } lock (Game.GameAssets) Game.GameAssets.Remove(asset.Uuid); } }
public AbstractGameAsset CreateGameAsset(Field f, AbstractGameAsset template, Vector3 pos, int level) { UUID uuid = UUID.Random(); AbstractGameAsset asset = null; string name = string.Format("{0} ({1})", template.InitialNames[level], f.Name); if (template is Factory) { asset = new Factory(name, uuid, pos, level); } else if (template is Houses) { asset = new Houses(name, uuid, pos, level); } else if (template is Crops) { asset = new Crops(name, uuid, pos, level); } else { throw new Exception(string.Format("Unrecognized asset type {0}", template)); } asset.InitialNames = template.InitialNames; asset.ConstructionCostsPerBuildStep = template.ConstructionCostsPerBuildStep; asset.StepsToBuilds = template.StepsToBuilds; asset.NormalRevenues = template.NormalRevenues; asset.WaterUsages = template.WaterUsages; asset.MaintenanceCosts = template.MaintenanceCosts; asset.InitialTimesToLive = template.InitialTimesToLive; asset.TimeToLive = asset.InitialTimesToLive[level]; asset.Field = f; asset.Game = m_controller.Game; int revenue = m_controller.EconomicDistributor.Allocate(asset.Game.EconomicActivity, asset); if (template is Houses) { asset.MarketPrice = revenue; } else { asset.RevenueThisTurn = revenue; } return(asset); }
public void ChangeAllocation(AbstractGameAsset a, Player p, int allocation) { int adjustment = allocation - a.WaterAllocated; if (adjustment > p.Water) throw new WaterWarsGameLogicException( string.Format( "Player {0} only has {1} water, not enough to satisfy allocation adjustment of {2}", p.Name, p.Water, adjustment)); if (adjustment == 0) return; else if (adjustment > 0) TakeWater(a, p, adjustment); else GiveBackWater(a, p, -adjustment); }
protected void GiveBackWater(AbstractGameAsset a, Player p, int waterToReturn) { m_log.InfoFormat( "[WATER WARS]: Deallocating {0} water from {1} owned by {2}", waterToReturn, a.Name, a.Field.BuyPoint.DevelopmentRightsOwner); BuyPoint assetBp = a.Field.BuyPoint; // If the asset owner has no water rights anywhere then the water is simply lost. if (assetBp.DevelopmentRightsOwner.WaterRightsOwned.Count == 0) return; // If the asset sits in a parcel for which the owner also owns water rights, then just return the water // there if (assetBp.DevelopmentRightsOwner == assetBp.WaterRightsOwner) { assetBp.WaterAvailable += waterToReturn; a.WaterAllocated -= waterToReturn; } else { lock (p.WaterRightsOwned) { int waterForEach = (int)Math.Ceiling(waterToReturn / (double)p.WaterRightsOwned.Count); a.WaterAllocated -= waterToReturn; foreach (BuyPoint bp in p.WaterRightsOwned.Values) { int water = Math.Min(waterForEach, waterToReturn); bp.WaterAvailable += water; waterToReturn -= water; if (waterToReturn <= 0) break; } } } a.TriggerChanged(); }
/// <summary> /// Add a game asset. /// </summary> /// Will throw an exception if the asset is different from the asset type chosen for this buy point. /// <param name="asset"></param> public virtual void AddGameAsset(AbstractGameAsset asset) { if (ChosenGameAssetTemplate != AbstractGameAsset.None && asset.Type != ChosenGameAssetTemplate.Type) { throw new Exception( string.Format( "Tried to add game asset {0} to a buy point which has chosen {1}", asset, ChosenGameAssetTemplate.Type)); } lock (GameAssets) { GameAssets.Add(asset.Uuid, asset); switch (asset.Type) { case AbstractGameAssetType.Crops: ChosenGameAssetTemplate = Crops.Template; Cropss.Add((Crops)asset); break; case AbstractGameAssetType.Houses: ChosenGameAssetTemplate = Houses.Template; Housess.Add((Houses)asset); break; case AbstractGameAssetType.Factory: ChosenGameAssetTemplate = Factory.Template; Factories.Add((Factory)asset); break; default: throw new Exception(string.Format("Unrecognized asset {0}", asset)); } lock (Game.GameAssets) Game.GameAssets[asset.Uuid] = asset; } }
public Field RemoveGameAssetView(AbstractGameAsset asset) { Field f = null; lock (m_gameAssetViews) { GameAssetView gav = GetGameAssetView(asset.Uuid); Vector3 pos = gav.RootPart.AbsolutePosition; gav.Close(); lock (m_buyPointViews) { f = asset.Field; BuyPoint bp = f.BuyPoint; GetBuyPointView(bp.Uuid).CreateFieldView(f, pos); } } f.TriggerChanged(); return f; }
protected void CreateGameAssetView(AbstractGameAsset asset) { lock (m_buyPointViews) { GameAssetView gav = GetBuyPointView(asset.BuyPointUuid).CreateGameAssetView(asset); asset.Position = gav.RootPart.AbsolutePosition; lock (m_gameAssetViews) m_gameAssetViews.Add(gav.Uuid, gav); } }
protected void ProcessGameAssetSoldToEconomy(AbstractGameAsset ga, Player p, int price) { if (p.Uuid != UserId) return; DevelopmentRightsOwned = p.DevelopmentRightsOwned.Count; Money = p.Money; Water = p.Water; WaterEntitlement = p.WaterEntitlement; m_statusButton.SendAlert( p.Uuid, "You just sold {0} to the market for {1}{2}", ga.Name, WaterWarsConstants.MONEY_UNIT, price); }
/// <summary> /// Create a game asset view /// </summary> /// <param name="asset"></param> /// <returns>A view that doesn't have a link to the model. The caller needs to link this subsequently</returns> public GameAssetView CreateGameAssetView(AbstractGameAsset asset) { GameAssetView v = null; FieldView fv = m_fieldViews[asset.Field.Uuid]; Vector3 pos = fv.RootPart.AbsolutePosition; if (asset.Type == AbstractGameAssetType.Factory) v = new FactoryView(m_controller, m_scene, asset, m_itemStoreView); else if (asset.Type == AbstractGameAssetType.Houses) v = new HousesView(m_controller, m_scene, asset, m_itemStoreView); else if (asset.Type == AbstractGameAssetType.Crops) v = new CropsView(m_controller, m_scene, asset, m_itemStoreView); else throw new Exception(string.Format("Unrecognized asset type {0} at position {1}", asset.Type, pos)); fv.Close(); m_fieldViews.Remove(asset.Field.Uuid); v.Initialize(pos); return v; }
public void TriggerGameAssetBuildStarted(AbstractGameAsset ga) { if (OnGameAssetBuildStarted != null) OnGameAssetBuildStarted(ga); }
public void TriggerGameAssetSoldToEconomy(AbstractGameAsset ga, Player p, int price) { if (OnGameAssetSoldToEconomy != null) OnGameAssetSoldToEconomy(ga, p, price); }
public void TriggerWaterUsed(AbstractGameAsset ga, Player user, int amount) { if (OnWaterUsed != null) OnWaterUsed(ga, user, amount); }
/// <summary> /// Add a game asset. /// </summary> /// Will throw an exception if the asset is different from the asset type chosen for this buy point. /// <param name="asset"></param> public virtual void AddGameAsset(AbstractGameAsset asset) { if (ChosenGameAssetTemplate != AbstractGameAsset.None && asset.Type != ChosenGameAssetTemplate.Type) throw new Exception( string.Format( "Tried to add game asset {0} to a buy point which has chosen {1}", asset, ChosenGameAssetTemplate.Type)); lock (GameAssets) { GameAssets.Add(asset.Uuid, asset); switch (asset.Type) { case AbstractGameAssetType.Crops: ChosenGameAssetTemplate = Crops.Template; Cropss.Add((Crops)asset); break; case AbstractGameAssetType.Houses: ChosenGameAssetTemplate = Houses.Template; Housess.Add((Houses)asset); break; case AbstractGameAssetType.Factory: ChosenGameAssetTemplate = Factory.Template; Factories.Add((Factory)asset); break; default: throw new Exception(string.Format("Unrecognized asset {0}", asset)); } lock (Game.GameAssets) Game.GameAssets[asset.Uuid] = asset; } }
public override void UpgradeGameAsset(Player p, AbstractGameAsset ga, int newLevel) { BuyPoint bp = ga.Field.BuyPoint; if (!ga.IsBuilt) throw new WaterWarsGameLogicException( "{0} tried to upgrade {1} at {2} in {3} but the asset is only partially built", p.Name, ga.Name, bp.Name, bp.Location.RegionName); if (newLevel <= ga.Level) throw new WaterWarsGameLogicException( "{0} tried to upgrade {1} to level {2} but asset is already at level {3}", p, ga, newLevel, ga.Level); if (newLevel > ga.MaxLevel) throw new WaterWarsGameLogicException( "{0} tried to upgrade {1} to level {2} but max level of asset is {3}", p, ga, newLevel, ga.MaxLevel); int price = ga.ConstructionCosts[newLevel] - ga.ConstructionCosts[ga.Level]; if (p.Money < price) throw new WaterWarsGameLogicException( "{0} has {1}, not enough to upgrade {2} to level {3} which costs {4}", p, p.Money, ga, newLevel, price); m_log.InfoFormat( "[WATER WARS]: {0} upgrading {1} on {2} in {3} to level {4}", p.Name, ga.Name, bp.Name, bp.Location.RegionName, newLevel); // Perform the transaction p.Money -= price; int oldLevel = ga.Level; ga.Level = newLevel; ga.Name = string.Format("{0} ({1})", ga.InitialNames[ga.Level], ga.Field.Name); ga.RevenueThisTurn = m_controller.EconomicDistributor.Allocate(ga.Game.EconomicActivity, ga); m_controller.EventManager.TriggerGameAssetUpgraded(ga, oldLevel); bp.TriggerChanged(); p.TriggerChanged(); ga.TriggerChanged(); UpdateHudStatus(p); }
public override AbstractGameAsset BuildGameAsset(Field f, AbstractGameAsset templateAsset, int level) { BuyPoint bp = f.BuyPoint; Player p = bp.DevelopmentRightsOwner; if (!p.Role.AllowedAssets.Contains(templateAsset)) throw new WaterWarsGameLogicException( "[WATER WARS]: Player {0} tried to buy a {1} on {2} but this is not one of their allowed assets", p.Name, templateAsset.Type, bp.Name); AbstractGameAsset ga = m_controller.ModelFactory.CreateGameAsset(f, templateAsset, Vector3.Zero, level); int price = ga.ConstructionCostPerBuildStep; if (p.Money < price) { // TODO: Signal this to the player in-world in some way throw new WaterWarsGameLogicException( "[WATER WARS]: Player {0} has {1}, not enough to starting building a {2} costing {3}", p, p.Money, templateAsset.Type, price); } m_log.InfoFormat( "[WATER WARS]: Player {0} building a {1} on {2} in {3} for {4} (approx {5} each turn)", p.Name, ga.Type, bp.Name, bp.Location.RegionName, ga.ConstructionCost, price); p.Money -= price; p.BuildCostsThisTurn += price; ga.StepsBuilt++; ga.StepBuiltThisTurn = true; // We have to remove the field from the buy point - it is now attached to the game asset if we want it back // later on. This is pretty nasty - fields and game assets should probably occupy specific slots on the // BuyPoint now. // NO! The buy point has to be kept here since this is actually the only way that the game asset can // retrieve the field. // f.BuyPoint = null; bp.AddGameAsset(ga); m_controller.EventManager.TriggerGameAssetBuildStarted(ga); if (ga.IsBuilt) m_controller.EventManager.TriggerGameAssetBuildCompleted(ga); p.TriggerChanged(); bp.TriggerChanged(); // Don't trigger this change since we are deleting the field view //f.TriggerChanged(); UpdateHudStatus(p); return ga; }
public override void SellGameAssetToEconomy(AbstractGameAsset ga) { if (!ga.CanBeSoldToEconomy) throw new WaterWarsGameLogicException( "{0} tried to sell asset {1} on {2} in {3} to the economy but it is not of the appropriate type", ga.OwnerName, ga.Name, ga.Field.BuyPoint.Name, ga.Field.BuyPoint.Location.RegionName); if (!ga.IsBuilt) throw new WaterWarsGameLogicException( "{0} tried to sell asset {1} on {2} in {3} to the economy but it is only partially built", ga.OwnerName, ga.Name, ga.Field.BuyPoint.Name, ga.Field.BuyPoint.Location.RegionName); Player owner = ga.Field.Owner; BuyPoint bp = ga.Field.BuyPoint; if (ga.WaterUsage > owner.WaterEntitlement) throw new WaterWarsGameLogicException( "Game asset {0} on parcel {1} in {2} cannot be sold to the market since this requires water rights of {3} whereas {4} has only {5} available", ga.Name, bp.Name, bp.Location.RegionName, owner.Name, owner.WaterEntitlement); m_log.InfoFormat( "[WATER WARS]: {0} selling {1} on {2} in {3} to economy", owner.Name, ga.Name, bp.Name, bp.Location.RegionName); int revenue = ga.MarketPrice; owner.Money += revenue; owner.BuildRevenueThisTurn += revenue; owner.WaterEntitlement -= ga.WaterUsage; ga.IsSoldToEconomy = true; ga.Field.Owner = m_controller.Game.Economy; ga.TriggerChanged(); bp.TriggerChanged(); owner.TriggerChanged(); m_controller.EventManager.TriggerGameAssetSoldToEconomy(ga, owner, revenue); }
public override Field RemoveGameAsset(AbstractGameAsset ga) { Player owner = ga.Field.Owner; BuyPoint bp = ga.Field.BuyPoint; m_log.InfoFormat( "[WATER WARS]: {0} removing {1} on {2} in {3}", owner.Name, ga.Name, bp.Name, bp.Location.RegionName); // Player balance does not need to change since sold assets actually do not recoup any cash bp.RemoveGameAsset(ga); Field replacementField = m_controller.Dispatcher.RemoveGameAssetView(ga); m_controller.EventManager.TriggerGameAssetRemoved(ga); bp.TriggerChanged(); // Do this after events have been triggered so that listeners can still retrieve field details about // the asset removed ga.Field = Field.None; return replacementField; }
public override AbstractGameAsset ContinueBuildingGameAsset(AbstractGameAsset ga) { BuyPoint bp = ga.Field.BuyPoint; if (ga.IsBuilt) throw new WaterWarsGameLogicException( "{0} tried to continue building game asset {1} on {2} in {3} but this is already fully built", bp.DevelopmentRightsOwner.Name, ga.Name, bp.Name, bp.Location.RegionName); Player p = bp.DevelopmentRightsOwner; int price = ga.ConstructionCostPerBuildStep; if (p.Money < price) { // TODO: Signal this to the player in-world in some way throw new WaterWarsGameLogicException( "[WATER WARS]: Player {0} has {1}, not enough to build the next phase of {2} in {3} at {4} costing {5}", p, p.Money, ga.Name, bp.Name, bp.Location.RegionName, price); } p.Money -= price; p.BuildCostsThisTurn += price; ga.StepsBuilt++; ga.StepBuiltThisTurn = true; m_controller.EventManager.TriggerGameAssetBuildContinued(ga); if (ga.IsBuilt) m_controller.EventManager.TriggerGameAssetBuildCompleted(ga); ga.TriggerChanged(); p.TriggerChanged(); bp.TriggerChanged(); UpdateHudStatus(p); return ga; }
/// <summary> /// Buy a game asset /// </summary> /// <param name="f"></param> /// <param name="template"></param> /// <param name="level"></param> public AbstractGameAsset BuildGameAsset( Field f, AbstractGameAsset template, int level) { return m_controller.State.BuildGameAsset(f, template, level); }
public int Allocate( IDictionary<AbstractGameAssetType, double[]> economicConditions, AbstractGameAsset ga) { return (int)Math.Ceiling(economicConditions[ga.Type][ga.Level] * ga.NormalRevenue); }
public virtual void UndoUseWater(AbstractGameAsset a) { throw new NotImplementedException(); }
public virtual void UseWater(AbstractGameAsset a, Player p, int amount) { throw new NotImplementedException(); }
public virtual void RemoveGameAsset(AbstractGameAsset asset) { if (asset.Type != ChosenGameAssetTemplate.Type) throw new Exception( string.Format( "Tried to remove game asset {0} from a buy point which has chosen {1}", asset, ChosenGameAssetTemplate.Type)); lock (GameAssets) { if (GameAssets.ContainsKey(asset.Uuid)) { GameAssets.Remove(asset.Uuid); switch (asset.Type) { case AbstractGameAssetType.Crops: Cropss.Remove((Crops)asset); break; case AbstractGameAssetType.Houses: Housess.Remove((Houses)asset); break; case AbstractGameAssetType.Factory: Factories.Remove((Factory)asset); break; default: throw new Exception(string.Format("Unrecognized asset {0}", asset)); } } else { m_log.WarnFormat( "[WATER WARS]: Request to remove asset {0} but it wasn't in buy point {1}", asset, this); } if (GameAssets.Count == 0) ChosenGameAssetTemplate = AbstractGameAsset.None; lock (Game.GameAssets) Game.GameAssets.Remove(asset.Uuid); } }
public virtual void UpgradeGameAsset(Player p, AbstractGameAsset ga, int level) { throw new NotImplementedException(); }
public void TriggerGameAssetRemoved(AbstractGameAsset ga) { if (OnGameAssetRemoved != null) OnGameAssetRemoved(ga); }
public AbstractGameAsset CreateGameAsset(Field f, AbstractGameAsset template, Vector3 pos, int level) { UUID uuid = UUID.Random(); AbstractGameAsset asset = null; string name = string.Format("{0} ({1})", template.InitialNames[level], f.Name); if (template is Factory) asset = new Factory(name, uuid, pos, level); else if (template is Houses) asset = new Houses(name, uuid, pos, level); else if (template is Crops) asset = new Crops(name, uuid, pos, level); else throw new Exception(string.Format("Unrecognized asset type {0}", template)); asset.InitialNames = template.InitialNames; asset.ConstructionCostsPerBuildStep = template.ConstructionCostsPerBuildStep; asset.StepsToBuilds = template.StepsToBuilds; asset.NormalRevenues = template.NormalRevenues; asset.WaterUsages = template.WaterUsages; asset.MaintenanceCosts = template.MaintenanceCosts; asset.InitialTimesToLive = template.InitialTimesToLive; asset.TimeToLive = asset.InitialTimesToLive[level]; asset.Field = f; asset.Game = m_controller.Game; int revenue = m_controller.EconomicDistributor.Allocate(asset.Game.EconomicActivity, asset); if (template is Houses) asset.MarketPrice = revenue; else asset.RevenueThisTurn = revenue; return asset; }
public void TriggerGameAssetUpgraded(AbstractGameAsset ga, int oldLevel) { if (OnGameAssetUpgraded != null) OnGameAssetUpgraded(ga, oldLevel); }
public Field RemoveGameAssetView(AbstractGameAsset asset) { return null; }
public static string Serialize(AbstractGameAsset ga) { return JsonConvert.SerializeObject(ga, Formatting.Indented); }
protected void TakeWater(AbstractGameAsset a, Player p, int waterRequired) { m_log.InfoFormat( "[WATER WARS]: Allocating {0} water to {1} owned by {2}", waterRequired, a.Name, a.Field.BuyPoint.DevelopmentRightsOwner); int totalWaterTaken = 0; if (a.Field.BuyPoint.WaterRightsOwner == p) { int waterTaken = Math.Min(a.Field.BuyPoint.WaterAvailable, waterRequired); a.Field.BuyPoint.WaterAvailable -= waterTaken; waterRequired -= waterTaken; totalWaterTaken += waterTaken; } // We need to pull in water from other places. if (waterRequired > 0) { lock (p.WaterRightsOwned) { Dictionary<UUID, BuyPoint> otherRights = new Dictionary<UUID, BuyPoint>(p.WaterRightsOwned); otherRights.Remove(a.Field.BuyPoint.Uuid); while (waterRequired > 0) { BuyPoint waterMaxBp = BuyPoint.None; foreach (BuyPoint bp in otherRights.Values) { if (bp.WaterAvailable > waterMaxBp.WaterAvailable) waterMaxBp = bp; } m_log.InfoFormat( "[WATER WARS]: ParcelOrientedAllocator determined buy point {0} has max water of {1}", waterMaxBp.Name, waterMaxBp.WaterAvailable); int waterTaken = Math.Min(waterMaxBp.WaterAvailable, waterRequired); waterMaxBp.WaterAvailable -= waterTaken; waterRequired -= waterTaken; totalWaterTaken += waterTaken; } } } a.WaterAllocated += totalWaterTaken; //p.Water -= a.WaterUsage; a.TriggerChanged(); }