view atcgame/maps.py @ 57:35999e551d7a server tip

Call state's update method 2 times per second
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sun, 03 Jun 2007 01:25:28 -0400
parents a793c2b15c71
children
line wrap: on
line source

#/*
# * ATC - Air Traffic Controller simulation game
# *
# * Copyright (C) 2004-2007 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License version 2 as
# * published by the Free Software Foundation.
# *
# * 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.
# */

import os
import sys

import config
import beacon

def get_map_info(file, name):
	fd = open(file, 'r')
	s = fd.read()
	fd.close()
	
	s = s.split("\n")
	
	return Map(	name,        # map name
			float(s[0]), # centerx
			float(s[1]), # centery
			float(s[2]), # mperpix
			float(s[3]), # atcx
			float(s[4])) # atcy

def get_navaids(file, m):
	fd = open(file, 'r')
	s = fd.read()
	fd.close()
	
	s = s.split("\n")
	
	for line in s:
		if (line.__len__() == 0):
			continue
		
		line = line.split(" ")
		
		m.ninfo.append({
			"xoff": float(line[0]),
			"yoff": float(line[1]),
			"freq": float(line[2]),
			"name": line[3],
			"type": line[4],
			"output": float(line[5])})

class Map:
	def __init__(self, name, centerx, centery, mperpix, atcx, atcy):
		self.name	= name
		self.centerx	= centerx
		self.centery	= centery
		self.mperpix	= mperpix
		self.atcx	= atcx
		self.atcy	= atcy
		
		self.ninfo	= []
		self.navs	= []
		
		self.image	= ""

	def bootstrap_navs(self):
		for nav in self.ninfo:
			if nav["type"] == "VOR":
				self.navs.append(beacon.VOR_Beacon(nav["name"], nav["freq"], (nav["xoff"], nav["yoff"])))
			elif nav["type"] == "ILS":
				pass # FIXME: ILS beacons are little complicated
	
	def cleanup(self):
		while self.navs.__len__():
			self.navs.pop()
	
def getmap(name):
	maps_dir = os.listdir(config.maps_dir())

	MAP = None

	for map_dir in maps_dir:
		if map_dir != name:
			continue

		files = os.listdir(config.maps_dir() + "/" + map_dir)
		
		if (files.count("map.info") == 0):
			print "Missing " + config.maps_dir() + "/" + map_dir + "/map.info"
			sys.exit(1)
		
		if (files.count("map.png") == 0):
			print "Missing " + config.maps_dir() + "/" + map_dir + "/map.png"
			sys.exit(1)
		
		if (files.count("navaid.info") == 0):
			print "Missing " + config.maps_dir() + "/" + map_dir + "/navaid.info"
			sys.exit(1)
		
		map_info = get_map_info(config.maps_dir() + "/" + map_dir + "/map.info", map_dir)
		get_navaids(config.maps_dir() + "/" + map_dir + "/navaid.info", map_info)
		
		map_info.image = "maps/" + map_dir + "/map.png"
		print "Parsed and added '" + map_dir + "' map (" + str(map_info.ninfo.__len__()) + " navaids)"

		MAP = map_info

	if not MAP: raise ValueError

	return MAP