2015-03-29 15:41:02 +00:00
|
|
|
from direct.fsm.FSM import FSM
|
|
|
|
import PetUtil, PetDNA
|
2015-03-29 22:16:26 +00:00
|
|
|
from toontown.toonbase import ToontownGlobals
|
|
|
|
from toontown.toonbase import TTLocalizer
|
2015-03-29 15:41:02 +00:00
|
|
|
import cPickle, time, random, os
|
2015-03-03 22:10:12 +00:00
|
|
|
|
2015-03-29 15:41:02 +00:00
|
|
|
MINUTE = 60
|
|
|
|
HOUR = 60 * MINUTE
|
|
|
|
DAY = 24 * HOUR
|
2015-03-03 22:10:12 +00:00
|
|
|
|
2015-03-29 15:41:02 +00:00
|
|
|
def getDayId():
|
|
|
|
return int(time.time() // DAY)
|
|
|
|
|
2015-03-03 22:10:12 +00:00
|
|
|
class PetManagerAI:
|
2015-03-29 22:16:26 +00:00
|
|
|
NUM_DAILY_PETS = 5 # per hood
|
2015-03-29 15:41:02 +00:00
|
|
|
cachePath = config.GetString('air-pet-cache', 'astron/databases/air_cache/')
|
2015-03-29 22:16:26 +00:00
|
|
|
|
2015-03-03 22:10:12 +00:00
|
|
|
def __init__(self, air):
|
|
|
|
self.air = air
|
2015-03-29 15:41:02 +00:00
|
|
|
self.cacheFile = '%spets_%d.pets' % (self.cachePath, self.air.districtId)
|
|
|
|
if os.path.isfile(self.cacheFile):
|
|
|
|
with open(self.cacheFile, 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
|
|
|
|
self.seeds = cPickle.loads(data)
|
|
|
|
if self.seeds.get('day', -1) != getDayId() or len(self.seeds.get(ToontownGlobals.ToontownCentral, [])) != self.NUM_DAILY_PETS:
|
|
|
|
self.generateSeeds()
|
|
|
|
|
|
|
|
else:
|
2015-03-29 22:16:26 +00:00
|
|
|
self.generateSeeds()
|
|
|
|
|
2015-03-29 15:41:02 +00:00
|
|
|
def generateSeeds(self):
|
|
|
|
seeds = range(0, 255)
|
|
|
|
random.shuffle(seeds)
|
|
|
|
|
|
|
|
self.seeds = {}
|
|
|
|
for hood in (ToontownGlobals.ToontownCentral, ToontownGlobals.DonaldsDock, ToontownGlobals.DaisyGardens,
|
2015-03-29 22:16:26 +00:00
|
|
|
ToontownGlobals.MinniesMelodyland, ToontownGlobals.TheBrrrgh, ToontownGlobals.DonaldsDreamland,
|
|
|
|
ToontownGlobals.FunnyFarm):
|
2015-03-29 15:41:02 +00:00
|
|
|
self.seeds[hood] = [seeds.pop() for _ in xrange(self.NUM_DAILY_PETS)]
|
|
|
|
|
|
|
|
self.seeds['day'] = getDayId()
|
|
|
|
|
|
|
|
with open(self.cacheFile, 'wb') as f:
|
2015-03-29 22:16:26 +00:00
|
|
|
f.write(cPickle.dumps(self.seeds))
|
2015-03-03 22:10:12 +00:00
|
|
|
|
2015-03-29 15:41:02 +00:00
|
|
|
def getAvailablePets(self, seed, safezoneId):
|
|
|
|
if self.seeds.get('day', -1) != getDayId():
|
|
|
|
self.generateSeeds()
|
|
|
|
|
2015-03-29 22:16:26 +00:00
|
|
|
return list(set(self.seeds.get(safezoneId, [seed])))
|
2015-03-03 22:10:12 +00:00
|
|
|
|
2015-03-29 15:41:02 +00:00
|
|
|
def createNewPetFromSeed(self, avId, seed, nameIndex, gender, safeZoneId):
|
|
|
|
av = self.air.doId2do[avId]
|
|
|
|
|
|
|
|
name = TTLocalizer.getPetName(nameIndex)
|
|
|
|
_, dna, traitSeed = PetUtil.getPetInfoFromSeed(seed, safeZoneId)
|
|
|
|
head, ears, nose, tail, body, color, cs, eye, _ = dna
|
|
|
|
numGenders = len(PetDNA.PetGenders)
|
|
|
|
gender %= numGenders
|
|
|
|
|
|
|
|
fields = {'setOwnerId' : avId, 'setPetName' : name, 'setTraitSeed' : traitSeed, 'setSafeZone' : safeZoneId,
|
|
|
|
'setHead' : head, 'setEars' : ears, 'setNose' : nose, 'setTail' : tail, 'setBodyTexture' : body,
|
|
|
|
'setColor' : color, 'setColorScale' : cs, 'setEyeColor' : eye, 'setGender' : gender}
|
|
|
|
|
|
|
|
def response(doId):
|
|
|
|
if not doId:
|
|
|
|
self.air.notify.warning("Cannot create pet for %s!" % avId)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.air.writeServerEvent('bought-pet', avId, doId)
|
|
|
|
av.b_setPetId(doId)
|
|
|
|
|
|
|
|
self.air.dbInterface.createObject(self.air.dbId, self.air.dclassesByName['DistributedPetAI'],
|
|
|
|
{k: (v,) for k,v in fields.items()}, response)
|
|
|
|
|
|
|
|
def deleteToonsPet(self, avId):
|
|
|
|
av = self.air.doId2do[avId]
|
|
|
|
pet = av.getPetId()
|
|
|
|
if pet:
|
|
|
|
if pet in self.air.doId2do:
|
|
|
|
self.air.doId2do[pet].requestDelete()
|
|
|
|
|
2015-03-29 22:16:26 +00:00
|
|
|
av.b_setPetId(0)
|
|
|
|
# XXX to do: check for current pet and destroy it if generated
|
|
|
|
|