1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright (C) 2000-2022 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "Zombie.h"
#include "ignore_unused_variable_warning.h"
/*
* Figure 'dir' (1 or -1) and abs. value of 'delta'.
*/
inline void Figure_dir(long delta, unsigned int& abs_delta, int& dir) {
if (delta >= 0) {
dir = 1;
abs_delta = delta;
} else {
dir = -1;
abs_delta = -delta;
}
}
/*
* Find path from source to destination.
*
* Output: true if successful, else false.
*/
bool Zombie::NewPath(
const Tile_coord& s, const Tile_coord& d,
const Pathfinder_client* client) {
ignore_unused_variable_warning(client);
src = s; // Store start, destination.
dest = d;
cur = s; // Get current coords.
sum1 = sum2 = 0; // Clear accumulators.
const long deltax = Tile_coord::delta(cur.tx, dest.tx);
const long deltay = Tile_coord::delta(cur.ty, dest.ty);
const long deltaz = Tile_coord::delta(cur.tz, dest.tz);
if (!deltax && !deltay && !deltaz) { // Going nowhere?
major_distance = 0;
return false;
}
unsigned int abs_deltax;
unsigned int abs_deltay;
unsigned int abs_deltaz;
int x_dir;
int y_dir;
int z_dir; // Figure directions.
Figure_dir(deltax, abs_deltax, x_dir);
Figure_dir(deltay, abs_deltay, y_dir);
Figure_dir(deltaz, abs_deltaz, z_dir);
if (abs_deltaz >= abs_deltax && // Moving fastest along z?
abs_deltaz >= abs_deltay) {
major_coord = &cur.tz;
minor_coord1 = &cur.tx;
minor_coord2 = &cur.ty;
major_dir = z_dir;
minor_dir1 = x_dir;
minor_dir2 = y_dir;
major_delta = abs_deltaz;
minor_delta1 = abs_deltax;
minor_delta2 = abs_deltay;
} else if (
abs_deltay >= abs_deltax && // Moving fastest along y?
abs_deltay >= abs_deltaz) {
major_coord = &cur.ty;
minor_coord1 = &cur.tx;
minor_coord2 = &cur.tz;
major_dir = y_dir;
minor_dir1 = x_dir;
minor_dir2 = z_dir;
major_delta = abs_deltay;
minor_delta1 = abs_deltax;
minor_delta2 = abs_deltaz;
} else { // Moving fastest along x?
major_coord = &cur.tx;
minor_coord1 = &cur.ty;
minor_coord2 = &cur.tz;
major_dir = x_dir;
minor_dir1 = y_dir;
minor_dir2 = z_dir;
major_delta = abs_deltax;
minor_delta1 = abs_deltay;
minor_delta2 = abs_deltaz;
}
major_distance = major_delta; // How far to go.
return true;
}
/*
* Get next point on path to go to (in tile coords).
*
* Output: false if all done.
*/
bool Zombie::GetNextStep(Tile_coord& n, bool& done) {
if (major_distance <= 0) {
done = true;
return false;
}
// Subtract from distance to go.
major_distance -= major_frame_incr;
// Accumulate change.
sum1 += major_frame_incr * minor_delta1;
sum2 += major_frame_incr * minor_delta2;
// Figure change in slower axes.
const int minor_frame_incr1 = sum1 / major_delta;
const int minor_frame_incr2 = sum2 / major_delta;
sum1 = sum1 % major_delta; // Remove what we used.
sum2 = sum2 % major_delta; // Remove what we used.
// Update coords. within world.
*major_coord += major_dir * major_frame_incr;
*minor_coord1 += minor_dir1 * minor_frame_incr1;
*minor_coord2 += minor_dir2 * minor_frame_incr2;
// Watch for wrapping.
*major_coord = (*major_coord + c_num_tiles) % c_num_tiles;
*minor_coord1 = (*minor_coord1 + c_num_tiles) % c_num_tiles;
*minor_coord2 = (*minor_coord2 + c_num_tiles) % c_num_tiles;
if (cur.tz < 0) { // We are below ground level.
cur.tz = 0;
major_distance = 0;
done = true;
return false;
}
n = cur; // Return new tile.
done = (major_distance <= 0); // Indicate if this is the last one.
return true;
}
|