Skip to content

Commit

Permalink
- Added Brain class (for ai stuff)
Browse files Browse the repository at this point in the history
- Added Brain instance to the character class/objects.

to-do: make use of the new Brain class/instance.
  • Loading branch information
Florian R. A. Angermeier committed Jun 26, 2011
1 parent 5df350e commit 375cd38
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 29 deletions.
94 changes: 94 additions & 0 deletions gameEngine/Brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@


class Brain:
# The brain class provides attributes and methodes for the ai of an character.

def __init__( self, character ):
self.character = character
self.activitiesHigh = { 'sleep': 'rest',
'food': 'eatSnack',
'water': 'drink',
'urination': 'use',
'hygiene': 'shower'
'fun': 'watch',
'social': 'talk' }
self.activitiesMedium = { 'sleep': 'rest',
'food': 'cook',
'water': 'drink',
'urination': 'use',
'hygiene': 'shower'
'fun': 'watch',
'social': 'talk' }
self.activitiesLow = { 'sleep': 'sleep',
'food': 'cook',
'water': 'drink',
'urination': 'use',
'hygiene': 'shower'
'fun': 'watch',
'social': 'talk' }

def getLowestNeed( self ):
# Returns the lowest need including its value as a tuple.
lowestNeed = (None, 1000)
for need in self.character.needs.keys():
try:
if( self.character.needs[need] < lowestNeed(1) ):
lowestNeed(0) = need
lowestNeed(1) = self.character.needs[need]
except:
print '\033[31mError\033[0m Character: %i\tNeed %s does not exist.' % ( self.character.id, need )
return lowestNeed

def getHighestNeed( self ):
# Returns the highest need including its value as a tuple.
highestNeed = (None, 0)
for need in self.character.needs.keys():
try:
if( self.character.needs[need] > highestNeed(1) ):
highestNeed(0) = need
highestNeed(1) = self.character.needs[need]
except:
print '\033[31mError\033[0m Character: %i\tNeed %s does not exist.' % ( self.character.id, need )
return highestNeed

def getNextActivity( self ):
# Get the next activity to satisfy the lowest need.
if( self.getLowestNeed()(1) <= 750 ): # 3/4, High
return self.activitiesHigh[ self.getLowestNeed()(0) ]
elif( self.getLowestNeed()(1) <= 500 ): # 1/2, Medium
return self.activitiesMedium[ self.getLowestNeed()(0) ]
elif( self.getLowestNeed()(1) <= 250 ): # 1/4, Low
return self.activitiesLow[ self.getLowestNeed()(0) ]
else:
return None

def getObjectById( self, id ):
# Searches for an object in the world by id and returns it.
for object in self.character.world.objects:
if( object.id == id ):
return object

def getObjectByType( self, type ):
# Searches for an object in the world by type and returns it.
for object in self.character.world.objects:
if( object.type == type ):
return object

def isInRange( self, characterOrObject, radiusType ):
# Checks if a character or object is in range by utilization of a bounding box and returns True if the object is in range.
# X axis
if( self.character.pos[ 0 ] >= characterOrObject.pos[ 0 ] - characterOrObject.radii[ radiusType ]
self.character.pos[ 0 ] <= characterOrObject.pos[ 0 ] + characterOrObject.radii[ radiusType ] ):
# Y axis
if( self.character.pos[ 1 ] >= characterOrObject.pos[ 1 ] - characterOrObject.radii[ radiusType ]
self.character.pos[ 1 ] <= characterOrObject.pos[ 1 ] + characterOrObject.radii[ radiusType ] ):
# Z axis
if( self.character.pos[ 2 ] >= characterOrObject.pos[ 2 ] - characterOrObject.radii[ radiusType ]
self.character.pos[ 2 ] <= characterOrObject.pos[ 2 ] + characterOrObject.radii[ radiusType ] ):
return True
else:
return False
else:
return False
else:
return False
78 changes: 49 additions & 29 deletions gameEngine/Character.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,38 @@

from random import choice

from Brain import Brain

class Character:
def __init__( self, id, world, gender ):
self.id = id # Unique integer number to identify a character.
self.world = world # The world a character belongs to.
self.pos = [ 0, 0, 0 ] # A characters current position.
self.destination = [ 0, 0, 0 ] # A characters destination for movement.
self.world = world # The world a character belongs to.
self.id = id # Unique integer number to identify a character.
self.pos = [ 0, 0, 0 ] # A characters current position.
self.destination = [ 0, 0, 0 ] # A characters destination for movement.

if( gender.lower() == 'random' ): # A characters gender.
self.gender = choice( ( 'male', 'female' ) ) #
else: #
self.gender = gender #
if( gender.lower() == 'random' ): # A characters gender.
self.gender = choice( ( 'male', 'female' ) ) #
else: #
self.gender = gender #

self.activity = None # A characters activity.
self.activityTimer = 0 # How much game seconds are left for an activity.
self.activityInteractive = None # The character or object to interact with.
self.activity = None # A characters activity.
self.activityTimer = 0 # How much game seconds are left for an activity.
self.activityInteractive = None # The character or object to interact with.

self.activityRadiusOn = 0 # The radius for activities which the character is
# on a character or object.
self.activityRadiusNextTo = 1 # The radius for activities which the character is
# next to a character or object.
self.activityRadiusNear = 2 # The radius for activities which the character is
# near the character or object.
# to-do: use the new brain class for ai
self.activityRadiusOn = 0
self.activityRadiusNextTo = 1
self.activityRadiusNear = 2

# The brain which provides attributes and methodes for the ai of a character.
self.brain = Brain( self ) # near the character or object.

self.needs = { 'sleep': 500, # The chracters needs.
'food': 500, #
'water': 500, #
'hygiene': 500, #
'fun': 1000, #
'social': 1000 } #
self.needs = { 'sleep': 500, # The chracters needs.
'food': 500, #
'water': 500, #
'hygiene': 500, #
'fun': 1000, #
'social': 1000 } #

def teleportTo( self, x, y, z ):
print 'DEBUG: Character %i teleports to X: %i Y: %i Z: %i.' % ( self.id, x, y, z )
Expand Down Expand Up @@ -131,8 +134,8 @@ def decreaseNeeds( self ):
self.needs['water'] -= 0.011574074
if( self.activity != 'shower' ):
self.needs['hygiene'] -= 0.005787037
# if( self.activity != 'watchTV' ):
# self.needs['fun'] -= 1
if( self.activity != 'watch' ):
self.needs['fun'] -= 1
# if( self.activity != 'chat' ):
# self.needs['social'] -= 1
except:
Expand Down Expand Up @@ -160,6 +163,8 @@ def processActivity( self ):
radiusType = 'NextTo'
elif( self.activity == 'shower' ):
radiusType = 'On'
elif( self.activity == 'watch' ):
radiusType = 'Near'
else:
radiusType = 'Near'

Expand Down Expand Up @@ -265,7 +270,22 @@ def processActivity( self ):
self.activity = 'beHappy' # Reset the activity to a standard beHappy activity.
self.activityTimer = 1 # Reset the timer to 1.
self.activityInteractive = None # Reset the interactive to None.
else:
self.activity = 'beHappy' # Reset the activity to a standard beHappy activity.
self.activityTimer = 1 # Reset the timer to 1.
self.activityInteractive = None # Reset the interactive to None.
elif( lowestNeedName == 'fun' ):
if( lowestNeedValue < 833.333333333 ):
for object in self.world.objects:
if( object.type == 'TvSet' ):
self.activity = 'watch'
self.activityTimer = 1800 # 30 game minutes.
self.activityInteractive = object.id
if( self.isInRange( object, 'On' ) == False ):
# If the character is not in range set the new destination and move to it.
self.goTo( object.pos[0], object.pos[1], object.pos[2] )
self.move()
else: # The lowest need is not below the minimal limit.
self.activity = 'beHappy' # Reset the activity to a standard beHappy activity.
self.activityTimer = 1 # Reset the timer to 1.
self.activityInteractive = None # Reset the interactive to None.
else: # There is no lowest need. Should never happen.
self.activity = 'beHappy' # Reset the activity to a standard beHappy activity.
self.activityTimer = 1 # Reset the timer to 1.
self.activityInteractive = None # Reset the interactive to None.

0 comments on commit 375cd38

Please sign in to comment.