This is in python 3.4
I'm by no means an expert programmer. It's pretty rough so let me actually add comments.
I thought about designing an interface for it so it would be useable, as well as expanding what things it can randomize. I'm pretty busy though, maybe one day. I'm definitely going to use hammerwatch randomization as a case study in the near future.
import random
def shift(x, startrange, endrange):
#shifts a value parsed from a string in a random direction
shiftvalue = int(random.randrange(startrange, endrange))
inx = x
if shiftvalue % 2 == 1:
inx += (shiftvalue*0.01)
else:
inx -= (shiftvalue*0.01)
return inx
def stringcut(string):
#strips a line of a doodad to find the X, Y coordinate
first = string
second = first.split(">")
third = second[1].split("<", 2)
nums = third[0].split()
return nums
def fixline(string, startrange=None, endrange=None):
#takes the raw X, Y data from stringcut, randomizes it, and puts it back into a format readable by Hammerwatch
if startrange is None:
startrange = 0
if endrange is None:
endrange = 100
line = stringcut(string)
x = shift(float(line[0]), startrange, endrange)
y = shift(float(line[1]), startrange, endrange)
#fixline() returns a string which can be inserted into an XML document
return "\t" + "\t" + "\t" + "\t" + '<vec2 name="pos">' + str(x) + ' ' + str(y) + '</vec2>' + "\n"
#this filepath is the location and filename you would like to read
f = open('D:/PycharmProjects/Experience/TestTxt/treetest4.xml')
linelist = f.readlines()
f.close()
j = 0
while len(linelist) > j:
#change this string '/trees/'to whatever doodads you would like to randomize
#you can even randomize an entire folder of doodads e.g. "/trees/" will randomize all of the trees folder
if '/trees/' in linelist[j]:
#if you call fixline(linelist[j+1], n, m) it will randomize the doodads in a range from n/100 to m/100
#this is configured in a way which will take an object and shift it a maximum of 1.5 units in any direction
flip = fixline(linelist[j+1], 0, 150)
linelist[j+1] = flip
j += 1
#this filepath is the location and filename you would like to output to.
#I didn't want to modify the original file in case of an error
d = open('D:/PycharmProjects/Experience/TestTxt/output.xml', 'w')
i = 0
while len(linelist) > i:
d.write(str(linelist[i]))
i += 1
d.close()
If you have any questions just let me know.