コード例 #1
0
ファイル: Pathfinder.cs プロジェクト: thecodertom/RTS
    public static List <Point> ASSearch(Point start, Point end, bool *concreteMatrix, int width, int height, int stackSize)
    {
        IPathfinderContext ctx    = ASCreateContext(width, height, stackSize);
        List <Point>       buffer = ASSearch(
            ctx,
            start,
            end,
            concreteMatrix);

        ctx.Dispose();
        return(buffer);
    }
コード例 #2
0
    private void test()
    {
        Point st = Point.Empty;
        Point en = Point.Empty;

        bool[,] m = load("paths/maze.astar", out st, out en);


        //st = new Point(200, 0);
        //en = new Point(999, 999);

        //st = new Point(3, 0);
        //en = new Point(7, 9);

        for (int x = 0; x < 1000; x++)
        {
            for (int y = 0; y < 1000; y++)
            {
                Block *b = translateToPointer(x, y);
                (*b).TypeID = (short)(m[x, y] ? Globals.RESOURCE_WOOD : Globals.TERRAIN_GRASS);
            }
        }

        updateConcreteMatrix();

        IPathfinderContext ctx = Pathfinder.ASCreateContext(p_Width, p_Height);

        while (true)
        {
            int          time = Environment.TickCount;
            List <Point> path = Pathfinder.ASSearch(
                ctx,
                st,
                en,
                p_ConcreteMatrix);

            Console.WriteLine((Environment.TickCount - time) + "ms for " + path.Count + " items");

            for (int c = 0; c < path.Count; c++)
            {
                Point  p  = path[c];
                Block *bl = translateToPointer(p.X, p.Y);
                (*bl).Selected = true;
            }
            break;
        }
    }
コード例 #3
0
ファイル: Pathfinder.cs プロジェクト: thecodertom/RTS
    public static List <Point> ASSearch(IPathfinderContext context, Point start, Point end, bool *concreteMatrix)
    {
        int startX = start.X, startY = start.Y;
        int endX = end.X, endY = end.Y;
        int width  = context.Width;
        int height = context.Height;

        //validate context
        if (!(context is ASContext) || context == null)
        {
            throw new Exception("Invalid A* search context!");
        }
        ASContext ctx = context as ASContext;

        //verify start/end is not collidable!
        bool startCollide = *(concreteMatrix + (startY * width) + startX);
        bool endCollide   = *(concreteMatrix + (endY * width) + endX);

        if (startCollide || endCollide)
        {
            bool resolved = false;

            /*attempt to resolve the collision*/
            if (startCollide)
            {
                resolved = ResolveCollision(ref start, end, width, height, concreteMatrix);
            }
            if (endCollide)
            {
                resolved = ResolveCollision(ref end, start, width, height, concreteMatrix);
            }

            //not resolved?
            if (!resolved)
            {
                return(new List <Point>());
            }
            startX = start.X; startY = start.Y;
            endX   = end.X; endY = end.Y;
        }

        //already there?
        if (startX == endX && startY == endY)
        {
            return(new List <Point>());
        }

        //reset the context
        ASNode *nodeMatrix = ctx.reset(
            concreteMatrix,
            endX,
            endY);

        //get the node at the start and end
        ASNode *startNode = nodeMatrix + (startY * width) + startX;
        ASNode *endNode   = nodeMatrix + (endY * width) + endX;

        //search
        bool found = ctx.search(startNode, endNode);

        if (!found)
        {
            return(new List <Point>());
        }

        //cycle back from the end point all the way back to the start
        //adding each node location on the way.
        //note: since we have done this backwards, just reverse the list
        ASNode *     current = endNode;
        List <Point> buffer  = new List <Point>();

        buffer.Add(end);

        while (current != startNode)
        {
            //get the X/Y from the pointer.
            short x = (short)((current - nodeMatrix) % width);
            short y = (short)((current - nodeMatrix) / width);
            buffer.Add(new Point(x, y));

            //resolve the parent
            ASNode *parent = nodeMatrix + ((*current).ParentY * width) + (*current).ParentX;
            current = parent;
        }

        buffer.Add(start);
        buffer.Reverse();

        return(buffer);
    }
コード例 #4
0
    public Game(GameWindow wnd)
    {
        p_Window = wnd;

        /*hook crash events at the first opportunity.*/
        Application.ThreadException += handleCrash;

        /*fullscreen*/
        p_Window.Shown += delegate(object s, EventArgs e) {
            //p_Window.ToggleFullscreen();
            p_Window.Focus();
            p_Window.BringToFront();

            cmd("warp 0 0");
            cmd("zoom 0 0");

            cmd("toggle debug");
            cmd("toggle debug full");

            cmd("toggle fog");
            cmd("toggle los");

            testCode();
        };

        /*initialize hotloader*/
        p_Hotloader = new Hotloader();

        /*initialize the camera*/
        p_Camera = new Camera(this);

        /*initialize map*/
        p_Map         = new Map(this, 1000, 1000);
        p_MapRenderer = new MapRenderer(this, p_Map, p_Camera);

        /*setup camera position*/
        p_Camera.ZoomAbs(32);
        p_Camera.MoveCenter(250, 250);

        /*init sub-systems*/
        initUI();
        initMouse();
        initPlayers();
        initLogic();
        initDebug();
        initDraw();

        /*hook events*/
        p_Cursor.HookEvents();
        wnd.Click      += handleMouseClick;
        wnd.MouseWheel += handleMouseScroll;
        wnd.MouseMove  += handleMouseMove;
        wnd.MouseUp    += handleMouseUp;
        wnd.KeyDown    += handleKeyDown;
        wnd.KeyUp      += handleKeyUp;
        wnd.Resize     += handleResize;
        wnd.GotFocus   += handleFocusChanged;
        wnd.LostFocus  += handleFocusChanged;

        unsafe {
            IPathfinderContext ctx = Pathfinder.ASCreateContext(p_Map.Width, p_Map.Height);
            while (true)
            {
                break;
                int time = Environment.TickCount;
                //break;
                List <Point> lol = Pathfinder.ASSearch(
                    ctx,
                    Point.Empty,
                    new Point(p_Map.Width - 1, p_Map.Height - 1),
                    p_Map.GetConcreteMatrix(true));

                Console.WriteLine((Environment.TickCount - time) + "ms for " + lol.Count + " items");
            }
        }
        wnd.HookCoreEvents();

        p_Camera.EnableMargin = true;
        p_Camera.SetMargin(10, 10);
    }