Browse code
fixing broblems
Ed L authored on 24/07/2012 00:02:44
Showing 6 changed files
Showing 6 changed files
- src/construct_call_graph.py
- src/font_converter.py
- src/libtcod.dylib
- src/libtcod.dylib
- src/player.py
- src/test.png
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,124 +0,0 @@ |
1 |
-#!/usr/bin/env python |
|
2 |
- |
|
3 |
-''' |
|
4 |
-generates call graph of given python code file |
|
5 |
-in dot format input for graphviz. |
|
6 |
- |
|
7 |
-limitations: |
|
8 |
-* statically tried to figure out functions calls |
|
9 |
-* does not understand classes |
|
10 |
-* algorithm is naive and may not statically find |
|
11 |
- all cases |
|
12 |
-''' |
|
13 |
- |
|
14 |
-import sys |
|
15 |
-import parser |
|
16 |
-import symbol, token |
|
17 |
-import pprint |
|
18 |
-import optparse |
|
19 |
- |
|
20 |
-try: s = set() |
|
21 |
-except: import sets; set = sets.Set |
|
22 |
- |
|
23 |
-def annotate_ast_list(ast_list): |
|
24 |
- code = ast_list[0] |
|
25 |
- if code in symbol.sym_name: code = symbol.sym_name[code] |
|
26 |
- else: code = token.tok_name[code] |
|
27 |
- ast_list[0] = code |
|
28 |
- |
|
29 |
- for index, item in enumerate(ast_list): |
|
30 |
- if index == 0: continue |
|
31 |
- if isinstance(item, list): |
|
32 |
- ast_list[index] = annotate_ast_list(item) |
|
33 |
- return ast_list |
|
34 |
- |
|
35 |
-def get_atom_name(atom): |
|
36 |
- first_child = atom[1] |
|
37 |
- first_child_code = first_child[0] |
|
38 |
- if first_child_code != token.NAME: return None |
|
39 |
- return first_child[1] |
|
40 |
- |
|
41 |
-def get_fn_call_data(ast_list): |
|
42 |
- if len(ast_list) < 3: return None |
|
43 |
- first_child, second_child = ast_list[1:3] |
|
44 |
- first_child_code = first_child[0] |
|
45 |
- if first_child_code != symbol.atom: return None |
|
46 |
- fn_name = get_atom_name(first_child) |
|
47 |
- |
|
48 |
- second_child_code = second_child[0] |
|
49 |
- if second_child_code != symbol.trailer: return None |
|
50 |
- |
|
51 |
- if len(second_child) < 3: return None |
|
52 |
- if second_child[1][0] == token.LPAR and second_child[-1][0] == token.RPAR: |
|
53 |
- return fn_name |
|
54 |
- else: return None |
|
55 |
- |
|
56 |
-def find_fn_call(ast_list, calls): |
|
57 |
- code = ast_list[0] |
|
58 |
- if code == symbol.power: |
|
59 |
- fn_name = get_fn_call_data(ast_list) |
|
60 |
- if fn_name != None and getattr(__builtins__, fn_name, None) == None: calls.add(fn_name) |
|
61 |
- |
|
62 |
- for item in ast_list[1:]: |
|
63 |
- if isinstance(item, list): |
|
64 |
- find_fn_call(item, calls) |
|
65 |
- |
|
66 |
-def process_fn(fn_ast_list, call_graph): |
|
67 |
- dummy, dummy, func_name = fn_ast_list[:3] |
|
68 |
- dummy, func_name = func_name |
|
69 |
- |
|
70 |
- calls = set() |
|
71 |
- find_fn_call(fn_ast_list, calls) |
|
72 |
- |
|
73 |
- call_graph[func_name] = list(calls) |
|
74 |
- |
|
75 |
-def construct_call_graph(ast_list, call_graph): |
|
76 |
- code = ast_list[0] |
|
77 |
- if code == symbol.funcdef: |
|
78 |
- process_fn(ast_list, call_graph) |
|
79 |
- |
|
80 |
- for item in ast_list[1:]: |
|
81 |
- if isinstance(item, list): |
|
82 |
- construct_call_graph(item, call_graph) |
|
83 |
- |
|
84 |
- return call_graph |
|
85 |
- |
|
86 |
-def generate_dot_code(python_code): |
|
87 |
- ast = parser.suite(python_code) |
|
88 |
- ast_list = parser.ast2list(ast) |
|
89 |
- #annotated_ast_list = annotate_ast_list(ast_list) |
|
90 |
- #pprint.pprint(annotated_ast_list) |
|
91 |
- |
|
92 |
- call_graph = {} |
|
93 |
- construct_call_graph(ast_list, call_graph) |
|
94 |
- #pprint.pprint(call_graph) |
|
95 |
- |
|
96 |
- dot = [] |
|
97 |
- |
|
98 |
- dot.append("digraph G {") |
|
99 |
- dot.append("rankdir=LR") |
|
100 |
- for from_fn, to_fns in call_graph.iteritems(): |
|
101 |
- if not to_fns: |
|
102 |
- dot.append('%s;' % from_fn) |
|
103 |
- |
|
104 |
- for to_fn in to_fns: |
|
105 |
- if to_fn not in call_graph: continue |
|
106 |
- dot.append('%s -> %s;' % (from_fn, to_fn)) |
|
107 |
- dot.append("}") |
|
108 |
- |
|
109 |
- return '\n'.join(dot) |
|
110 |
- |
|
111 |
-if __name__ == '__main__': |
|
112 |
- oparser = optparse.OptionParser() |
|
113 |
- |
|
114 |
- oparser.add_option('-i', '--input-file', default=None, metavar='FILE', help='python code file to process') |
|
115 |
- |
|
116 |
- options, args = oparser.parse_args() |
|
117 |
- |
|
118 |
- if options.input_file: |
|
119 |
- python_code = open(options.input_file).read() |
|
120 |
- else: |
|
121 |
- python_code = sys.stdin.read() |
|
122 |
- |
|
123 |
- dot_code = generate_dot_code(python_code) |
|
124 |
- print dot_code |
125 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,24 +0,0 @@ |
1 |
-#!/usr/bin/python |
|
2 |
-#USAGE: |
|
3 |
-#python TCODfont.py /path/to/font/font.ttf 12 |
|
4 |
- |
|
5 |
-import Image, ImageFont, ImageDraw, sys |
|
6 |
- |
|
7 |
-fontpath = sys.argv[1] |
|
8 |
-fontpoint = int(sys.argv[2]) |
|
9 |
- |
|
10 |
-image = Image.new('RGB', [fontpoint*16, fontpoint*16]) |
|
11 |
-draw = ImageDraw.Draw(image) |
|
12 |
-font = ImageFont.truetype(fontpath, fontpoint) |
|
13 |
- |
|
14 |
-i = 0 |
|
15 |
-for row in range(0, 16): |
|
16 |
- for col in range(0, 16): |
|
17 |
- |
|
18 |
- xpos = fontpoint*col+1 |
|
19 |
- ypos = fontpoint*row |
|
20 |
- |
|
21 |
- draw.text((xpos, ypos), chr(i), font=font) |
|
22 |
- i += 1 |
|
23 |
- |
|
24 |
-image.save('tcod.png') |
5 | 3 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,111 @@ |
1 |
+import libtcodpy as libtcod |
|
2 |
+from objects import Object, Fighter |
|
3 |
+ |
|
4 |
+def get_pos_pair(x,y): |
|
5 |
+ if y is None: |
|
6 |
+ if hasattr(x, '__iter__'): |
|
7 |
+ x,y = x |
|
8 |
+ elif hasattr(x,'x'): |
|
9 |
+ x,y = x.x,x.y |
|
10 |
+ else: |
|
11 |
+ x,y = x.pos |
|
12 |
+ return x,y |
|
13 |
+ |
|
14 |
+class Player(Object): |
|
15 |
+ def triggers_recompute(func): |
|
16 |
+ def _inner(self, *a, **kw): |
|
17 |
+ self.fov_recompute = True |
|
18 |
+ return func(self, *a, **kw) |
|
19 |
+ return _inner |
|
20 |
+ |
|
21 |
+ @triggers_recompute |
|
22 |
+ def __init__(self, map, con, x,y, char, color, fighter=None, level=None): |
|
23 |
+ Object.__init__(self, None, con, x,y, char, |
|
24 |
+ libtcod.namegen_generate('Celtic male'), color, True, fighter=fighter, level=level |
|
25 |
+ ) |
|
26 |
+ |
|
27 |
+ map.player = self |
|
28 |
+ self.inventory = [] |
|
29 |
+ |
|
30 |
+ def draw(self, player=None): |
|
31 |
+ if player is None: |
|
32 |
+ player = self |
|
33 |
+ return Object.draw(self, player) |
|
34 |
+ |
|
35 |
+ def pick_up(self, obj): |
|
36 |
+ if len(self.inventory) >= 26: |
|
37 |
+ game.message('Your inventory is full, cannot pick up %s' % obj.name, libtcod.red) |
|
38 |
+ else: |
|
39 |
+ self.inventory.append( |
|
40 |
+ self.level.claim_object(obj) |
|
41 |
+ ) |
|
42 |
+ game.message('you picked up a %s!' % obj.name, libtcod.green) |
|
43 |
+ obj.item.user = self |
|
44 |
+ return self |
|
45 |
+ |
|
46 |
+ def drop(self, obj): |
|
47 |
+ self.level.objects.insert(0, obj) |
|
48 |
+ obj.x, obj.y = self.x, self.y |
|
49 |
+ game.message('you dropped a %s' % obj.name, libtcod.yellow) |
|
50 |
+ |
|
51 |
+ def use(self, item): |
|
52 |
+ item.owner.enter_level(self.level) |
|
53 |
+ success = item.use() |
|
54 |
+ |
|
55 |
+ try: |
|
56 |
+ index = self.inventory.index(item.owner) |
|
57 |
+ except ValueError: |
|
58 |
+ index = -1 |
|
59 |
+ |
|
60 |
+ if success and index != -1: |
|
61 |
+ self.inventory.pop(index) |
|
62 |
+ |
|
63 |
+ def get_item(self, index): |
|
64 |
+ return self.inventory[index].item |
|
65 |
+ |
|
66 |
+ def get_item_names(self): |
|
67 |
+ return [item.name for item in self.inventory] |
|
68 |
+ |
|
69 |
+ def tick(self): |
|
70 |
+ if self.fighter: |
|
71 |
+ self.fighter.tick() |
|
72 |
+ |
|
73 |
+ def can_see(self, x,y=None): |
|
74 |
+ x,y = get_pos_pair(x,y) |
|
75 |
+ return self.level.is_visible(x,y) |
|
76 |
+ |
|
77 |
+ torch_radius = 19 |
|
78 |
+ |
|
79 |
+ @property |
|
80 |
+ def pos(self): |
|
81 |
+ return self.x, self.y |
|
82 |
+ @pos.setter |
|
83 |
+ def pos(self, val): |
|
84 |
+ #print 'pos:', val |
|
85 |
+ self.x, self.y = val |
|
86 |
+ |
|
87 |
+ @triggers_recompute |
|
88 |
+ def move(self, dx, dy): |
|
89 |
+ return Object.move(self, dx,dy) |
|
90 |
+ |
|
91 |
+ def move_or_attack(self, dx, dy): |
|
92 |
+ x = self.x + dx |
|
93 |
+ y = self.y + dy |
|
94 |
+ |
|
95 |
+ import monsters |
|
96 |
+ target = monsters.monster_at(x,y) |
|
97 |
+ |
|
98 |
+ if target is not None: |
|
99 |
+ self.fighter.attack(target) |
|
100 |
+ else: |
|
101 |
+ self.move(dx, dy) |
|
102 |
+ |
|
103 |
+ def enter_level(self, level): |
|
104 |
+ print '\nenter level\n' |
|
105 |
+ self.level = level |
|
106 |
+ level.enter(self) |
|
107 |
+ self.x, self.y = self.level.map.map_entrance |
|
108 |
+ return self |
|
109 |
+ |
|
110 |
+ |
|
111 |
+import game |