Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
eranimo committed Jul 9, 2016
2 parents aab7442 + d79e2b5 commit 07c37bd
Show file tree
Hide file tree
Showing 19 changed files with 1,422 additions and 182 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -59,3 +59,4 @@ target/

.DS_Store
bin/map_*.png
bin/export*.json
13 changes: 8 additions & 5 deletions bin/example.py
Expand Up @@ -3,13 +3,16 @@

options = {
"map_type": MapType.terran,
"surface_pressure": 1013.25,
"axial_tilt": 23,
"size": 100,
"base_temp": 0,
"avg_temp": 15,
"base_temp": -19.50,
"avg_temp": 12,
"sea_percent": 60,
"hydrosphere": True,
"num_rivers": 50,
"num_territories": 50
"num_rivers": 125,
"num_territories": 0
}

generate(options)
gen = generate(options, image=True)
# gen.export('bin/export3.json')
1 change: 1 addition & 0 deletions bin/export.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions bin/parse.js
@@ -0,0 +1,20 @@
var JSONStream = require('JSONStream');
var fs = require('fs');

var fileName = 'export.json';
var fd = fs.openSync(fileName, 'r');
var readStream = fs.createReadStream(fileName);
var fileSize = fs.fstatSync(fd)['size'];
console.log('File size: ', fileSize);

var place = 0;

readStream
.on('data', function (chunk) {
console.log(Math.round(place / fileSize * 100) / 100 + '%');
place += chunk.length;
})
.pipe(JSONStream.parse('hexes.*'))
.on('data', function(data) {
//console.dir(this);
});
28 changes: 28 additions & 0 deletions climate.md
@@ -0,0 +1,28 @@



pressure zonal changes

| zone | season | pressure |
|-------|--------|----------|
| land | winter | high |
| | summer | low |
| ocean |


seasons

| hemisphere | end year | mid year |
|------------|----------|----------|
| northern | winter | summer |
| southern | summer | winter |


ITCZ changes

mid_year = move north
end_year = move south

max range of movement:
ocean 5 degrees
land 40 degrees
93 changes: 81 additions & 12 deletions hexgen/__init__.py
Expand Up @@ -3,13 +3,14 @@
from hexgen.mapgen import MapGen
from hexgen.hex import HexFeature
from hexgen.constants import *
from hexgen.enums import GeoformType
from hexgen.draw import HexGridDraw

# @exec_time
def draw_grid(hex_grid):
def color_heightmap(h):
alt = h.altitude
return alt, alt, alt
alt = int(h.altitude)
return (alt, alt, alt)

# make terrain map
def color_terrain(h):
Expand All @@ -18,8 +19,17 @@ def color_terrain(h):
def color_rivers(h):
return h.color_rivers

def color_temperature(h):
return h.color_temperature
def color_temperature_end_year(h):
return h.color_temperature[0]

def color_temperature_mid_year(h):
return h.color_temperature[1]

def temperature_end_year(h):
return h.temperature[0]

def temperature_mid_year(h):
return h.temperature[1]

def color_biome(h):
return h.color_biome
Expand All @@ -44,16 +54,75 @@ def color_resources(h):
return h.resource.get('type').color
return (100, 100, 100)

HexGridDraw(hex_grid, color_features, "map_features.png", show_coasts=True, rivers=False)
HexGridDraw(hex_grid, color_heightmap, "map_height.png", rivers=False, show_coasts=True)
HexGridDraw(hex_grid, color_terrain, "map_terrain.png", rivers=True)
HexGridDraw(hex_grid, color_rivers, "map_rivers.png", rivers=True)
HexGridDraw(hex_grid, color_temperature, "map_temp.png", rivers=False, show_coasts=True)
def color_zone(h):
return h.zone.color

def key_zone(h):
return h.zone.map_key

def hex_latitude(h):
return h.latitude

def color_pressure_end_year(h):
return h.color_pressure[0]

def color_pressure_mid_year(h):
return h.color_pressure[1]

def pressure_number_end_year(h):
return h.pressure[0]

def pressure_number_mid_year(h):
return h.pressure[1]

def color_wind_end_year(h):
return h.color_pressure[0]

def color_wind_mid_year(h):
return h.color_pressure[1]

def wind_display_end_year(h):
wind = h.wind[0].get('direction')
if wind:
return wind.arrow
return "-"

def wind_display_mid_year(h):
wind = h.wind[1].get('direction')
if wind:
return wind.arrow
return "-"

def color_hex_type(h):
if h.is_land:
return (0, 255, 0)
return (0, 0, 255)

def color_geoforms(h):
for g in GeoformType.list():
if h.geoform.type is g:
return g.color
return (0, 0, 255)

# HexGridDraw(hex_grid, color_features, "map_features.png", show_coasts=True, rivers=False)
# HexGridDraw(hex_grid, color_heightmap, "map_height.png", rivers=False, show_coasts=True)
# HexGridDraw(hex_grid, color_terrain, "map_terrain.png", rivers=True, show_coasts=True)
# HexGridDraw(hex_grid, color_hex_type, "map_hex_types.png", rivers=True, show_coasts=True)
HexGridDraw(hex_grid, color_geoforms, "map_geoforms.png", rivers=False, show_coasts=True)
HexGridDraw(hex_grid, color_rivers, "map_rivers.png", rivers=True, show_coasts=True)
HexGridDraw(hex_grid, color_temperature_end_year, "map_temp_end_year.png", rivers=False, show_coasts=True)
HexGridDraw(hex_grid, color_temperature_mid_year, "map_temp_mid_year.png", rivers=False, show_coasts=True)
HexGridDraw(hex_grid, color_biome, "map_biome.png", rivers=False)
HexGridDraw(hex_grid, color_territories, "map_territories.png", rivers=False,
show_coasts=True, borders=True)
# HexGridDraw(hex_grid, color_territories, "map_territories.png", rivers=False,
# show_coasts=True, borders=True)
HexGridDraw(hex_grid, color_satellite, "map_satellite.png")
HexGridDraw(hex_grid, color_resources, "map_resources.png")
# HexGridDraw(hex_grid, color_resources, "map_resources.png")
# HexGridDraw(hex_grid, color_zone, "map_zone.png", text_func=key_zone, rivers=False, show_coasts=False)
# HexGridDraw(hex_grid, color_zone, "map_latitude.png", text_func=hex_latitude, rivers=False, show_coasts=False)
# HexGridDraw(hex_grid, color_pressure_end_year, "map_pressure_end_year.png", rivers=False, show_coasts=True)
# HexGridDraw(hex_grid, color_pressure_mid_year, "map_pressure_mid_year.png", rivers=False, show_coasts=True)
# HexGridDraw(hex_grid, color_wind_end_year, "map_wind_end_year.png", text_func=wind_display_end_year, rivers=False, show_coasts=True)
# HexGridDraw(hex_grid, color_wind_mid_year, "map_wind_mid_year.png", text_func=wind_display_mid_year, rivers=False, show_coasts=True)

# report on territories
for t in hex_grid.territories:
Expand Down
73 changes: 73 additions & 0 deletions hexgen/calendar.py
@@ -0,0 +1,73 @@
import math
import random

class Month:

def __init__(self, ordinal, num_days, day_length):
self.ordinal = ordinal
self.num_days = num_days
self.day_length = day_length

def __repr__(self):
return "<Month ordinal={} " \
"num_days={} " \
"day_length={}>".format(self.ordinal, self.num_days, self.day_length)


class Calendar:

def __init__(self, year_length, day_length, month_length_target=None):
self.year_length = year_length
self.day_length = day_length

self.months = []

# determine the number of months
# split the year_length into twelve months with an integer number of days
# each month should have around 30 days
if month_length_target is None:
if year_length > 35:
month_length_target = random.randint(25, 35)
else:
month_length_target = year_length / 2
# print("Target length of month: {}".format(month_length_target))

found_month_number = year_length
while(math.floor(year_length / found_month_number) < month_length_target):
num_months = found_month_number
found_month_number -= 1
# print("Number of months: {}".format(num_months))
even_split = year_length / num_months
# print("Length of each month if split evenly: {}".format(even_split))

days_left = year_length
even_split = math.floor(even_split)
for num in range(1, num_months + 1):
# print(days_left, '-', even_split, days_left - even_split, '<', 0)
if days_left - even_split < even_split:
num_days = days_left
else:
num_days = even_split
self.months.append( Month(num, num_days, day_length) )
days_left -= even_split

# print('total', sum([x.num_days for x in self.months]))

#
# import pprint
# pp = pprint.PrettyPrinter(indent=4)
# echo = pp.pprint
#
# def generate_calendar(len_year, len_day):
# """
# len_year = (int) length of year in Earth days
# len_day = (int) length of day in Earth hours
# """
# print("Generating calendar")
# calendar = Calendar(len_year, len_day)
# echo(calendar.months)
# return calendar
#
#
# generate_calendar(365, 24)
# generate_calendar(231, 34)
93 changes: 49 additions & 44 deletions hexgen/draw.py
@@ -1,67 +1,68 @@
from hexgen.hex import HexSide
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont
from hexgen.constants import SIDE_LENGTH, HEX_HEIGHT, HEX_RADIUS, HEX_RECT_HEIGHT, HEX_RECT_WIDTH
from hexgen.util import Timer

class HexGridDraw:
"""
Draws a hexagon grid to an image. For debugging and development purposes
"""

def __init__(self, grid, color_func, file_name, rivers=True,
numbers=False, show_coasts=False, borders=False):
numbers=False, show_coasts=False, borders=False, text_func=None):
self.image = Image.new("RGB", (int(HEX_RECT_WIDTH * (grid.hex_grid.size + 0.6)),
int((HEX_RECT_WIDTH) * grid.hex_grid.size)))
self.draw = ImageDraw.Draw(self.image)
self.Grid = grid
self.color_func = color_func
self.text_func = text_func

self.numbers = numbers
self.show_coasts = show_coasts
self.borders = borders

for y in range(grid.hex_grid.size):
for x in range(grid.hex_grid.size):
h = grid.hex_grid.find_hex(x, y)
self.draw_hexagon(y * HEX_RECT_WIDTH + ((x % 2) * HEX_RADIUS),
x * (SIDE_LENGTH + HEX_HEIGHT), x, y)
with Timer("Making {}".format(file_name), True):
for y in range(grid.hex_grid.size):
for x in range(grid.hex_grid.size):
h = grid.hex_grid.find_hex(x, y)
self.draw_hexagon(y * HEX_RECT_WIDTH + ((x % 2) * HEX_RADIUS),
x * (SIDE_LENGTH + HEX_HEIGHT), x, y)

if self.show_coasts and grid.params.get('hydrosphere'):
for e in h.edges:
if h.is_land and e.two.is_water:
self.draw_hex_edge(x, y, e.side, 4)
if self.borders:
for e in h.edges:
if e.one.is_owned and e.two.is_owned and \
e.one.territory.id != e.two.territory.id:
self.draw_hex_edge(x, y, e.side, 2)
if rivers:
cx = y * HEX_RECT_WIDTH + ( (x % 2) * HEX_RADIUS)
cy = x * (SIDE_LENGTH + HEX_HEIGHT)
origin = (cx + HEX_RADIUS, cy)
pointer = (cx + HEX_RECT_WIDTH, cy + HEX_HEIGHT)
pointer_2 = (cx + HEX_RECT_WIDTH, cy + HEX_HEIGHT + SIDE_LENGTH)
pointer_3 = (cx + HEX_RADIUS, cy + HEX_RECT_HEIGHT)
pointer_4 = (cx, cy + SIDE_LENGTH + HEX_HEIGHT)
pointer_5 = (cx, cy + HEX_HEIGHT)
segments = self.Grid.find_river(x, y)
river_blue = (21, 52, 60) # (255, 255, 255)
for s in segments:
# print("RiverSegment {} at {}, {}".format(s, x, y))
if s is HexSide.north_east:
self.draw.line([origin, pointer], river_blue, width=3)
elif s is HexSide.east:
self.draw.line([pointer, pointer_2], river_blue, width=3)
elif s is HexSide.south_east:
self.draw.line([pointer_2, pointer_3], river_blue, width=3)
elif s is HexSide.south_west:
self.draw.line([pointer_3, pointer_4], river_blue, width=3)
elif s is HexSide.west:
self.draw.line([pointer_4, pointer_5], river_blue, width=3)
elif s is HexSide.north_west:
self.draw.line([pointer_5, origin], river_blue, width=3)

print("Making ", file_name)
self.image.save('bin/' + file_name)
if self.show_coasts and grid.params.get('hydrosphere'):
for e in h.edges:
if h.is_land and e.two.is_water:
self.draw_hex_edge(x, y, e.side, 4)
if self.borders:
for e in h.edges:
if e.one.is_owned and e.two.is_owned and \
e.one.territory.id != e.two.territory.id:
self.draw_hex_edge(x, y, e.side, 2)
if rivers:
cx = y * HEX_RECT_WIDTH + ( (x % 2) * HEX_RADIUS)
cy = x * (SIDE_LENGTH + HEX_HEIGHT)
origin = (cx + HEX_RADIUS, cy)
pointer = (cx + HEX_RECT_WIDTH, cy + HEX_HEIGHT)
pointer_2 = (cx + HEX_RECT_WIDTH, cy + HEX_HEIGHT + SIDE_LENGTH)
pointer_3 = (cx + HEX_RADIUS, cy + HEX_RECT_HEIGHT)
pointer_4 = (cx, cy + SIDE_LENGTH + HEX_HEIGHT)
pointer_5 = (cx, cy + HEX_HEIGHT)
segments = self.Grid.find_river(x, y)
river_blue = (200, 200, 200) # (255, 255, 255)
for s in segments:
# print("RiverSegment {} at {}, {}".format(s, x, y))
if s is HexSide.north_east:
self.draw.line([origin, pointer], river_blue, width=3)
elif s is HexSide.east:
self.draw.line([pointer, pointer_2], river_blue, width=3)
elif s is HexSide.south_east:
self.draw.line([pointer_2, pointer_3], river_blue, width=3)
elif s is HexSide.south_west:
self.draw.line([pointer_3, pointer_4], river_blue, width=3)
elif s is HexSide.west:
self.draw.line([pointer_4, pointer_5], river_blue, width=3)
elif s is HexSide.north_west:
self.draw.line([pointer_5, origin], river_blue, width=3)
self.image.save('bin/' + file_name)

def draw_hex_edge(self, x, y, side, width=3, color=(0, 0, 0)):
s = side
Expand Down Expand Up @@ -121,3 +122,7 @@ def draw_hexagon(self, cx, cy, x, y):
self.draw.text((cx + 4, cy + 19), str(y), fill=(200, 200, 200))
self.draw.text((cx + 18, cy + 11), str(h.moisture), fill=(200, 200, 200))
self.draw.text((cx + 18, cy + 19), str(h.temperature), fill=(200, 200, 200))

if self.text_func:
font = ImageFont.truetype("FreeSans.ttf", 14)
self.draw.text((cx + 5, cy + 5), str(self.text_func(h)), fill=(200, 200, 200), font=font)

0 comments on commit 07c37bd

Please sign in to comment.