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

📄 board_io.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Board IO functions (transfer between client and server)
//
// (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"


// ============================================================================
// Update the board with a new cell and explore around it (if that option is
// enabled).
// ============================================================================

void Board::new_data(Socket * socket)
{
   static Cell new_cell;

   read_cell(*socket, new_cell);

   int i(new_cell.i);
   int j(new_cell.j);

   Cell * cell = &(cells[j][i]);
   cell->merge(new_cell);

   cell->draw(window, hidden, player);

   // If we are exploring, and the cell belongs to us, and we haven't explored
   // from here before then explore.

   if (!exploring || (cell->player != player) || cell->scanned)return;

   cell->explored = true;
   cell->scanned  = true;

   int start_i (i - 3);
   int start_j (j - 2);
   int end_i   (i + 4);
   int end_j   (j + 3);

   if (start_i < 0) start_i = 0;
   if (start_j < 0) start_j = 0;

   if (end_i > board_x_size) end_i = board_x_size;
   if (end_j > board_y_size) end_j = board_y_size;

   for (j = start_j; j < end_j; j++) {
      for (i = start_i; i < end_i; i++) {
         cell = &(cells[j][i]);
         if (! cell->explored) {
            cell->explored = true;
            cell->draw(window, hidden, player);
         }
      }
   }
}


// ============================================================================
// Transmit the board to a client
// ============================================================================

void Board::transmit(Socket * socket) 
{
   (*socket) <<  exploring << " " << hidden << "\n";

   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         send_cell(*socket, cells[j][i]);
      }
   }
}


// ============================================================================
// Receive a new board
// ============================================================================

void Board::receive(Socket * socket) 
{
   (*socket) >> exploring >> hidden;

   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         cells[j][i].reset(exploring);
         read_cell(*socket, cells[j][i]);
      }
   }
}


// ============================================================================
// Send any changed cells
// ============================================================================

void Board::send_updates(Socket * socket)
{
   int i, j;
   int count(0);
  

   for (j = 0; j < board_y_size; j++) {
      for (i = 0; i < board_x_size; i++) {
         if (cells[j][i].changed) count++;
      }
   }

   (*socket) << count << "\n";

   for (j = 0; j < board_y_size; j++) {
      for (i = 0; i < board_x_size; i++) {
          if (cells[j][i].changed) {
            send_cell(*socket, cells[j][i]);
         }
      }
   }
}


// ============================================================================
// Read any changed cells
// ============================================================================

void Board::get_updates(Socket * socket)
{
   int count(0);
  
   (*socket) >> count;

   for (int k = 0; k < count; k++) {
      new_data(socket);
   }
}


// ============================================================================
// Reset all cell changed flags
// ============================================================================

void Board::end_of_updates()
{
   for (int j = 0; j < board_y_size; j++) {
      for (int i = 0; i < board_x_size; i++) {
         cells[j][i].changed = false;
      }
   }
}

⌨️ 快捷键说明

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