Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / scripts / levelfancy
1 #!/usr/bin/python
2 """<title>a magic tool that prettifies your levels for you</title>
3
4 <pre>
5 usage: levelpretty map.tga in.tga out.tga
6
7 a basic example:
8
9 map.tga contains "base" tiles drawn in the background and pretty tiles drawn in
10 the foreground.  it should contain all the knowldge needed to infer how to 
11 pretty up your level.
12
13 in.tga contains a level drawn using all "base" tiles (except where the algorithm
14 doesn't work -- you can put in fancy tiles to fill in those spaces, the algorithm
15 will back-wards infer what "base" tile the fancy tile is a substitute for)
16
17 out.tga will be generated using the map.tga and the in.tga
18
19 (to edit in leveledit and switch between background and foreground press 't')
20
21 map.tga bg  map.tga fg
22 ..........  ..........
23 .xxx......  ..789..... 
24 .x.x......  ..4.6.....
25 .xxx......  ..123..... 
26 ..........  .......... 
27
28 in.tga      out.tga
29 ..........  ..........
30 .xxx......  .789......
31 .x.x.xxx..  .4.6.789..
32 .xxx.x.x..  .123.4.6..
33 .....xxx..  .....123..
34
35 levelpretty takes a level drawn with "base" tiles and auto-magically (using the knowledge
36 supplied in map.tga) turns a basic level into a fancy level.  this tool is useful for
37 rapid development of fancy looking levels that only have a few kinds of base tiles
38 but those base tiles look better when rendered using lots of fancier tiles.
39 </pre>
40 """
41
42 import os,sys
43 from optparse import OptionParser
44
45 try:
46     import psyco
47     psyco.full()
48     print 'psyco installed'
49 except:
50     print 'psyco not installed'
51
52 from pgu.tilevid import Tilevid
53
54 scoring = [
55     [    1,   10,  100,   10,    1],
56     [   10, 1000,10000, 1000,   10],
57     [  100,10000,    0,10000,  100],
58     [   10, 1000,10000, 1000,   10],
59     [    1,   10,  100,   10,    1],
60     ]
61     
62        
63 def get(l,tx,ty):
64     width,height = len(l[0]),len(l)
65     if ty >= 0 and ty < height and tx >= 0 and tx < width: return l[ty][tx]
66     return None
67
68 def get2(l,tx,ty):
69     width,height = len(l[0]),len(l)
70     if ty >= 0 and ty < height and tx >= 0 and tx < width: return l[ty][tx]
71     return 0
72
73 def get3(l,tx,ty):
74     global used, rmap
75     width,height = len(l[0]),len(l)
76     if ty >= 0 and ty < height and tx >= 0 and tx < width: 
77          v = l[ty][tx]
78          if v not in used: 
79              if v in rmap: return rmap[v]
80              else: return 0
81          return v
82     return None
83     
84 def diff(a,b):
85     r = 0
86     for y in xrange(0,5):
87         for x in xrange(0,5):
88             va,vb = a[y][x],b[y][x]
89             if va == vb: r += scoring[y][x]
90             else: r -= scoring[y][x]
91     return r        
92     
93 usage = "usage: %prog map.tga in.tga out.tga"
94 parser = OptionParser(usage)
95 (opts, args) = parser.parse_args()
96 if len(args) != 3:
97     parser.error("incorrect number of arguments")
98     
99 m_fname,i_fname, o_fname = args
100
101 m_level = Tilevid()
102 m_level.tga_load_level(m_fname,1)
103 at = m_level.blayer
104 bt = m_level.tlayer
105
106 i_level = Tilevid()
107 i_level.tga_load_level(i_fname,1)
108 it = i_level.tlayer
109
110 o_level = Tilevid()
111 o_level.tga_load_level(i_fname,1)
112 ot = o_level.tlayer
113
114 width,height = m_level.size
115 lookup = {}
116 used = []
117 rmap = {}
118 for y in xrange(0,height):
119     for x in xrange(0,width):
120         v = get(at,x,y)
121         if v not in used: used.append(v)
122         
123         k = get(bt,x,y)
124         rmap[k] = v
125             
126         #NOTE: optimization
127         k = (get2(bt,x,y),get2(bt,x,y-1),get2(bt,x+1,y),get2(bt,x,y+1),get2(bt,x-1,y))
128         if k == (0,0,0,0,0): continue 
129         
130         #NOTE: optimization
131         k = (get2(at,x,y),get2(at,x,y-1),get2(at,x+1,y),get2(at,x,y+1),get2(at,x-1,y))
132         if k == (0,0,0,0,0): continue
133     
134         k = (get(at,x,y),get(at,x,y-1),get(at,x+1,y),get(at,x,y+1),get(at,x-1,y))
135         
136         if k not in lookup: lookup[k] = []
137         lookup[k].append((x,y))
138         
139
140 width,height = i_level.size
141 for y in xrange(0,height):
142     print y
143     for x in xrange(0,width):
144         v = get(it,x,y)
145         if v in used:
146             idata = [[get3(it,tx,ty) for tx in xrange(x-2,x+3)] for ty in xrange(y-2,y+3)]
147             k = (get3(it,x,y),get3(it,x,y-1),get3(it,x+1,y),get3(it,x,y+1),get3(it,x-1,y))
148             if k in lookup:
149                 v,score = 0,-100000
150                 for xx,yy in lookup[k]:
151                     adata = [[get(at,tx,ty) for tx in xrange(xx-2,xx+3)] for ty in xrange(yy-2,yy+3)]
152                     _v = get(bt,xx,yy)
153                     _score = diff(idata,adata)
154                     if _score > score: v,score = _v,_score
155         ot[y][x] = v
156         
157 o_level.tga_save_level(o_fname) #save the o_fname
158

UCC git Repository :: git.ucc.asn.au