game2006/server/gameserver/mapservice.cc
2022-12-31 20:52:01 +08:00

84 lines
1.7 KiB
C++

#include "precompile.h"
#include <math.h>
#include <memory.h>
#include "mapservice.h"
#include "entity.h"
#include "roomobstacle.h"
#include "room.h"
MapService::MapService()
{
}
MapService::~MapService()
{
if (map_cells_) {
free(map_cells_);
map_cells_ = nullptr;
}
}
void MapService::Init(int width, int height, int cell_width)
{
if (width < 1 || height < 1 || cell_width < 1) {
A8_ABORT();
}
map_width_ = width;
map_height_ = height;
cell_width_ = cell_width;
max_grid_id_ = map_width_ * map_height_;
grid_offset_arr_[0] = - (map_width_ + 1);
grid_offset_arr_[1] = - (map_width_ + 0);
grid_offset_arr_[2] = - (map_width_ + -1);
grid_offset_arr_[3] = - 1;
grid_offset_arr_[4] = 0;
grid_offset_arr_[5] = 1;
grid_offset_arr_[6] = map_width_ - 1;
grid_offset_arr_[7] = map_width_;
grid_offset_arr_[8] = map_width_ + 1;
map_cells_ = (list_head*)malloc(sizeof(list_head) * width * height);
memset(map_cells_, 0, sizeof(list_head) * width * height);
for (int i = 0; i < max_grid_id_; ++i) {
INIT_LIST_HEAD(&map_cells_[i]);
}
}
void MapService::UnInit()
{
}
bool MapService::CanAdd(const glm::vec3& pos, int rad)
{
//上
if (pos.z + rad + 10 > map_height_ * cell_width_) {
return false;
}
//下
if (rad + 10 > pos.z) {
return false;
}
//左
if (rad + 10 > pos.x) {
return false;
}
//右
if (pos.x + rad + 10 > map_width_ * cell_width_) {
return false;
}
return true;
}
int MapService::GetGridId(float world_x, float world_y)
{
int grid_id = (int)(world_x/cell_width_) + (int)(world_y/cell_width_) * map_width_;
return grid_id;
}