예제 #1
0
def main():
    global base
    r = ([0, 0, 0], [1, 1, 0], [1, 0, 0], [0, 1, 0], [-1, 1, 0], [1, -1, 0],
         [-1, 0, 0], [0, -1, 0], [-1, -1, 0])
    StatParser.readStats()
    Map.makeMap()
    startPos = setupStartPos(r)
    FogOfWar.createFogOfWar(startPos)
    FogOfWar.updateFogOfWar(agents)
    Resources.findMaterials()
    Pygame.init()

    # Adds jobs to the job list for workers to be assigned to.
    for j in range(StatParser.statDict["workers"]):
        if j < 30:
            Agents.addToJobList("woodCutter")
        elif j >= 30 and j < 40:
            Agents.addToJobList("miner")
        elif j >= 40 and j < 46:
            Agents.addToJobList("explorer")
        else:
            Agents.addToJobList("builder")

    # Adds some buildings to the build list so the builders know what to build.
    for k in range(8):
        if k < 6:
            base.addToBuildList(["coalFurnace", 0])
        else:
            base.addToBuildList(["smeltery", 0])

    # Allows user to speed up the application (can be laggy).
    TimeMultiplier.setTimeMultiplier(int(input("Set a time multiplier: ")))
    # Allows the user to choose if they want debug info to be displayed or not
    debug = input("Debug info y/n?: ")
    if debug == "y":
        debug = True
    else:
        debug = False

    # Runs and times update to see how long it took for the AI to complete the wanted task
    gameStart = time.time()
    if updateGame(debug):
        print("Time to finish: " +
              str((time.time() - gameStart) * TimeMultiplier.timeMultiplier))
        return True
    def execute(self, agent):
        readyToBuild = False
        # Where to build
        if agent.base.getBuildList() and not agent.getLocked():
            pos = agent.getPos()
            # find unused buildable tile
            for next in Map.r:
                if Map.map[pos[0] + next[0]][pos[1] + next[1]] in (
                        "M",
                        "G") and FogOfWar.fogOfWar[pos[0] + next[0]][pos[1] +
                                                                     next[1]]:
                    # What to build
                    for building in agent.base.getBuildList():
                        if building[0] == "coalFurnace":
                            if building[1] == 0 or building[1] == agent.getId(
                            ):
                                readyToBuild = agent.base.buildCoalFurnace(
                                    agent,
                                    (pos[0] + next[0], pos[1] + next[1]))
                                if readyToBuild == True:
                                    agent.setPos(
                                        (pos[0] + next[0], pos[1] + next[1]))
                                    building[1] = agent.getId()
                                    agent.setLocked(True)
                                break

                        elif building[0] == "smeltery":
                            if building[1] == 0 or building[1] == agent.getId(
                            ):
                                readyToBuild = agent.base.buildSmeltery(
                                    agent,
                                    (pos[0] + next[0], pos[1] + next[1]))
                                if readyToBuild == True:
                                    agent.setPos(
                                        (pos[0] + next[0], pos[1] + next[1]))
                                    building[1] = agent.getId()
                                    agent.setLocked(True)
                                break

                        elif building[0] == "blacksmith":
                            if building[1] == 0 or building[1] == agent.getId(
                            ):
                                readyToBuild = agent.base.buildBlacksmith(
                                    agent,
                                    (pos[0] + next[0], pos[1] + next[1]))
                                if readyToBuild:
                                    agent.setPos(
                                        (pos[0] + next[0], pos[1] + next[1]))
                                    building[1] = agent.getId()
                                    agent.setLocked(True)
                                break

                        elif building[0] == "trainingCamp":
                            if building[1] == 0 or building[1] == agent.getId(
                            ):
                                agent.base.buildTrainingcamp(
                                    agent,
                                    (pos[0] + next[0], pos[1] + next[1]))
                                building[1] = agent.getId()
                                if readyToBuild:
                                    agent.setPos(
                                        (pos[0] + next[0], pos[1] + next[1]))
                                    agent.setLocked(True)
                                break
                    break
        elif agent.getPos() == agent.getJob()[1]:
            diff = (time.time() -
                    agent.getTimer()) * TimeMultiplier.timeMultiplier

            if agent.getJob()[0] == "coalFurnace*":
                if diff >= StatParser.statDict["cfBuildTime"]:
                    agent.base.addBuilding(
                        BaseManager.coalFurnace(agent.getPos()))
                    agent.setJob("builder")
                    agent.setLocked(False)
                    agent.setState(returnHome())

            elif agent.getJob()[0] == "smeltery*":
                if diff >= StatParser.statDict["smelteryBuildTime"]:
                    agent.base.addBuilding(BaseManager.smeltery(
                        agent.getPos()))
                    agent.setJob("builder")
                    agent.setLocked(False)
                    agent.setState(returnHome())

            elif agent.getJob()[0] == "blacksmith*":
                if diff >= StatParser.statDict["bsBuildTime"]:
                    agent.base.addBuilding(
                        BaseManager.blacksmith(agent.getPos()))
                    agent.setJob("builder")
                    agent.setLocked(False)
                    agent.setState(returnHome())

            elif agent.getJob()[0] == "trainingCamp":
                if diff >= StatParser.statDict["tcBuildTime"]:
                    agent.base.addBuilding(
                        BaseManager.trainingCamp(agent.getPos()))
                    agent.setJob("builder")
                    agent.setLocked(False)
                    agent.setState(returnHome())

            # When to hire
            elif agent.getJob()[0] == "coalFurnace":
                if StatParser.statDict["cfBuildTime"] - (
                        time.time() - agent.getTimer()
                ) <= StatParser.statDict["artisanUpgradeTime"]:
                    Agents.addToJobList("coalWorker")
                    agent.setJob(("coalFurnace*", agent.getJob()[1]))
            elif agent.getJob()[0] == "smeltery":
                if StatParser.statDict["smelteryBuildTime"] - (
                        time.time() - agent.getTimer()
                ) <= StatParser.statDict["artisanUpgradeTime"]:
                    Agents.addToJobList("smelteryWorker")
                    agent.setJob(("smeltery*", agent.getJob()[1]))
            elif agent.getJob()[0] == "blacksmith":
                if StatParser.statDict["bsBuildTime"] - (
                        time.time() - agent.getTimer()
                ) <= StatParser.statDict["artisanUpgradeTime"]:
                    Agents.addToJobList("weaponSmith")
                    agent.setJob(("blacksmith*", agent.getJob()[1]))

        elif agent.base.getBuildList() == []:
            # When finished go idle until build list has content
            agent.setState(returnHome())