Version updates for 0.30c.
[singularity-git.git] / code / singularity.py
blob047561d8cd3659913f58cec49cc4696bf333392b
1 #! /usr/bin/env python
2 #file: singularity.py
3 #Copyright (C) 2005, 2006, 2007 Evil Mr Henry, Phil Bordelon, and Brian Reid
4 #This file is part of Endgame: Singularity.
6 #Endgame: Singularity is free software; you can redistribute it and/or modify
7 #it under the terms of the GNU General Public License as published by
8 #the Free Software Foundation; either version 2 of the License, or
9 #(at your option) any later version.
11 #Endgame: Singularity is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 #GNU General Public License for more details.
16 #You should have received a copy of the GNU General Public License
17 #along with Endgame: Singularity; if not, write to the Free Software
18 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #This file is the starting file for the game. Run it to start the game.
22 # Since we require numpy anyway, we might as well ask pygame to use it.
23 try:
24 import pygame
25 pygame.surfarray.use_arraytype("numpy")
26 except AttributeError:
27 pass # Pygame older than 1.8.
28 except ValueError:
29 raise SystemExit("Endgame: Singularity requires NumPy.")
30 except ImportError:
31 raise SystemExit("Endgame: Singularity requires pygame.")
33 import sys
34 import ConfigParser
35 import os.path
36 import optparse
38 import g, graphics.g
39 from screens import main_menu, map
41 pygame.init()
42 pygame.font.init()
43 pygame.key.set_repeat(1000, 50)
45 #load prefs from file:
46 save_dir = g.get_save_folder(True)
47 save_loc = os.path.join(save_dir, "prefs.dat")
48 if os.path.exists(save_loc):
50 prefs = ConfigParser.SafeConfigParser()
51 savefile = open(save_loc, "r")
52 try:
53 prefs.readfp(savefile)
54 except Exception, reason:
55 sys.stderr.write("Cannot load preferences file %s! (%s)\n" % (save_loc, reason))
56 sys.exit(1)
58 if prefs.has_section("Preferences"):
59 try:
60 if prefs.getboolean("Preferences", "fullscreen"):
61 graphics.g.fullscreen = pygame.FULLSCREEN
62 except:
63 sys.stderr.write("Invalid or missing 'fullscreen' setting in preferences.\n")
65 try:
66 g.nosound = prefs.getboolean("Preferences", "nosound")
67 except:
68 sys.stderr.write("Invalid or missing 'nosound' setting in preferences.\n")
70 try:
71 pygame.event.set_grab(prefs.getboolean("Preferences", "grab"))
72 except:
73 sys.stderr.write("Invalid or missing 'grab' setting in preferences.\n")
75 try:
76 g.daynight = prefs.getboolean("Preferences", "daynight")
77 except:
78 sys.stderr.write("Invalid or missing 'daynight' setting in preferences.\n")
80 try:
81 g.soundbuf = prefs.getint("Preferences", "soundbuf")
82 except:
83 sys.stderr.write("Invalid or missing 'soundbuf' setting in preferences.\n")
85 try:
86 graphics.g.screen_size = (prefs.getint("Preferences", "xres"),
87 graphics.g.screen_size[1])
88 except:
89 sys.stderr.write("Invalid or missing 'xres' resolution in preferences.\n")
91 try:
92 graphics.g.screen_size = (graphics.g.screen_size[0],
93 prefs.getint("Preferences", "yres"))
94 except:
95 sys.stderr.write("Invalid or missing 'yres' resolution in preferences.\n")
97 #If language is unset, default to English.
98 try: desired_language = prefs.get("Preferences", "lang")
99 except: desired_language = "en_US"
100 try:
101 if os.path.exists(g.data_loc + "strings_" + desired_language + ".dat"):
102 g.language = desired_language
103 g.set_locale()
104 except:
105 sys.stderr.write("Cannot find language files for language '%s'.\n" % desired_language)
107 #Handle the program arguments.
108 desc = """Endgame: Singularity is a simulation of a true AI. Go from computer to computer, pursued by the entire world. Keep hidden, and you might have a chance."""
109 parser = optparse.OptionParser(version=g.version, description=desc,
110 prog="singularity")
111 parser.add_option("--sound", action="store_true", dest="sound",
112 help="enable sound (default)")
113 parser.add_option("--nosound", action="store_false", dest="sound",
114 help="disable sound")
115 parser.add_option("--daynight", action="store_true", dest="daynight",
116 help="enable day/night display (default)")
117 parser.add_option("--nodaynight", action="store_false", dest="daynight",
118 help="disable day/night display")
119 langs = g.available_languages()
120 parser.add_option("-l", "--lang", "--language", dest="language", type="choice",
121 choices=langs, metavar="LANG",
122 help="set the language to LANG (available languages: " +
123 " ".join(langs) + ", default en_us)")
124 parser.add_option("-g", "--grab", help="grab the mouse pointer", dest="grab",
125 action="store_true")
126 parser.add_option("--nograb", help="don't grab the mouse pointer (default)",
127 dest="grab", action="store_false")
128 parser.add_option("-s", "--singledir", dest="singledir",
129 help="keep saved games and settings in the Singularity directory",
130 action="store_true")
131 parser.add_option("--multidir", dest="singledir",
132 help="keep saved games and settings in an OS-specific, per-user directory (default)",
133 action="store_false")
134 parser.add_option("--soundbuf", type="int",
135 help="set the size of the sound buffer (default 2048)")
137 display_options = optparse.OptionGroup(parser, "Display Options")
138 display_options.add_option("-r", "--res", "--resolution", dest="resolution",
139 help="set resolution to RES (default 800x600)",
140 metavar="RES")
141 for common_res in [(640,480), (800,600), (1024,768), (1280,1024)]:
142 x = str(common_res[0])
143 res_str = "%dx%d" % common_res
144 display_options.add_option("--" + x, action="store_const",
145 dest="resolution", const=res_str,
146 help="set resolution to %s" % res_str)
147 display_options.add_option("--fullscreen", action="store_true",
148 help="start in fullscreen mode")
149 display_options.add_option("--windowed", action="store_false",
150 help="start in windowed mode (default)")
151 parser.add_option_group(display_options)
153 olpc_options = optparse.OptionGroup(parser, "OLPC-specific Options")
154 olpc_options.add_option("--xo1", action="store_const",
155 dest="resolution", const="1200x900",
156 help="set resolution to 1200x900 (OLPC XO-1)")
157 olpc_options.add_option("--ebook", help="enables gamepad buttons for use in ebook mode. D-pad moves mouse, check is click. O speeds up time, X slows down time, and square stops time.",
158 action="store_true", default=False)
159 parser.add_option_group(olpc_options)
161 hidden_options = optparse.OptionGroup(parser, "Hidden Options")
162 hidden_options.add_option("-p", help="(ignored)", metavar=" ")
163 hidden_options.add_option("-d", "--debug", help="for finding bugs",
164 action="store_true", default=False)
165 hidden_options.add_option("--cheater", help="for bad little boys and girls",
166 action="store_true", default=False)
167 # Uncomment to make the hidden options visible.
168 #parser.add_option_group(hidden_options)
170 (options, args) = parser.parse_args()
172 if options.language is not None:
173 g.language = options.language
174 g.set_locale()
175 if options.resolution is not None:
176 try:
177 xres, yres = options.resolution.split("x")
178 graphics.g.screen_size = (int(xres), int(yres))
179 except Exception:
180 parser.error("Resolution must be of the form <h>x<v>, e.g. 800x600.")
181 if options.grab is not None:
182 pygame.event.set_grab(options.grab)
183 if options.fullscreen is not None:
184 graphics.g.fullscreen = options.fullscreen
185 if options.sound is not None:
186 g.nosound = not options.sound
187 if options.daynight is not None:
188 g.daynight = options.daynight
189 if options.soundbuf is not None:
190 g.soundbuf = options.soundbuf
191 if options.singledir is not None:
192 g.singledir = options.singledir
194 graphics.g.ebook_mode = options.ebook
196 g.cheater = options.cheater
197 g.debug = options.debug
199 g.load_strings()
200 g.load_events()
202 pygame.display.set_caption("Endgame: Singularity")
204 #I can't use the standard image dictionary, as that requires the screen to
205 #be created.
206 if pygame.image.get_extended() == 0:
207 print "Error: SDL_image required. Exiting."
208 sys.exit(1)
210 # Initialize the screen with a dummy size.
211 pygame.display.set_mode(graphics.g.screen_size)
213 #init data:
214 g.init_graphics_system()
215 g.reinit_mixer()
216 g.load_sounds()
217 g.load_items()
218 g.load_music()
219 g.load_locations()
221 # Set the application icon.
222 pygame.display.set_icon(graphics.g.images["icon.png"])
224 #Display the main menu
225 menu_screen = main_menu.MainMenu()
226 menu_screen.show()