-
Notifications
You must be signed in to change notification settings - Fork 0
/
juice.py
executable file
·148 lines (116 loc) · 3.99 KB
/
juice.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
#!/usr/bin/env python3
import argparse
import array
import logging
import pickle
import pprint
import random
import sys
import time
from logging import debug, info, warning, error
import numpy as np
import pyglet
import pyglet.gl as gl
from juice.config import config
from juice.gameview import GameView
from juice.terrain import Terrain
from juice.terrainlayer import \
TerrainLayer, RiverLayer, DeltaLayer, SeaLayer, BiomeLayer, CityLayer, RoadLayer
from juice.window import Window
GAME_WIDTH = 1184
GAME_HEIGHT = 736
DEBUG_EVENTS = False
DEBUG_GL = True
_g = {}
def timed_print(*args):
msg = ""
delta = 0
t = time.process_time()
if ("timer_last_t" not in _g):
delta = t
else:
delta = t - _g["timer_last_t"]
for i in args:
msg += str(i) + " "
_g["timer_last_t"] = t
print("{:.4f} {:.4f} {}".format(t, delta, msg))
def parse_command_line():
parser = argparse.ArgumentParser(description = "Juice: the power grid game")
parser.add_argument(
"-r", "--random-seed", type=int, help="Specify random seed")
parser.add_argument(
"-d", "--dimension", type=int, default=64,
help="Game field side length"
)
parser.add_argument(
"-t", "--timing", action="store_true",
help="Timing / profiling mode, exit after terrain generation"
)
parser.add_argument(
"-L", "--log-level", type=str, default="INFO",
help="Log level, one of CRITICAL, ERROR, WARNING, INFO, DEBUG."
)
parser.add_argument(
"-m", "--map", action="store_true",
help="Display overview map instead of entering the game"
)
parser.add_argument(
"-s", "--save", type=str, help="Save a map to file")
parser.add_argument(
"-l", "--load", type=str, help="Load a saved map")
return parser.parse_args()
def setup_logging(loglevel_str):
handler = logging.StreamHandler()
loglevel = getattr(logging, loglevel_str.upper())
logformat = "%(asctime)s %(levelname)7s: %(message)s"
logging.basicConfig(level=loglevel, format=logformat, handlers=[handler])
def setup_misc():
""" Miscellaneous global setup tasks. """
if (not DEBUG_GL):
pyglet.options['debug_gl'] = False
np.set_printoptions(threshold=float("nan"), linewidth=140)
pyglet.gl.glEnable(gl.GL_BLEND)
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
def save_state(obj, fn):
f = open(fn, "wb")
pickle.dump(obj, f)
def load_state(fn):
f = open(fn, "rb")
return pickle.load(f)
def generate(dim, randseed=None):
""" Generate a Terrain and return it. """
terr = Terrain(dim, randseed=randseed)
terr.add_layer(SeaLayer(terr, randseed=randseed))
terr.add_layer(RiverLayer(terr, randseed=randseed))
terr.add_layer(DeltaLayer(terr, randseed=randseed))
terr.add_layer(BiomeLayer(terr, randseed=randseed))
terr.add_layer(CityLayer(terr, randseed=randseed))
terr.add_layer(RoadLayer(terr, randseed=randseed))
terr.generate(post_generate_cb=timed_print)
return terr
def main():
args = parse_command_line()
randseed = args.random_seed \
if args.random_seed \
else random.randint(1, 10000)
window = Window(GAME_WIDTH, GAME_HEIGHT, caption="Juice")
setup_misc()
setup_logging(args.log_level)
info("random seed: %d", randseed)
if (not args.load):
terr = generate(args.dimension, randseed)
if (args.save):
save_state(terr, args.save)
else:
info("Loading map from `{}`".format(args.load))
terr = load_state(args.load)
if (args.timing):
sys.exit(0)
if (DEBUG_EVENTS):
window.push_handlers(pyglet.window.event.WindowEventLogger())
if (args.map):
window.image = terr.get_map_imgdata(scaling=min(GAME_WIDTH, GAME_HEIGHT) // terr.dim)
else:
window.gameview = GameView(terr)
pyglet.app.run()
main()