Hey guys,
For my project I needed the NPC (computer players) to make decisions based off their personality, specifically based off five basic emotions, anger, disgust, fear, happiness, and sadness.
I built a simple script “logicHandler.py” which will do all of this, and make random decisions based off the personality of these.
You can of course, use other emotions or categories as you wish, by using a call to setWeight(name, weightValue) as you wish.
Without further ado, here you go:
import random
#- A logicHandler makes emotional decisions, based off the emotional traites of the logicHandler
#- You may assign 'weights' to the logicHandler, for basic emotions such as:
#- Anger, disgust, fear, happiness, and sadness
#- Then the logicHandler can make choices from a dict, that defines choices for each possible emotion
#- For instance {'anger': ['beAngry], 'disgust': ['beDisgusted', 'beDisgustedTwo']} and so forth
def weightedChoice(inputList):
n = random.uniform(0, 1)
for possibleChoice, weight in inputList:
if n < weight:
break
n = n - weight
return possibleChoice
class logicHandler:
def __init__(self, angerWeight = None, disgustWeight = None, fearWeight = None, happinessWeight = None, sadnessWeight = None):
self.__weights = {}
self.setAngerWeight(angerWeight)
self.setDisgustWeight(disgustWeight)
self.setFearWeight(fearWeight)
self.setHappinessWeight(happinessWeight)
self.setSadnessWeight(sadnessWeight)
def choice(self, choiceDict):
if len(choiceDict.keys()) == 0:
return
validWeights = []
for weightName, weight in self.__weights.items():
if weight:
validWeights.append((weightName, weight))
percentWeights = []
totalWeight = 0
for weightName, weight in validWeights:
totalWeight += weight
for weightName, weight in validWeights:
percentWeight = weight / totalWeight
percentWeights.append((weightName, percentWeight))
#- Make weighted choice
choiceList = []
for weightName, weight in percentWeights:
if weightName in choiceDict:
choiceList.append((choiceDict[weightName], weight))
picked = weightedChoice(choiceList)
if len(picked) == 0:
return
picked = random.choice(picked)
return picked
def __checkWeight(self, weight):
if weight == None:
return
assert weight >= 0.0
assert weight <= 1.0
return float(weight)
''' set/get weight value '''
def setWeight(self, weight, value):
self.__weights[weight] = self.__checkWeight(value)
def getWeight(self, weight):
return self.__weights[weight]
def hasWeight(self, weight):
return weight in self.__weights
def getWeights(self):
return self.__weights
''' set/get anger weight'''
def setAngerWeight(self, weight):
self.setWeight('anger', weight)
def getAngerWeight(self):
return self.getWeight('anger')
''' set/get disgust weight'''
def setDisgustWeight(self, weight):
self.setWeight('disgust', weight)
def getDisgustWeight(self):
return self.getWeight('disgust')
''' set/get fear weight'''
def setFearWeight(self, weight):
self.setWeight('fear', weight)
def getFearWeight(self):
return self.getWeight('fear')
''' set/get happiness weight'''
def setHappinessWeight(self, weight):
self.setWeight('happiness', weight)
def getHappinessWeight(self):
return self.getWeight('happiness')
''' set/get sadness weight'''
def setSadnessWeight(self, weight):
self.setWeight('sadness', weight)
def getSadnessWeight(self):
return self.getWeight('sadness')
Put that in a “logicHandler.py” file, or whatever
And you can use this code like this:
from logicHandler import *
l = logicHandler()
l.setAngerWeight(1.0) #- Very angry
l.setFearWeight(0.5) #- Midly afraid
choiceDict = {
'anger': ['I am angry!', 'I am angry 2!'],
'fear': ['I am afraid!', 'I am afraid 2!'],
}
print l.choice(choiceDict)