예제 #1
7
파일: Room.cpp 프로젝트: ronw23/prime-osx
//RETURNS: 0 on success, -1 o/w
int
shMapLevel::pushCreature (shCreature *c, shDirection dir)
{
    shDirection dirlist[3];
    int r = RNG (1, 2);
    int i;

    dirlist[0] = dir;
    dirlist[r] = (shDirection) ((dir + 1) % 8);
    dirlist[3-r] = (shDirection) ((dir + 7) % 8);

    for (i = 0; i < 3; i++) {
        int x = c->mX;
        int y = c->mY;
        shDirection dir = dirlist[i];

        moveForward (dir, &x, &y);

        if (isObstacle (x, y) or isOccupied (x, y))
            continue;

        return moveCreature (c, x, y);
    }

    if (c->isHero ()) {
        I->p ("You are crushed between the walls!");
    } else if (Hero.canSee (c)) {
        I->p ("%s is crushed!", THE (c));
    }
    c->die (kMisc, "Crushed by a garbage compactor");
    return -1;
}
예제 #2
0
    int Problem::movAction(MapNode *cur, MovAction_t action, MapNode **next)
    {
        int x1, x2, y1, y2 = 0;

        switch(action)
        {
            case MOV_ACT_RIGHT:
                x1 = cur->getNodeA().getX() + 1;
                y1 = cur->getNodeA().getY();
                x2 = cur->getNodeB().getX();
                y2 = cur->getNodeB().getY() + 1;
                break;

            case MOV_ACT_DOWN:
                x1 = cur->getNodeA().getX();
                y1 = cur->getNodeA().getY() + 1;
                x2 = cur->getNodeB().getX() - 1;
                y2 = cur->getNodeB().getY();
                break;

            case MOV_ACT_LEFT:
                x1 = cur->getNodeA().getX() - 1;
                y1 = cur->getNodeA().getY();
                x2 = cur->getNodeB().getX();
                y2 = cur->getNodeB().getY() - 1;
                break;

            case MOV_ACT_TOP:
                x1 = cur->getNodeA().getX();
                y1 = cur->getNodeA().getY() - 1;
                x2 = cur->getNodeB().getX() + 1;
                y2 = cur->getNodeB().getY();
                break;

            default:
                break;
        }

        if(x1 < 1 || x2 < 1 || x1 > _size || x2 > _size ||
           y1 < 1 || y2 < 1 || y1 > _size || y2 > _size) 
        {
            return NO_OP;
        }

        if(isObstacle(x1, y1) || isObstacle(x2, y2))
        {
            return NO_OP;
        }


        int cost = cur->getPathCost() + getActCost(action);
        *next = new MapNode(x1, y1, x2, y2, cur, action, cost);

        return OP_OK;
    }
예제 #3
0
void
shMapLevel::buildTwistyRooms ()
{
    int i = 1000;
    int n = 0;
    int x, y;

    n = buildTwistyCorridor (RNG (mColumns / 3, 2 * mColumns / 3),
                             4, kSouth);
    while (--i and n < 4 * (mRows + mColumns)) {
        do {
            x = RNG (mColumns);
            y = RNG (mRows);
        } while (!TESTSQ (x, y, kFloor));
        n += buildTwistyCorridor (x, y, (shDirection) (2 * RNG (4)));
        if (n > 4 * (mRows + mColumns)) break;
    }


    buildSnuggledRooms ();
//    wallCorridors ();

    n = RNG (1, 6) + RNG (1, 6) + RNG (1, 6) + RNG (1, 6);
    for (i = 0; i < n; i++) {
        do {
            x = RNG (mColumns);
            y = RNG (mRows);
        } while (!TESTSQ (x, y, kFloor)  or
                 isObstacle (x, y));
        putObject (generateObject (), x, y);
    }

}
예제 #4
0
파일: graph.c 프로젝트: Ka3l/GrandPrix
path aStar(map c, coord p, finish f){
	int end=0;
	int i,j;
	list* open=createList();
	list* closed=createList();
	node* startNode=createNode(p);
	node* current;
	node* neighbor;
	addElement(open,startNode);
	coord bestFinish=getbestFinish(p,f);

	while(!end)
		{
		current= removeElement(open);
		addElement(closed,current);
		end=isFinish(current->coord,f);
		for(i=-1;i<=1;i++)
			{
			for(j=-1;j<=1;j++)
				{
				if(i!=0 || j!=0)
					{
					neighbor=createNode(createCoord(current->coord.x+i, current->coord.y+j));
					if(! (isObstacle(neighbor->coord,c) || contains(closed,neighbor)))
						{
						if(contains(open,neighbor))
							{
							if(current->cost+1 < neighbor->cost)
								{
								neighbor->cost= current->cost+1;
								if(isSand(neighbor->coord,c))
									{
									neighbor->cost += 4;
									}
								neighbor->pred=current;
								}
							}
						else
							{
							neighbor->pred=current;
							neighbor->cost=current->cost+1;
							if(isSand(neighbor->coord,c))
								{
								neighbor->cost += 4;
								}
							neighbor->heuristic=neighbor->cost=current->cost + neighbor->cost+norm(substractCoords(bestFinish,current->coord));
							addSortedElement(open,neighbor);
							}
						}
					}
				}
		}
	}
	freeList(closed);
	freeList(open);
	path path=reconstructPath(current);
	return path;
}
예제 #5
0
bool segmentFeasibility(State<2> a,State<2> b){
	double res=0.05;
	State<2> inc=b-a;
	double l=inc.norm();
	inc=inc*(res/l);
	for(int i=1;i<l/res;++i){
		if(isObstacle(a+inc*i))
			return false;
	}
	return true;
}
void displayAllBullets()
{
	int pos;
	for (int i=0;i<server_data.bullet_count;i++)
        {   

		Bullet b=server_data.bullets[i];
	        switch(b.dir)
		{
			case DOWN:
				b.i = b.i-1;
				break;
			case UP:
				b.i = b.i+1;
				break;
			case RIGHT:
				b.j = b.j+1;
				break;
			case LEFT:
				b.j = b.j-1;
				break;
		}
		if(CheckPlayerCollide(b.i,b.j)!=-1)
		{      
		
		     pos=CheckPlayerCollide(b.i,b.j);
			deleteBullet(i);
			Alien a=server_data.aliens[pos];
			a.energy -=20;	
			server_data.aliens[pos]=a;	
			
		
		if(getCommandoIndex(server_data.bullets[i].pid)!=-1)
		{
			int commpos =getCommandoIndex(server_data.bullets[i].pid);
			 server_data.commandos[commpos].score +=100; 
			
		}
		if(getAlienIndex(server_data.bullets[i].pid)!=-1)
		{
			int alienpos =getAlienIndex(server_data.bullets[i].pid);
			 server_data.aliens[alienpos].score -=100; 
			
		}
			

			if(a.energy <= 0)
			{server_data.map_matrix[a.i][a.j]=NONE;
			deleteAlien(pos);}
		 
				
		}
		else if(checkAppleCollide(b.i,b.j)!=-1)
		{	
			pos=checkAppleCollide(b.i,b.j);

			deleteBullet(i);

			destroyGenerateApple(pos);
			server_data.map_matrix[b.i][b.j]=NONE;
		}
		else if(isObstacle(b.i,b.j))
		{

			deleteBullet(i);
			server_data.map_matrix[b.i][b.j]=NONE;
		}
		else if(checkBulletCollide(b.i,b.j)!=-1)
		{
			pos=checkBulletCollide(b.i,b.j);

			if(i>pos)
			{
			deleteBullet(i);
			deleteBullet(pos);
			}
			else
			{
			deleteBullet(pos);
			deleteBullet(i);
			}
			server_data.map_matrix[b.i][b.j]=NONE;
		}
		if(checkCommandoCollide(b.i,b.j)!=-1)
		{       
		
			pos=checkCommandoCollide(b.i,b.j);
			
			Commando c=server_data.commandos[pos];
			c.energy -=20;	
			server_data.commandos[pos]=c;
			
			
			
		if(getCommandoIndex(server_data.bullets[i].pid)!=-1)
		{
			int commpos =getCommandoIndex(server_data.bullets[i].pid);
			 server_data.commandos[commpos].score -=100; 
			
		}
		if(getAlienIndex(server_data.bullets[i].pid)!=-1)
		{
			int alienpos =getAlienIndex(server_data.bullets[i].pid);
			 server_data.aliens[alienpos].score +=100; 
			
		}
			
   			
			if(c.energy <= 0)
			{server_data.map_matrix[c.i][c.j]=NONE;
			deleteCommando(pos);}
			deleteBullet(i);
		
		}
		else
		{
			server_data.bullets[i]=b;
			if(b.i<0 || b.i>(ROWS-1) ||b.j <0 || b.j >(COLUMNS-1) )
			{
				deleteBullet(i);
			}
		}
	}
}
예제 #7
0
void demo_7_main_core_0() {

	int threads = TOTAL_PROC_NUM;

	// Initialize obstacle map from heightmap
	if (1) {
		logStart("initialize obstacle map");

		// Recode heightmap to global_obstacles
		int a, b, c;

		for (a = 0; a < WORLD_SIZE_X; a++) {
			for (b = 0; b < WORLD_SIZE_Y; b++) {
				int height = getHeightmapValue(a, b);

				// some regions cannot be flown over
				if (height > 174)
					height = 255;

				for (c = 0; c < height; c++) {
					global_obstacles[a][b][c] = 1;
				}
				for (c = height; c < WORLD_SIZE_Z; c++) {
					global_obstacles[a][b][c] = 0;
				}
			}
		}

		logEnd("initialize obstacle map");
	}

	// Set goal
	setGoal(WORLD_SIZE_X - 3, WORLD_SIZE_Y - 3, getHeightmapValue(WORLD_SIZE_X - 3, WORLD_SIZE_Y - 3) + 2);

	// Set current position
	setPosition(3, 3, getHeightmapValue(3, 3) + 2);

	// Init weightmap from obstacle map
	if (1) {
		int a, b, c;

		logStart("initialize weightmap from obstacle map");

		for (a = 0; a < WORLD_SIZE_X; a++) {
			for (b = 0; b < WORLD_SIZE_Y; b++) {
				for (c = 0; c < WORLD_SIZE_Z; c++) {
					if (isGoal(a, b, c)) {
						world[a][b][c] = WEIGHT_SINK;
					} else if (isObstacle(a, b, c)) {
						world[a][b][c] = WEIGHT_OBSTACLE;
					} else {
						world[a][b][c] = WEIGHT_INIT;
					}
				}
			}
		}

		logEnd("initialize weightmap from obstacle map");
	}

	// Propagate weights with selected algorithm
	promote_world(ITERATIONS, threads);

	// Find waypoints to goal
	findWaypoints();

	printf("Iterations: %i\n", ITERATIONS);

	return;
}
예제 #8
0
void findWaypoints() {
	int position[3]; // Current position
	int i;
	double path_length = 0; // Total length of the path
	int path_steps = 0; // Total length of the path in steps

	logStart("findWaypoints");

	position[0] = getPosition()[0];
	position[1] = getPosition()[1];
	position[2] = getPosition()[2];

	for (i = 0; i < 1024; i++) {
		int a, b, c;

		double best_value = world[position[0]][position[1]][position[2]]; // So far best weigt == current weight
		int next[3] = { -1, -1, -1 }; // Next position

		for (a = -1; a <= 1; a++) {
			if (position[0] + a >= 0 && position[0] + a < WORLD_SIZE_X) {
				for (b = -1; b <= 1; b++) {
					if (position[1] + b >= 0 && position[1] + b < WORLD_SIZE_Y) {
						for (c = -1; c <= 1; c++) {
							if (position[2] + c >= 0 && position[2] + c < WORLD_SIZE_Z) {
								if (!isObstacle(position[0] + a, position[1] + b, position[2] + c)) {
									double new_value = world[position[0] + a][position[1] + b][position[2] + c];
									/* if (LL_DEBUG || 1)
									 printf(
									 "Surrounding point [%i, %i, %i] has value %2.12f (%e) - best know has value %2.12f (%e)\n",
									 position[0] + a, position[1]
									 + b, position[2] + c,
									 new_value, best_value); */
									if (new_value > best_value) {
										best_value = new_value;
										next[0] = position[0] + a;
										next[1] = position[1] + b;
										next[2] = position[2] + c;
									}
								}
							}
						}
					}
				}
			}
		}

		printf("Next waypoint: [%i, %i, %i] with value %2.12f (%e)\n", next[0], next[1], next[2], best_value,
				best_value);

		// Calculate path length
		int del_0 = position[0] - next[0];
		int del_1 = position[1] - next[1];
		int del_2 = position[2] - next[2];

		del_0 *= del_0;
		del_1 *= del_1;
		del_2 *= del_2;

		if (del_0 + del_1 + del_2 == 1)
			path_length += 1;
		if (del_0 + del_1 + del_2 == 2)
			path_length += 1.414; // SQRT(2)
		if (del_0 + del_1 + del_2 == 3)
			path_length += 1.732; // SQRT(3)

		path_steps++;

		if (isGoal(next[0], next[1], next[2]))
			break;
		else {
			// Set found waypoint as next position
			position[0] = next[0];
			position[1] = next[1];
			position[2] = next[2];
		}

	}

	printf("Total path length: %f\n", path_length);
	printf("Total path length in steps: %i\n", path_steps);

	logEnd("findWaypoints");
}
예제 #9
0
 bool CostMap2D::isObstacle(unsigned int mx, unsigned int my) const {
   unsigned int ind = MC_IND(mx, my);
   return isObstacle(ind);
 }
예제 #10
0
파일: Vision.cpp 프로젝트: orbisvicis/prime
/* Computes all the squares visible by the creature */
void
shMapLevel::computeVisibility (shCreature *eyes)
{
    int slope;     /* slope in v coordinate */
    int u, v;
    int x, y;
    int corner;
    int min, max;     /* width of beam along v axis */

    //I->debug ("**** computing vis ****");

    for (x = 0; x < MAPMAXCOLUMNS; x++) {
        for (y = 0; y < MAPMAXROWS; y++) {
            mVisibility[x][y] = 0;
        }
    }

    /* orthogonal first */

    mVisibility[eyes->mX][eyes->mY] = 1;

    if (eyes->mZ < 0) {
        /* in a pit */
        if (isObstacle (eyes->mX, eyes->mY))
            return; /* sealed in */
        if (eyes-> isUnderwater ())
            return;
        for (x = eyes->mX-1; x <= eyes->mX+1; x++) {
            for (y = eyes->mY-1; y < eyes->mY+1; y++) {
                mVisibility[x][y] = 1;
            }
        }
        return;
    }

    for (x = eyes->mX;  x < MAPMAXCOLUMNS and !isOcclusive (x, eyes->mY);  x++)
        mVisibility[x][eyes->mY] = 1;
    for (x = eyes->mX;  x >= 0 and !isOcclusive (x, eyes->mY);  x--)
        mVisibility[x][eyes->mY] = 1;
    for (y = eyes->mY;  y < MAPMAXROWS and !isOcclusive (eyes->mX, y);  y++)
        mVisibility[eyes->mX][y] = 1;
    for (y = eyes->mY;  y >= 0 and !isOcclusive (eyes->mX, y);  y--)
        mVisibility[eyes->mX][y] = 1;

    /* now each quadrant */

#define BEAMS 32
#define MAXD 100

    for (slope = 1; slope < BEAMS; slope++) {
        for (u = 1, v = slope, min = 0, max = BEAMS-1;
             u < MAXD and min <= max;
             u++, v += slope)
        {
            y = v / BEAMS;
            x = u - y;
            x += eyes->mX;
            y += eyes->mY;

            if (x >= MAPMAXCOLUMNS or y >= MAPMAXROWS)
                break;

            corner = BEAMS - v % BEAMS;

            if (min < corner) {
                mVisibility[x][y] = 1;
                if (isOcclusive (x,y)) {
                    min = corner;
                }
            }
            if (max > corner and y < MAPMAXROWS-1) {
                mVisibility[x-1][y+1] = 1;
                if (isOcclusive (x - 1, y + 1)) {
                    max = corner;
                }
            }
        }
    }
    for (slope = 1; slope < BEAMS; slope++) {
        for (u = 1, v = slope, min = 0, max = BEAMS-1;
             u < MAXD and min <= max;
             u++, v += slope)
        {
            y = v / BEAMS;
            x = u - y;
            x = eyes->mX - x;
            y += eyes->mY;

            if (x < 0 or y >= MAPMAXROWS)
                break;

            corner = BEAMS - v % BEAMS;

            if (min < corner) {
                mVisibility[x][y] = 1;
                if (isOcclusive (x,y)) {
                    min = corner;
                }
            }
            if (max > corner and y < MAPMAXROWS-1) {
                mVisibility[x+1][y+1] = 1;
                if (isOcclusive (x + 1, y + 1)) {
                    max = corner;
                }
            }
        }
    }

    for (slope = 1; slope < BEAMS; slope++) {
        for (u = 1, v = slope, min = 0, max = BEAMS-1;
             u < MAXD and min <= max;
             u++, v += slope)
        {
            y = v / BEAMS;
            x = u - y;
            x = eyes->mX - x;
            y = eyes->mY - y;

            if (x < 0 or y < 0)
                break;

            corner = BEAMS - v % BEAMS;

            if (min < corner) {
                mVisibility[x][y] = 1;
                if (isOcclusive (x,y)) {
                    min = corner;
                }
            }
            if (max > corner and y > 0) {
                mVisibility[x+1][y-1] = 1;
                if (isOcclusive (x + 1, y - 1)) {
                    max = corner;
                }
            }
        }
    }

    for (slope = 1; slope < BEAMS; slope++) {
        for (u = 1, v = slope, min = 0, max = BEAMS-1;
             u < MAXD and min <= max;
             u++, v += slope)
        {
            y = v / BEAMS;
            x = u - y;
            x = x + eyes->mX;
            y = eyes->mY - y;

            if (x >= MAPMAXCOLUMNS or y < 0)
                break;

            corner = BEAMS - v % BEAMS;

            if (min < corner) {
                mVisibility[x][y] = 1;
                if (isOcclusive (x,y)) {
                    min = corner;
                }
            }
            if (max > corner and y > 0) {
                mVisibility[x-1][y-1] = 1;
                if (isOcclusive (x - 1, y - 1)) {
                    max = corner;
                }
            }
        }
    }

    /* corner peeking */
    if (eyes->mY > 1) {
        for (x = eyes->mX, y = eyes->mY-1;
             x < MAPMAXCOLUMNS-1 and !isOcclusive (x, y) ;
             x++)
        {
            mVisibility[x][y-1] = mVisibility[x+1][y-1] =
                mVisibility[x+1][y] = 1;
        }
        for (x = eyes->mX, y = eyes->mY-1; x > 0 and !isOcclusive (x, y);
             x--)
        {
            mVisibility[x][y-1] = mVisibility[x-1][y-1] =
                mVisibility[x-1][y] = 1;
        }
    }
    if (eyes->mY < MAPMAXROWS - 2) {
        for (x = eyes->mX, y = eyes->mY+1;
             x < MAPMAXCOLUMNS-1 and !isOcclusive (x, y) ;
             x++)
        {
            mVisibility[x][y+1] = mVisibility[x+1][y+1] =
                mVisibility[x+1][y] = 1;
        }
        for (x = eyes->mX, y = eyes->mY+1; x > 0 and !isOcclusive (x, y);
             x--)
        {
            mVisibility[x][y+1] = mVisibility[x-1][y+1] =
                mVisibility[x-1][y] = 1;
        }
    }
    if (eyes->mX > 1) {
        for (x = eyes->mX-1, y = eyes->mY;
             y < MAPMAXROWS-1 and !isOcclusive (x, y);
             y++)
        {
            mVisibility[x-1][y] = mVisibility[x-1][y+1] =
                mVisibility[x][y+1] = 1;
        }
        for (x = eyes->mX-1, y = eyes->mY; y > 0 and !isOcclusive (x, y);
             y--)
        {
            mVisibility[x-1][y] = mVisibility[x-1][y-1] =
                mVisibility[x][y-1] = 1;
        }
    }
    if (eyes->mX < MAPMAXCOLUMNS - 2) {
        for (x = eyes->mX+1, y = eyes->mY;
             y < MAPMAXROWS-1 and !isOcclusive (x, y);
             y++)
        {
            mVisibility[x+1][y] = mVisibility[x+1][y+1] =
                mVisibility[x][y+1] = 1;
        }
        for (x = eyes->mX+1, y = eyes->mY; y > 0 and !isOcclusive (x, y);
             y--)
        {
            mVisibility[x+1][y] = mVisibility[x+1][y-1] =
                mVisibility[x][y-1] = 1;
        }
    }
}
예제 #11
0
void Astar_planning(){
	ROS_INFO("A star planning");
	std::priority_queue<PQItem> open_list;
	const unsigned num_cells = local_map->info.height*local_map->info.width;
	std::vector<bool> seen(num_cells); // Default initialized to all false
	const occupancy_grid_utils::index_t dest_ind = occupancy_grid_utils::pointIndex(local_map->info,goal.point);
	const occupancy_grid_utils::index_t src_ind = occupancy_grid_utils::pointIndex(local_map->info,statePoint(startState));
	open_list.push(PQItem(src_ind, 0, h(src_ind),src_ind));

	std::vector<occupancy_grid_utils::index_t> parent(num_cells,0);

	while (!open_list.empty()) {
	    const PQItem current = open_list.top();
	    open_list.pop();
	    const occupancy_grid_utils::Cell c = occupancy_grid_utils::indexCell(local_map->info, current.ind);
	    if (seen[current.ind])
	      continue;
	    parent[current.ind] = current.parent_ind;
	    seen[current.ind] = true;
	    ROS_DEBUG_STREAM_NAMED ("shortest_path_internal", "  Considering " << c << " with cost " <<
		                    current.g_cost << " + " << current.h_cost);
	    if (current.ind == dest_ind) {
		std::cout << "solution found" << std::endl;
	        std::cout << "Visited " << std::count(seen.begin(), seen.end(), true) << " states out of " << num_cells << std::endl;;
		std::deque<occupancy_grid_utils::index_t> path;
		path.push_back(dest_ind);
		occupancy_grid_utils::index_t last = dest_ind;
		while (parent[last]!=src_ind){
			path.push_front(parent[last]);
			last=parent[last];
		}
		/*for(std::deque<occupancy_grid_utils::index_t>::iterator it=path.begin(),end=path.end();it!=end;++it){
			std::cout << occupancy_grid_utils::indexCell(local_map->info, *it) << " -- ";
		}
		std::cout << std::endl;*/
		current_path_raw.resize(path.size());
		std::transform(path.begin(), path.end(), current_path_raw.begin(), &indexState);
		/*std::cout << "Path length: " << current_path_raw.size() << std::endl;
		std::cout << "Path cost: " << current.g_cost << std::endl;
		std::cout << "Path :" << std::endl;
		for(std::deque<State<2>>::iterator it=current_path_raw.begin(),end=current_path_raw.end();it!=end;++it){
			std::cout << (*it) << " -- ";
		}
		std::cout << std::endl;*/
		planned=true;
		return;
	    }
	      
	    for (int d=-1; d<=1; d+=2) {
		for (int vertical=0; vertical<2; vertical++) {
			const int cx = c.x + d*(1-vertical);
			const int cy = c.y + d*vertical;
			if (cx>=0 && cy>=0) {
				const occupancy_grid_utils::Cell c2((occupancy_grid_utils::coord_t) cx, (occupancy_grid_utils::coord_t) cy);
				if (withinBounds(local_map->info, c2)) {
					const occupancy_grid_utils::index_t ind = cellIndex(local_map->info, c2);
					if (!isObstacle(pointState(indexPoint(ind))) && !seen[ind]) {
						open_list.push(PQItem(ind, current.g_cost + g(ind), h(ind), current.ind));
					}
					//ROS_DEBUG_STREAM_COND_NAMED (g.data[ind]!=UNOCCUPIED, "shortest_path_internal",
					//		 "  Skipping cell " << indexCell(g.info, ind) <<
					//		 " with cost " << (unsigned) g.data[ind]);
				}
			}
		}
	    }
	  }
	planning=false;
	ROS_INFO("Planning failed");
}
예제 #12
0
 bool isValid(const ob::State* s) const
 {
     return !isObstacle(obstateState(s));
 }
예제 #13
0
void MutableMap::toggle(unsigned _x, unsigned _y) {
    setObstacle(_x,_y,isObstacle(_x,_y)?false:true);
}
예제 #14
0
파일: AI.cpp 프로젝트: Jenaral/AI_Tank
//平台从第1回合开始调用此函数获得每回合指令,请勿修改此函数声明。
extern "C" Order makeOrder(DataForAI data)
{
	//printf("===================round%d tank%d==================\n",data.round,data.myID);
	Order order;
	order.type=STOP;
	mydata=data;
	updateResource(data);
	if(data.round==1&&data.myID==0)
	{
		assignResource(data);
		//getNext(data);
	}

	short range=data.tank[data.myID].range;
	//printf("range:%d\n",range);
	short vision=range;
	Point me;
	me.row=data.tank[data.myID].row;
	me.col=data.tank[data.myID].col;
	for(int i=-vision;i<=vision;i++)
	{
		for(int j=-vision;j<=vision;j++)
		{
			if(abs(i)+abs(j)<=vision)//in vision
			{
				short nr=me.row+i,nc=me.col+j;
				Point target;
				target.row=nr;
				target.col=nc;
				if(isInMap(nr,nc))//in map
					if(data.map[nr][nc].whoIsHere!=-1)//tank detected
					{
						short n=data.map[nr][nc].whoIsHere;
						if(data.tank[n].flag!=data.myFlag)
						{
							//printf("enemy tank%d\n",n);
							order.row=nr;
							order.col=nc;
							order.type=FIRE;
							return order;
						}

					}
					else if((data.map[nr][nc].type==BREAKBRICK||data.map[nr][nc].type==BRICK)&&isObstacle(target,nextStep[data.myID]))//bricks detected
					{
						//printf("tank%dbricks\n",data.myID);
						order.row=nr;
						order.col=nc;
						order.type=FIRE;
						return order;

					}
			}
		}
	}

	//

	//printf("the nearest point for tank%d (%d,%d)\n",data.myID,nearestS[data.myID].row,nearestS[data.myID].col);
	//assert(nearestS[data.myID].row>0);
	if(data.tank[data.myID].life!=0)
	{
		clock_t tstart,tend;
		float timeuse;
		tstart=clock();

		if(data.map[nearestS[data.myID].row][nearestS[data.myID].col].isHereSource==RedSource||data.map[data.tank[data.myID].row][data.tank[data.myID].col].isHereSource==RedSource||nextStep[data.myID].empty())
		{
			getNearestRS(data);
		}
		getNext(data);
		tend=clock();
		timeuse=(tend-tstart)/CLOCKS_PER_SEC;
		//printf("round%d tank%dchange resource(%d,%d) and next step:(%d,%d),%f seconds used\n",data.round,data.myID,nearestS[data.myID].row,nearestS[data.myID].col,next[data.myID].row,next[data.myID].col,timeuse);
	};

	/*
	if(data.map[next[data.myID].row][next[data.myID].col].whoIsHere!=-1||next[data.myID].row==-1)
	{
	while(1)
	{
	srand((int)time(0));
	short i=rand()%4;
	short nr=data.tank[data.myID].row+dir[i][0],nc=data.tank[data.myID].col+dir[i][1];
	if(data.map[nr][nc].type==PERVIOUS&&data.map[nr][nc].whoIsHere!=-1)
	{
	next[data.myID].row=nr;
	next[data.myID].col=nc;
	break;
	}
	}
	}
	*/
	short x=next[data.myID].row-data.tank[data.myID].row,y=next[data.myID].col-data.tank[data.myID].col;
	//printf("(x,y) for tank%d\n",x,y,data.myID);
	/*if(x<0)x=-1;
	else if(x>0)x=1;
	if(y<0)y=-1;
	else if(y>0)y=1;*/
	for(int i=0;i<4;i++)
	{
		if(dir[i][0]==x&&dir[i][1]==y)
		{

			order.type=direction[i];
			//printf("tank%d is going %s\n",data.myID,ds[i]);
			return order;
		}
		else
		{	
			order.type=STOP;
		}
		//random step
		/*
		else
		{
		srand((int)time(0));
		order.type=direction[rand()%4];
		return order;

		}*/
	}
	return order;
}
void moveAutoPlayers()
{
  int key;
	for (int i=0;i<server_data.alien_count;i++)
        { 
        	if(server_data.aliens[i].isauto)
          {		   
		int nearerpos;
		Alien a=server_data.aliens[i];
		int pos;
		
		int previ=a.i,prevj=a.j;
		
		if(server_data.commando_count ==0) break;

  	int min=0;;	
	    for(int pos=0;pos<server_data.commando_count;pos++)
	{
				
		nearerpos=0;
		if(min>calculate_distance(a.i,a.j,server_data.commandos[pos].i,server_data.commandos[pos].j))			
		{min=calculate_distance(a.i,a.j,server_data.commandos[pos].i,server_data.commandos[pos].j); nearerpos=pos;}

	}


		if(timercount%5==0)
		{
		if(a.i > server_data.commandos[nearerpos].i) {key =0;}
		else if(a.i < server_data.commandos[nearerpos].i) {key =1;} 
		else if(a.j < server_data.commandos[nearerpos].j) {key=3;
		}
		else if(a.j > server_data.commandos[nearerpos].j) {key=2;
					
		}
		 
		switch (key)
    		{
	
    			case 0:
				if(a.dir!=DOWN)a.dir=DOWN;
				else if(a.i>0)
				a.i--;
        			break;
			    case 1:
				if(a.dir!=UP)a.dir=UP;
				else if(a.i<(ROWS-1))
					a.i++;		
				break;
			    case 2:
				if(a.dir!=LEFT)a.dir=LEFT;
				else if(a.j>0)
					a.j--;
				
				break;
			    case 3:
				if(a.dir!=RIGHT)a.dir=RIGHT;
				else if(a.j<(COLUMNS-1))
					a.j++;
				break;
	
			 
    		}
		
		
		
		 if(!(isObstacle(a.i,a.j)))
    		{
			if(checkAppleCollide(a.i,a.j)!=-1)
			{
				Bullet b;
	 			b.i = previ;
				b.j = prevj;
				b.dir=a.dir;
				b.pid=a.pid;
				addBullet(b);
				
			}
			else if(checkBulletCollide(a.i,a.j)!=-1)
			{
				a.energy -=20;
				
		int bulletpos =checkBulletCollide(a.i,a.j);
		if(getCommandoIndex(server_data.bullets[bulletpos].pid)!=-1)
		{
			int commpos =getCommandoIndex(server_data.bullets[bulletpos].pid);
			 server_data.commandos[commpos].score +=100; 
			
		}
		if(getAlienIndex(server_data.bullets[bulletpos].pid)!=-1)
		{
			int alienpos =getAlienIndex(server_data.bullets[bulletpos].pid);
			 server_data.aliens[alienpos].score -=100; 
			
		}
				
			        if(a.energy <=0)
				{server_data.map_matrix[a.i][a.j]=NONE;}
			}
			else if(CheckPlayerCollide(a.i,a.j)!=-1 && CheckPlayerCollide(a.i,a.j) !=i)
				{       
					
				}
			else
			{
				if(checkCommandoCollide(a.i,a.j)==-1)
				{
				//server_data.map_matrix[previ][prevj]=NONE;
				server_data.map_matrix[a.i][a.j]=BOOKED;
				a.energy -=1;
				server_data.aliens[i]=a;}
				else
				{Bullet b;
	 			b.i = previ;
				b.j = prevj;
				b.dir=a.dir;
				b.pid=a.pid;
				addBullet(b);
				}
			}
			
    		}
    		else
		{		Bullet b;
	 			b.i = previ;
				b.j = prevj;
				b.dir=a.dir;
				b.pid=a.pid;
				addBullet(b);	}
		
		if(a.energy <= 0)
		{deleteAlien(i);}
		
    
				
		if(((key == 2 || key ==3 ) && (!aliensenserhorizontal(nearerpos,i)) )|| (prevj = server_data.commandos[nearerpos].j) && (!aliensenservertical(nearerpos,i)) )  {
		Bullet b;
	 	b.i = a.i;
		b.j = a.j;
		b.dir=a.dir;
		b.pid=a.pid;
		addBullet(b);	}
			
		}
		
      }
      }
      
      for (int i=0;i<server_data.commando_count;i++)
        { 
        
        
        if(server_data.commandos[i].isauto)
          {
        	
		int nearerpos;
		Commando a=server_data.commandos[i];
		int pos;
		int previ=a.i,prevj=a.j;
		
	
 	if(server_data.alien_count ==0) break;
  	 int min=0;
  	 
	    for(int pos=0;pos<server_data.alien_count;pos++)
	{
				
		nearerpos=0;
		if(min>calculate_distance(a.i,a.j,server_data.aliens[pos].i,server_data.aliens[pos].j))			
		{min=calculate_distance(a.i,a.j,server_data.aliens[pos].i,server_data.aliens[pos].j); nearerpos=pos;}

	}
 

		if(timercount%5==0)
		{
		if(a.i > server_data.aliens[nearerpos].i) {key =0;}
		else if(a.i < server_data.aliens[nearerpos].i) {key =1;} 
		else if(a.j < server_data.aliens[nearerpos].j) {key=3;
		}
		else if(a.j > server_data.aliens[nearerpos].j) {key=2;
					
		}
		 
		switch (key)
    		{
	
    			case 0:
				if(a.dir!=DOWN)a.dir=DOWN;
				else if(a.i>0)
				a.i--;
        			break;
			    case 1:
				if(a.dir!=UP)a.dir=UP;
				else if(a.i<(ROWS-1))
					a.i++;		
				break;
			    case 2:
				if(a.dir!=LEFT)a.dir=LEFT;
				else if(a.j>0)
					a.j--;
				
				break;
			    case 3:
				if(a.dir!=RIGHT)a.dir=RIGHT;
				else if(a.j<(COLUMNS-1))
					a.j++;
				break;
	
			 
    		}
		
		
		
		 if(!(isObstacle(a.i,a.j)))
    		{
			if(checkAppleCollide(a.i,a.j)!=-1)
			{
				
				
				server_data.map_matrix[server_data.commandos[i].i][server_data.commandos[i].j]=NONE;
		int pos_app=checkAppleCollide(a.i,a.j);
		destroyGenerateApple(pos_app);
		if(server_data.commandos[i].energy <= 100 && server_data.commandos[i].energy >= 80) {server_data.commandos[i].energy =100;}
		else if(server_data.commandos[i].energy < 80){
		server_data.commandos[i].energy+=20;
		}
		server_data.commandos[i].score+=100;
		server_data.commandos[i].i=a.i;
		server_data.commandos[i].j=a.j;
		server_data.map_matrix[a.i][a.j]=BOOKED;
		server_data.commandos[i]=a;
		
		
		
		
		
				
			}
			else if(checkBulletCollide(a.i,a.j)!=-1)
			{
				
				a.energy -=25;
				
		int bulletpos =checkBulletCollide(a.i,a.j);
		if(getCommandoIndex(server_data.bullets[bulletpos].pid)!=-1)
		{
			int commpos =getCommandoIndex(server_data.bullets[bulletpos].pid);
			 server_data.commandos[commpos].score -=100; 
			
		}
		if(getAlienIndex(server_data.bullets[bulletpos].pid)!=-1)
		{
			int alienpos =getAlienIndex(server_data.bullets[bulletpos].pid);
			 server_data.aliens[alienpos].score +=100; 
			
		}
				
			      if(a.energy <=0)
				{
				
				server_data.map_matrix[a.i][a.j]=NONE;}
				
						
				
			}
			else if(checkCommandoCollide(a.i,a.j)!=-1 && checkCommandoCollide(a.i,a.j) !=i)
				{       
					
					
				}
			else
			{
				
				if(CheckPlayerCollide(a.i,a.j)==-1)
				{
			//	server_data.map_matrix[previ][prevj]=NONE;
				server_data.map_matrix[a.i][a.j]=BOOKED;
				server_data.commandos[i]=a;
									
				}
				
				else
				{Bullet b;
	 			b.i = previ;
				b.j = prevj;
				b.dir=a.dir;
				b.pid=a.pid;
				addBullet(b);
				
				}
				
										
			}
    		}
    		else{Bullet b;
	 	b.i = previ;
		b.j = prevj;
		b.dir=a.dir;
		b.pid=a.pid;
		addBullet(b);
		}
		
		if(a.energy <= 0)
		{deleteCommando(i);}
		
		
	//	printf("asfasfasdfas*****  %d \n  ",aliensenserhorizontal(nearerpos,i)));
				
		
		if(((key == 2 || key ==3 ) && (!commandosenserhorizontal(nearerpos,i)) )|| (a.j == server_data.aliens[nearerpos].j) && (!commandosenservertical(nearerpos,i)) )  {
		
		Bullet b;
	 	b.i = a.i;
		b.j = a.j;
		b.dir=a.dir;
		b.pid=a.pid;
		addBullet(b);}
			
		}
		
      }
      
     } 
      
      
      
	
	
}
예제 #16
0
파일: Room.cpp 프로젝트: ronw23/prime-osx
void
shMapLevel::mundaneRoom (int sx, int sy, int ex, int ey)
{
    int i, npiles, n;
    int x, y;

    if (mDLevel != TOWNLEVEL) {
        if (RNG (mDLevel + 6) > 9) {
            darkRoom (sx, sy, ex, ey);
        }

        if (!RNG (30)) {
            /* Radioactive room. */
            for (x = sx + 1; x < ex; x++) {
                for (y = sy + 1; y < ey; y++) {
                    mSquares[x][y].mFlags |= shSquare::kRadioactive;
                }
            }
        }
    }

    /* treasure expectation per room:
       version 0.3:
         0.375 piles x 1.1875 obj per pile +
         (~) 0.125 niches x 0.8 obj per niche
         ~ .5453125 objs per room

       version 0.2.9:
         0.375 piles x 1.375 obj per pile
         = .515625 obj per room
   */

    npiles = RNG (4) ? 0 : RNG (2) + RNG (2) + RNG (2);

    while (npiles--) {
        x = RNG (sx + 1, ex - 1);
        y = RNG (sy + 1, ey - 1);
        if (TESTSQ (x, y, kFloor)  and !isObstacle (x, y)) {
            n = RNG (10) ? 1 : RNG (2, 3);
            for (i = 0; i < n; i++) {
                putObject (generateObject (mDLevel), x, y);
            }
        }
    }

    if (0 == RNG (9)) {
        x = RNG (sx + 1, ex - 1);
        y = RNG (sy + 1, ey - 1);
        if (TESTSQ (x, y, kFloor) and !isObstacle (x, y) and
            0 == countObjects (x, y))
        {
            addVat (x, y);
        }
    }

    /* Reticulans of PRIME are weak enough to get instakilled in a pit.
       Do not place traps at all on first level. */
    while (!RNG (12) and mDLevel > 1) {
        x = RNG (sx + 1, ex - 1);
        y = RNG (sy + 1, ey - 1);
        if (TESTSQ (x, y, kFloor) and !isObstacle (x, y) and
            !getFeature (x, y))
        {
            shFeature::Type ttype;
        retrap:
            /* I'm using this retrap label because putting a for or while
               loop here uncovers a bug in g++! */
            /* FIXME: Probably the bug is long gone.  Get rid of goto. */
                ttype = (shFeature::Type) RNG (shFeature::kPit,
                                              shFeature::kPortal);
                switch (ttype) {
                case shFeature::kPit:
                case shFeature::kTrapDoor:
                    break;
                case shFeature::kAcidPit:
                    if (mDLevel < 7) goto retrap;
                    break;
                case shFeature::kHole:
                    if (RNG (5)) goto retrap;
                    break;
                case shFeature::kWeb:
                    goto retrap;
                case shFeature::kRadTrap:
                    if (mDLevel < 5) goto retrap;
                    break;
                case shFeature::kPortal:
                default: /* these traps are unimplemented */
                    goto retrap;
                }

            addTrap (x, y, ttype);
        }
    }
}
예제 #17
0
파일: Room.cpp 프로젝트: ronw23/prime-osx
void
shMapLevel::moveWalls (int action)
{
    shFeature *f;

    int north = (1 == action or -1 == action) ? 1 : 0;

    int squares[20];
    int n = 0;
    int x, y;
    int i, j;
    int seen = 0;
    int interrupt = 0;
    int heard = 0;

    // move the wall
    y = -1;
    for (i = 0; i < mFeatures.count (); i++) {
        f = mFeatures.get (i);
        if (shFeature::kMovingHWall != f->mType) {
            continue;
        }
        if (action > 0) {
            /* close in */
            if (north and f->mMovingHWall.mBeginY < f->mMovingHWall.mEndY and
                f->mY < f->mMovingHWall.mEndY)
            {
                //if (Hero.canSee (f->mX, f->mY))
                //    setMemory (f->mX, f->mY, ' ');
                mVisibility[f->mX][f->mY] = 0;
                y = f->mY + 1;
                squares[n++] = f->mX;
                f->mY++;
                addMachinery (f->mX, f->mY-1);
            } else if (!north and f->mMovingHWall.mBeginY > f->mMovingHWall.mEndY and
                f->mY > f->mMovingHWall.mEndY)
            {
                //if (Hero.canSee (f->mX, f->mY))
                //    setMemory (f->mX, f->mY, ' ');
                mVisibility[f->mX][f->mY] = 0;
                y = f->mY - 1;
                squares[n++] = f->mX;
                f->mY--;
                addMachinery (f->mX, f->mY+1);
            } else {
                continue;
            }
            if (Hero.canSee (f->mX, f->mY)) {
                interrupt++;
                seen++;
            } else if (distance (&Hero, f->mX, f->mY) < 100) {
                heard++;
            }
        } else if (action < 0) {
            /* reset */
            int oldy = f->mY;
            if (north and f->mMovingHWall.mBeginY < f->mMovingHWall.mEndY and
                f->mY > f->mMovingHWall.mBeginY)
            {
                y = f->mY - 1;
                shFeature *machinery = getFeature (f->mX, y);
                if (machinery)
                    removeFeature (machinery);
                f->mY--;
            } else if (!north and
                       f->mMovingHWall.mBeginY > f->mMovingHWall.mEndY and
                       f->mY < f->mMovingHWall.mBeginY)
            {
                y = f->mY + 1;
                shFeature *machinery = getFeature (f->mX, y);
                if (machinery)
                    removeFeature (machinery);
                f->mY++;
            } else {
                continue;
            }
            if (Hero.canSee (f->mX, oldy))
                interrupt++;
        }
    }

    if (!Hero.getStoryFlag ("walls moving")) {
        if (seen) {
            I->p ("The walls are moving!");
            Hero.setStoryFlag ("walls moving", 1);
        } else if (heard and !Hero.getStoryFlag ("walls heard")) {
            I->p ("You hear a loud rumbling!");
            Hero.setStoryFlag ("walls heard", 1);
        }
    }

    // displace objects and creatures

    if (n) {
        shuffle (squares, n, sizeof(int));

        for (i = 0; i < n; i++) {
            x = squares[i];

            shObjectVector *v = getObjects (x, y);
            if (v) {
                int y2 = north ? y + 1 : y - 1;
                int safe = !isObstacle (x, y2);
                for (j = 0; j < v->count (); j++) {
                    shObject *obj = v->get (j);
                    if (safe) {
                        putObject (obj, x, y2);
                    } else {
                        delete obj;
                    }
                }
                if (Hero.mX == x and Hero.mY == y2) {
                    I->p ("%s pushed into your vicinity.",
                          v->count () > 1 ? "Some objects are"
                                          : "An object is");
                }
                delete v;
                setObjects (x, y, NULL);
            }

            shCreature *c = getCreature (x, y);
            if (c) {
                if (c->mZ < 0) {
                    if (c->isHero ()) {
                        I->p ("You are sealed below the moving wall!");
                        I->p ("That's not supposed to be possible!");
                        I->p ("Please send me a bug report! -cyrus");
                    }
                } else {
                    pushCreature (c, north ? kSouth : kNorth);
                }
            }
        }
    }
    if (interrupt)
        Hero.interrupt ();
}
예제 #18
0
파일: main2.c 프로젝트: ohayak/tars_code
void test_evitement(void)
{


	for(int i = 0;i < 32;i++)
	{
		trajectory_goto_arel(&traj, END, 45);
		while(!trajectory_is_ended(&traj));
	}

		// addTask(&tkm, &returnFire, LOW_PRIORITY, R1);
// 		addTask(&tkm, &throwSpears, LOW_PRIORITY, YELLOW);
// 		addTask(&tkm, &returnFire, LOW_PRIORITY, Y2);
// //	addTask(&tkm, &returnFire, HIGH_PRIORITY, Y3);
// 		addTask(&tkm, &takeFruitYellow, HIGH_PRIORITY, team);
//	addTask(&tkm, &putPaint, HIGH_PRIORITY, team);

//	addTask(&tkm, &findPosition, HIGH_PRIORITY, YELLOWPAINT);
		// addTask(&tkm, &returnFire, HIGH_PRIORITY, R1);
		// addTask(&tkm, &throwSpears, HIGH_PRIORITY, RED);
	// addTask(&tkm, &putFruit, HIGH_PRIORITY, team);


		// addTask(&tkm, &returnFire, HIGH_PRIORITY, R3);

		// addTask(&tkm, &takeFruitRed, HIGH_PRIORITY, team);

		// addTask(&tkm, &putFruit, HIGH_PRIORITY, team);
		// addTask(&tkm, &returnFire, HIGH_PRIORITY, R2);
	// asserv_set_vitesse_normal(&asserv);

	// while (TIRETTE);
	// printf("begin match \n");


	// avoidance_init();


	// while(doNextTask(&tkm));
	// 	printf("finish \n");	// throwSpears(RED);

	// 	printf("succeed");

		// trajectory_goto_d(&traj, END, 10);
		// while(!trajectory_is_ended(&traj));
		// printf("astar test \n");	    
		// set_startCoor(G_LENGTH * 2 + 2);
		// set_goalCoor(G_LENGTH * 8 + 13);
		// while(!astarMv() && !actionIsFailed());

		// trajectory_goto_d(&traj, END, -100);
		// while(!trajectory_is_ended(&traj));
	while(1);
			// trajectory_goto_arel(&traj, END, 180);
			// while(!trajectory_is_ended(&traj));
			// trajectory_goto_d(&traj, END, 100);
			// while(!trajectory_is_ended(&traj));


		//




	double x = fxx_to_double(position_get_y_cm(&pos));
	double y = fxx_to_double(position_get_x_cm(&pos));
	printf("x : %lf    y : %lf\n", x, y);
	printf("isOut  %d    obs %d\n", isOutOfGraphe(x, y), isObstacle(x, y));
		//go_to_node(x, y,graphe);

}
예제 #19
0
파일: Pacman.cpp 프로젝트: pisc3s/Pacman
//Movement Function
void Pacman::move() {
	if (isPowerUp()) {
		setPowerUp(false);
	}

	//If there is next direction then check is there any obstacle
	//If it has no obstacle then it will become the current direction
	switch (NEXT_DIRECTION) {
	case UP:
		y -= VEL;
		if (!isObstacle()) {
			DIRECTION = NEXT_DIRECTION;
		}
		y += VEL;
		break;

	case RIGHT:
		x += VEL;
		if (!isObstacle()) {
			DIRECTION = NEXT_DIRECTION;
		}
		x -= VEL;
		break;

	case DOWN:
		y += VEL;
		if (!isObstacle()) {
			DIRECTION = NEXT_DIRECTION;
		}
		y -= VEL;
		break;

	case LEFT:
		x -= VEL;
		if (!isObstacle()) {
			DIRECTION = NEXT_DIRECTION;
		}
		x += VEL;
		break;
	}

	//Move to the direction set and with equivalent sprite animation
	switch (DIRECTION) {
	case UP:
		angle = 270;
		flip = SDL_FLIP_NONE;
		y -= VEL;
		if (isObstacle()) {
			y += VEL;
		}
		else {
			eat();
		}
		break;

	case RIGHT:
		angle = 0;
		flip = SDL_FLIP_NONE;
		x += VEL;
		if (isObstacle()) {
			x -= VEL;
		}
		else {
			eat();
		}
		break;

	case DOWN:
		angle = 90;
		flip = SDL_FLIP_NONE;
		y += VEL;
		if (isObstacle()) {
			y -= VEL;
		}
		else {
			eat();
		}
		break;

	case LEFT:
		flip = SDL_FLIP_HORIZONTAL;
		angle = 0;
		x -= VEL;
		if (isObstacle()) {
			x += VEL;
		}
		else {
			eat();
		}
		break;
	}
}