Skip to content

Commit f71c0b0

Browse files
authored
Added a small tool to convert feature distribution maps into feature placement files for mapconv
1 parent 58a00be commit f71c0b0

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

tree_placer_springrts.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from PIL import Image
2+
import random
3+
import numpy as np
4+
import sys
5+
6+
input_imagefile_name= "tetrad_v3_feature_distribution.png"
7+
base_treename = "allpinesb_ad0_%s_%s_%s" #color type size
8+
outputline = '{ name = \'%s\', x = %d, z = %d, rot = "%d" ,scale = 1.000000 },'
9+
colors = ['green','brown','snow','snowgreen']
10+
types = ['a','b','c']
11+
sizes = ['m','l','xl','xxl']
12+
snowy_pine_trees = []
13+
green_pine_trees = []
14+
for size in sizes:
15+
for type in types:
16+
for color in colors:
17+
if 'snow' in color:
18+
snowy_pine_trees.append(base_treename%(color,type,size))
19+
else:
20+
green_pine_trees.append(base_treename%(color,type,size))
21+
rocks = ['agorm_rock1', 'agorm_rock2', 'agorm_rock3', 'agorm_rock4', 'agorm_rock5']
22+
palm_trees = ['ad0_senegal_1', 'ad0_senegal_2', 'ad0_senegal_3', 'ad0_senegal_4', 'ad0_senegal_5', 'ad0_senegal_6', 'ad0_senegal_7', 'ad0_senegal_1_large', 'ad0_senegal_2_large', 'ad0_senegal_3_large', 'ad0_senegal_4_large', 'ad0_senegal_5_large', 'ad0_senegal_6_large', 'ad0_senegal_7_large' ]
23+
shrooms = ['mushroom0%d'%i for i in range(1,10)]
24+
ferns = ['cycas%d'%i for i in range(1,9)]
25+
26+
# SPECIFY WHAT KIND OF FEATURES YOU WANT
27+
RED_FEATURES = shrooms
28+
GREEN_FEATURES = ferns
29+
BLUE_FEATURES = green_pine_trees
30+
31+
# SPECIFY THE PROBABILITY THAT A FEATURE WILL BE PLACED
32+
red_probability = 0.03 *5
33+
green_probability = 0.005 *5
34+
blue_probability = 0.03 *5
35+
36+
#SPECIFY WHETHER YOU WANT HIGHER INPUT PIXEL COLOR INTERPRETED AS A SIZE BIAS OR AS A PROBABILITY FOR PLACEMENT # TODO
37+
red_sizebias = False
38+
green_sizebias = False
39+
blue_sizebias = False
40+
41+
42+
43+
geos = [] # list of geo clusters, [centerx, centery, [pointsx],[pointsy] ]
44+
print ("Usage: specify a .bmp image to work on\n Red, Green and Blue channels specify different feature classes that will be randomly placed\n Alpha channel small values mark geovents.\nEdit the feature lists in this file to change what kind of features you want.")
45+
if len(sys.argv) >=2:
46+
input_imagefile_name = sys.argv[1]
47+
48+
def clusterGeos(x, y):
49+
newGeo = True
50+
for geo in geos:
51+
if (abs(geo[0]-x) + abs(geo[1]-y)) < 10:
52+
geo[2].append(x)
53+
geo[3].append(y)
54+
geo[0] = int(sum(geo[2])/len(geo[2]))
55+
geo[1] = int(sum(geo[3])/len(geo[3]))
56+
newGeo = False
57+
if newGeo:
58+
geos.append([x,y,[x],[y]])
59+
60+
61+
def pixelvalue_choice(p,treegroup,sizebias = False):
62+
if len(treegroup)> 0:
63+
if sizebias:
64+
return treegroup[int((p)*len(treegroup))]
65+
else:
66+
return treegroup[int((p)*len(treegroup))]
67+
else:
68+
return None
69+
70+
input_featureimage = Image.open(input_imagefile_name)
71+
imshape = input_featureimage.size
72+
input_pixels = input_featureimage.load()
73+
74+
output_featureimage = Image.new('RGB', imshape, color=(0, 0, 0))
75+
output_pixels = output_featureimage.load()
76+
print ("Image shape is:",imshape, input_featureimage)
77+
new_features = []
78+
for x in range(imshape[0]):
79+
for y in range(imshape[1]):
80+
pix = input_pixels[x,y]
81+
#print (pix)
82+
cx = 8*x+4
83+
cz = 8*y+4
84+
if pix[0] > 0: #snowy
85+
if random.random() < (red_probability * pix[0] / 255.0):
86+
feature = [pixelvalue_choice(random.random(), RED_FEATURES), cx, cz]
87+
new_features.append(feature)
88+
if pix[1]>0:
89+
if random.random() < (green_probability * pix[1] / 255.0):
90+
feature = [pixelvalue_choice(random.random(), GREEN_FEATURES), cx, cz]
91+
new_features.append(feature)
92+
if pix[2]>0: #snowy
93+
if random.random() < (blue_probability * pix[2] / 255.0):
94+
feature = [pixelvalue_choice(random.random(), BLUE_FEATURES), cx, cz]
95+
new_features.append(feature)
96+
if len(pix)>=4:
97+
if pix[3] < 10:
98+
clusterGeos(x,y)
99+
100+
for geo in geos:
101+
cx = 8 * geo[0] + 4
102+
cz = 8 * geo[1] + 4
103+
new_features.append(['geovent',cx,cz])
104+
print("Placing geovent at:", cx,cz)
105+
106+
outf = open('feature_placer_springrts_featureplacement.lua','w')
107+
typecounts = {}
108+
for featurename, cx,cz in new_features:
109+
if feature[0] is not None:
110+
output_pixel = [0,0,0]
111+
if featurename in RED_FEATURES:
112+
output_pixel[0] = 255-RED_FEATURES.index(featurename)
113+
if featurename in GREEN_FEATURES:
114+
output_pixel[1] = 255-GREEN_FEATURES.index(featurename)
115+
if featurename in BLUE_FEATURES:
116+
output_pixel[2] = 255-BLUE_FEATURES.index(featurename)
117+
output_pixels[int(cx/8),int(cz/8)] = tuple(output_pixel)
118+
# { name = 'ad0_senegal_2', x = 7884, z = 228, rot = "3852" ,scale = 1.000000 },
119+
outf.write(outputline % (featurename, cx,cz, random.randint(-32700, 32700)) + '\n')
120+
typecounts[featurename] = 1 if featurename not in typecounts else typecounts[featurename] + 1
121+
122+
#print ('\n'.join(['%s:%d'%(ftype,fcount) for ftype, fcount in typecounts.items()]))
123+
print ('\n'.join(['%s:%d'%(ftype,typecounts[ftype]) for ftype in sorted(typecounts.keys())]))
124+
outf.close()
125+
print ("total trees=",len(new_features))
126+
127+
128+
129+
output_featureimage.save(input_imagefile_name+"_featuremapout.bmp")

0 commit comments

Comments
 (0)