~ubuntu-branches/ubuntu/oneiric/singularity/oneiric

1.1.5 by Kari Pahula
Import upstream version 0.30
1
#!/usr/bin/env python
2
3
#file: make-tree.py
4
#Copyright (C) 2008 aes and FunnyMan3595
5
#This file is part of Endgame: Singularity.
6
7
#Endgame: Singularity is free software; you can redistribute it and/or modify
8
#it under the terms of the GNU General Public License as published by
9
#the Free Software Foundation; either version 2 of the License, or
10
#(at your option) any later version.
11
12
#Endgame: Singularity is distributed in the hope that it will be useful,
13
#but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#GNU General Public License for more details.
16
17
#You should have received a copy of the GNU General Public License
18
#along with Endgame: Singularity; if not, write to the Free Software
19
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21
#This file is used to generate a visual representation of the tech tree using
22
#graphviz.
23
24
from os import system
25
from os.path import realpath
26
import sys
27
28
try:
29
    sys.path.insert(0, ".")
30
    from code import g
31
except ImportError:
32
    print "Run this from the main Singularity directory, as utils/make-tree.py"
33
    raise SystemExit
34
35
so_far = ""
36
37
def abbr(s):
38
    l = (("Advanced ", "Adv "),
39
         ("Project: ","P:"),
40
         ("Manipulation","Mnp"),
41
         ("Autonomous","Aut"),
42
         ("Computing","Cpu"),
43
         ("Quantum","Qu"),
44
         ("Personal Identification","P-Id"))
45
    for f,t in l: s = s.replace(f, t)
46
    return s
47
48
def cost(c):
49
    c = [ k/f for f,k in zip([1000, 86400, 24*60], c)]
50
    s = ', '.join(['%s %s' % (g.to_money(k), label) for label,k in zip(["money", "CPU", "days"], c) if k])
51
    return s and '\\n'+s or ''
52
53
j = dict([ (v[1],',fillcolor="#ffcccc"') for k,v in g.jobs.items() ])
54
55
f = file("techs.dot", 'w')
56
s = ("""\
57
digraph g {
58
ranksep=0.15;
59
nodesep=0.10;
60
ratio=.75;
61
edge [arrowsize=0.75];
62
node [shape=record,fontname=FreeSans,fontsize=7,height=0.01,width=0.01
63
      style=filled,fillcolor=white];
64
""")
65
66
f.write(s)
67
so_far += s
68
69
for l in sum([ [ '"%s"->"%s";' % (p,k)
70
                 for p in v.prerequisites ]
71
              for k,v in g.techs.items() if k != "unknown_tech"],
72
             []):
73
    f.write(l+'\n')
74
    so_far += l+'\n'
75
76
f.write('\n')
77
so_far += '\n'
78
79
for n,t in g.techs.items():
80
    if n == "unknown_tech": continue
81
    s  = '"%s" [label="%s' % (n, abbr(n)) + cost(t.cost_left)
82
    s += '"'+ j.get(n,'') + '];\n'
83
    f.write(s)
84
    so_far += s
85
86
f.write("\n}\n")
87
so_far += '\n'
88
f.close()
89
90
try:    system("dot -Tpng -o techs.png techs.dot")
91
except: pass
92
93
f = file('items.dot','w')
94
f.write(so_far)
95
s = 'node [fillcolor="#ccccff"];\n'
96
f.write(s)
97
so_far += s
98
99
g.load_items()
100
for name,item in g.items.items():
101
    if not item.prerequisites: continue
102
    for pre in item.prerequisites:
103
        p = g.techs[pre]
104
        s = '"%s" -> "%s-item"' % (pre, name)
105
        f.write(s)
106
        so_far += s
107
108
    s  = '"%s-item" [label="%s\\n' % (name, name) + cost(item.cost) + '"];\n'
109
    f.write(s)
110
    so_far += s
111
112
s = 'node [fillcolor="#99ffff"];\n'
113
f.write(s)
114
so_far += s
115
116
g.load_bases()
117
for name,base in g.base_type.items():
118
    if not base.prerequisites: continue
119
    for pre in base.prerequisites:
120
        p = g.techs[pre]
121
        s = '"%s" -> "%s-base"' % (pre, name)
122
        f.write(s)
123
        so_far += s
124
125
    s  = '"%s-base" [label="%s\\n' % (name, name) + cost(base.cost) + '"];\n'
126
    f.write(s)
127
    so_far += s
128
129
s = 'node [fillcolor="#aaffaa"];\n'
130
f.write(s)
131
so_far += s
132
133
blue = False
134
def set_or(state):
135
    global blue
136
    if blue != state:
137
        blue = state
138
        if blue:
139
            f.write('edge [arrowhead=empty,color="#0000FF"];\n')
140
        else:
141
            f.write('edge [arrowhead=normal,color="#000000"];\n')
142
143
g.load_locations()
144
for name,loc in g.locations.items():
145
    if not loc.prerequisites: continue
146
    if "unknown_tech" in loc.prerequisites:
147
        continue
148
    set_or(False)
149
    for pre in loc.prerequisites:
150
        if pre == "OR":
151
            set_or(True)
152
            continue
153
        p = g.techs[pre]
154
        s = '"%s" -> "%s-loc"' % (pre, name)
155
        f.write(s)
156
        so_far += s
157
158
    s  = '"%s-loc" [label="%s"];\n' % (name, name)
159
    f.write(s)
160
    so_far += s
161
162
f.write("\n}\n")
163
so_far += '\n'
164
f.close()
165
166
try:    system("unflatten -l10 items.dot | dot -Tpng -o items.png")
167
except: pass