/// <summary> /// The constructor used to initiate the Victor Turner /// </summary> /// <param name="players">The players in the game</param> /// <param name="world">The world the game takes place in</param> public VictorTurner(GameInitializer gameInitializer) { this.gameInitializer = gameInitializer; this.players = gameInitializer.theWorld.players; this.world = gameInitializer.theWorld; this.humanControl = new WorldController(players[0],world); this.graphControl = GraphController.Instance; }
/// <summary> /// Random generates a world of tiles and places the players /// spawnpoints. The same seeds generates the same world. /// /// </summary> /// <param name="mapSeed"> /// The seed that the random generater will use.</param> /// <returns>The newly initiated world</returns> public static World GenerateWorld(int mapSeed) { myLogger = LoggerFactory.GetLogger(); Tile[,] tileMatrix = GenerateTileMatrixFromSeed(mapSeed); //Constructs a new world using the dimensions. World returWorld = new World(tileMatrix, mapSeed); return returWorld; }
// Create public WorldController(Player p, World theWorld) { //Debugging finished = false; myLogger = LoggerFactory.GetLogger(); myLogger.SetThreshold(LogLevel.TRACE); this.playerInControll = p; this.theWorld = theWorld; createGUIRegionGridAndScrollZone(); }
/// <summary> /// Generates a list of optimal building placements for connecting the two given points. /// </summary> /// <param name="resource"></param> /// <param name="sourcePosition"></param> /// <returns></returns> public static List<Vector2> GenerateBuildPathBetween(Vector2 p_source, Vector2 p_dest, World world) { Vector2 source = p_source; Vector2 dest = p_dest; List<Vector2> path = new List<Vector2>(); do { source = Util.GetClosestPointFromList(dest, CreateMatrixFromInterval(BuildingController.GetValidBuildingInterval(source, world))); path.Add(source); } while (!WithinBuildRangeOf(source, dest, world)); if (source != dest) //safeguard for double add path.Add(p_dest); return path; }
private bool CreateGameObjects(int seed) { myLogger.Info("Generating world."); theWorld = WorldGenerator.GenerateWorld(seed); myLogger.Info("Done."); // Let all units belong to the world! Unit.SetWorld(theWorld); new SoundsController(theWorld); Random randomer = new Random(seed); myLogger.Info("Adding players."); Player human = new Player(Color.Blue, "John"); theWorld.AddPlayer(human); List<Player> temp2 = new List<Player>(); temp2.Add(human); AIPlayer ai = new AIPlayer(new AIView(theWorld),Color.Red); theWorld.AddPlayer(ai); human.Enemy = ai; ai.Enemy = human; myLogger.Info("Creating spawnpoints."); SpawnPoints(theWorld.players, theWorld.map.width, theWorld.map.height, randomer); myLogger.Info("Spawning units."); foreach(Player p in theWorld.players) { // We want 50 units! for(int i = 0; i < 10; i++) { p.unitAcc.ProduceUnits(); } } int xOffset = (Recellection.viewPort.Width/Globals.TILE_SIZE)/2; int yOffset = (Recellection.viewPort.Height/Globals.TILE_SIZE)/2; theWorld.LookingAt = new Point( (int)(theWorld.players[0].GetGraphs()[0].baseBuilding.position.X-xOffset), (int)(theWorld.players[0].GetGraphs()[0].baseBuilding.position.Y-yOffset)); myLogger.Info("Setting lookingAt to X: " + theWorld.LookingAt.X + " y: " + theWorld.LookingAt.Y); return true; }
//############## Construction ##############// /// <summary> /// Constructor. /// </summary> /// <param name="p_world"></param> public AIView(World p_world) { log = Utility.Logger.LoggerFactory.GetLogger(); //LoggerFactory.globalThreshold = LogLevel.FATAL; world = p_world; mapHeight = world.GetMap().map.GetLength(1); mapWidth = world.GetMap().map.GetLength(0); myBuildings = new List<Building>(); opponents = world.players; //Remove the AI player when it has called RegisterPlayer friendlyPoints = new List<Vector2>(); resourcePoints = new List<Vector2>(); enemyPoints = new List<Vector2>(); }
public static void Initiate(World world) { if (Instance == null) { Instance = new WorldView(); } Instance.World = world; world.lookingAtEvent += Instance.UpdateBg; world.lookingAtEvent += Instance.CreateCurrentView; Instance.CreateCurrentView(Instance, new Event<Point>(world.LookingAt, EventType.ALTER)); Instance.alignViewport(); //Color c1 = new Color(0xb2, 0xc9, 0x9f); //Color c2 = new Color(0x9f, 0xc4, 0xc9); Color c1 = Color.HotPink;//new Color(0xac, 0x33, 0x2d); Color c2 = Color.Crimson;//new Color(0xea, 0xe4, 0x7c); Instance.cMatrix = Instance.generateColorMatrix(c1, c2); }
/// <summary> /// Create the list of tiles that the fromBuilding is surrounded with and the /// tile it is placed on. /// </summary> /// <param name="middleTile">The coordinates for the tile the fromBuilding is built on.</param> /// <param name="world">The world the fromBuilding is being built in.</param> /// <returns></returns> public static LinkedList<Tile> CreateControlZone(Vector2 middleTile, World world) { LinkedList<Tile> retur = new LinkedList<Tile>(); //Iterate over the tiles that shall be added to the list for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { //The tile the fromBuilding is standing on shall be first in the //linked list. if (dx == 0 && dy == 0) { retur.AddFirst(world.GetMap().GetTile(dx + (int)middleTile.X, dy + (int)middleTile.Y)); } //The other tiles shall be appended to the list else { try { retur.AddLast(world.GetMap().GetTile(dx + (int)middleTile.X, dy + (int)middleTile.Y)); } catch (IndexOutOfRangeException e) { logger.Error(e.Message); //The fromBuilding is being built close to an edge //the exception is not handled. } } } } return retur; }
/// <summary> /// Returns the fromBuilding at the given coordinates provided that it is visible. /// </summary> /// <param name="point"></param> /// <param name="world"></param> /// <returns></returns> public static Building GetBuildingAt(Vector2 point, World world) { return GetTileAt(point, world).GetBuilding(); }
public void Init() { testWorld = WorldGenerator.GenerateWorld(0); }
/// <summary> /// Checks if the given source building is within building range of dest. /// </summary> /// <param name="source"></param> /// <param name="dest"></param> /// <returns></returns> public static bool WithinBuildRangeOf(Vector2 source, Vector2 dest, World world) { List<Point> valid = BuildingController.GetValidBuildingInterval(dest, world); Point v1 = valid[0]; Point v2 = valid[1]; if ((int)source.X < v1.X || (int)source.X > v2.X || (int)source.Y < v1.Y || (int)source.Y > v2.Y) { return false; } return true; }
/// <param name="worldInstance">A World instance used to calculate distance to objects</param> public SoundsController(World worldInstance) { theWorld = worldInstance; }
/// <summary> /// Returns a valid build point randomly chosen from the given choices. /// </summary> /// <param name="list"></param> /// <param name="world"></param> /// <returns></returns> public static Vector2 GetRandomBuildPointFrom(List<Vector2> list, World world) { List<Vector2> valids = new List<Vector2>(); for (int i = 0; i < list.Count; i++) { Vector2 temp = list[i]; if (GetBuildingAt(temp, world) == null) { //The spot is free! valids.Add(temp); } } Random randomFactory = new Random(); return valids[randomFactory.Next(valids.Count)]; }
/// <summary> /// Returns the Tile located in the given coordinates provided that it is visible. /// If it is not visible, null is returned. /// </summary> /// <param name="coords"></param> /// <param name="world"></param> /// <returns></returns> public static Tile GetTileAt(Vector2 coords, World world) { //log.Fatal("Accessing Tile at "+coords.X+","+coords.Y); Tile tempTile = world.GetMap().GetTile((int)coords.X, (int)coords.Y); ///* Uncomment when fog of war is properly implemented //if (tempTile.IsVisible(ai)) //{ // return tempTile; //} return tempTile; }
/// <summary> /// Add a fromBuilding to the source buildings owners graph, /// the source fromBuilding will be used to find the correct graph. /// </summary> /// <param name="buildingType">The type of fromBuilding to build.</param> /// <param name="sourceBuilding">The fromBuilding used to build this fromBuilding.</param> /// <param name="targetCoordinate">The tile coordinates where the fromBuilding will be built.</param> /// <param name="world">The world to build the fromBuilding in.</param> public static bool AddBuilding(Globals.BuildingTypes buildingType, Building sourceBuilding, Vector2 targetCoordinate, World world, Player owner) { if (sourceBuilding != null && buildingType != Globals.BuildingTypes.Base && (Math.Abs(((int)sourceBuilding.position.X) - (int)targetCoordinate.X) > MAX_BUILDING_RANGE || (Math.Abs(((int)sourceBuilding.position.Y) - (int)targetCoordinate.Y) > MAX_BUILDING_RANGE))) { logger.Debug("Building position out of range"); throw new BuildingOutOfRangeException(); } uint price = owner.unitAcc.CalculateBuildingCostInflation(buildingType); if (sourceBuilding != null && (uint)sourceBuilding.CountUnits() < price) { logger.Debug("Building too expensive"); return false; } logger.Info("Building a building at position "+targetCoordinate+" of "+buildingType+"."); lock (owner.GetGraphs()) { LinkedList<Tile> controlZone = CreateControlZone(targetCoordinate, world); //The Base building is handled in another way due to it's nature. if (buildingType == Globals.BuildingTypes.Base) { logger.Trace("Adding a Base Building and also constructing a new graph"); BaseBuilding baseBuilding = new BaseBuilding("Base Buidling", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, controlZone); world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).SetBuilding(baseBuilding); owner.AddGraph(GraphController.Instance.AddBaseBuilding(baseBuilding, sourceBuilding)); } else { //The other buildings constructs in similiar ways but they are constructed //as the specified type. Building newBuilding = null; switch (buildingType) { case Globals.BuildingTypes.Aggressive: logger.Trace("Building a new Aggressive building"); newBuilding = new AggressiveBuilding("Aggresive Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; case Globals.BuildingTypes.Barrier: logger.Trace("Building a new Barrier building"); newBuilding = new BarrierBuilding("Barrier Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; case Globals.BuildingTypes.Resource: logger.Trace("Building a new Resource building"); newBuilding = new ResourceBuilding("Resource Building", (int)targetCoordinate.X, (int)targetCoordinate.Y, owner, GraphController.Instance.GetGraph(sourceBuilding).baseBuilding, controlZone); break; } world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).SetBuilding(newBuilding); newBuilding.Parent = sourceBuilding; GraphController.Instance.AddBuilding(sourceBuilding, newBuilding); } if (sourceBuilding != null && world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).GetBuilding() != null) { logger.Info("The building has " + sourceBuilding.CountUnits() + " and the building costs " + price); owner.unitAcc.DestroyUnits(sourceBuilding.units, (int)price); logger.Info("The source building only got " + sourceBuilding.CountUnits() + " units left."); } else if (world.map.GetTile((int)targetCoordinate.X, (int)targetCoordinate.Y).GetBuilding() == null) { throw new Exception("A building was not placed on the tile even though it should have been."); } SoundsController.playSound("buildingPlacement"); } // Let's update the fog of war! /* for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { try { world.map.GetTile((int)targetCoordinate.X + j, (int)targetCoordinate.Y + i).MakeVisibleTo(owner); } catch(IndexOutOfRangeException e) { } } } */ return true; }
/// <summary> /// Generates a list of optimal building placements for connecting the two given points. /// </summary> /// <param name="resource"></param> /// <param name="sourcePosition"></param> /// <returns></returns> public static List <Vector2> GenerateBuildPathBetween(Vector2 p_source, Vector2 p_dest, World world) { Vector2 source = p_source; Vector2 dest = p_dest; List <Vector2> path = new List <Vector2>(); do { source = Util.GetClosestPointFromList(dest, CreateMatrixFromInterval(BuildingController.GetValidBuildingInterval(source, world))); path.Add(source); } while (!WithinBuildRangeOf(source, dest, world)); if (source != dest) //safeguard for double add { path.Add(p_dest); } return(path); }
private static void FindEnemy(Unit u, World.Map worldMap) { if (! u.IsAggressive) { return; } Tile t = worldMap.GetTile((int)u.position.X, (int)u.position.Y); // Search for units lock (t.GetUnits()) { foreach (Unit ou in t.GetUnits(u.owner.Enemy)) { // Is he dead already? if (ou.isDead) continue; // FIGHT TO THE DEATH! u.TargetEntity = ou; return; } } // Is there a building to kill? if (t.GetBuilding() != null && t.GetBuilding().owner != u.owner) { u.TargetEntity = t.GetBuilding(); return; } }
public static void SetWorld(World w) { Unit.world = w; }
/// <summary> /// /// </summary> /// <param name="player"></param> public static void ConstructBuilding(Player player, Tile constructTile, Building sourceBuilding, World theWorld) { logger.Trace("Constructing a building for a player"); //TODO Somehow present a menu to the player, and then //use the information to ADD (not the document) the fromBuilding. MenuIcon baseCell = new MenuIcon(Language.Instance.GetString("BaseCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Base)+")", Recellection.textureMap.GetTexture(Globals.TextureTypes.BaseBuilding), Color.Black); MenuIcon resourceCell = new MenuIcon(Language.Instance.GetString("ResourceCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Resource) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.ResourceBuilding), Color.Black); MenuIcon defensiveCell = new MenuIcon(Language.Instance.GetString("DefensiveCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Barrier) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.BarrierBuilding), Color.Black); MenuIcon aggressiveCell = new MenuIcon(Language.Instance.GetString("AggressiveCell") + " (" + player.unitAcc.CalculateBuildingCostInflation(Globals.BuildingTypes.Aggressive) + ")", Recellection.textureMap.GetTexture(Globals.TextureTypes.AggressiveBuilding), Color.Black); MenuIcon cancel = new MenuIcon(Language.Instance.GetString("Cancel"), Recellection.textureMap.GetTexture(Globals.TextureTypes.No)); List<MenuIcon> menuIcons = new List<MenuIcon>(); menuIcons.Add(baseCell); menuIcons.Add(resourceCell); menuIcons.Add(defensiveCell); menuIcons.Add(aggressiveCell); menuIcons.Add(cancel); Menu ConstructBuildingMenu = new Menu(Globals.MenuLayout.NineMatrix, menuIcons, Language.Instance.GetString("ChooseBuilding"), Color.Black); MenuController.LoadMenu(ConstructBuildingMenu); Recellection.CurrentState = MenuView.Instance; Globals.BuildingTypes building; MenuIcon choosenMenu = MenuController.GetInput(); Recellection.CurrentState = WorldView.Instance; MenuController.UnloadMenu(); if (choosenMenu.Equals(baseCell)) { building = Globals.BuildingTypes.Base; } else if (choosenMenu.Equals(resourceCell)) { building = Globals.BuildingTypes.Resource; } else if (choosenMenu.Equals(defensiveCell)) { building = Globals.BuildingTypes.Barrier; } else if (choosenMenu.Equals(aggressiveCell)) { building = Globals.BuildingTypes.Aggressive; } else { return; } // If we have selected a tile, and we can place a building at the selected tile... try { if (!AddBuilding(building, sourceBuilding, constructTile.position, theWorld, player)) { //SoundsController.playSound("Denied"); } } catch (BuildingOutOfRangeException bore) { throw bore; } }
/// <summary> /// Returns the fromBuilding at the given coordinates provided that it is visible. /// </summary> /// <param name="point"></param> /// <param name="world"></param> /// <returns></returns> public static Building GetBuildingAt(Vector2 point, World world) { return(GetTileAt(point, world).GetBuilding()); }
//############## Utility functions ##############// /// <summary> /// Checks whether or not there is a Resource Point at the given coordinates. /// Overloaded function, also works with a tile. /// </summary> /// <param name="current"></param> /// <returns></returns> internal bool ContainsResourcePoint(Vector2 current, World world) { Tile tempTile = Util.GetTileAt(current, world); return ContainsResourcePoint(tempTile); }
/// <summary> /// Returns two points with the interval of wich the building can be built in. /// The points is returned inclusively, both points are valid coordinates. /// </summary> /// <param name="sourceBuildingPosition"></param> /// <param name="world"></param> /// <returns>First in the list is the upperLeft point and the last is the lowerRight point.</returns> public static List<Point> GetValidBuildingInterval(Vector2 sourceBuildingPosition, World world) { List<Point> retur = new List<Point>(2); Point upperLeft = new Point( (int)MathHelper.Clamp(sourceBuildingPosition.X - MAX_BUILDING_RANGE, 1, world.GetMap().width-2), (int)MathHelper.Clamp(sourceBuildingPosition.Y - MAX_BUILDING_RANGE, 1, world.GetMap().height-2)); Point lowerRight = new Point( (int)MathHelper.Clamp(sourceBuildingPosition.X + MAX_BUILDING_RANGE, 1, world.GetMap().width-2), (int)MathHelper.Clamp(sourceBuildingPosition.Y + MAX_BUILDING_RANGE, 1, world.GetMap().height-2)); retur.Add(upperLeft); retur.Add(lowerRight); return retur; }
/// <summary> /// Call the update method on each unit causing them to move towards their baseEntity. /// If the unit has arrived to its baseEntity, it will recieve new orders. If it was previously /// travelling to a fromBuilding, it will be joined with that fromBuilding upon arrival. /// </summary> /// <param name="units">The set of units to be updated</param> /// <param name="systemTime">The time passed since something</param> public static void Update(IEnumerable<Unit> units, int systemTime, World.Map worldMap) { lock (units) { foreach (Unit u in units) { // Try to find enemies for this unit! FindEnemy(u, worldMap); // Update position u.Update(systemTime); } RemoveDeadUnits(); } }