-
Notifications
You must be signed in to change notification settings - Fork 3
/
people.py
668 lines (528 loc) · 20.9 KB
/
people.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
import math
import random
import copy
import database as d
from conversation import Convo
from profiles import PersonProfile
from profiles import StoreProfile
class People:
name = "John Doe"
gender = 0 #0 are men, 1 are women
age = 0
locality = None #a localMap
home = None #a Unit in homeTown
# skills = [0 for i in d.getMaterials()] #each index represents a particular skill- shoemaking, or lumberjacking, etc.
job = None #None is unemployed
salary = 0
capital = 0
spouse = None
church = None
def __init__(self, model, firstname, lastname, theirGender, theirHometown, theirHome, theirReligion):
d.addPeople(self)
self.model = model
self.firstname = firstname
self.lastname = lastname
self.gender = theirGender
# self.age = theirAge
self.birthday = self.model.calendar.date()
self.locality = theirHometown
self.home = theirHome
self.location = theirHome.location
# self.skills = theirSkills
self.religion = theirReligion
self.muList = [0 for i in range(len(d.materialsList))]
self.inventory = [0 for i in range(len(d.materialsList))]
self.thoughts = []
#profiles
self.knownPeople = {}
self.knownManus = []
self.knownStores = []
self.knownHomes = []
self.knownChurches = []
self.knownUnitLists = [self.knownManus, self.knownStores, self.knownHomes, self.knownChurches]
self.myProfile = self.peopleManager(self)
self.myProfile.updateOpinion(100)
self.businesses = []
#initial values
self.allMu()
self.maxSadness = sum(self.muList)
self.isboss = False
@property
def name(self):
return self.firstname + " " + self.lastname
@name.setter
def name(self, value):
if len(value.split(' ')) >= 2:
self.firstname, self.lastname = value.split(maxsplit=1)
else:
self.firstname = value
self.peopleManager(self).name = value
def workHandler(self):
self.think("Off to work!")
self.updateJobPrices()
self.workConversations()
def restHandler(self):
self.think("I can finally relax a little.")
self.jobHandler()
self.friendConversations()
def shopHandler(self):
self.allMu()
self.goShopping()
self.allMu()
def sleepHandler(self):
self.think("My bed is so cozy.")
self.update_my_profile()
self.spouseConversations()
self.eat()
self.drink()
def jobHandler(self):
if self.job is None:
bestJob = self.jobSearch()
if bestJob is not None:
self.applyForJob(bestJob)
else:
self.think("I hate looking for jobs.")
def churchHandler(self):
if self.church is None:
churchSearch = self.churchSearch()
bestChurch = churchSearch[0]
if bestChurch is not None:
self.newChurch(bestChurch)
self.think("I joined ", bestChurch.name, " today.")
else:
self.think("I can't find a church in this neighborhood.")
else:
self.church.attend(self)
def update_my_profile(self):
profile = self.peopleManager(self)
dayNum = self.model.clock.getDayNum()
profile.updateBirthday(self.birthday)
profile.updateJob(self.job, dayNum)
profile.updateFamily(spouse = (self.spouse, dayNum))
profile.updateHouse(self.home, dayNum)
profile.updateMuList(self.muList, dayNum)
#only bosses can create businesses
def bossmaker(self):
if not self.isboss:
if self.capital >= 1000000 and self.model.char is not self:
d.addBoss(self)
self.model.builder.newBusiness(self)
self.isboss = True
def toString(self):
name = self.getName()
gender = self.genderCheck()
homeAddress = str(self.getHome().getLocation())
town = self.getLocality().getName()
personsJob = self.getJob()
if (gender == "man"):
genderPronoun = "He"
else:
genderPronoun = "She"
if (self.job != None):
jobDescription = personsJob.getJobType()
else:
jobDescription = "Beggar"
return(name + " is a " + gender + " who lives at " + homeAddress + " in "
+ town + ". " + genderPronoun + " works as a " + jobDescription + ".")
def jobSearch(self):
bestJob = None
jobList = []
for manu in self.knownManus:
localJobList = manu.getStore().getJobs(self)
jobList += localJobList
for store in self.knownStores:
localJobList = store.getStore().getJobs(self)
jobList += localJobList
for job in jobList:
slots = job.getSlots()
if slots > 0:
if bestJob is None:
bestJob = job
else:
if job.getSalary() > bestJob.getSalary():
bestJob = job
return bestJob
def churchSearch(self):
bestWeight = (None, 0)
for church in self.knownChurches:
if church.getReligion() == self.religion:
pythTuple = self.pyth(church)
distance = pythTuple[0]
isLocal = pythTuple[1]
weight = 1/distance
if weight > bestWeight[1] or bestWeight[0] is None:
if isLocal:
bestWeight = (church, weight)
return bestWeight
def applyForJob(self, job):
isHired = self.model.hirer.jobApplication(self, job)
return isHired
def updateJobPrices(self):
#if works at market, update workplaces price profile
workplace = None
worksAtMarket = False
if self.job is not None:
workplace = self.job.getUnit()
if workplace is not None:
worksAtMarket = workplace.getMissions()[d.STORE_INDEX]
if worksAtMarket:
workProfile = self.unitManager(workplace)
workPrices = workplace.getPrice()
dayNum = workplace.getDayNum()
workProfile.updatePrices(workPrices, dayNum)
#thoughts
self.think("I should check today's prices once I'm already here at work.")
#sleep
def spouseConversations(self):
if self.spouse is not None:
Convo.beginConversation(self, self.spouse)
#work
def workConversations(self):
if self.job is not None:
coworkers = self.job.getEmployees()
totalCoworkers = len(coworkers)
# numberOfConvos = random.randrange(4)
numberOfConvos = 1
if numberOfConvos > totalCoworkers:
numberOfConvos = totalCoworkers
#conversations
for conversation in range(numberOfConvos):
conversee = coworkers[random.randrange(totalCoworkers)]
if conversee is not self:
Convo.beginConversation(self, conversee)
#don't use unless you add children
def familyConversations(self):
family = self.peopleManager(self).getFamilyList()
if len(family) > 0:
conversee = family[random.randrange(len(family))]
Convo.beginConversation(self, conversee)
else:
self.think("I'm all alone in the world.")
def friendConversations(self):
friend = self.randomPerson().person
if friend is not self:
Convo.beginConversation(self, friend)
self.think(friend.name + " and I hung out this afternoon.")
else:
self.think("I enjoyed spending some time alone this afternoon.")
def newChurch(self, church):
self.church = church
self.church.addMember(self)
def getHappiness(self):
sadness = sum(self.muList)
happiness = 100 * ((self.maxSadness - sadness) / self.maxSadness)
return happiness
#updates muList- run at start and end of shop state. We want it to be persistent even after they eat, for example
#limit zeroes mu curve for each item
#scale scales mu curve for each item
def allMu(self):
limitList = d.getUtilityLimit()
scaleList = d.getUtilityScale()
owned = self.home.getAllOutput()
#muList calc
muList = [0 for item in owned]
for itemIndex in range(len(owned)):
itemCount = owned[itemIndex]
if itemCount == 0:
itemCount = .1
limit = limitList[itemIndex]
scale = scaleList[itemIndex]
mu = self.singleMu(itemIndex, itemCount)
muList[itemIndex] = mu
self.muList = muList
def singleMu(self, i, count):
limitList = d.getUtilityLimit()
scaleList = d.getUtilityScale()
limit = limitList[i]
scale = scaleList[i]
mu = scale * ((math.sqrt(limit) / math.sqrt(count)) - 1)
return mu
def goShopping(self):
p_chosenStore = self.chooseStore()[0]
if p_chosenStore is not None:
store = p_chosenStore.store
self.purchase(store)
self.storeAtHome()
else:
self.think("I don't know of any stores.")
def pyth(self, unit):
locality = unit.getLocality()
location = unit.getLocation()
isLocal = False
#distance
if (locality == self.locality) and (location is not None):
distanceX = self.location[0] - location[0]
distanceY = self.location[1] - location[1]
distance = math.sqrt(distanceX ** 2 + distanceY ** 2)
isLocal = True
else:
#distance must be defined and != 0
distance = 1000000
return (distance, isLocal)
#returns (storeProfile, weight)
def chooseStore(self):
#weights
F = 1
E = 2
D = 1
bestWeight = (None, 0)
#possible stores
for store in self.possStores():
#vars
familiarity = store.getFamiliarity()
experience = store.getExperience()
avgPrices = store.getPrices()
#distance
distance, isLocal = self.pyth(store)
#desiredItemWeight
muList = self.getMuList()
maxMu = max(muList)
price = avgPrices[muList.index(maxMu)]
if price != 0:
desiredItemWeight = maxMu/price
else:
desiredItemWeight = 0
#formula
weight = (1 / distance) * (familiarity * F) * (experience * E) * (desiredItemWeight * D)
if (weight > bestWeight[1]) or (bestWeight[0] is None):
if isLocal:
bestWeight = (store, weight)
#thoughts
if bestWeight[0] is not None:
self.think(bestWeight[0].name + " seems like a good place to shop.")
else:
self.think("I can't think of anywhere to go shopping.")
return bestWeight
def possStores(self):
possStores = []
for store in self.knownStores:
if store.familiarity == 1:
self.think("I want to check out that new place I heard about, " + store.name + ".")
possStores = [store]
break
else:
possStores.append(store)
return possStores
def canAfford(self, amount):
if self.capital >= amount:
return True
else:
return False
def purchase(self, store):
buyMore = True
price = store.getPrice()
cash = self.capital
muList = self.muList
buy = [0 for i in d.getMaterials()]
value = [self.muList[i] / price[i] if price[i] != 0 else 0 for i in range(len(self.muList))]
#we don't want them to spend all their money on overpriced stuff- they'll need it tomorrow! Utility of money is CONSTANT.
MONEYUTIL = 1
while True:
i = value.index(max(value))
if (value[i] < MONEYUTIL) or (cash < price[i]):
break
bestPrice = price[i]
cash -= bestPrice
buy[i] += 1
value[i] = (self.singleMu(i, self.home.output[i] + buy[i]) / price[i])
if sum(buy) > 0:
(amounts, cost, sold) = store.sell(self, copy.copy(buy))
if sold:
if (amounts == buy):
self.think("I bought " + self.listtostr(amounts) + " for " + str(cost) + " today at " + store.name + ".")
else:
self.think("I went to buy " + self.listtostr(buy) + " at " + store.name + " but they only had " + self.listtostr(amounts) + ".")
else:
self.think("I went to " + store.name + " to buy " + self.listtostr(buy) + " but they ran out. I hate that place!")
else:
sold = False
self.think("I don't really need anything from " + store.name + ". What a waste of time.")
#update profile
self.updateStore(store, sold)
def listtostr(self, array):
string = ""
for i in range(len(array)):
if array[i] > 0:
string += str(array[i]) + " " + d.getMaterials()[i] + ", "
string = string[:-2]
return string
def updateStore(self, store, sold):
storeProfile = self.unitManager(store)
price = store.getPrice()
# familiarity = storeProfile.getFamiliarity()
experience = storeProfile.getExperience()
#familiarity should increase with each visit
storeProfile.updateFamiliarity()
#experience should increase only if they had a good experience
if sold:
experience = experience * 1.1
else:
experience = experience / 1.1
storeProfile.updateExperience(experience)
#price
storeProfile.updatePrices(price, store.getLocality().getDayNum())
def storeAtHome(self):
storage = self.home.output
for i in range(len(self.inventory)):
amount = self.inventory[i]
self.inventory[i] -= amount
storage[i] += amount
def eat(self):
storage = self.home.output
if storage[d.BREAD_INDEX] >= 1:
storage[d.BREAD_INDEX] -= 1
self.think("I ate some delicious bread today.")
else:
self.think("I'm starving! I don't want to die!")
def drink(self):
storage = self.home.output
if storage[d.BEER_INDEX] >= 1:
storage[d.BEER_INDEX] -= 1
self.think("That was a delicious beer!")
else:
self.think("I could really use a beer right now.")
def randomManu(self, oldManu=None):
length = len(self.knownManus)
if length >= 2:
manuIndex = random.randint(0, length-1)
tryManu = self.knownManus[manuIndex]
if (tryManu is not oldManu) or (oldManu is None):
newManu = tryManu
else:
newManu = self.randomManu(oldManu)
elif length == 1 and oldManu == None:
newManu= self.knownManus[0]
else:
newManu = None
return newManu
def randomPerson(self, oldPerson=None):
length = len(self.knownPeople)
if length >= 2:
tryPerson = self.knownPeople[random.choice(list(self.knownPeople.keys()))]
if (tryPerson is not oldPerson) or (oldPerson is None):
newPerson = tryPerson
else:
newPerson = self.randomPerson(oldPerson)
elif length == 1 and oldPerson == None:
newPerson = self.knownPeople[list(self.knownPeople.keys())[0]]
else:
newPerson = None
return newPerson
def randomStore(self, oldStore=None):
length = len(self.knownStores)
if length >= 2:
storeIndex = random.randint(0, length-1)
tryStore = self.knownStores[storeIndex]
if (tryStore is not oldStore) or (oldStore is None):
newStore = tryStore
else:
newStore = self.randomStore(oldStore)
elif length == 1 and oldStore == None:
newStore = self.knownStores[0]
else:
newStore = None
return newStore
def peopleManager(self, target):
if target in self.knownPeople:
targetProfile = self.knownPeople[target]
else:
targetProfile = PersonProfile(target)
self.knownPeople[target] = targetProfile
return targetProfile
def unitManager(self, target, heardabout=None):
from profiles import StoreProfile
notFound = True
missions = target.getMissions()
#search
for i in range(len(missions)):
if missions[i] and notFound:
unitList = self.knownUnitLists[i]
for testProfile in unitList:
if testProfile.store is target:
targetProfile = testProfile
notFound = False
break
#create
if notFound:
targetProfile = StoreProfile(target, heardabout)
for i in range(len(missions)):
if missions[i]:
unitList = self.knownUnitLists[i]
unitList.append(targetProfile)
#return
return targetProfile
def think(self, thought):
dayNum = self.model.getDayNum()
weekDay = self.model.week.state
date = self.model.calendar.date()
state = self.model.clock.state
thought = (dayNum, weekDay, date, state, thought)
self.thoughts.append(thought)
if self.model.char == self:
self.model.out(str(thought[0]).ljust(6) + thought[1].ljust(10) + str(thought[2]).ljust(19) + thought[3].ljust(10) + thought[4] + "\n")
def printThoughts(self):
print(self.name, "thought:")
print("Day: " + "Weekday: " + "Date: " + "Period: " + "Thought:")
for thought in self.thoughts:
print(str(thought[0]).ljust(6) + thought[1].ljust(10) + str(thought[2]).ljust(19) + thought[3].ljust(10) + thought[4])
def getName(self):
return self.name
def getGender(self):
return self.gender
# def getAge(self):
# return self.age
def getLocality(self):
return self.locality
def getHome(self):
return self.home
def setHome(self, home):
self.home = home
def getJob(self):
return self.job
def getBusinesses(self):
return self.businesses
def getMuList(self):
return self.muList
def getKnownPeople(self):
return list(self.knownPeople.values())
def getKnownStores(self):
return self.knownStores
def getKnownManus(self):
return self.knownManus
def getKnownChurches(self):
return self.knownChurches
def getSalary(self):
return self.salary
# def getSkill(self, skillIndex):
# return self.skills[skillIndex]
# def getSkills(self):
# return self.skills
def setJob(self, job, salary):
self.job = job
self.salary = salary
dayNum = self.model.clock.getDayNum()
self.myProfile.updateJob(job, dayNum)
def unsetJob(self):
job = self.getJob()
self.job = None
dayNum = self.model.clock.getDayNum()
self.myProfile.updateJob(None, dayNum)
def genderCheck(self):
if self.getGender() == 0:
return("man")
else:
return("woman")
def setSpouse(self, spouse):
self.spouse = spouse
if self.getGender() == 1:
self.lastname = spouse.lastname
def getCapital(self):
return self.capital
def addCapital(self, capital):
self.capital += capital
def addInventory(self, i_item, amount):
self.inventory[i_item] += amount
def getInventory(self):
return self.inventory
def getReligion(self):
return self.religion