⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setup.cpp

📁 一个VC写A*寻路的程序库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Setup.cpp: implementation of the Setup class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Setup.h"

#include "stdio.h"

#include "math.h" //sqrt

#include "stdlib.h" //for rand
#include <time.h> //for time for srand
#include "string.h"
#include "io.h"  //for _open _close ,etc
#include <fcntl.h> //file constants
#include <sys/stat.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Setup::Setup()
{
	use_terrain=false;
	distance_method=MANHATTAN_DISTANCE;
	cost=1.0f; //0.25f;
	diagonal_cost=1.0f;
	
	iterations_per_frame=1;
	
	directions=8; //4;
	
	median_terrain_cost=1.0f;
	median_terrain_cost_auto=1.0f;
	
	startx=10;
	starty=10;
	
	endx=(BYTE)(WIDTH-21);
	endy=(BYTE)(HEIGHT-21);
	
	for(int y=0;y<HEIGHT;y++)
	{
		for(int x=0;x<WIDTH;x++)
		{
			world[y][x].terrain_cost=0;
		}
	}
	
	//
	colorscheme=COLORSCHEME_BLACK;
	
	//
	options.uniform_cost=false;
	options.terrain_cost=false;
	options.distance=false;
	options.search_directions=false;

	//
	okToPath=true;

	//
	frame=0;
	bigtick.QuadPart=0;

	// presearch
	presearch_toggle=true;
	presearch_maxgroup=1;

		// make the routing lookup table
	DXY[0].y=-1;	DXY[0].x=0;
	DXY[1].y=0;		DXY[1].x=1;
	DXY[2].y=1;		DXY[2].x=0;
	DXY[3].y=0;		DXY[3].x=-1;

	DXY[4].y=-1;	DXY[4].x=1;
	DXY[5].y=1;		DXY[5].x=1;
	DXY[6].y=-1;	DXY[6].x=-1;
	DXY[7].y=1;		DXY[7].x=-1;

	//in case a NO_ROUTE accidentally is passed for lookup
	DXY[8].y=0;		DXY[8].x=0;
}

Setup::~Setup()
{
}

// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
/*
On Sat, 07 Jun 2003 11:36:47 GMT, Eternal Vigilance <wotan@oneeye.com> wrote:
>
>A frequent trick is also to have the edge of the map be BLOCKED nodes so that
>you  can eliminate the test:
>if (x < 0 || x >= width || y < 0 || y >= height)  return;       which rarely
>fails and thus is executed alot....

  Good point.
  
	//		(x<0 || x>=WIDTH || y<0 || y>=HEIGHT) || //if x,y is off edges of map ignore
*/
void Setup::blocking_walls()
{
	register n;
	
	for(n=1;n<HEIGHT-1;n++)
	{
		world[n][0].terrain_cost=IMPASSABLE_TERRAIN_COST;
		world[n][WIDTH-1].terrain_cost=IMPASSABLE_TERRAIN_COST;
	}
	for(n=0;n<WIDTH;n++)
	{
		world[0][n].terrain_cost=IMPASSABLE_TERRAIN_COST;
		world[HEIGHT-1][n].terrain_cost=IMPASSABLE_TERRAIN_COST;
	}
}

void Setup::blank(BYTE elevation)
{
	for(register int y=0;y<HEIGHT;y++)
	{
		for(register int x=0;x<WIDTH;x++)
		{
			world[y][x].terrain_cost=elevation;
		}
	}
}

void Setup::rectangle(int x0, int y0, int x1, int y1, BYTE elevation)
{
	if(x0<0) x0=0;
	if(y0<0) y0=0;
	if(x1>WIDTH-1) x1=WIDTH-1;
	if(y1>HEIGHT-1) y1=HEIGHT-1;
	for(register int y=y0;y<y1;y++)
	{
		for(register int x=x0;x<x1;x++)
		{
			world[y][x].terrain_cost=elevation;
		}
	}
}
void Setup::openrectangle(int x0, int y0, int x1, int y1, BYTE elevation)
{
	if(x0<0) x0=0;
	if(y0<0) y0=0;
	if(x1>WIDTH-1) x1=WIDTH-1;
	if(y1>HEIGHT-1) y1=HEIGHT-1;
	for(int y=y0;y<y1;y++)
	{
		world[y][x0].terrain_cost=elevation;
		world[y][x1].terrain_cost=elevation;
		world[y][x0+1].terrain_cost=elevation;
		world[y][x1-1].terrain_cost=elevation;
	}
	for(int x=x0;x<x1;x++)
	{
		world[y0][x].terrain_cost=elevation;
		world[y1][x].terrain_cost=elevation;
		world[y0+1][x].terrain_cost=elevation;
		world[y1-1][x].terrain_cost=elevation;
	}
}

void Setup::finish_load()
{
	blocking_walls();
	median();
	Presearch();
}


///////////////////////////////////////////////////////////////////////////
// 45,60 - 155,140
// 85,30 - 110, 60
void Setup::Map_BoxOnBoxNoG()
{
	blank(0);
	rectangle(WIDTH/5,HEIGHT/3,WIDTH-WIDTH/5,HEIGHT-HEIGHT/4,IMPASSABLE_TERRAIN_COST);
	rectangle(WIDTH/2-WIDTH/8,HEIGHT/8,WIDTH/2+WIDTH/8,HEIGHT/3,4);
	
	finish_load();
}

void Setup::Map_BoxOnBox()
{
	blank(1);
	rectangle(WIDTH/5,HEIGHT/3,WIDTH-WIDTH/5,HEIGHT-HEIGHT/4,IMPASSABLE_TERRAIN_COST);
	rectangle(WIDTH/2-WIDTH/8,HEIGHT/8,WIDTH/2+WIDTH/8,HEIGHT/3,4);
	
	finish_load();
}

void Setup::Map_CheckerBoard()
{
	blank(1);
	
	for(int y=0;y<HEIGHT;y+=2)
	{
		for(int x=0;x<WIDTH;x+=2)
		{
			world[y][x+((y>>1)&1)].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}
	
	world[starty][startx].terrain_cost=0;
	world[endy][endx].terrain_cost=0;

	finish_load();
}

//
void Setup::Map_Grid()
{
	blank(1);
	
	for(int y=0;y<HEIGHT;y+=2)
	{
		for(int x=1;x<WIDTH;x+=2)
		{
			world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}
	
	world[starty][startx].terrain_cost=0;
	world[endy][endx].terrain_cost=0;

	finish_load();
}

void Setup::Map_PipeMaze()
{
	blank(1);
	for(int x=1;x<WIDTH;x+=2)
	{
		if(x&2)
		{
			for(int y=2;y<HEIGHT;y++) world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
		else
		{
			for(int y=0;y<HEIGHT-2;y++) world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}
	
	world[starty][startx].terrain_cost=0;
	world[endy][endx].terrain_cost=0;

	finish_load();
}

void Setup::Map_Random()
{
	blank(1);
	
	int y,x,c;
	for(int n=0;n<2000;n++)
	{
		y=rand()%HEIGHT;
		x=rand()%WIDTH;
		c=rand()%16;
		world[y][x].terrain_cost=c;
	}
	
	finish_load();
}


void Setup::Map_Clear_Path()
{
	blank(0);

	finish_load();
}

void Setup::Map_No_Path()
{
	for(register int y=0;y<HEIGHT;y++)
	{
		for(register int x=0;x<WIDTH;x++)
		{
			world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}

	finish_load();
}

void Setup::Map_StraightLine()
{
	blank(1);
	
	for(register int y=HEIGHT/2-HEIGHT/20;y<HEIGHT/2+HEIGHT/20;y++)
	{
		for(register int x=WIDTH/4;x<WIDTH-WIDTH/4;x++)
		{
			world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}
	
	finish_load();
}

void Setup::Map_CrashMe()
{
	blank(1);
	
	for(int x=0;x<WIDTH;x+=2)
	{
		if(x&2)
		{
			for(int y=1;y<HEIGHT;y++) world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
		else
		{
			for(int y=0;y<HEIGHT-1;y++) world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
		}
	}
	
	world[starty][startx].terrain_cost=0;
	world[endy][endx].terrain_cost=0;

	finish_load();
}

void Setup::Map_BigBox()
{
	blank(1);
	rectangle(WIDTH/8,HEIGHT/8,WIDTH-WIDTH/8,HEIGHT-HEIGHT/8,IMPASSABLE_TERRAIN_COST);
	
	finish_load();
}

void Setup::Map_RandomBoxes()
{
	blank(1);
	
	int y,x;
	BYTE c;
	for(int n=0;n<2000;n++)
	{
		y=rand()%HEIGHT;
		x=rand()%WIDTH;
		c=rand()%16;
		rectangle(x-5,y-5,x+5,y+5,c);
	}
	
	finish_load();
}

void Setup::Map_RandomOpenBoxes()
{
	blank(1);
	
	int y,x;
	BYTE c;
	for(int n=0;n<2000;n++)
	{
		y=rand()%HEIGHT;
		x=rand()%WIDTH;
		c=rand()%16;
		openrectangle(x-5,y-5,x+5,y+5,c);
	}
	
	finish_load();
}

void Setup::Map_RandomTerrain()
{
	blank(1);
	
	for(int y=0;y<HEIGHT;y++)
	{
	for(int x=0;x<WIDTH;x++)
	{
		world[y][x].terrain_cost=rand()%16;
	}
	}
	
	finish_load();
}


void Setup::Map_Gates()
{
	blank(1);

	for(int y=10;y<HEIGHT-10;y+=10)
	{

	for(int x=10;x<WIDTH-10;x++)
	{
		world[y][x].terrain_cost=IMPASSABLE_TERRAIN_COST;
	}
	
	}

	finish_load();
}

// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////

//
void Setup::Load(char *filename)
{
	//
	BYTE *infile=NULL;
	int inlength=0;
	
	//
	int stream=_open( (const char *)filename, _O_RDONLY | _O_BINARY );
	if(stream!=-1)
	{
		inlength=_filelength(stream);
		
		infile = new BYTE[inlength];
		if(infile)
		{
			memset(infile,0,inlength);
			_read( stream, infile, inlength);
		}
		_close(stream);
	}
	
	
	// B
	if(infile)
	{
		//
		if (inlength<sizeof(Header))
			; //printf("Header length WORD.");
		else
		{
			//
			BYTE* s=(BYTE *)infile;
			
			///////////////////////////////////////////////////////////
			//
			Header.IDLength=READBYTE(s);
			Header.ColorMapType=READBYTE(s);
			Header.ImageType=READBYTE(s);
			
			Header.ColorMapOrigin=READWORD(s);
			Header.ColorMapLength=READWORD(s);
			Header.ColorMapEntrySize=READBYTE(s);
			
			Header.XOrigin=READWORD(s);
			Header.YOrigin=READWORD(s);
			
			Header.Width=READWORD(s);
			Header.Height=READWORD(s);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -