Skip to content

Commit

Permalink
Moves --save-mode to the config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Apr 13, 2013
1 parent d3b249b commit 688c50e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
30 changes: 27 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,22 @@ def get_key(key_name):


def initialize_config():
#
# General
#

general = 'General'

globals.DEBUG = get_or_update_config(
general, 'debug', globals.DEBUG, conv=bool)

get_or_update_config(
general, 'save_mode', globals.SAVE_MODE, choices=globals.SAVE_MODES)

#
# Graphics
#

graphics = 'Graphics'

globals.WINDOW_WIDTH = get_or_update_config(
Expand All @@ -84,21 +95,35 @@ def initialize_config():
globals.MOTION_BLUR = get_or_update_config(
graphics, 'motion_blur', globals.MOTION_BLUR, conv=bool)

#
# World
#

world = 'World'

# TODO: This setting must be removed when terrain generation will improve.
get_or_update_config(world, 'size', 64, conv=int)

#
# Controls
#

controls = 'Controls'

# Adds missing keys to configuration file and converts to pyglet keys.
for control, default_key_name in globals.KEY_BINDINGS.items():
key_name = get_or_update_config('Controls', control, default_key_name)
key_name = get_or_update_config(controls, control, default_key_name)
try:
pyglet_key = get_key(key_name)
except InvalidKey:
pyglet_key = get_key(default_key_name)
globals.config.set('Controls', control, default_key_name)
globals.config.set(controls, control, default_key_name)
setattr(globals, control.upper() + '_KEY', pyglet_key)

#
# Save config file
#

with open(globals.config_file, 'wb') as handle:
globals.config.write(handle)

Expand Down Expand Up @@ -221,7 +246,6 @@ def main(options):
save_group.add_argument("--disable-auto-save", action="store_false", default=True, help="Do not save world on exit.")
save_group.add_argument("--save", default=globals.SAVE_FILENAME, help="Type a name for the world to be saved as.")
save_group.add_argument("--disable-save", action="store_false", default=True, help="Disables saving.")
save_group.add_argument("--save-mode", choices=globals.SAVE_MODES, default=globals.SAVE_MODE, help="Flatfile Struct (flatfile) is the smallest and fastest")

parser.add_argument("--seed", default=None)

Expand Down
8 changes: 4 additions & 4 deletions savingsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ def save_world(window, game_dir, world=None):
pickle.dump(save, open(os.path.join(game_dir, world, "save.pkl"), "wb"))

#blocks and sectors (window.model and window.model.sectors)
if globals.LAUNCH_OPTIONS.save_mode == globals.FLATFILE_SAVE_MODE:
if globals.SAVE_MODE == globals.FLATFILE_SAVE_MODE:
blocks = window.model
with open(os.path.join(game_dir, world, "blocks.dat"), "wb", 1024*1024) as f:
f.write(struct.pack("Q",len(blocks)))
for blockpos in blocks:
id = blocks[blockpos].id
f.write(structvec.pack(*blockpos) + structuchar2.pack(id.main, id.sub))
elif globals.LAUNCH_OPTIONS.save_mode == globals.PICKLE_COMPRESSED_SAVE_MODE:
elif globals.SAVE_MODE == globals.PICKLE_COMPRESSED_SAVE_MODE:
worldsave = (window.model.items(), window.model.sectors)
save_string = zlib.compress(pickle.dumps(worldsave), 9)
file = open(os.path.join(game_dir, world, "blocks.pkl"), "wb")
file.write(struct.pack("B", 1)) #Save Version
file.write(save_string)
file.close()
elif globals.LAUNCH_OPTIONS.save_mode == globals.PICKLE_SAVE_MODE:
elif globals.SAVE_MODE == globals.PICKLE_SAVE_MODE:
worldsave = (window.model.items(), window.model.sectors)
save_string = pickle.dumps(worldsave)
file = open(os.path.join(game_dir, world, "blocks.pkl"), "wb")
Expand Down Expand Up @@ -75,7 +75,7 @@ def open_world(gamecontroller, game_dir, world=None):
if isinstance(loaded_save[1], Player): gamecontroller.player = loaded_save[1]
if isinstance(loaded_save[2], float): gamecontroller.time_of_day = loaded_save[2]
#blocks and sectors (window.model and window.model.sectors)
if globals.LAUNCH_OPTIONS.save_mode == globals.FLATFILE_SAVE_MODE:
if globals.SAVE_MODE == globals.FLATFILE_SAVE_MODE:
sectors = gamecontroller.model.sectors
blocks = gamecontroller.model
SECTOR_SIZE = globals.SECTOR_SIZE
Expand Down

0 comments on commit 688c50e

Please sign in to comment.