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

📄 board_utility.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Board untility functions
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"
#include "cell.h"
#include "board.h"
#include "universal.h"

extern double random(double min, double max);


// ============================================================================
// Given a position, return a pointer to its cell taking into account
// the tiling.
// ============================================================================

Cell * Board::get_cell(Point position)
{
   int x(position.x);
   int y(position.y);

   int i (x / (cell_size - (cell_size / 4)));
   int j (y / cell_size);

   if ((i & 1) != 0) {
      if (y < half_cell_size) return 0;
      j = (y - half_cell_size) / cell_size;
   }

   if ((i >= board_x_size) || (j >= board_y_size))return 0;
   
   Cell * cell = (& cells[j][i]);

   int ox          (x - cell->position.x);
   int oy          (y - cell->position.y);
   int boundary    (0);
   double gradient ((double) quarter_cell_size / (double) half_cell_size);
   
   if (oy < half_cell_size) {
      boundary = quarter_cell_size - (int) (gradient * oy);
      if (ox < boundary) {
         if ((i & 1) == 0) j--;
         i--;
      }
   } else {
      boundary = (int) (gradient * (oy - half_cell_size));
      if (ox < boundary) {
         if ((i & 1) != 0) j++; 
         i--;
      }
   }

   if ((i < 0) || (i >= board_x_size)) return 0;
   if ((j < 0) || (j >= board_y_size)) return 0;
 
   return &cells[j][i];
}


// ============================================================================
// Get a pointer to a cell on a given side of a cell
// ============================================================================

Cell * Board::get_cell(int x, int y, int side)
{
   static int xoffset[] = { p75_cell_size,   0,         -p75_cell_size, 
                           -p75_cell_size,   0,          p75_cell_size};

   static int yoffset[] = { half_cell_size,  cell_size,  half_cell_size,
                           -half_cell_size, -cell_size, -half_cell_size};

   Point position = cells[y][x].get_centre();

   position.x += xoffset[side];
   position.y += yoffset[side];

   return get_cell(position);
}

  
// ============================================================================
// Return an empty cell picked at random. If coordinates are specified then
// pick a cell close to that spot
// ============================================================================

Cell * Board::random_cell(int pi, int pj)
{
   int i     (0);
   int j     (0);
   int min_i (1);
   int max_i (board_x_size - 1);
   int min_j (1);
   int max_j (board_y_size - 1);
   int range (2);

   if (pi >= 0) {
      min_i = pi - range;
      max_i = pi + range;
      min_j = pj - range;
      max_j = pj + range;

      if (min_i < 0) min_i = 0;
      if (min_j < 0) min_j = 0;
      if (max_i > board_x_size) max_i = board_x_size  - 1;
      if (max_j > board_y_size) max_j = board_y_size  - 1;
   }

   while (true) {
      i = (int) random(min_i, max_i);
      j = (int) random(min_j, max_j);

      if (cells[j][i].type == Empty) break;
   }

   return & cells[j][i];
}

⌨️ 快捷键说明

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