Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- from pheromone import Pheromone
- """
- Editors notes: I now need to create and deal with x and y positions for both ants and anthills.
- """
- class Ant:
- def __init__(self, home):
- """Initialises the ant object"""
- self.max_ph_level = 18 # Every how many steps can we place a pheremone drop
- self.ph_time = 30 # How long do we keep direction after finding pheromones
- self.carryingFood = False # Indicate whether we have any food with us
- self.pheromoneLevel = self.max_ph_level # How much pheromone do we have right now
- self.foundLastPheromone = 0 # How well do we remember the last pheromone (larger number: more recent)
- self.max_speed = 3 # The maximum movement speed of the ant
- self.currentXspeed = 0
- self.currentYspeed = 0
- self.home = home # Sets the anthill that created the ant as home
- def live(self):
- """Does all the actions that an ant does"""
- if self.carryingFood:
- self.walkTowardsHome()
- self.handlePheromoneDrop()
- self.checkHome()
- else:
- self.searchForFood()
- def randomChance(self, percent):
- """Returns true in exactly 'percent' number of calls (i.e randomChance(25) has a 25% chance of returning true"""
- return random.randint(100) < percent
- def walkTowardsHome(self):
- """Tries to walk home. Added a little bit of randomness"""
- if not self.home: # If we don't have a home, we can't do anything
- return
- if self.randomChance(2): # Has a 2% chance of being true
- self.randomWalk() # Walk slightly off course
- else:
- self.headInGeneralDirection(self.home) # Turn in the general direction of home
- self.walk() # Walk in the current direction with current speed
- def randomWalk(self):
- """Walk around randomly (random direction and speed)"""
- if self.randomChance(50):
- self.currentXspeed = self.adjustSpeed(self.currentXspeed)
- self.currentYspeed = self.adjustSpeed(self.currentYspeed)
- def adjustSpeed(self, speed):
- """Adjusts the speed randomly (start moving, continue or slow down"""
- speed = speed + random.randint(2 * self.max_speed - 1) - self.max_speed + 1
- return self.capSpeed(speed)
- def capSpeed(self, speed):
- """Makes sure the speed returned is in the range (-self.max_speed - self.max_speed"""
- if speed < -self.max_speed: # Returns the max backwards speed if speed is smaller than it
- return -self.max_speed
- elif speed > self.max_speed: # Returns the max speed if speed is larger than it
- return self.max_speed
- else: # Otherwise, the speed is within the range, so its fine.
- return speed
- def headInGeneralDirection(self, target):
- """Adjusts the direction to heat somewhat towards the given coordinates."""
- distanceX = abs(self.x - target.x) # Finds the x distance between the ant and the target
- distanceY = abs(self.y - target.y) # Finds the y distance between the ant and the target
- # The following code in the function is yet to be understood - further testing may be necessary
- if distanceX > 0 and random.randint(distanceX + distanceY) < distanceX:
- moveX = True
- else:
- moveX = False
- if distanceY > 0 and random.randint(distanceX + distanceY) < distanceY:
- moveY = True
- else:
- moveY = False
- self.currentXspeed = self.computeHomeDelta(moveX, self.x, target.x)
- self.currentYspeed = self.computeHomeDelta(moveY, self.y, target.y)
- def computeHomeDelta(self, move, ant, home):
- """Computes and returns the direction (delta) that the ant should steer in on its way home"""
- if move:
- if ant > home:
- return -self.max_speed
- else:
- return self.max_speed
- else:
- return 0
- def walk(self):
- """Walk forward in the current direction with the current speed (doesn't change either)"""
- self.x += self.currentXspeed
- self.y += self.currentYspeed
- # The next line is not understood whatsoever and needs further testing. I will attempt to create a self.rotation
- # and use it to replicate the Java version.
- self.rotation = 180 * math.atan2(self.currentYspeed, self.currentXspeed) / math.pi
- def checkHome(self):
- """Are we at home? Drop food if we are, and then leave to get some more"""
- if self.atHome():
- self.dropFood()
- def atHome(self):
- if self.home != False:
- return abs(self.x - self.home.x) < 4 and abs(self.y - self.home.y) < 4
- else:
- return False
- def dropFood(self):
- """Drops the food into the anthill"""
- self.carryingFood = False
- self.home.countFood()
- ### NEEDS TO UPDATE ITS IMAGE SOMEOHOW
- def searchForFood(self):
- """Walk around in search of food"""
- if self.foundLastPheromone > 0:
- self.foundLastPheromone -= 1
- self.walkAwayFromHome()
- elif self.smellPheromone():
- self.walkTowardsPheromone()
- else:
- self.randomWalk()
- self.checkFood()
- def walkAwayFromHome(self):
- """Try to walk away from home (with slightly random walking added)"""
- if not self.home:
- return
- elif self.randomChance(2):
- self.randomWalk()
- else:
- self.headInGeneralDirection(self.home)
- self.currentXspeed = -self.currentXspeed
- self.currentYspeed = -self.currentYspeed
- self.walk()
- def smellPheromone(self):
- print("How will I 'smell' pheromones?")
- def handlePheromoneDrop(self):
- """Checks whether we can drop some pheromone yet. If we can, do it"""
- if self.pheromoneLevel == self.max_ph_level:
- ph = Pheromone(self.x, self.y)
- # The above command should create a pheromone
- self.pheromoneLevel = 0
- else:
- self.pheromoneLevel += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement