comparison gamelib/visualize.py @ 100:b27b4dede626

added visualization for research tree
author Rizmari Versfeld <rizziepit@gmail.com>
date Wed, 09 May 2012 22:10:22 +0200
parents
children e92035dce1f6
comparison
equal deleted inserted replaced
98:e386ec5d179b 100:b27b4dede626
1 import networkx
2
3 import matplotlib.pyplot as plt
4
5 import pylab
6
7 from gamelib import research
8 from gamelib import schematics
9 from gamelib.lab import Lab
10
11
12 def construct_research_tree(lab):
13 rt = networkx.DiGraph()
14 for research in lab.new_research:
15 rt.add_node(research, label=research.NAME, color='green')
16 for research in lab.new_research:
17 for prereq in research.PREREQUISITES:
18 rt.add_edge(prereq[0], research, weight=1.0/prereq[1], \
19 label=prereq[1], color='green')
20 for schematic in lab.new_schematics:
21 rt.add_node(schematic, label=schematic.NAME, color='red')
22 for schematic in lab.new_schematics:
23 for prereq in schematic.PREREQUISITES:
24 rt.add_edge(prereq[0], schematic, weight=1.0/prereq[1], \
25 label=prereq[1], color='red', size=schematic.COST/10)
26 return rt
27
28
29 def draw(rt):
30 agraph = networkx.to_agraph(rt)
31 agraph.graph_attr['label'] = 'Research Tree'
32 #agraph.layout(prog='dot')
33 agraph.write('research_tree.dot')
34
35
36 def main():
37 lab = Lab({'science':{}})
38 research_tree = construct_research_tree(lab)
39 draw(research_tree)