예제 #1
0
파일: Node.cs 프로젝트: wwarnick/Sever
        /// <summary>Creates a segment to connect to the specified node.</summary>
        /// <param name="node">The node to connect to.</param>
        public void createSegment(Node node)
        {
            Segment segment = new Segment(InWorld);

            segment.Owner = Owner;
            segment.Nodes[0] = this;
            segment.Nodes[1] = node;
            segment.refreshMath();
            segment.EndLength[1] = segment.Length;
            segment.refreshEndLoc(0);
            segment.State[0] = Segment.SegState.Building;
            segment.State[1] = Segment.SegState.Complete;

            InWorld.addSegment(segment);
            addSegment(segment, true);
            alterDensity(0, segment.Capacity, segment);
        }
예제 #2
0
파일: Segment.cs 프로젝트: wwarnick/Sever
        /// <summary>Splits this segment into two pieces, and retracts them away from the split point.</summary>
        /// <param name="splitPoint">The distance from Node[0] to perform the split.</param>
        public void split(FInt splitPoint)
        {
            // prepare to update densities
            int[] nodePpl = new int[2];
            FInt[] nodeCap = new FInt[2];
            for (int i = 0; i < 2; i++)
            {
                if (State[i] == SegState.Complete)
                {
                    int oppLane = 1 - i;
                    int index = 0;
                    while (Nodes[oppLane].Segments[index] != this) { index++; }

                    // prepare densities
                    nodePpl[oppLane] = Nodes[oppLane].SegNumPeople[index];
                    nodeCap[oppLane] = Nodes[oppLane].SegCapacity[index];
                }
            }

            // prepare to update parentage
            List<Node>[] nodePar = new List<Node>[2];
            if (State[0] == SegState.Complete && State[1] == SegState.Complete)
            {
                nodePar[0] = new List<Node>(Nodes[1].Parents.Count + ((Nodes[1].IsParent) ? 1 : 0));
                nodePar[1] = new List<Node>(Nodes[0].Parents.Count + ((Nodes[0].IsParent) ? 1 : 0));

                if (Nodes[1].IsParent)
                    nodePar[0].Add(Nodes[1]);

                int segIndex = 0;
                while (Nodes[1].Segments[segIndex] != this) { segIndex++; }

                for (int i = 0; i < Nodes[1].Parents.Count; i++)
                {
                    if (Nodes[1].ParentFromSeg[i] == segIndex)
                        nodePar[1].Add(Nodes[1].Parents[i]);
                    else
                        nodePar[0].Add(Nodes[1].Parents[i]);
                }
            }

            // create new segment
            Segment newSeg = new Segment(InWorld);
            newSeg.Angle = Angle;
            newSeg.Capacity = Capacity;
            newSeg.Destroyed = Destroyed;
            newSeg.Direction = Direction;
            newSeg.DirectionPerp = DirectionPerp;
            newSeg.Length = Length;
            newSeg.Nodes[0] = Nodes[0];
            newSeg.Nodes[1] = Nodes[1];
            newSeg.Owner = Owner;
            newSeg.State[0] = State[0];
            newSeg.State[1] = SegState.Retracting;
            newSeg.EndLength[0] = EndLength[0];
            newSeg.EndLength[1] = Length - splitPoint;
            newSeg.EndLoc[1] = EndLoc[1];
            newSeg.refreshEndLoc(0);

            // divvy up the people

            // first lane
            for (LListNode<FInt> p = People[0].Last; p != null && p.Value >= splitPoint; p = People[0].Last)
            {
                People[0].RemoveLast();
                newSeg.People[0].AddFirst(p);
            }

            // second lane
            for (LListNode<FInt> p = People[1].First; p != null && p.Value <= newSeg.EndLength[1]; p = People[1].First)
            {
                People[1].RemoveFirst();
                newSeg.People[1].AddLast(p);
            }

            NumPeople = People[0].Count + People[1].Count;
            newSeg.NumPeople = newSeg.People[0].Count + newSeg.People[1].Count;

            nodePpl[0] -= NumPeople;
            nodePpl[1] -= newSeg.NumPeople;

            // update densities and parentage
            for (int i = 0; i < 2; i++)
            {
                if (State[i] == SegState.Complete)
                {
                    int oppLane = 1 - i;
                    Nodes[oppLane].alterDensity(-nodePpl[oppLane], -nodeCap[oppLane], this);

                    // only update parentage if both sides are complete
                    if (State[1 - i] == SegState.Complete)
                        Nodes[i].removeParents(this, nodePar[i]);
                }
            }

            // update the node connections
            if (State[0] == SegState.Complete)
            {
                for (int i = 0; i < Nodes[1].Segments.Length; i++)
                {
                    if (Nodes[1].Segments[i] == this)
                    {
                        Nodes[1].Segments[i] = newSeg;
                        InWorld.addEvent(Nodes[1].ID, WorldEvent.EventType.NodeChangeState);
                        break;
                    }
                }
            }

            // update this segment's values
            State[0] = SegState.Retracting;
            EndLength[0] = splitPoint;
            refreshEndLoc(1);

            // add segments to the player and world
            InWorld.addSegment(newSeg);

            // add event
            InWorld.addEvent(ID, WorldEvent.EventType.SegChangeState);
        }
예제 #3
0
        private static void readSegmentVars3(List<string[]> commands, World map, Segment segment, List<Segment> segNoID)
        {
            double tempDbl = 0d;
            int tempInt = 0;

            for (int j = 1; j < commands.Count; j++)
            {
                string value = translateValue(commands[j][2], "[Segment]", map, segment);
                bool valueValid = true;
                switch (commands[j][0])
                {
                    case "ID":
                    case "Node":
                        // do nothing
                        break;
                    case "EndLength":
                        valueValid = int.TryParse(commands[j][1], out tempInt);
                        valueValid &= double.TryParse(value, out tempDbl);
                        segment.EndLength[tempInt] = (FInt)tempDbl;
                        segment.refreshEndLoc(1 - tempInt);
                        break;
                    case "Owner":
                    case "State":
                    case "Person":
                        // do nothing
                        break;
                    default:
                        throw new Exception("Segment variable not recognized: " + commands[j][0]);
                }

                if (!valueValid)
                    throw new Exception("Value '" + commands[j][2] + "' not valid for variable '" + commands[j][0] + "'.");
            }

            if (segment.ID != null)
                map.addSegment(segment);
            else
                segNoID.Add(segment);
        }