Example #1
0
        /// <summary>
        /// Builds normal RR between two specified locations
        /// </summary>
        /// <returns>false if the operation was unsuccessful</returns>
        public static bool Build(Location here, Location there)
        {
            // ensure that nothing is on our way between "from" and "to"
            WorldDefinition world = WorldDefinition.World;
            int             cost;

            if (ComputeRoute(here, there, out cost) == null)
            {
                return(false);
            }

            Direction d = here.getDirectionTo(there);

            while (true)
            {
                TrafficVoxel v = TrafficVoxel.getOrCreate(here);
                if (v == null)
                {
                    Voxel vv = WorldDefinition.World[here];
                    Debug.Assert(vv.Entity.isSilentlyReclaimable);
                    vv.Entity.remove();
                    v = TrafficVoxel.getOrCreate(here);
                }

                Direction dd;
                if (here != there)
                {
                    dd = here.getDirectionTo(there);
                }
                else
                {
                    dd = d;
                }

                if (v.railRoad != null)
                {
                    v.railRoad.Attach(here == there ? d.opposite : d);
                    WorldDefinition.World.OnVoxelUpdated(here);
                }
                else
                {
                    v.railRoad = new SingleRailRoad(v, RailPattern.get(d.opposite, dd));

                    // if this RR is elevated, elect a bridge support.
                    //					if((++cycle%2)==0)
                    BridgePierVoxel.electBridgeSupport(here, typeof(BridgePierVoxel.DefaultImpl), v);
                }

                if (here == there)
                {
                    break;
                }

                d    = dd;
                here = here.toward(there);
            }

            Accounting.AccountGenre.RailService.Spend(cost);    // charge the cost
            return(true);
        }
Example #2
0
        /// <summary>
        /// Removes this platform from the world.
        /// </summary>
        public override void remove()
        {
            WorldDefinition world = WorldDefinition.World;

            onHostDisconnected();


            foreach (YardRailRoad[] yrrs in lanes)
            {
                if (yrrs != null)
                {
                    foreach (YardRailRoad yrr in yrrs)
                    {
                        // canRemove must be true before this method is called.
                        Debug.Assert(yrr.Voxel.car == null);

                        Location loc = yrr.Location;
                        yrr.Voxel.railRoad = null;
                        new SingleRailRoad(
                            TrafficVoxel.getOrCreate(loc),
                            RailPattern.get(direction, direction.opposite));
                        world.OnVoxelUpdated(loc);
                    }
                }
            }

            // remove the platform itself
            foreach (FatPlatformVoxel pv in voxels)
            {
                world.remove(pv.Location);
                world.OnVoxelUpdated(pv.Location);
            }

            base.remove();
        }
Example #3
0
        // for junction rail
        private static RailPattern createJunction(int imageIndexX, int angle1, int angle2, int angle3offset)
        {
            RailPattern p = new RailPattern(12 + imageIndexX, imageIndexX, 1, angle1, angle2, new Size(32, 16), 0);

            p.dir[(angle2 + angle3offset + 8) % 8] = true;
            return(p);
        }
Example #4
0
        /// <summary>
        /// Creates a new slope. A slope consists of four consective
        /// blocks of railroads. The base parameter specifies the location
        /// of the lowest railroad and the direction parameter
        /// specifies the direction to climb.
        ///
        /// The caller must use the canCreateSlope method to check
        /// if this method can be invoked.
        /// </summary>
        public static void createSlope(Location _base, Direction dir)
        {
            Debug.Assert(canCreateSlope(_base, dir));

            // charge the cost before we alter something
            Accounting.AccountGenre.RailService.Spend(calcCostOfNewSlope(_base, dir));

            SlopeEntity entity = new SlopeEntity(_base, dir);

            for (int i = 0; i < 4; i++)
            {
                if (_base.z < WorldDefinition.World.GetGroundLevel(_base))
                {
                    new SlopeRailRoad(entity, TrafficVoxel.getOrCreate(
                                          _base.x, _base.y, _base.z + (i / 2)),
                                      RailPattern.getUGSlope(dir, i));
                    if (i < 2)
                    {
                        // space filler
                        new SlopeFillerVoxel(entity, _base.x, _base.y, _base.z + 1, i);
                    }
                    else
                    {
                        new SlopeSupportVoxel(entity, _base.x, _base.y, _base.z, i,
                                              RailPattern.slopeWalls[dir.index + i - 2]);
                    }
                }
                else
                {
                    new SlopeRailRoad(entity, TrafficVoxel.getOrCreate(
                                          _base.x, _base.y, _base.z + (i / 2)),
                                      RailPattern.getSlope(dir, i));
                    if (i < 2)
                    {
                        // space filler
                        new SlopeFillerVoxel(entity, _base.x, _base.y, _base.z + 1, i);
                    }
                    else
                    {
                        new SlopeSupportVoxel(entity, _base.x, _base.y, _base.z, i,
                                              RailPattern.slopeSupports[dir.index + (i - 2)]);
                    }
                }

                Type bridgeStyle;
                if (dir == Direction.NORTH || dir == Direction.EAST)
                {
                    bridgeStyle = typeof(BridgePierVoxel.DefaultImpl);
                }
                else
                {
                    bridgeStyle = typeof(BridgePierVoxel.SlopeNEImpl);
                }
                BridgePierVoxel.electBridgeSupport(_base, bridgeStyle, entity);

                _base += dir;
            }
        }
Example #5
0
        // for angle rail
        private static RailPattern createSlope(Picture pic, int baseIndexX, int imageIndexX, int upDir, int zdiff, int zangle, bool isVisible)
        {
            RailPattern p = new RailPattern(pic,
                                            baseIndexX + imageIndexX,
                                            isVisible ? imageIndexX : 12,
                                            isVisible ? 2 : 0,
                                            upDir, (upDir + 4) % 8,
                                            isVisible ? new Size(32, 32) : new Size(1, 1), 16);

            p._climbDir = Direction.get(upDir);
            p._level    = imageIndexX % 4;

            p._zdiff  = zdiff;
            p._zangle = Direction.get(zangle);
            return(p);
        }
Example #6
0
        /// <summary>
        /// Removes this platform from the world.
        /// </summary>
        public override void remove()
        {
            WorldDefinition world = WorldDefinition.World;

            onHostDisconnected();

            Location loc = this.location;

            for (int i = 0; i < length; i++, loc += direction)
            {
                new SingleRailRoad(
                    TrafficVoxel.get(loc),
                    RailPattern.get(direction, direction.opposite));
            }

            base.remove();
        }
Example #7
0
        /// <summary>
        /// Computes the route of RRs between specified two points.
        /// </summary>
        /// <returns>
        ///		null if it is impossible to build the route between two.
        ///		Otherwise returns a map from Location to RailPattern.
        /// </returns>
        /// <param name="cost">The total cost of construction will be returned here</param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public static IDictionary ComputeRoute(Location from, Location to, out int cost)
        {
            cost = 0;
            int c;

            Hashtable route = new Hashtable();

            if (from == to)
            {
                return(route);
            }

            Direction dir = null;

            for (Location loc = from; loc != to; dir = loc.getDirectionTo(to).opposite, loc = loc.toward(to))
            {
                Direction dd = loc.getDirectionTo(to);

                c     = calcRailRoadCost(loc, dir, dd);
                cost += c;
                if (c == 0)
                {
                    return(null);
                }

                route.Add(loc, RailPattern.get(dir != null ? dir : dd.opposite, dd));
            }

            c     = calcRailRoadCost(to, dir, null);
            cost += c;
            if (c == 0)
            {
                return(null);
            }

            route.Add(to, RailPattern.get(dir, dir.opposite));

            return(route);
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="here"></param>
        /// <param name="to"></param>
        public override void Build(Location here, Location to)
        {
            Debug.Assert(CanBeBuilt(here, to));

            Direction d = here.getDirectionTo(to);

            while (true)
            {
                if (RailRoad.get(here) == null)
                {
                    MountainVoxel mv = WorldDefinition.World[here] as MountainVoxel;
                    if (mv != null)
                    {
                        // build a tunnel
                        byte[] heights = new byte[4];
                        for (int i = 0; i < 4; i++)
                        {
                            heights[i] = (byte)mv.getHeight(Direction.get(i * 2 + 1));
                        }

                        WorldDefinition.World.remove(here);     // remove this mountain

                        create(TrafficVoxel.getOrCreate(here), d, heights);
                    }
                    else
                    {
                        // build a normal tunnel
                        new SingleRailRoad(TrafficVoxel.getOrCreate(here), RailPattern.get(d, d.opposite));
                    }
                }
                if (here == to)
                {
                    return;
                }
                here = here.toward(to);
            }
        }
Example #9
0
        /// <summary>
        /// If this RR has two RRs with specified direction,
        /// remove it completely. If one RR matches, change
        /// it to a single RR.
        /// </summary>
        public override void Detach(Direction d1, Direction d2)
        {
            if (hasRail(d1) && hasRail(d2))
            {
                Voxel.railRoad = null;
                return;
            }
            if (hasRail(d1) || hasRail(d2))
            {
                // this needs to be changed to a single RR.
                Direction a = null, b = null;   // two directions of such a single RR.
                foreach (Direction x in Direction.directions)
                {
                    if (hasRail(x) && x != d1 && x != d2)
                    {
                        if (a == null)
                        {
                            a = x;
                        }
                        else
                        {
                            b = x;
                        }
                    }
                }
                Debug.Assert(a != null && b != null);

                Location     loc = Location;
                TrafficVoxel v   = Voxel;               // these references can be lost, so use local variables.

                // replace the railroad
                new SingleRailRoad(v, RailPattern.get(a, b));
                WorldDefinition.World.OnVoxelUpdated(loc);

                return;
            }
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="newDir"></param>
        /// <returns></returns>
        public override bool Attach(Direction newDir)
        {
            if (hasRail(newDir))
            {
                return(true);                   // already added
            }
            Direction d1 = Dir1, d2 = Dir2;

            if (IsWellConnected)
            {
                Debug.Assert(is1or3or4(Direction.angle(d1, newDir)) && is1or3or4(Direction.angle(d2, newDir)));
                // if the line is already well connected, make it a junction
                TrafficVoxel v = Voxel;
                v.railRoad = new JunctionRailRoad(v, RailPattern.getJunction(d1, d2, newDir));
                WorldDefinition.World.OnVoxelUpdated(Voxel);
                return(true);
            }
            else
            {
                // if the line is not well connected, change the
                // direction
                if (Direction.angle(d1, newDir) >= 3)
                {
                    pattern = RailPattern.get(d1, newDir);
                    WorldDefinition.World.OnVoxelUpdated(Voxel);
                    return(true);
                }
                if (Direction.angle(d2, newDir) >= 3)
                {
                    pattern = RailPattern.get(d2, newDir);
                    WorldDefinition.World.OnVoxelUpdated(Voxel);
                    return(true);
                }
            }

            return(false);       // unable to attach this.
        }
Example #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="tv"></param>
 /// <param name="pattern"></param>
 protected RailRoad(TrafficVoxel tv, RailPattern pattern)
 {
     this.Voxel     = tv;
     this.pattern   = pattern;
     Voxel.railRoad = this;
 }
Example #12
0
 private SlopeRailRoad(SlopeEntity e, TrafficVoxel v, RailPattern rp)
     : base(v, rp)
 {
     entity = e;
 }
Example #13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="tv"></param>
 /// <param name="p"></param>
 public JunctionRailRoad(TrafficVoxel tv, RailPattern p) : base(tv, p)
 {
 }
Example #14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="tv"></param>
 /// <param name="p"></param>
 public SingleRailRoad(TrafficVoxel tv, RailPattern p) : base(tv, p)
 {
 }
Example #15
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="voxel"></param>
 /// <param name="d"></param>
 public SpecialPurposeRailRoad(TrafficVoxel voxel, Direction d)
     : base(voxel, RailPattern.get(d, d.opposite))
 {
 }