forked from fogleman/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·165 lines (134 loc) · 5.24 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# Imports, sorted alphabetically.
# Python packages
from ConfigParser import NoSectionError, NoOptionError
import argparse
import os
import random
import time
import gettext
import sys
# Third-party packages
import pyglet
# Disable error checking for increased performance
pyglet.options['debug_gl'] = False
from pyglet.gl import *
from pyglet.window import key
# Modules from this project
from controllers import MainMenuController
import globals as G
from timer import Timer
from debug import log_info
from mod import load_modules
from savingsystem import save_world
class Window(pyglet.window.Window):
def __init__(self, **kwargs):
kwargs.update(
caption=G.APP_NAME,
)
super(Window, self).__init__(
G.WINDOW_WIDTH, G.WINDOW_HEIGHT, **kwargs)
self.exclusive = False
self.reticle = None
self.controller = None
controller = MainMenuController(self)
self.switch_controller(controller)
if G.FULLSCREEN:
self.set_fullscreen()
self.total_fps = 0.0
self.iterations = 0
pyglet.clock.schedule_interval(self.update, 1.0 / G.MAX_FPS)
if G.FOG_ENABLED:
self.enableFog()
def enableFog(self):
glEnable(GL_FOG)
glFogfv(GL_FOG_COLOR, (GLfloat * 4)(0.5, 0.69, 1.0, 1))
glHint(GL_FOG_HINT, GL_DONT_CARE)
glFogi(GL_FOG_MODE, GL_LINEAR)
glFogf(GL_FOG_START, 30.0)
glFogf(GL_FOG_END, 120)
def disableFog(self):
glDisable(GL_FOG)
def set_exclusive_mouse(self, exclusive):
super(Window, self).set_exclusive_mouse(exclusive)
self.exclusive = exclusive
def update(self, dt):
self.controller.update(dt)
self.total_fps += pyglet.clock.get_fps()
self.iterations += 1
def switch_controller(self, new_controller):
if self.controller:
self.controller.pop_handlers()
self.controller = new_controller
self.controller.push_handlers()
def on_key_press(self, symbol, modifiers):
if self.exclusive:
if symbol == G.ESCAPE_KEY and not self.fullscreen:
self.set_exclusive_mouse(False)
elif symbol == key.Q and self.fullscreen: # FIXME: Better fullscreen mode.
pyglet.app.exit() # for fullscreen
def on_draw(self):
if self.exclusive:
self.reticle.draw(GL_LINES)
if G.MOTION_BLUR:
glAccum(GL_MULT, 0.65)
glAccum(GL_ACCUM, 0.35)
glAccum(GL_RETURN, 1.0)
def on_resize(self, width, height):
if self.reticle:
self.reticle.delete()
x, y = width / 2, height / 2
n = 10
self.reticle = pyglet.graphics.vertex_list(
4,
('v2i', (x - n, y, x + n, y, x, y - n, x, y + n))
)
def on_close(self):
log_info('Average FPS: %f' % (self.total_fps / self.iterations))
super(Window, self).on_close()
def main(options):
G.GAME_MODE = options.game_mode
G.SAVE_FILENAME = options.save
G.DISABLE_SAVE = options.disable_save
for name, val in options._get_kwargs():
setattr(G.LAUNCH_OPTIONS, name, val)
if options.fast:
G.TIME_RATE /= 20
if G.LANGUAGE != 'default':
reload(sys)
sys.setdefaultencoding('utf8')
gettext.install(True, localedir=None, unicode=1)
gettext.find(G.APP_NAME.lower(), 'locale')
gettext.textdomain(G.APP_NAME.lower())
gettext.bind_textdomain_codeset(G.APP_NAME.lower(), 'utf8')
language = gettext.translation(G.APP_NAME.lower(), 'locale', languages=[G.LANGUAGE], fallback=True)
G._ = lambda s: language.ugettext(s)
load_modules()
# try:
# window_config = Config(sample_buffers=1, samples=4) #, depth_size=8) #, double_buffer=True) #TODO Break anti-aliasing/multisampling into an explicit menu option
# window = Window(resizable=True, config=window_config)
# except pyglet.window.NoSuchConfigException:
window = Window(resizable=True, vsync=False)
pyglet.app.run()
if G.CLIENT:
G.CLIENT.stop()
if G.SERVER:
print 'Saving...'
save_world(G.SERVER, "world")
print 'Shutting down internal server...'
G.main_timer.stop()
G.SERVER._stop.set()
G.SERVER.shutdown()
if __name__ == '__main__':
log_info('Starting pyCraft...')
parser = argparse.ArgumentParser(description='Play a Python made Minecraft clone.')
game_group = parser.add_argument_group('Game options')
game_group.add_argument("--fast", action="store_true", default=False, help="Makes time progress faster then normal.")
game_group.add_argument("--game-mode", choices=G.GAME_MODE_CHOICES, default=G.GAME_MODE)
save_group = parser.add_argument_group('Save 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=G.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.")
parser.add_argument("--seed", default=None)
options = parser.parse_args()
main(options)