#! /usr/bin/python -O
# -*- python -*-

# Slune
# Copyright (C) 2002 Jean-Baptiste LAMY
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os, os.path, sys
# If installed in /usr, /usr/local, ... add /usr/share (or ...) to the module path.

HERE = os.path.dirname(sys.argv[0])

if   HERE.endswith("games"): # /usr/{local/}games
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share", "games"))
elif HERE.endswith("bin"): # /usr/{local/}bin
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..", "share"))
else: # Raw source not installed
  APPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), ".."))

print "* Slune * Slune lives in %s" % APPDIR

if not "--dont-add-path" in sys.argv:
  sys.path.insert(0, APPDIR)

import slune.globdef as globdef, soya
soya.init(width = globdef.SCREEN_WIDTH, height = globdef.SCREEN_HEIGHT, fullscreen = globdef.FULLSCREEN, sound = 1, sound_doppler_factor = 0.005, sound_reference_distance = 3.0)
#soya.init(width = globdef.SCREEN_WIDTH, height = globdef.SCREEN_HEIGHT, fullscreen = globdef.FULLSCREEN)
soya.set_quality(globdef.QUALITY)
soya.set_sound_volume(globdef.SOUND_VOLUME)

LEVEL        = ""
PARRAIN_HOST = None
TK           = 0

i = 1
while i < len(sys.argv):
  arg = sys.argv[i]
  i = i + 1
  
  if   arg == "--verif":
    globdef.VERIFICATION_SERVER = sys.argv[i]
    print "* Slune * Using verification server %s..." % globdef.VERIFICATION_SERVER
    i = i + 1
    
  elif arg == "--fullscreen": globdef.FULLSCREEN = 1
    
  elif arg == "--windowed": globdef.FULLSCREEN = 0
    
  elif arg == "--no-sound":
    globdef.SOUND = 0
    globdef.MUSIC = 0
    
  elif arg == "--screensize":
    globdef.SCREEN_WIDTH  = int(sys.argv[i])
    globdef.SCREEN_HEIGHT = int(sys.argv[i + 1])
    i += 2
    
  elif arg == "--name":
    globdef.NAME = sys.argv[i]
    i += 1
    
  elif arg == "--port":
    globdef.PORT = int(sys.argv[i])
    i += 1
    
  elif arg == "--parrain-host":
    globdef.PARRAIN_HOST = PARRAIN_HOST = sys.argv[i]
    i += 1
    
  elif arg == "--parrain-port":
    globdef.PARRAIN_PORT = int(sys.argv[i])
    i += 1
    
  elif arg == "-2":
    globdef.PORT = 36179
    globdef.PARRAIN_HOST = PARRAIN_HOST = "127.0.0.1"
    globdef.PARRAIN_PORT = 36079
    
  elif arg == "--level":
    LEVEL = sys.argv[i]
    i += 1
    
  elif arg == "--no-fps-limit":
    import py2play.idler
    py2play.idler.MIN_FRAME_DURATION = 0.0
    
  elif arg == "--tk": TK = 1
  
  elif arg == "--version":
    print "Slune version", globdef.VERSION
    sys.exit()
    
  elif arg == "--help":
    print """Slune: A fun game
Synopsis: slune [options...]

where options are:
--help                     This help
--version                  Shows version number
--tk                       (Old) TK interface
--fullscreen               Fullscreen mode
--windowed                 Windowed mode (default)
--screensize WIDTH HEIGHT  Sets the screen size (in pixel, default is 640 480)
--no-sound                 Disable sound
--no-fps-limit             Disable FPS limit, for benchmarking (default is to limit FPS to 40)
--level LEVELNAME          Immediately starts the game in level LEVELNAME (e.g. "mission-1")
--name NAME                Sets the player's name (default is your username)
--port PORT                Sets the socket's TCP port number (default 36079)
--parrain-host HOST        Join an already existant multiplayer game that someone (the "parrain") is playing at HOST.
--parrain-port PORT        Sets the parrain's socket's TCP port number (default 36079)
-2                         Join an already existant multiplayer game on the *same* computer (e.g. for testing)
                           Equivalent to "--port 36179 --parrain-host 127.0.0.1 --parrain-port 36079"

If no --level, --parrain-host and -2 options are given, the GUI is launched.
Else the game immediately begins."""
    sys.exit()
    
  else:
    print "Unknown command line arg : '%s' !" % arg
    sys.exit(2)
    


import slune.level, slune.character, slune.player


try:
  import psyco
  psyco.full()
except:
  print "* Slune * (Psyco not found ; if you are using an x86 processor, installing psyco can speed up Slune a little)"

if   PARRAIN_HOST:
  slune.player.join_network_game()
  
elif LEVEL:
  slune.player.start_new_game("level-" + LEVEL)
  
else:
  if TK:
    import slune.gui, Tkinter
    slune.gui.MainScreen()
    Tkinter.mainloop()
  else:
    import slune.gui_gl
    slune.gui_gl.MainScreen()
    

