Exemple #1
0
void tile::calculate_corner(int n)
{
	const DIRECTION neighbour_a = static_cast<DIRECTION>(n);
	const DIRECTION neighbour_b = static_cast<DIRECTION>((n+1)%6);

	const int point_a = (n+2)%6;
	const int point_b = (n+4)%6;

	const tile* adja = neighbours_[neighbour_a];
	const tile* adjb = neighbours_[neighbour_b];

	if(adja != NULL && adja->corners_[point_a].init) {
		corners_[n] = adja->corners_[point_a];
		return;
	}

	if(adjb != NULL && adjb->corners_[point_b].init) {
		corners_[n] = adjb->corners_[point_b];
		return;
	}

	location adj[6];
	get_adjacent_tiles(loc_,adj);

	corners_[n].position.x() = (translate_x(loc_) +
	                            translate_x(adj[neighbour_a]) +
	                            translate_x(adj[neighbour_b]))/3.0;
	corners_[n].position.y() = (translate_y(loc_) +
	                            translate_y(adj[neighbour_a]) +
	                            translate_y(adj[neighbour_b]))/3.0;

	int sum = height_;
	int num = 1;
	if(adja != NULL) {
		sum += adja->height_;
		++num;
	}

	if(adjb != NULL) {
		sum += adjb->height_;
		++num;
	}

	sum /= num;
	corners_[n].position.z() = translate_height(sum);
	corners_[n].init = true;
}
Exemple #2
0
void tile::invalidate()
{
	texture_ = graphics::texture();
	center_.position.x() = translate_x(loc_);
	center_.position.y() = translate_y(loc_);
	center_.position.z() = translate_height(height_);
	center_.init = false;
	for(int n = 0; n != 6; ++n) {
		corners_[n].init = false;
	}
}
/**
 * @brief Updates the x position of the entity if it wants to move
 * (non-smooth version).
 */
void StraightMovement::update_non_smooth_x() {

  if (x_move != 0) {

    // make the move only if there is no collision
    if (!test_collision_with_obstacles(x_move, y_move)) {
      translate_x(x_move);
    }
    else {
      stop(); // also stop on y
    }
    next_move_date_x += x_delay;
  }
}
Exemple #4
0
void StraightMovement::update_x() {
	uint32_t next_move_time_x;
	if(move_x != 0) {	// entity wants to move in x direction.
		next_move_time_x = delay_x;
		if(!test_collision_with_obstacles(move_x, 0) && !test_collision_with_borders(move_x, 0)) {
			translate_x(move_x);	// no collision in x direction 
			if(move_y != 0 && test_collision_with_obstacles(0, move_y)) {
				// if there is also a y move and this move is stopped by an obstacles.
				next_move_time_x = (int) (1000 / get_speed());
				update_y();
			}
		}
	} else {	//entity does not want to move in x direction so we stop the movement.
	    stop();
	}
	next_move_date_x += next_move_time_x;
}
Exemple #5
0
void CreateMap() {        //Our 'main' function
  
  int x,y,color;                               //Holds the result of slove
  

  for(y=0;y<HEIGHT;y++)              //Main loop for map generation
    for(x=0;x<WIDTH;x++){  
      color=solve(translate_x(x),translate_y(y))*colorcount/100;
#ifdef GRAPHICS
      gs_plot(x,y,colortable[color]); //Plot the coordinate to map
#else
      crc+=colortable[color];
#endif
    }
#ifdef GRAPHICS
  gs_update();
#endif
}
Exemple #6
0
void tile::init(int height, const_base_terrain_ptr terrain,
                const_terrain_feature_ptr feature)
{
	std::fill(neighbours_,neighbours_+
					sizeof(neighbours_)/sizeof(*neighbours_),
					static_cast<const tile*>(NULL));
	std::fill(cliffs_,cliffs_+
					sizeof(cliffs_)/sizeof(*cliffs_),
					static_cast<const tile*>(NULL));

	height_ = height;
	terrain_ = terrain;
	feature_ = feature;

	center_.position.x() = translate_x(loc_);
	center_.position.y() = translate_y(loc_);
	center_.position.z() = translate_height(height_);
	center_.init = false;
}
/**
 * @brief Updates the y position of the entity if it wants to move
 * (smooth version).
 */
void StraightMovement::update_smooth_y() {

  if (y_move != 0) {  // The entity wants to move on y.

    // By default, next_move_date_y will be incremented by y_delay,
    // unless we modify the movement in such a way that the
    // y speed needs to be fixed.
    uint32_t next_move_date_y_increment = y_delay;

    if (!test_collision_with_obstacles(0, y_move)) {

      translate_y(y_move);  // Make the move.

      if (x_move != 0 && test_collision_with_obstacles(x_move, 0)) {
        // If there is also an x move, and if this x move is illegal,
        // we still allow the y move and we give it all the speed.
        next_move_date_y_increment = (int) (1000.0 / get_speed());
      }
    }
    else {
      if (x_move == 0) {
        // The move on y is not possible and there is no x move:
        // let's try to add a move on x to make a diagonal move,
        // but only if the wall is really diagonal: otherwise, the hero
        // could bypass sensors.

        if (!test_collision_with_obstacles(1, y_move)    // Can move diagonally and:
            && (test_collision_with_obstacles(-1, 0) ||  // the wall is really diagonal
                test_collision_with_obstacles(1, 0))     // or we don't have a choice anyway.
        ) {
          translate_xy(1, y_move);
          next_move_date_y_increment = (int) (y_delay * Geometry::SQRT_2);  // Fix the speed.
        }
        else if (!test_collision_with_obstacles(-1, y_move)
            && (test_collision_with_obstacles(1, 0) ||
                test_collision_with_obstacles(-1, 0))
        ) {
          translate_xy(-1, y_move);
          next_move_date_y_increment = (int) (y_delay * Geometry::SQRT_2);
        }
        else {
          // The diagonal moves didn't work either.
          // So we look for a place (up to 8 pixels on the left and on the right)
          // where the required move would be allowed.
          // If we find a such place, then we move towards this place.

          bool moved = false;
          for (int i = 1; i <= 8 && !moved; i++) {

            if (!test_collision_with_obstacles(i, y_move) && !test_collision_with_obstacles(1, 0)) {
              translate_x(1);
              moved = true;
            }
            else if (!test_collision_with_obstacles(-i, y_move) && !test_collision_with_obstacles(-1, 0)) {
              translate_x(-1);
              moved = true;
            }
          }
        }
      }
      else {
        // The move on y is not possible, but there is also a horizontal move.
        if (!test_collision_with_obstacles(x_move, y_move)) {
          // This case is only necessary in narrow diagonal passages.
          translate_xy(x_move, y_move);
          next_move_date_x += x_delay;  // Delay the next update_smooth_x() since we just replaced it.
        }
        else if (!test_collision_with_obstacles(x_move, 0)) {
          // Do the horizontal move right now, don't wait uselessly.
          update_x();
        }
      }
    }
    next_move_date_y += next_move_date_y_increment;
  }
}
void cave_map_generator::cave_map_generator_job::generate_chambers()
{
	for (const config &ch : params.cfg_.child_range("chamber"))
	{
		// If there is only a chance of the chamber appearing, deal with that here.
		if (ch.has_attribute("chance") && int(rng_() % 100) < ch["chance"].to_int()) {
			continue;
		}

		const std::string &xpos = ch["x"];
		const std::string &ypos = ch["y"];

		size_t min_xpos = 0, min_ypos = 0, max_xpos = params.width_, max_ypos = params.height_;

		if (!xpos.empty()) {
			const std::vector<std::string>& items = utils::split(xpos, '-');
			if(items.empty() == false) {
				min_xpos = atoi(items.front().c_str()) - 1;
				max_xpos = atoi(items.back().c_str());
			}
		}

		if (!ypos.empty()) {
			const std::vector<std::string>& items = utils::split(ypos, '-');
			if(items.empty() == false) {
				min_ypos = atoi(items.front().c_str()) - 1;
				max_ypos = atoi(items.back().c_str());
			}
		}
		const size_t x = translate_x(min_xpos + (rng_()%(max_xpos-min_xpos)));
		const size_t y = translate_y(min_ypos + (rng_()%(max_ypos-min_ypos)));

		int chamber_size = ch["size"].to_int(3);
		int jagged_edges = ch["jagged"];

		chamber new_chamber;
		new_chamber.center = map_location(x,y);
		build_chamber(new_chamber.center,new_chamber.locs,chamber_size,jagged_edges);

		const config &items = ch.child("items");
		new_chamber.items = items ? &items : nullptr;

		const std::string &id = ch["id"];
		if (!id.empty()) {
			chamber_ids_[id] = chambers_.size();
		}

		chambers_.push_back(new_chamber);

		for(const config &p : ch.child_range("passage"))
		{
			const std::string &dst = p["destination"];

			// Find the destination of this passage
			const std::map<std::string,size_t>::const_iterator itor = chamber_ids_.find(dst);
			if(itor == chamber_ids_.end())
				continue;

			assert(itor->second < chambers_.size());

			passages_.push_back(passage(new_chamber.center, chambers_[itor->second].center, p));
		}
	}
}
Exemple #9
0
/**
 * \brief Updates the x position of the entity if it wants to move
 * (smooth version).
 */
void StraightMovement::update_smooth_x() {

  if (x_move != 0) {  // The entity wants to move on x.

    // By default, next_move_date_x will be incremented by x_delay,
    // unless we modify below the movement in such a way that the
    // x speed needs to be fixed.
    uint32_t next_move_date_x_increment = x_delay;

    if (!test_collision_with_obstacles(x_move, 0)) {

      translate_x(x_move);  // Make the move.

      if (y_move != 0 && test_collision_with_obstacles(0, y_move)) {
        // If there is also a y move, and if this y move is illegal,
        // we still allow the x move and we give it all the speed.
        next_move_date_x_increment = (int) (1000.0 / get_speed());
      }
    }
    else {
      if (y_move == 0) {
        // The move on x is not possible and there is no y move:
        // let's try to add a move on y to make a diagonal move,
        // but only if the wall is really diagonal: otherwise, the hero
        // could bypass sensors.

        if (!test_collision_with_obstacles(x_move, 1)    // Can move diagonally and:
            && (test_collision_with_obstacles(0, -1) ||  // the wall is really diagonal
                test_collision_with_obstacles(0, 1))     // or we don't have a choice anyway.
        ) {
          translate_xy(x_move, 1);
          next_move_date_x_increment = (int) (x_delay * Geometry::SQRT_2);  // Fix the speed.
        }
        else if (!test_collision_with_obstacles(x_move, -1)
            && (test_collision_with_obstacles(0, 1) ||
                test_collision_with_obstacles(0, -1))
        ) {
          translate_xy(x_move, -1);
          next_move_date_x_increment = (int) (x_delay * Geometry::SQRT_2);
        }
        else {

          // The diagonal moves didn't work either.
          // So we look for a place (up to 8 pixels up and down)
          // where the required move would be allowed.
          // If we find a such place, then we move towards this place.

          bool moved = false;
          for (int i = 1; i <= 8 && !moved; i++) {

            if (!test_collision_with_obstacles(x_move, i) && !test_collision_with_obstacles(0, 1)) {
              translate_y(1);
              moved = true;
            }
            else if (!test_collision_with_obstacles(x_move, -i) && !test_collision_with_obstacles(0, -1)) {
              translate_y(-1);
              moved = true;
            }
          }
        }
      }
      else {
        // The move on x is not possible, but there is also a vertical move.
        if (!test_collision_with_obstacles(0, y_move)) {
          // Do the vertical move right now, don't wait uselessly.
          update_y();
        }
        else {
          // The x move is not possible and neither is the y move.
          // Last chance: do both x and y moves in one step.
          // This case is only necessary in narrow diagonal passages.
          // We do it as a last resort, because we want separate x and y
          // steps whenever possible: otherwise, the hero could bypass sensors.
          if (!test_collision_with_obstacles(x_move, y_move)) {
            translate_xy(x_move, y_move);
            next_move_date_y += y_delay;  // Delay the next update_smooth_y() since we just replaced it.
          }
        }
      }
    }
    next_move_date_x += next_move_date_x_increment;
  }
}
/**
 * @brief Updates the y position of the entity if it wants to move
 * (smooth version).
 */
void StraightMovement::update_smooth_y() {

  if (y_move != 0) { // the entity wants to move on y

    // by default, next_move_date_y will be incremented by y_delay,
    // unless we modify the movement in such a way that the
    // y speed needs to be fixed
    uint32_t next_move_date_y_increment = y_delay;

    if (!test_collision_with_obstacles(0, y_move)) {

      translate_y(y_move); // make the move

      if (x_move != 0 && test_collision_with_obstacles(x_move, 0)) {
        // if there is also an x move, and if this x move is illegal,
        // we still allow the y move and we give it all the speed
        next_move_date_y_increment = (int) (1000.0 / get_speed());
      }
    }
    else {
      if (x_move == 0) {
        // The move on y is not possible: let's try
        // to add a move on x to make a diagonal move.

        if (!test_collision_with_obstacles(1, y_move)
            && test_collision_with_obstacles(-1, 0)) {
          translate_xy(1, y_move);
          next_move_date_y_increment = (int) (y_delay * Geometry::SQRT_2); // fix the speed
        }
        else if (!test_collision_with_obstacles(-1, y_move)
            && test_collision_with_obstacles(1, 0)) {
          translate_xy(-1, y_move);
          next_move_date_y_increment = (int) (y_delay * Geometry::SQRT_2);
        }
        else {
          /* The diagonal moves didn't work either.
           * So we look for a place (up to 8 pixels on the left and on the right)
           * where the required move would be allowed.
           * If we find a such place, then we move towards this place.
           */

          bool moved = false;
          for (int i = 1; i <= 8 && !moved; i++) {

            if (!test_collision_with_obstacles(i, y_move) && !test_collision_with_obstacles(1, 0)) {
              translate_x(1);
              moved = true;
            }
            else if (!test_collision_with_obstacles(-i, y_move) && !test_collision_with_obstacles(-1, 0)) {
              translate_x(-1);
              moved = true;
            }
          }
        }
      }
      else {
        // no attractive place was found, but there is a horizontal move
        if (!test_collision_with_obstacles(x_move, 0)) {
          // do the horizontal move right now, don't wait uselessly
          update_x();
        }
      }
    }
    next_move_date_y += next_move_date_y_increment;
  }
}