/// <summary>Creates a new instance of PlayerAction.</summary> /// <param name="actor">The player performing the action.</param> /// <param name="action">The action to perform.</param> /// <param name="arguments">The operands in this player action.</param> public PlayerAction(Player actor, ActionType action, params object[] arguments) : this() { Actor = actor; Action = action; Arguments = arguments; }
/// <summary>Creates a new instance of FogOfWar.</summary> /// <param name="rows">The number of rows.</param> /// <param name="cols">The number of columns.</param> /// <param name="worldSize">The size of the world.</param> /// <param name="owner">The player that owns this fog of war.</param> public FogOfWar(int rows, int cols, VectorF worldSize, Player owner) { clear(); Owner = owner; Grid = new VisOption[rows, cols]; // set grid square size SqrSize = new VectorF(worldSize.X / (FInt)cols, worldSize.Y / (FInt)rows); SqrSizeHalf = SqrSize / FInt.F2; // begin unexplored resetTo(VisOption.Unexplored); // only use drawn if human drawn = (owner.Type == Player.PlayerType.Human) ? new bool[rows, cols] : null; }
/// <summary>Searches for a collision between the provided node and any node, segment, or geo. Ignores collisions with the specified segments and nodes.</summary> /// <param name="center">The center of the node.</param> /// <param name="radius">The radius of the node.</param> /// <param name="ignoreSeg">The segments to ignore.</param> /// <param name="ignoreNode">The nodes to ignore.</param> /// <param name="includeUnbuilt">Whether or not to test against unfinished nodes or segments owned by a certain player.</param> /// <param name="owner">The owner of the unbuilt nodes and segments to test against.</param> /// <returns>Whether or not there was a collision.</returns> public bool nodeCollision(VectorF center, FInt radius, List<SegmentSkel> ignoreSeg, List<NodeSkel> ignoreNode, bool includeUnbuilt, Player owner) { List<SegmentSkel> segColls = new List<SegmentSkel>(1); List<NodeSkel> nodeColls = new List<NodeSkel>(1); List<GeoSkel> geoColls = new List<GeoSkel>(1); object[] array = new object[7]; array[0] = center; array[1] = radius; array[4] = CollSrchType.DoesItCollide; array[5] = includeUnbuilt; array[6] = owner; // first look for collisions with nodes array[2] = ignoreNode; array[3] = nodeColls; Grid.PointExpand(center, array, gridNodeCollNode); if (nodeColls.Count == 0) { // next look for collisions with segments array[2] = ignoreSeg; array[3] = segColls; Grid.PointExpand(center, array, gridNodeCollSeg); if (segColls.Count == 0) { // next look for collisions with geos array[2] = null; array[3] = geoColls; Grid.PointExpand(center, array, gridNodeCollGeo); } } return nodeColls.Count > 0 || segColls.Count > 0 || geoColls.Count > 0; }
/// <summary>Updates all variables in this mode.</summary> /// <param name="gameTime">The current game time.</param> public override void Update(GameTime gameTime) { base.Update(gameTime); FInt elapsed = (FInt)gameTime.ElapsedGameTime.TotalSeconds; // update gui if (Inp.OldMse.Position != Inp.Mse.Position) desktop.mouseMove(Inp.Mse.Position); GUI.Desktop.Event evnt = Desktop.Event.MouseRightUp; bool guiOwnedInput = false; if (Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Released) guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseLeftUp, Inp.Mse.Position, Inp); if (Inp.OldMse.LeftButton == ButtonState.Released && Inp.Mse.LeftButton == ButtonState.Pressed) guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseLeftDown, Inp.Mse.Position, Inp); if (Inp.OldMse.RightButton == ButtonState.Pressed && Inp.Mse.RightButton == ButtonState.Released) guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseRightUp, Inp.Mse.Position, Inp); if (Inp.OldMse.RightButton == ButtonState.Released && Inp.Mse.RightButton == ButtonState.Pressed) guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseRightUp, Inp.Mse.Position, Inp); Keys[] newKeys = Inp.Key.GetPressedKeys(); if (newKeys.Length != 0) guiOwnedInput |= desktop.PerformKeyEvent(newKeys, Inp); if (guiOwnedInput) { hoveredNode = null; hoveredSeg = null; hoveredSegEnd = null; hoveredSegEndOwner = null; hoveredGeo = null; hoveredHotspot = null; isDragging = false; } else { // prepare grid manager world.Grid.startNewUpdate(gameTime); // move camera if (Inp.Key.IsKeyDown(Keys.OemPlus)) world.Cam.Zoom += world.Cam.Zoom * elapsed; if (Inp.Key.IsKeyDown(Keys.OemMinus)) world.Cam.Zoom -= world.Cam.Zoom * elapsed; if (Inp.Key.IsKeyDown(Keys.A)) world.Cam.CenterX -= (700 / world.Cam.Zoom) * elapsed; if (Inp.Key.IsKeyDown(Keys.D)) world.Cam.CenterX += (700 / world.Cam.Zoom) * elapsed; if (Inp.Key.IsKeyDown(Keys.W)) world.Cam.CenterY -= (700 / world.Cam.Zoom) * elapsed; if (Inp.Key.IsKeyDown(Keys.S)) world.Cam.CenterY += (700 / world.Cam.Zoom) * elapsed; world.Cam.Zoom += (FInt)(Inp.Mse.ScrollWheelValue - Inp.OldMse.ScrollWheelValue) / (FInt)120 * (FInt).1d * world.Cam.Zoom; world.Cam.refreshCorners(); // get cursor world coordinates VectorF cursorPos = world.Cam.screenToWorld(new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y)); // check for hovered node, segment, segment end, and hotspot hoveredNode = world.NodeAtPoint(cursorPos, false); hoveredSeg = (hoveredNode == null) ? world.segmentAtPoint(cursorPos, null) : null; hoveredSegEnd = world.SegmentEndAtPoint(cursorPos); if (hoveredSegEnd != null) { foreach (Segment seg in world.Segments) { if (seg.Nodes[0] == hoveredSegEnd || seg.Nodes[1] == hoveredSegEnd) { hoveredSegEndOwner = seg.Owner; break; } } } else { hoveredSegEndOwner = null; } hoveredHotspot = world.HotspotAtPoint(cursorPos); // test geo vertices hoveredGeo = world.geoAtPoint(cursorPos, out hoveredGeoVertex, true); // test geo lines if (hoveredGeo == null) { hoveredGeo = world.geoAtPoint(cursorPos, out hoveredGeoVertex, false); hoveredGeoIsLine = true; } else { hoveredGeoIsLine = false; } // if the user just released the left mouse button if (Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Released) { if (selectedSegEnd != null) { // do nothing } // add node else if (btnAddNode.Pressed && cmbNodeTypes.SelectedIndex != -1 && selectedGeo == null && (selectedNode == null || isDragging) && selectedSeg == null && selectedHotspot == null) { bool useHoveredEnd = (hoveredSegEnd != null && (selectedNode == null || selectedNode.Owner == hoveredSegEndOwner)); // add node Node node = new Node(world, world.NodeTypes[cmbEditorAddNodeType.SelectedIndex]); node.Pos = useHoveredEnd ? hoveredSegEnd.Pos : cursorPos; node.Active = true; if (selectedNode != null && isDragging) { node.Owner = selectedNode.Owner; if (selectedNode.NumSegments < selectedNode.Segments.Length) // add segment too { Segment seg = new Segment(world); seg.Owner = selectedNode.Owner; seg.Nodes[0] = selectedNode; seg.Nodes[1] = node; selectedNode.addSegment(seg, false); node.addSegment(seg, false); seg.refreshMath(); seg.EndLength[0] = seg.Length; seg.EndLength[1] = seg.Length; seg.refreshEndLocs(); world.addSegment(seg); } } else if (selectedNode == null && hoveredSegEnd != null) { node.Owner = hoveredSegEndOwner; } else if (cmbEditorAddNodeOwner.SelectedIndex != 0) { node.Owner = world.Players[cmbEditorAddNodeOwner.SelectedIndex - 1]; } if (useHoveredEnd) { // link segments to it foreach (Segment seg in (hoveredSegEndOwner == null ? world.Segments : hoveredSegEndOwner.Segments)) { for (int i = 0; i < 2; i++) { if (seg.Nodes[i] == hoveredSegEnd) { seg.Nodes[i] = node; break; } } } } world.addNode(node); selectedNode = node; selectedSeg = null; loadObjectEditor(); } // add segment else if (btnAddSeg.Pressed && isDragging && selectedSeg == null && selectedGeo == null && (selectedNode == null || selectedNode.NumSegments < selectedNode.Segments.Length) && selectedHotspot == null) { Segment seg = new Segment(world); seg.Owner = (selectedNode != null) ? selectedNode.Owner : (hoveredNode != null) ? hoveredNode.Owner : (cmbEditorAddSegOwner.SelectedIndex > 0) ? world.Players[cmbEditorAddSegOwner.SelectedIndex - 1] : null; if (selectedNode != null) { seg.Nodes[0] = selectedNode; seg.Owner = selectedNode.Owner; selectedNode.addSegment(seg, false); } else { seg.State[1] = SegmentSkel.SegState.Retracting; seg.Nodes[0] = new Node(world); seg.Nodes[0].Pos = lastClickedPoint; seg.Nodes[0].Active = false; seg.Nodes[0].Destroyed = true; seg.Nodes[0].initSegArrays(0); } if (hoveredNode != null && hoveredNode.NumSegments != hoveredNode.Segments.Length && (selectedNode == null || (hoveredNode != selectedNode && hoveredNode.Owner == selectedNode.Owner && hoveredNode.relatedSeg(selectedNode) == -1))) { seg.Nodes[1] = hoveredNode; hoveredNode.addSegment(seg, false); } else { seg.State[0] = SegmentSkel.SegState.Building; seg.Nodes[1] = new Node(world); seg.Nodes[1].Pos = cursorPos; seg.Nodes[1].Active = false; seg.Nodes[1].Destroyed = true; seg.Nodes[1].initSegArrays(0); } seg.refreshMath(); seg.EndLength[0] = seg.Length; seg.EndLength[1] = seg.Length; seg.refreshEndLocs(); world.addSegment(seg); selectedNode = null; selectedSeg = seg; loadObjectEditor(); } // add geo vertex else if (btnAddGeo.Pressed && selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedHotspot == null) { // add vertex to existing geo if (selectedGeo != null && !selectedGeoIsLine && isDragging && (selectedGeoVertex == 0 || selectedGeoVertex == selectedGeo.Vertices.Length - 1)) { VectorF[] vertices; if (selectedGeo.Vertices.Length == 1) { vertices = new VectorF[selectedGeo.Vertices.Length + 1]; vertices[0] = cursorPos; vertices[1] = selectedGeo.Vertices[0]; } else if (selectedGeoVertex == 0) { vertices = new VectorF[selectedGeo.Vertices.Length + 2]; selectedGeo.Vertices.CopyTo(vertices, 2); vertices[0] = cursorPos; vertices[1] = selectedGeo.Vertices[0]; } else { vertices = new VectorF[selectedGeo.Vertices.Length + 2]; selectedGeo.Vertices.CopyTo(vertices, 0); vertices[selectedGeo.Vertices.Length] = selectedGeo.Vertices[selectedGeo.Vertices.Length - 1]; vertices[selectedGeo.Vertices.Length + 1] = cursorPos; selectedGeoVertex = vertices.Length - 1; } world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridRemoveGeo); selectedGeo.Vertices = vertices; selectedGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height)); world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridAddGeo); selectedGeoIsLine = false; loadObjectEditor(); } // add vertex in the middle of a line else if (hoveredGeo != null && hoveredGeoIsLine) { VectorF[] vertices = new VectorF[hoveredGeo.Vertices.Length + 2]; int v2 = (hoveredGeoVertex == hoveredGeo.Vertices.Length - 1) ? 0 : hoveredGeoVertex + 1; float dist = (float)Calc.getAdj(VectorF.Distance(cursorPos, hoveredGeo.Vertices[hoveredGeoVertex]), Calc.LinePointDistance(cursorPos, hoveredGeo.Vertices[hoveredGeoVertex], hoveredGeo.Vertices[v2])); Vector2 dir = Vector2.Normalize((Vector2)(hoveredGeo.Vertices[v2] - hoveredGeo.Vertices[hoveredGeoVertex])); VectorF pos = (VectorF)((Vector2)hoveredGeo.Vertices[hoveredGeoVertex] + (dir * dist)); for (int i = 0; i <= hoveredGeoVertex; i++) { vertices[i] = hoveredGeo.Vertices[i]; } if (hoveredGeoVertex == selectedGeo.Vertices.Length - 1) { vertices[hoveredGeoVertex + 1] = selectedGeo.Vertices[selectedGeo.Vertices.Length - 1]; vertices[hoveredGeoVertex + 2] = pos; } else { vertices[hoveredGeoVertex + 1] = pos; vertices[hoveredGeoVertex + 2] = pos; for (int i = hoveredGeoVertex + 1; i < hoveredGeo.Vertices.Length; i++) { vertices[i + 2] = hoveredGeo.Vertices[i]; } } world.Grid.Rect(hoveredGeo.UpperLeft, hoveredGeo.LowerRight, hoveredGeo, world.gridRemoveGeo); hoveredGeo.Vertices = vertices; hoveredGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height)); world.Grid.Rect(hoveredGeo.UpperLeft, hoveredGeo.LowerRight, hoveredGeo, world.gridAddGeo); hoveredGeoIsLine = false; hoveredGeoVertex += 2; selectedGeo = hoveredGeo; selectedGeoIsLine = false; selectedGeoVertex = hoveredGeoVertex; loadObjectEditor(); } // add new geo else if (hoveredGeo == null) { Geo geo = new Geo(world); geo.Vertices = new VectorF[] { cursorPos }; geo.CloseLoop = false; geo.Display = false; geo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height)); world.addGeo(geo); selectedGeo = geo; selectedGeoIsLine = false; selectedGeoVertex = 0; loadObjectEditor(); } } else if (btnAddHotspot.Pressed && selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedGeo == null) { // add hotspot Hotspot hotspot = new Hotspot(world); hotspot.Pos = cursorPos; world.addHotspot(hotspot); selectedHotspot = hotspot; loadObjectEditor(); } selectedSegEnd = null; isDragging = false; } // if the player just released the right mouse button else if (Inp.OldMse.RightButton == ButtonState.Pressed && Inp.Mse.RightButton == ButtonState.Released) { } // user just pressed the left mouse button else if (Inp.OldMse.LeftButton == ButtonState.Released && Inp.Mse.LeftButton == ButtonState.Pressed) { lastClickedPoint = world.Cam.screenToWorld(new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y)); isDragging = false; // check for selected node selectedNode = hoveredNode; // check for selected segment end selectedSegEnd = (selectedNode == null) ? hoveredSegEnd : null; // check for selected segment selectedSeg = (selectedNode == null && selectedSegEnd == null) ? hoveredSeg : null; // check for selected hotspot selectedHotspot = (selectedNode == null && selectedSegEnd == null && selectedSeg == null) ? hoveredHotspot : null; // check for selected geo if (selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedHotspot == null) { selectedGeo = hoveredGeo; selectedGeoIsLine = hoveredGeoIsLine; selectedGeoVertex = hoveredGeoVertex; } else { selectedGeo = null; } loadObjectEditor(); } // user just pressed the right mouse button else if (Inp.OldMse.RightButton == ButtonState.Released && Inp.Mse.RightButton == ButtonState.Pressed) { } // if the left mouse button is still pressed else if (desktop.Focused == null && Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Pressed) { if (!isDragging && Vector2.Distance(world.Cam.worldToScreen(lastClickedPoint), new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y)) >= distToDrag) { isDragging = true; if (selectedNode != null) dragOffset = selectedNode.Pos - lastClickedPoint; else if (selectedSegEnd != null) dragOffset = selectedSegEnd.Pos - lastClickedPoint; else if (selectedSeg != null) dragOffset = selectedSeg.Nodes[0].Pos - lastClickedPoint; else if (selectedHotspot != null) dragOffset = selectedHotspot.Pos - lastClickedPoint; else if (selectedGeo != null) { if (selectedGeoVertex == -1) dragOffset = selectedGeo.Center - lastClickedPoint; else dragOffset = selectedGeo.Vertices[selectedGeoVertex] - lastClickedPoint; } } } // if [Del] key is pressed else if (!Inp.OldKey.IsKeyDown(Keys.Delete) && Inp.Key.IsKeyDown(Keys.Delete)) { if (selectedNode != null) removeSelNode(); else if (selectedSeg != null) removeSelSeg(); else if (selectedHotspot != null) removeSelHotspot(); else if (selectedGeo != null) removeSelGeo(); } // drag if (isDragging) { // move node if (selectedNode != null && !btnAddSeg.Pressed && (!btnAddNode.Pressed || cmbEditorAddNodeType.SelectedIndex < 0)) { moveNode(selectedNode, cursorPos + dragOffset); } // move segment end else if (selectedSegEnd != null) { moveNode(selectedSegEnd, cursorPos + dragOffset); } // move segment else if (selectedSeg != null) { VectorF moveVect = cursorPos + dragOffset - selectedSeg.Nodes[0].Pos; moveNode(selectedSeg.Nodes[0], selectedSeg.Nodes[0].Pos + moveVect); moveNode(selectedSeg.Nodes[1], selectedSeg.Nodes[1].Pos + moveVect); } // move hotspot else if (selectedHotspot != null) { moveHotspot(selectedHotspot, cursorPos + dragOffset); } // move geo/vertex else if (selectedGeo != null && !btnAddGeo.Pressed) { world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridRemoveGeo); if (selectedGeoIsLine) { if (selectedGeoVertex == selectedGeo.Vertices.Length - 1) { selectedGeo.Vertices[0] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex]; } else { if (selectedGeoVertex != 0) selectedGeo.Vertices[selectedGeoVertex - 1] = cursorPos + dragOffset; selectedGeo.Vertices[selectedGeoVertex + 1] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex]; if (selectedGeoVertex != selectedGeo.Vertices.Length - 2) selectedGeo.Vertices[selectedGeoVertex + 2] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex]; } selectedGeo.Vertices[selectedGeoVertex] = cursorPos + dragOffset; } else if (selectedGeoVertex == -1) { VectorF toMove = cursorPos + dragOffset - selectedGeo.Center; for (int i = 0; i < selectedGeo.Vertices.Length; i++) { selectedGeo.Vertices[i] += toMove; } } else { if (selectedGeoVertex != 0 && selectedGeoVertex != selectedGeo.Vertices.Length - 1) selectedGeo.Vertices[selectedGeoVertex - 1] = cursorPos + dragOffset; selectedGeo.Vertices[selectedGeoVertex] = cursorPos + dragOffset; } selectedGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height)); world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridAddGeo); } } } }
/// <summary>Loads the currently selected file in lsbLoadSaveFile into the editor.</summary> private void loadSelectedFile() { StringBuilder path = new StringBuilder(); foreach (ButtonText b in btnSaveLoadPath) { path.Append(b.Text); path.Append('\\'); } path.Append(lsbSaveLoadFiles.SelectedItem); hoveredNode = null; selectedNode = null; hoveredSeg = null; selectedSeg = null; hoveredSegEnd = null; hoveredSegEndOwner = null; selectedSegEnd = null; hoveredGeo = null; selectedGeo = null; world = WorldLoader.loadWorld(path.ToString(), Graphics); refreshSideBar(); }
/// <summary>Creates a new instance of LevelEditorMode.</summary> /// <param name="graphics">The graphics device manager to use.</param> /// <param name="content">The content manager to use.</param> /// <param name="batch">The sprite batch to use.</param> /// <param name="bEffect">The basic effect to use.</param> public LevelEditorMode(GraphicsDeviceManager graphics, ContentManager content, SpriteBatch batch, BasicEffect bEffect) : base(graphics, content, batch, bEffect) { world = new World(true); world.Grid = new GridManager(1, 1, world); hoveredNode = null; selectedNode = null; hoveredSeg = null; selectedSeg = null; hoveredSegEnd = null; hoveredSegEndOwner = null; selectedSegEnd = null; hoveredGeo = null; hoveredGeoVertex = -1; hoveredGeoIsLine = false; selectedGeo = null; selectedGeoVertex = -1; selectedGeoIsLine = false; desktop = new Desktop(); lastClickedPoint = new VectorF(); dragOffset = new VectorF(); isDragging = false; }
/// <summary>Called when btnPlayerAddNew is clicked.</summary> private void btnPlayerAddNew_MouseLeftUp(object sender, EventArgs e) { // make sure the ID and name aren't duplicates foreach (Player p in world.Players) { if (p.ID == txtPlayerID.Text) { showMsg("A player with that ID already exists."); return; } if (!string.IsNullOrEmpty(p.Name) && p.Name == txtPlayerName.Text) { showMsg("A player with that name already exists."); return; } } Player player = new Player(world); player.Name = txtPlayerName.Text; player.Type = (Player.PlayerType)cmbPlayerTypes.SelectedIndex; player.Fog = new FogOfWar(world.FogRows, world.FogCols, world.Size, player); world.Players.Add(player); Dictionary<string, WorldEvent.EventType> nev = null; Dictionary<string, WorldEvent.EventType> sev = null; if (player.Type == Player.PlayerType.Computer) { nev = new Dictionary<string, WorldEvent.EventType>(); sev = new Dictionary<string, WorldEvent.EventType>(); } world.SegEvents.Add(nev); world.NodeEvents.Add(sev); if (string.IsNullOrWhiteSpace(txtPlayerID.Text)) { player.ID = world.getNextPlayerID(); txtPlayerID.Text = player.ID; } else { player.ID = txtPlayerID.Text; world.refreshNextGenIDs(); } string nm = string.IsNullOrEmpty(player.Name) ? ("[Unnamed " + player.Type.ToString() + " " + player.ID + "]") : player.Name; cmbPlayers.Menu.addItem(nm); cmbPlayers.SelectedIndex = cmbPlayers.Menu.Count - 1; // update other lists cmbEditorAddNodeOwner.Menu.addItem(nm); cmbEditorAddSegOwner.Menu.addItem(nm); cmbEditorNodeOwner.Menu.addItem(nm); cmbEditorSegOwner.Menu.addItem(nm); }
/// <summary>Adds a player to the world.</summary> /// <param name="player">The player to add.</param> public void addPlayer(Player player) { if (player.ID == null) player.ID = getNextPlayerID(); PlayerByID.Add(player.ID, player); Players.Add(player); }
/// <summary>Find a segment owned by the specified player that overlaps the specified point.</summary> /// <param name="point">The point to search for.</param> /// <param name="owner">The player that owns the segment.</param> /// <param name="checkEnds">If true, checks to see if the point is near one of the end nodes. Otherwise, sees if the point overlaps the active portion of the segment.</param> public Segment segmentAtPoint(VectorF point, Player owner) { object[] data = new object[3]; data[0] = point; data[1] = owner; Grid.PointExpand(point, data, gridSegmentAtPoint); return (Segment)data[2]; }
/// <summary>Removes a player from the world.</summary> /// <param name="player">The player to remove.</param> public void removePlayer(Player player) { PlayerByID.Remove(player.ID); Players.Remove(player); }
/// <summary>Creates a new instance of AIThread.</summary> /// <param name="owner">The player that owns this thread.</param> public AIThread(Player owner) : this() { Owner = owner; }
/// <summary>Gets a skeleton version of this grid manager.</summary> /// <param name="forPlayer">The player that will use it. His nodes and segments are full instead of skeletons.</param> /// <param name="playerNodes">A list of all of the player's nodes.</param> /// <param name="playerSegs">A list of all of the player's segments.</param> /// <param name="allSegs">A list of all segments.</param> /// <param name="allNodes">A list of all nodes.</param> /// <returns>A skeleton version of this grid manager.</returns> public GridManager getSkeleton(Player forPlayer, out Node[] playerNodes, out Segment[] playerSegs, out NodeSkel[] allNodes, out SegmentSkel[] allSegs) { playerNodes = new Node[forPlayer.Nodes.Count]; playerSegs = new Segment[forPlayer.Segments.Count]; allNodes = new NodeSkel[InWorld.Nodes.Count]; allSegs = new SegmentSkel[InWorld.Segments.Count]; int nextPlyrNode = 0; int nextPlyrSeg = 0; int nextAllNode = 0; int nextAllSeg = 0; GridManager skeleton = new GridManager(NumCols, NumRows, InWorld, false); Dictionary<string, NodeSkel> nodes = new Dictionary<string, NodeSkel>(InWorld.Nodes.Count); Dictionary<string, SegmentSkel> segments = new Dictionary<string, SegmentSkel>(InWorld.Segments.Count); Dictionary<string, GeoSkel> geos = new Dictionary<string, GeoSkel>(InWorld.Geos.Count); // add nodes, segments, and geos to dictionary foreach (Node node in InWorld.Nodes) { if (node.Owner == forPlayer) { Node clone = new Node(node); nodes.Add(clone.ID, clone); playerNodes[nextPlyrNode] = clone; nextPlyrNode++; allNodes[nextAllNode] = clone; } else if (!node.Destroyed) { NodeSkel skel = node.getSkeleton(); nodes.Add(node.ID, skel); allNodes[nextAllNode] = skel; } nextAllNode++; } foreach (Segment seg in InWorld.Segments) { if (seg.Owner == forPlayer) { Segment clone = new Segment(seg); segments.Add(clone.ID, clone); playerSegs[nextPlyrSeg] = clone; nextPlyrSeg++; allSegs[nextAllSeg] = clone; nextAllSeg++; } else if (!seg.Destroyed) { SegmentSkel skel = seg.getSkeleton(); segments.Add(seg.ID, skel); allSegs[nextAllSeg] = skel; nextAllSeg++; } } foreach (Geo geo in InWorld.Geos) { geos.Add(geo.ID, geo.getSkeleton()); } // connect cloned nodes and segments to each other foreach (Node node in playerNodes) { for (int i = 0; i < node.Segments.Length; i++) { Segment seg = node.Segments[i]; if (seg != null) node.Segments[i] = (Segment)segments[seg.ID]; } for (int i = 0; i < node.Parents.Count; i++) { Node n = node.Parents[i]; node.Parents[i] = (Node)nodes[n.ID]; } } foreach (Segment seg in playerSegs) { for (int i = 0; i < seg.Nodes.Length; i++) { Node node = seg.Nodes[i]; if (!node.Destroyed) seg.Nodes[i] = (Node)nodes[node.ID]; else seg.Nodes[i] = new Node(node); } } // build grid skeleton.SqrSize = this.SqrSize; for (int row = 0; row < NumRows; row++) { for (int col = 0; col < NumRows; col++) { GridSqr sqr = Squares[row, col]; skeleton.Squares[row, col] = new GridSqr(new List<NodeSkel>(sqr.Nodes.Count), new List<SegmentSkel>(sqr.Segments.Count), new List<GeoSkel>(sqr.Geos.Count), new List<Hotspot>(sqr.Hotspots.Count), false); foreach (NodeSkel node in sqr.Nodes) { if (((Node)node).Active && !((Node)node).Destroyed) skeleton.Squares[row, col].Nodes.Add(nodes[node.ID]); } foreach (SegmentSkel seg in sqr.Segments) { if (!((Segment)seg).Destroyed) skeleton.Squares[row, col].Segments.Add(segments[seg.ID]); } foreach (GeoSkel geo in sqr.Geos) { skeleton.Squares[row, col].Geos.Add(geos[geo.ID]); } } } return skeleton; }
private static void readPlayerVars(List<string[]> commands, World map, Dictionary<string, Player> players) { Player player = new Player(map); player.Fog = new FogOfWar(map.FogRows, map.FogCols, map.Size, player); for (int j = 1; j < commands.Count; j++) { string value = translateValue(commands[j][2], "[Player]", map); bool valueValid = true; switch (commands[j][0]) { case "Type": switch (value) { case "Human": player.Type = Player.PlayerType.Human; map.HumanPlayer = player; break; case "Network": player.Type = Player.PlayerType.Network; break; case "Computer": player.Type = Player.PlayerType.Computer; break; default: throw new Exception("Unrecognized player type: " + value); } break; case "Name": player.Name = value; break; case "ID": player.ID = value; players.Add(value, player); break; default: throw new Exception("Player variable not recognized: " + commands[j][0]); } if (!valueValid) throw new Exception("Value '" + commands[j][2] + "' not valid."); } map.addPlayer(player); if (player.Type == Player.PlayerType.Computer) { map.SegEvents.Add(new Dictionary<string, WorldEvent.EventType>()); map.NodeEvents.Add(new Dictionary<string, WorldEvent.EventType>()); } else { map.SegEvents.Add(null); map.NodeEvents.Add(null); } }
/// <summary>Searches for a collision between the provided line and any node or segment. Ignores collisions with the specified segments and nodes.</summary> /// <param name="point1">The first point in the line.</param> /// <param name="point2">The second point in the line.</param> /// <param name="ignoreSeg">The segments to ignore.</param> /// <param name="ignoreNode">The nodes to ignore.</param> /// <param name="segColls">The list of segment collisions.</param> /// <param name="nodeColls">The list of node collisions.</param> /// <param name="geoColls">The list of geo collisions.</param> /// <param name="includeUnbuilt">Whether or not to test against unfinished nodes and segments owned by a certain player.</param> /// <param name="owner">The owner of the unbuilt nodes and segments to test against.</param> /// <returns>Whether or not there was a collision.</returns> public bool segCollision(VectorF point1, VectorF point2, List<SegmentSkel> ignoreSeg, List<NodeSkel> ignoreNode, out List<SegmentSkel> segColls, out List<NodeSkel> nodeColls, out List<GeoSkel> geoColls, bool includeUnbuilt, Player owner) { segColls = new List<SegmentSkel>(); nodeColls = new List<NodeSkel>(); geoColls = new List<GeoSkel>(); object[] array = new object[7]; array[0] = point1; array[1] = point2; array[4] = CollSrchType.GetAllCollisions; array[5] = includeUnbuilt; array[6] = owner; // first look for collisions with nodes array[2] = ignoreNode; array[3] = nodeColls; Grid.LineExpand(point1, point2, array, gridSegCollNode); // next look for collisions with segments array[2] = ignoreSeg; array[3] = segColls; Grid.Line(point1, point2, array, gridSegCollSeg); // next look for collisions with geos array[2] = null; array[3] = geoColls; Grid.Line(point1, point2, array, gridSegCollGeo); return nodeColls.Count > 0 || segColls.Count > 0 || geoColls.Count > 0; }
/// <summary>Searches for a collision between the provided line and any node, segment, or geo. Ignores collisions with the specified segments and nodes.</summary> /// <param name="point1">The first point in the line.</param> /// <param name="point2">The second point in the line.</param> /// <param name="ignoreSeg">The segments to ignore.</param> /// <param name="ignoreNode">The nodes to ignore.</param> /// <param name="testSegs">Whether or not to test segments.</param> /// <param name="testNodes">Whether or not to test nodes.</param> /// <param name="testGeos">Whether or not to test geos.</param> /// <param name="includeUnbuilt">Whether or not to test against unfinished nodes and segments owned by a certain player.</param> /// <param name="owner">The owner of the unbuilt nodes and segments to test against.</param> /// <returns>Whether or not there was a collision.</returns> public bool segCollision(VectorF point1, VectorF point2, List<SegmentSkel> ignoreSeg, List<NodeSkel> ignoreNode, bool testSegs, bool testNodes, bool testGeos, bool includeUnbuilt, Player owner) { List<SegmentSkel> segColls = new List<SegmentSkel>(1); List<NodeSkel> nodeColls = new List<NodeSkel>(1); List<GeoSkel> geoColls = new List<GeoSkel>(1); object[] array = new object[7]; array[0] = point1; array[1] = point2; array[4] = CollSrchType.DoesItCollide; array[5] = includeUnbuilt; array[6] = owner; // first look for collisions with nodes if (testNodes) { array[2] = ignoreNode; array[3] = nodeColls; Grid.LineExpand(point1, point2, array, gridSegCollNode); } if (nodeColls.Count == 0) { // next look for collisions with segments if (testSegs) { array[2] = ignoreSeg; array[3] = segColls; Grid.Line(point1, point2, array, gridSegCollSeg); } if (segColls.Count == 0 && testGeos) { // next look for collisions with geos array[2] = null; array[3] = geoColls; Grid.Line(point1, point2, array, gridSegCollGeo); } } return nodeColls.Count > 0 || segColls.Count > 0 || geoColls.Count > 0; }
/// <summary>Searches for a collision between the provided node and any other node based on their spacing. Ignores collisions with the specified nodes.</summary> /// <param name="center">The center of the node.</param> /// <param name="radius">The radius of the node.</param> /// <param name="spacing">The spacing of the node.</param> /// <param name="owner">The owner of the circle.</param> /// <param name="ignoreNode">The nodes to ignore.</param> /// <param name="includeUnbuilt">Whether or not to include inactive nodes.</param> /// <returns>Whether or not there was a collision.</returns> public bool nodeCollNodeSpacing(VectorF center, FInt radius, FInt spacing, Player owner, List<NodeSkel> ignoreNode, bool includeUnbuilt) { List<NodeSkel> nodeColls = new List<NodeSkel>(1); object[] array = new object[8]; array[0] = center; array[1] = radius; array[2] = spacing; array[3] = owner; array[4] = ignoreNode; array[5] = nodeColls; array[6] = CollSrchType.DoesItCollide; array[7] = includeUnbuilt; Grid.PointExpand(center, array, gridNodeCollNodeSpacing); return nodeColls.Count > 0; }