Skip to content

Commit

Permalink
Config and in-game-world objects refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
cu2 committed Sep 22, 2013
1 parent bda41e9 commit 0a401a5
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 74 deletions.
85 changes: 84 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
"""
General config file
Usage:
You can change basically any value. Just bear the consequences. :-)
How to:
- simple values: ALLCAPS = <something>
change <something>
- enums: class CamelCase(MyEnum): values = [<list>]
change <list>
- dictionaries: ALLCAPS = { <key>: <value>, <key>: <value>, ... }
change <key> and/or <value>, add key-value pairs, delete them
- overrides: <something> *= 2 # i.e. <something> = <something> * 2
change it as you like, just don't create syntax error
"""

from myenum import MyEnum
from utils import multi_config


# Server
ZANDAGORT_SERVER_HOST = "localhost"
ZANDAGORT_SERVER_PORT = 3492
Expand All @@ -16,7 +33,73 @@
ADMIN_USER_EMAIL = "admin@admin"
ADMIN_USER_PASSWORD = "admin"

# World
# Speed
CRON_BASE_DELAY = 1
CRON_SIM_INTERVAL = 2
CRON_DUMP_INTERVAL = 5


# Planets
class PlanetClasses(MyEnum):
values = ["A", "B", "C", "D", "E"]

# Ecosystem
class Species(MyEnum):
values = ["null", "ebony", "panda", "tiger"]

MINIMUM_VIABLE_POPULATION = 10

BETAMATRIXES = {
PlanetClasses.B: {
(Species.ebony, Species.null): 10,
(Species.ebony, Species.ebony): -0.01,
(Species.ebony, Species.panda): -0.1,
(Species.panda, Species.ebony): 0.01,
(Species.panda, Species.panda): -0.1,
},
}

INITIAL_SPECIES = {
"": {}, # just a placeholder, don't change it
PlanetClasses.B: {
Species.ebony: 1000,
Species.panda: 100,
Species.tiger: 10,
},
}


# Economy

## Resources
class Resources(MyEnum):
values = ["raw_stone", "stone", "lumber"]

## Buildings
class Buildings(MyEnum):
values = ["quarry", "sawmill"]

## IO Matrix
BASIC_IOMATRIX = {
(Buildings.quarry, Resources.raw_stone): -100,
(Buildings.quarry, Resources.stone): 50,
(Buildings.sawmill, Species.ebony): -100,
(Buildings.sawmill, Resources.lumber): 50,
}
IOMATRIXES = multi_config(BASIC_IOMATRIX, PlanetClasses)

IOMATRIXES[PlanetClasses.B][(Buildings.quarry, Resources.stone)] = 75

## Initial resources
BASIC_INITIAL_RESOURCES = {
Resources.raw_stone: 500000,
}
INITIAL_RESOURCES = multi_config(BASIC_INITIAL_RESOURCES, PlanetClasses)

INITIAL_RESOURCES[PlanetClasses.B][Resources.raw_stone] *= 2

## Initial buildings
BASIC_INITIAL_BUILDINGS = {
Buildings.quarry: 2,
}
INITIAL_BUILDINGS = multi_config(BASIC_INITIAL_BUILDINGS, PlanetClasses)
37 changes: 5 additions & 32 deletions economy.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
from myenum import MyEnum
from ecosystem import Species


class Resources(MyEnum):

values = ["raw_stone", "stone", "lumber"]


class Buildings(MyEnum):

values = ["quarry", "sawmill"]
import config


class Economy(object):

def __init__(self):
self._resources = {
Resources.raw_stone: 500000,
}
self._buildings = {
Buildings.quarry: 2,
}
self._iomatrix = {
(Buildings.quarry, Resources.raw_stone): -100,
(Buildings.quarry, Resources.stone): 50,
(Buildings.sawmill, Species.ebony): -100,
(Buildings.sawmill, Resources.lumber): 50,
}
def __init__(self, class_):
self._class = class_
self._resources = config.INITIAL_RESOURCES[self._class]
self._buildings = config.INITIAL_BUILDINGS[self._class]

def sim(self):
pass


class EconomyClassB(Economy):

def __init__(self):
super(EconomyClassB, self).__init__()
self._resources[Resources.raw_stone] *= 2
39 changes: 10 additions & 29 deletions ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from myenum import MyEnum


class Species(MyEnum):

values = ["null", "ebony", "panda", "tiger"]
import config


class Ecosystem(object):

def __init__(self):
self._species = {}
self._betamatrix = {}
def __init__(self, class_):
self._class = class_
self._species = config.INITIAL_SPECIES[self._class]
self._area = 2000000

def __str__(self):
outstr = ""
Expand All @@ -20,27 +16,12 @@ def __str__(self):

def sim(self):
new_species = self._species.copy()
for species, other_species in self._betamatrix:
if other_species == Species.null:
new_species[species] += 1.0 * self._betamatrix[(species, other_species)]
for species, other_species in config.BETAMATRIXES[self._class]:
if other_species == config.Species.null:
new_species[species] += 1.0 * config.BETAMATRIXES[self._class][(species, other_species)]
else:
new_species[species] += 1.0 * self._betamatrix[(species, other_species)] * self._species[other_species]
new_species[species] += 1.0 * config.BETAMATRIXES[self._class][(species, other_species)] / (self._area/100000) * self._species[other_species]
for species in new_species:
if new_species[species] < 0:
if new_species[species] < config.MINIMUM_VIABLE_POPULATION:
new_species[species] = 0
self._species = new_species.copy()


class EcosystemClassB(Ecosystem):

def __init__(self):
self._species = {Species.ebony: 1000,
Species.panda: 100,
Species.tiger: 10}
self._betamatrix = {
(Species.ebony, Species.null): 10,
(Species.ebony, Species.ebony): -0.01,
(Species.ebony, Species.panda): -0.1,
(Species.panda, Species.ebony): 0.01,
(Species.panda, Species.panda): -0.1,
}
3 changes: 2 additions & 1 deletion galaxy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config
import planet


Expand All @@ -6,7 +7,7 @@ class Galaxy(object):
def __init__(self, number_of_planets=0):
self._planets = []
for _ in range(number_of_planets):
self._planets.append(planet.PlanetClassB())
self._planets.append(planet.Planet(config.PlanetClasses.B))

def sim(self):
for planet in self._planets:
Expand Down
4 changes: 4 additions & 0 deletions myenum.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def contains(mcs, value):
if not isinstance(value, MyEnumValue):
return False
return value[0] == mcs.__name__

def get_all_values(mcs):
"""Return list of all values"""
return [getattr(mcs, value) for value in mcs.values]


class MyEnum(object):
Expand Down
15 changes: 4 additions & 11 deletions planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

class Planet(object):

def __init__(self):
self._ecosystem = ecosystem.Ecosystem()
self._economy = economy.Economy()
def __init__(self, class_):
self._class = class_
self._ecosystem = ecosystem.Ecosystem(self._class)
self._economy = economy.Economy(self._class)

def sim(self):
self._ecosystem.sim()
self._economy.sim()


class PlanetClassB(Planet):

def __init__(self):
super(PlanetClassB, self).__init__()
self._ecosystem = ecosystem.EcosystemClassB()
self._economy = economy.EconomyClassB()
7 changes: 7 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Utility functions
"""

def multi_config(common_value, enum_):
"""Used in config to copy common values for all planet classes"""
return {key: common_value.copy() for key in enum_.get_all_values()}

0 comments on commit 0a401a5

Please sign in to comment.