Advertisement
Guest User

big_warps.py

a guest
Oct 29th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. """
  2. Copyleft 2013  ISD LackOfFaith; Do whatever with this
  3. """
  4.  
  5. import csv, sqlite3, sys
  6.  
  7. conn = sqlite3.connect(sys.argv[1])
  8. db = conn.cursor()
  9.  
  10. def distance(point1, point2):
  11.     return (
  12.         (point1[0]-point2[0])**2 +
  13.         (point1[1]-point2[1])**2 +
  14.         (point1[2]-point2[2])**2
  15.         )**0.5
  16.        
  17. def m_to_au(m):
  18.     return m/149597870700.
  19.  
  20. def gate_name(g_id):
  21.     Q_GATE_NAME = """
  22.        select mapsolarsystems.solarsystemname
  23.        from mapsolarsystems
  24.        inner join invitems, mapjumps on
  25.            invitems.locationid=mapsolarsystems.solarsystemid and
  26.            mapjumps.celestialid=invitems.itemid
  27.        where mapjumps.stargateid=?"""
  28.     results = db.execute(Q_GATE_NAME, [g_id])
  29.     try:
  30.         return results.next()[0]
  31.     except StopIteration:
  32.         return None
  33.  
  34. def system_name(s_id):
  35.     Q_SYSTEM_NAME = """
  36.        select mapsolarsystems.solarsystemname
  37.        from mapsolarsystems
  38.        where mapsolarsystems.solarsystemid=?"""
  39.     results = db.execute(Q_SYSTEM_NAME, [s_id])
  40.     if results:
  41.         return results.next()[0]
  42.     else:
  43.         return None
  44.  
  45.  
  46. Q_STARGATE = """
  47.        select invitems.itemid, invitems.locationid
  48.        from invitems
  49.        inner join invtypes, invgroups on
  50.              invitems.typeid=invtypes.typeid and
  51.              invtypes.groupid=invgroups.groupid
  52.        where invgroups.groupname="Stargate" """;
  53.  
  54.  
  55. system_gates = {}
  56.  
  57. for gate_id, system_id in db.execute(Q_STARGATE):
  58.     if system_id not in system_gates:
  59.         system_gates[system_id] = []
  60.     system_gates[system_id].append(gate_id)
  61.  
  62.  
  63. Q_GATE_LOCATION = """
  64.        select mapdenormalize.x, mapdenormalize.y, mapdenormalize.z
  65.        from mapdenormalize
  66.        inner join invitems on
  67.            invitems.itemid=mapdenormalize.itemid
  68.        where invitems.itemid=?
  69.        """
  70.  
  71. tmpgates = system_gates[system_gates.keys()[200]]
  72.  
  73. global_max = 0
  74. global_gate1 = None
  75. global_gate2 = None
  76. global_system = None
  77.  
  78. max_warps = []
  79.  
  80. for system_id in system_gates:
  81.     gate_coords = {}
  82.    
  83.     for gate_id in system_gates[system_id]:
  84.         gate_coords[gate_id] = db.execute(Q_GATE_LOCATION, [gate_id]).next()
  85.    
  86.     max_dist = 0
  87.     max_gate1 = None
  88.     max_gate2 = None
  89.    
  90.     print
  91.     print system_name(system_id)
  92.     print "-----------------------"
  93.     for gate1 in gate_coords:
  94.         for gate2 in gate_coords:
  95.             dist = distance(gate_coords[gate1], gate_coords[gate2])
  96.            
  97.             print "   ", m_to_au(dist), gate_coords[gate1], gate_coords[gate2]
  98.             if dist > max_dist:
  99.                 max_dist = dist
  100.                 max_gate1 = gate1
  101.                 max_gate2 = gate2
  102.  
  103.     if max_dist:
  104.         print "MAX", system_id, m_to_au(max_dist), gate_name(max_gate1), gate_name(max_gate2)
  105.  
  106.     if max_dist > global_max:
  107.         global_max = max_dist
  108.         global_gate1 = max_gate1
  109.         global_gate2 = max_gate2
  110.         global_system = system_id
  111.        
  112.     max_warps.append((max_dist, system_id, max_gate1, max_gate2))
  113.  
  114. print "Maximum gate-gate warp was..."
  115. print "\tin system %s" % system_name(global_system)
  116. print "\ta distance of %.2f AU" % m_to_au(global_max)
  117. print "\tfrom Stargate (%s) to Stargate (%s)" % (gate_name(global_gate1), gate_name(global_gate2))
  118.  
  119. max_warps.sort(cmp=lambda x,y: cmp(x[0],y[0]))
  120. max_warps.reverse()
  121.  
  122. with open("maxdists.csv", "w") as of:
  123.     writer = csv.writer(of)
  124.     writer.writerow(["System","Gate 1","Gate2","Distance (AU)"])
  125.     for max_dist, system_id, max_gate1, max_gate2 in max_warps:
  126.         if not max_dist:
  127.             continue
  128.         writer.writerow([
  129.                 system_name(system_id),
  130.                 gate_name(max_gate1),
  131.                 gate_name(max_gate2),
  132.                 "%.2f" % m_to_au(max_dist)
  133.                 ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement