This is OMoveTo.m in view mode; [Download] [Up]
//
// $Id: OMoveTo.m,v 1.1 1997/10/28 05:11:32 nygard Exp $
//
//
// This file is a part of Empire, a game of exploration and conquest.
// Copyright (C) 1996 Steve Nygard
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// You may contact the author by:
// e-mail: nygard@telusplanet.net
//
#import "Empire.h"
RCSID ("$Id: OMoveTo.m,v 1.1 1997/10/28 05:11:32 nygard Exp $");
#import "OMoveTo.h"
#import "Map.h"
#import "Unit.h"
//======================================================================
//
// This provides a very simple method of moving to a new location.
// First, it tries to move closer to the desired row and column. If
// it is blocked, it tries moving closer to first the column, and
// then the row, before giving up.
//
// It exhibits some undesirable properties. It doesn't move in a
// straight line, so when used for patrolling, it may not cover the
// area you wanted, and it follows two different paths when patrolling
// between two points
//
//======================================================================
#define OMoveTo_VERSION 1
@implementation OMoveTo
+ (void) initialize
{
if (self == [OMoveTo class])
{
[self setVersion:OMoveTo_VERSION];
}
}
//----------------------------------------------------------------------
- initForUnit:(Unit *)aUnit moveToLocation:(EMMapLocation)newLocation onMap:(Map *)map
{
[super initForUnit:aUnit];
destination = newLocation;
return self;
}
//----------------------------------------------------------------------
- (void) dealloc
{
[super dealloc];
}
//----------------------------------------------------------------------
- (NSString *) orderText
{
return [NSString stringWithFormat:@"Move to %d,%d", destination.row, destination.column];
}
//----------------------------------------------------------------------
- (Direction) nextMove
{
Direction dir = d_none;
dir = [self simpleAlgorithm];
return dir;
}
//----------------------------------------------------------------------
- (Direction) simpleAlgorithm
{
Direction delta_to_dir[9] = {d_northwest, d_north, d_northeast, d_west, d_none, d_east, d_southwest, d_south, d_southeast};
EMMapLocation unitLocation = [unit unitLocation];
int deltaRow = destination.row - unitLocation.row;
int deltaColumn = destination.column - unitLocation.column;
Direction dir;
if (deltaRow < 0)
deltaRow = -1;
else if (deltaRow > 0)
deltaRow = 1;
if (deltaColumn < 0)
deltaColumn = -1;
else if (deltaColumn > 0)
deltaColumn = 1;
if (blockedCount == 1)
deltaRow = 0;
else if (blockedCount == 2)
deltaColumn = 0;
dir = delta_to_dir[3 * (deltaRow + 1) + deltaColumn + 1];
return dir;
}
//----------------------------------------------------------------------
- (BOOL) wasBlocked
{
blockedCount++;
// First try is midpoint algorithm, next 3 are simple algorithm.
return blockedCount < 3;
}
//----------------------------------------------------------------------
- (BOOL) isOrderComplete
{
EMMapLocation unitLocation = [unit unitLocation];
return unitLocation.row == destination.row && unitLocation.column == destination.column;
}
//----------------------------------------------------------------------
- (EMMapLocation) destination
{
return destination;
}
@end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.