[ create a new paste ] login | about

Link: http://codepad.org/tnV1c7xZ    [ raw code | output | fork ]

Python, pasted on Nov 12:
import math
import random

planets = []

minPlanets = 15
maxPlanets = 30
minShips = 1
maxShips = 100
minGrowth = 1
maxGrowth = 5
minDistance = 2
maxRadius = 12
epsilon = 0.002

def make_planet(x, y, owner, num_ships, growth_rate):
    return {"x" : x, "y" : y, "owner" : owner, "num_ships" : num_ships, "growth_rate" : growth_rate}

def print_planet(p):
    print "P " + str(p["x"]) + " " + str(p["y"]) + " " + str(p["owner"]) + " " + str(p["num_ships"]) + " " + str(p["growth_rate"])

def translate_planets(planets):
    for p in planets:
        p["x"] += maxRadius+2
        p["y"] += maxRadius+2

def generate_coordinates(p, r, theta):
    if theta < 0:
        theta += 360
    if theta >= 360:
        theta -= 360
    p["x"] = r * math.cos( math.radians(theta) )
    p["y"] = r * math.sin( math.radians(theta) )


def rand_num(min, max):
    return (random.random() * (max-min) ) + min

def distance(p1, p2):
    dx = p1["x"] - p2["x"]
    dy = p1["y"] - p2["y"]
    return math.ceil(math.sqrt(dx * dx + dy * dy))

def actual_distance(p1, p2):
    dx = p1["x"] - p2["x"]
    dy = p1["y"] - p2["y"]
    return math.sqrt(dx * dx + dy * dy)

def not_valid(p1, p2):
    adist = actual_distance(p1, p2)
    if distance(p1, p2) < minDistance or abs(adist - round(adist)) < epsilon:
        return True
    for p in planets:
        adist1 = actual_distance(p, p1)
        adist2 = actual_distance(p, p2)
        if distance(p, p1) < minDistance or distance(p, p2) < minDistance or abs(adist1-round(adist1)) < epsilon or abs(adist2-round(adist2)) < epsilon:
            return True
    return False

def not_valids(p1):
    for p in planets:
        adist = actual_distance(p, p1)
        if distance(p, p1) < minDistance or abs(adist-round(adist)) < epsilon:
            return True
    return False

#works out which type of symmetry to use
if random.randint(0, 1) == 1:
    symmetryType = 1
else:
    symmetryType = -1;

noPlanets = random.randint(minPlanets, maxPlanets)


#picks out the home planets
r = rand_num(minDistance, maxRadius)

theta1 = rand_num(0, 360)
if symmetryType == 1 and theta1 < 180:
    theta2 = theta1+180
elif symmetryType == 1:
    theta2 = theta1-180
else:
    theta2 = rand_num(0, 360)

p1 = make_planet(0, 0, 1, 100, 5)
p2 = make_planet(0, 0, 2, 100, 5)
generate_coordinates(p1, r, theta1)
generate_coordinates(p2, r, theta2)

while not_valid(p1, p2):
    r = rand_num(minDistance, maxRadius)

    theta1 = rand_num(0, 360)
    if symmetryType == 1 and theta1 < 180:
        theta2 = theta1+180
    elif symmetryType == 1:
        theta2 = theta1-180
    else:
        theta2 = rand_num(0, 360)

    generate_coordinates(p1, r, theta1)
    generate_coordinates(p2, r, theta2)
planets.append(p1)
planets.append(p2)



#makes the centre neutral planets
if symmetryType == 1:
    noCenterNeutrals = 1
    planets.append(make_planet(0, 0, 0, random.randint(minShips, maxShips), random.randint(0, maxGrowth)))
else:
    noCenterNeutrals = random.randint(0, noPlanets/4)
    theta = (theta1+theta2)/2
    if random.randint(0, 1) == 1:
        theta += 180
    for i in range(noCenterNeutrals):
        r = rand_num(0, maxRadius)
        num_ships = random.randint(minShips, maxShips)
        growth_rate = random.randint(minGrowth, maxGrowth)
        p = make_planet(0, 0, 0, num_ships, growth_rate)
        generate_coordinates(p, r, theta)
        while not_valids(p):
            r = rand_num(0, maxRadius)
            generate_coordinates(p, r, theta)
        planets.append(p)



#picks out the rest of the neutral planets
iMax = (noPlanets-noCenterNeutrals-2)/2
for i in range(iMax):
    r = rand_num(minDistance, maxRadius)
    theta = rand_num(0, 360)
    if i == 0:
        num_ships = random.randint(minShips, 5*distance(p1, p2)-1)
    else:
        num_ships = random.randint(minShips, maxShips)
    growth_rate = random.randint(minGrowth, maxGrowth)
    p1 = make_planet(0, 0, 0, num_ships, growth_rate)
    p2 = make_planet(0, 0, 0, num_ships, growth_rate)
    generate_coordinates(p1, r, theta1+theta)
    generate_coordinates(p2, r, theta2 + symmetryType*theta)

    while not_valid(p1, p2):
        r = rand_num(minDistance, maxRadius)
        theta = rand_num(0, 360)
        generate_coordinates(p1, r, theta1 + theta)
        generate_coordinates(p2, r, theta2 + symmetryType*theta)
    planets.append(p1)
    planets.append(p2)

translate_planets(planets)

for p in planets:
    print_planet(p)


Output:
P 16.6475041105 11.683120762 1 100 5
P 11.3524958895 16.316879238 2 100 5
P 14 14 0 33 5
P 18.3323644151 4.915809524 0 5 5
P 9.66763558488 23.084190476 0 5 5
P 3.51785389819 12.0066212749 0 16 3
P 24.4821461018 15.9933787251 0 16 3
P 16.0500387753 15.7140972847 0 90 1
P 11.9499612247 12.2859027153 0 90 1
P 15.5223335276 10.4150432437 0 67 1
P 12.4776664724 17.5849567563 0 67 1
P 21.17340296 15.3181383582 0 11 4
P 6.82659703995 12.6818616418 0 11 4
P 9.34766295133 20.4585237399 0 47 3
P 18.6523370487 7.54147626007 0 47 3
P 3.91249620763 10.5578577797 0 44 1
P 24.0875037924 17.4421422203 0 44 1
P 24.3275211631 10.0763214961 0 77 4
P 3.67247883687 17.9236785039 0 77 4
P 18.6637286994 24.3088234964 0 14 4
P 9.33627130061 3.69117650361 0 14 4
P 19.5338059617 21.508916994 0 8 3
P 8.46619403826 6.49108300601 0 8 3
P 11.7729016302 14.8582224296 0 78 4
P 16.2270983698 13.1417775704 0 78 4
P 20.1840206736 5.34073443734 0 87 3
P 7.81597932641 22.6592655627 0 87 3
P 24.8364120063 13.4829444791 0 25 5
P 3.16358799366 14.5170555209 0 25 5


Create a new paste based on this one


Comments: