This is ORandom.m in view mode; [Download] [Up]
//
// $Id: ORandom.m,v 1.1 1997/10/28 05:11:39 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: ORandom.m,v 1.1 1997/10/28 05:11:39 nygard Exp $");
#import "ORandom.h"
#import "SNRandom.h"
//======================================================================
//
// This randomly moves a unit. It should not be used with fighters.
// It picks a random direction, and every time it is asked to move, it
// chooses to turn left, turn right, or continue in the last direction
// it moved. This gives a more meandering movement that simply picking
// a random direction each time.
//
// When blocked, it tries all directions in a clockwise sweep before
// giving up.
//
//======================================================================
#define ORandom_VERSION 1
@implementation ORandom
+ (void) initialize
{
if (self == [ORandom class])
{
[self setVersion:ORandom_VERSION];
}
}
//----------------------------------------------------------------------
- initForUnit:(Unit *)aUnit
{
[super initForUnit:aUnit];
lastDirectionIndex = [[SNRandom instance] randomNumberModulo:8];
return self;
}
//----------------------------------------------------------------------
- (void) dealloc
{
[super dealloc];
}
//----------------------------------------------------------------------
- (NSString *) orderText
{
return @"Go Random";
}
//----------------------------------------------------------------------
- (Direction) nextMove
{
Direction directions[8] = {d_northwest, d_north, d_northeast, d_east, d_southeast, d_south, d_southwest, d_west};
Direction dir;
int turn;
if (blockedCount > 0)
turn = 1;
else
turn = [[SNRandom instance] randomNumberBetween:-1:1];
turn += lastDirectionIndex;
if (turn < 0)
turn += 8;
else if (turn > 7)
turn -= 8;
dir = directions[turn];
lastDirectionIndex = turn;
return dir;
}
//----------------------------------------------------------------------
- (BOOL) wasBlocked
{
blockedCount++;
// Give up after we've tried all directions.
return blockedCount < 9;
}
@end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.