Example #1
0
def simulateNPCsYear(aliveNPCDict, businesses):
    for npcName in aliveNPCDict.copy():
        npc = aliveNPCDict[npcName]
        if not npc.isAlive:
            continue

        # Simulate
        returnDict = npc.yearUpdate()

        # Handle returnDict events that happened in a year
        for key, val in returnDict.items():
            if key == "died" and val == True:
                # NPC died
                npc.isAlive = False

                # NPC dying leaves behind a widow/widower
                if npc.spouse != None:
                    npc.spouse.isWidowed = True

                # If NPC had a job, it is now vacant
                if npc.job != None:
                    businesses[npc.workplace]["jobs"][npc.job] = None
                    businesses[npc.workplace]["vacancies"] += 1

                del aliveNPCDict[npc.firstName + " " + npc.lastName]

            if key == "childBorn" and val == True:
                npc.pregnant = False
                # NPC gave birth to a baby
                baby = NPC(race=npc.race, age=0, isAlive=True, bornIn=npc.currentLocation, lastName=npc.lastName)

                # Add mother/father to baby
                baby.mother = npc
                baby.father = npc.spouse

                # Add sibling relationships
                for sibling in baby.mother.children:
                    sibling.siblings.append(baby)
                    baby.siblings.append(sibling)

                # Add to mother/father children lists
                baby.mother.children.append(baby)
                baby.father.children.append(baby)

                aliveNPCDict[baby.firstName + " " + baby.lastName] = baby

            if key == "retired" and val == True:
                businesses[npc.workplace]["jobs"][npc.job] = None
                businesses[npc.workplace]["vacancies"] += 1

                npc.isRetired = True
                npc.job = None
                npc.workplace = None

            if key == "foundJob" and val == True:
                # Loop through all businesses
                foundJob = False
                for businessName in businesses:
                    # Find business with vacancy
                    if businesses[businessName]["vacancies"] >= 1:
                        # Loop through jobs to find the available one, or one of the available jobs
                        for jobName in businesses[businessName]["jobs"]:
                            if businesses[businessName]["jobs"][jobName] == None: # Eg. "worker1" : None
                                businesses[businessName]["jobs"][jobName] = npc
                                businesses[businessName]["vacancies"] -= 1

                                npc.job = jobName
                                npc.workplace = businessName

                                foundJob = True
                                break
                    # If a job is found, then stop looking for vacancies
                    if foundJob:
                        break

            if key == "gotMarried" and val == True:
                # NPC got married to another NPC
                # Find an elligible NPC
                for otherNPCName, otherNPC in aliveNPCDict.items():
                    if otherNPC.lastName != npc.lastName:
                        if otherNPC.ageGroup == npc.ageGroup and otherNPC.gender != npc.gender and \
                                (otherNPC.spouse == None or otherNPC.isWidowed) and npc.race == otherNPC.race:
                            marryNPCs(npc, otherNPC)
                            # print("Married NPCs!")
                            # print(npc)
                            # print(otherNPC)
                            break