📄 board_update.cpp
字号:
// ============================================================================
// Board update 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"
#include <set>
using namespace std;
const double march_rate = 20.0;
namespace {
set<Cell *> attacked_cells;
}
extern double random(double min, double max);
// ============================================================================
// Update the whole board
// ============================================================================
void Board::update(double elapsed)
{
attacked_cells.clear();
for (int j = 0; j < board_y_size; j++) {
for (int i = 0; i < board_x_size; i++) {
Cell * cell = (&(cells[j][i]));
cell->update(elapsed);
update_march(j, i, elapsed);
if (cell->troops > 0) {
if (attrition) {
cell->troops -= (attrition_rate * elapsed);
cell->changed = true;
}
move_troops(j, i, elapsed);
}
}
}
set<Cell *>::iterator p;
for (p = attacked_cells.begin(); p != attacked_cells.end(); p++) {
(*p)->battle(elapsed);
}
}
// ============================================================================
// Move troops
// ============================================================================
void Board::move_troops(int j, int i, double elapsed)
{
Cell * source = &cells[j][i];
int start_side ((int) random(0, 6));
for (int k = 0; k < 6; k++) {
if (source->troops <= 0) break;
int side = (start_side + k) % 6;
if (! source->vector[side]) continue;
Cell * target = get_cell(i, j, side);
if (target == 0) continue;
if (target->elevation < 0) continue;
if (target->player != source->player) {
if (target->troops > 0) {
if (disrupt) {
target->clear_vector(all_vectors);
}
target->attackers.insert(source);
attacked_cells.insert(target);
continue;
}
else {
target->clear_vector(all_vectors);
}
}
if (target->troops == max_troops)continue;
double gradient (source->elevation - target->elevation);
double rate (march_rate + (gradient / 2));
double to_move (rate * elapsed);
double keep (0);
if (to_move <= 0) continue;
if (source->type == Base) keep = 0.8 * max_troops;
if (to_move > (source->troops - keep)) {
to_move = source->troops - keep;
if (to_move <= 0) continue;
}
if ((target->troops + to_move) > max_troops) {
to_move = max_troops - target->troops;
}
source->troops -= to_move;
target->troops += to_move;
target->changed = true;
source->changed = true;
target->player = source->player;
if (target->troops > max_troops) target->troops = max_troops;
if (source->troops < 0) source->troops = 0;
}
}
// ============================================================================
// Update march vectors
// ============================================================================
void Board::update_march(int j, int i, double elapsed)
{
Cell * source = ( & cells[j][i] );
if (! source->march) return;
Cell * target = get_cell(i, j, source->march_vector);
if ((target == 0) || (target->elevation < 0) ||
((target->troops > 0) && (target->player != source->player))) {
source->march = false;
return;
}
source->march_timer -= elapsed;
if (source->march_timer > 0) return;
if (source->troops <= 0) return;
target->clear_vector(all_vectors);
target->march = true;
source->march = false;
target->march_timer = 2;
source->vector[source->march_vector] = true;
target->march_vector = source->march_vector;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -