# # This file is part of Gibraltar. # Copyright 2006, 2007, 2008 Gibraltar team (see AUTHORS file) # # Gibraltar is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Gibraltar is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Gibraltar; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # from pycel import * from gibutils.loader import * import re, sys class gibraltar: api_version = 2 def __init__(self, celEntity): print "Initializing game..." self.findAndMountMaps() self.entity = celEntity # key bindings self.input = celCommandInput(self.entity) self.input.LoadConfig("Bootstrap") # create a tooltip for onscreen messages self.createTooltip() # create the tooltip for messages self.loadMovieRecorder() # Load mountpoints if Config.Load("/vfs.cfg", None, True): print "Unable to load VFS file !" # go on and load the map self.map = "demo" self.startScreen(self.map) self.loadMap(self.map) self.create_player() print "End initializing game..." def create_player(self): loadEntity("player") def findAndMountMaps(self): """ Find all maps in data/maps and mount them in the VFS as /maps/$NAME """ Vfs.PushDir() Vfs.ChDirAuto("/tmp/celstart/data/game/maps") # Find all zipped maps and mount them for map_path in Vfs.FindFiles(Vfs.GetCwd()): try: map_name = re.search(r'(\w+)\.zip$', map_path).group(1) except: print "Error while loading map", map_path continue print "Mounting", map_path, "on", ("/maps/"+map_name) realpath = Vfs.GetRealPath(map_path).GetData() Vfs.Mount("/maps/" + map_name, realpath) Vfs.PopDir() def loadMovieRecorder(self): # load the movierecorder self.mr = CS_QUERY_REGISTRY(oreg, iMovieRecorder) if not self.mr: plugmgr = CS_QUERY_REGISTRY(oreg, iPluginManager) self.mr = CS_LOAD_PLUGIN (plugmgr, "crystalspace.utilities.movierecorder", iMovieRecorder) if self.mr: oreg.Register (self.mr, "iMovieRecorder") else: self.mr = None def createTooltip(self): self.logs = [] self.tooltip = celToolTip(self.entity) self.tooltip.SetBackgroundColor(-1, -1, -1) self.tooltip.SetTextColor(255, 255, 255) self.tooltip.Show(20, 20) self.log_event("celstart started") self.log_event("i: start record") self.log_event("o: stop record") self.log_event("p: pause record") self.log_event("m: change camera") self.log_event("c: switch character") self.log_event("y : attach object to right hand") self.log_event("u : detach object from right hand") self.log_event("f: fire") def startScreen(self, map_file): font = Graphics2D.GetFontServer().LoadFont("*large") if not Graphics3D.BeginDraw(CSDRAW_2DGRAPHICS): return Graphics2D.Clear(0) Graphics2D.Write(font, 100, 100, Graphics2D.FindRGB(255, 255, 255), Graphics2D.FindRGB(0, 0, 0), "Loading... " + str(map_file) ) Graphics2D.Print(csRect(0, 0, 640, 480)) Graphics3D.FinishDraw() def loadMap(self, map_name): """ Load a map generated by b2cs using celexport """ Vfs.PushDir() vfsdir = "/maps/" + map_name + "/" for mp in Vfs.GetRealMountPaths(vfsdir): print "Temporary adding", mp, "as module search path" sys.path.insert(0, mp) zoneManager = celZoneManager(self.entity) Vfs.ChDirAuto(vfsdir) zoneManager.Load(vfsdir, "level.xml") self.startregion = zoneManager.LastStartRegionName region = zoneManager.FindRegion(self.startregion) zoneManager.ActivateRegion(region) for mp in Vfs.GetRealMountPaths(vfsdir): print "Removing", mp, "from module search path" sys.path.remove(mp) Vfs.PopDir() # video record commands def pccommandinput_startrecord1(self, pc, args): self.log_event("start recording") if self.mr: self.mr.Start() def pccommandinput_stoprecord1(self, pc, args): self.log_event("stop recording") if self.mr: self.mr.Stop() def pccommandinput_pauserecord1(self, pc, args): if self.mr: if self.mr.IsPaused(): self.log_event("unpause recording") self.mr.UnPause() else: self.log_event("pause recording") self.mr.Pause() def pccommandinput_quit0(self, pc, args): self.shutdown(0) def log_event(self, event): self.logs.append(event) if len(self.logs) > 10: self.logs.pop(0) tooltip_text = "\n".join(self.logs) self.tooltip.Show(20, 20) self.tooltip.SetText(tooltip_text) def shutdown(self, exitcode=0): # TODO : add some thread and fork checks and shutdown calls # if applicable. sys.exit(exitcode)