Example #1
0
 public MoveEvent(
     RLCreature _actor,
     int _direction,
     RLTile _source = null
 )
 {
     Actor = _actor;
     Source = _source ?? _actor.Tile;
     Direction = _direction;
 }
Example #2
0
 public PathNode(
     PathNode _parent,
     int _score,
     int? _direction)
 {
     Parent = _parent;
     if(Parent != null)
         Tile = _parent.Tile[10-_direction.Value];
     PathScore = _score;
     ParentDirection = _direction;
 }
Example #3
0
 public static void Move(
     RLCreature _actor,
     RLTile _source,
     RLTile _destination
 )
 {
     if(_source != null)
         _source.Creature = null;
     _destination.Creature = _actor;
     _actor.Tile = _destination;
 }
Example #4
0
        public RLTileMap(int _w, int _h)
        {
            map = new RLTile[_w, _h];
            ID = -1;
            Width = _w;
            Height = _h;
            Creatures = new List<RLCreature>();

            //initialize
            for (int _x = 0; _x < _w; _x++)
                for (int _y = 0; _y < _h; _y++)
                    map[_x, _y] = new RLTile(this, _x, _y);
        }
Example #5
0
        public static RLTile LoadTile(
            string _tilestring,
            ref Dictionary<int, RLTileMap> _tilemaps
        )
        {
            RLTile _return;

            List<string> _elements =
                _tilestring.Split(
                    new char[] { ';' } //wtf
            ).ToList();

            List<string> _coord =
                _elements[0].Split(
                    new char[] { ',' }
            ).ToList();

            List<string> _walls =
                _elements[2].Split(
                    new char[] { ',' }
            ).ToList();

            int _x, _y;
            int _mapid; RLTileMap _map;
            //int _creatureid; RLCreature _creature;
            int _north, _west;

            Int32.TryParse(_coord[0], out _x);
            Int32.TryParse(_coord[1], out _y);
            Int32.TryParse(_elements[1], out _mapid);
            _map = _tilemaps[_mapid];
            //Int32.TryParse(_elements[2], out _creatureid);
            //_creature = _creatureid == -1 ? null : _creatures[_creatureid];
            Int32.TryParse(_walls[0], out _north);
            Int32.TryParse(_walls[1], out _west);

            _return = new RLTile(_map, _x, _y);
            //_return.Creature = _creature;
            _return.Creature = null; //setting this in creature loading instead
            _return.North = _north == 1;
            _return.West = _west == 1;

            _map[_x, _y] = _return; //add to tilemap

            return _return;
        }
Example #6
0
 public static List<RLTile> GetTilePath(
     RLTile _start,
     RLTile _destination)
 {
     List<RLTile> _return = new List<RLTile>();
     List<PathNode> _path = GetPath(_start, _destination);
     foreach (PathNode _pn in _path)
     {
         _return.Add(_pn.Tile);
     }
     return _return;
 }
Example #7
0
        public static List<PathNode> GetPath(
            RLTile _start,
            RLTile _destination)
        {
            List<PathNode> _path = new List<PathNode>();

            PathNode _startNode = new PathNode(null);
            _startNode.Tile = _start;
            List<PathNode> _open = new List<PathNode>() { _startNode };
            List<PathNode> _closed = new List<PathNode>();

            PathNode _current = _open[0];

            Func<PathNode, float> birdScore =
                _t =>
                    (float)(
                        Math.Sqrt(
                            Math.Pow(Math.Abs(_t.Tile.X - _destination.X), 2) +
                            Math.Pow(Math.Abs(_t.Tile.Y - _destination.Y), 2)
                        )
                    );

            bool _pathFound = false;

            while (_pathFound == false)
            {
                //add all good neighbours
                for (int _i = 1; _i <= 9; _i++)
                {
                    if (_i == 5) continue;
                    if (
                        _current.Tile[_i] == _destination &&
                        //TRUE FOR DISREGARD
                        _current.Tile.Walkable(_i, true)
                    ) {
                        _pathFound = true;
                        while (_current.Parent != null)
                        {
                            _path.Add(_current);
                            _current = _current.Parent;
                        }
                        List<PathNode> _reversePath = new List<PathNode>();
                        for (int _n = 0; _n < _path.Count; _n++)
                        {
                            _reversePath.Insert(0, _path[_n]);
                            _reversePath[0].ParentDirection =
                                10 - _reversePath[0].ParentDirection;
                        }
                        return _reversePath;
                    }
                    if (
                        //walkable and not already in _open
                        _current.Tile.Walkable(_i) &&
                        !_open.Any(
                            _t => _t.Tile == _current.Tile[_i])
                        )
                    {
                        //create a new node, cleverly pointing back to
                        //the _current node
                        _open.Add(
                            new PathNode(
                                //_current.Tile[_i],
                                _current,
                                _current.PathScore +
                                    ((10 - _i) % 2 == 0 ? 10 : 14),
                                10 - _i
                            )
                        );
                    }
                }
                _open.Remove(_current);
                _closed.Add(_current);

                //no path was found
                if (_open.Count == 0)
                    return null;

                //grab the tile with the best score
                _current = _open.OrderBy(
                    _t => _t.PathScore + birdScore(_t)
                ).ToList()[0];
            }

            throw new Exception("PEBKAC");
        }