Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions build_terrain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from data import world_gen
from terrain import in_chunk, spawn_hierarchy
from console import log, DEBUG

import terrain_gen, terrain


def ground_height_feature(features, x):
ground_height = world_gen['ground_height']

slice_features = features.get(x)
if slice_features and slice_features.get('ground_height'):
ground_height = slice_features['ground_height']

return ground_height


def build_tree(chunk, chunk_pos, x, tree_feature, features):
""" Adds a tree feature at x to the chunk. """

# Add trunk
if in_chunk(x, chunk_pos):
air_height = world_gen['height'] - ground_height_feature(features, x)
for trunk_y in range(air_height - tree_feature['height'], air_height - (bool(DEBUG) * 3)):
chunk[x][trunk_y] = spawn_hierarchy(('|', chunk[x][trunk_y]))

# Add leaves
leaves = world_gen['trees'][tree_feature['type']]['leaves']
half_leaves = int(len(leaves) / 2)

for leaf_dx, leaf_slice in enumerate(leaves):
leaf_x = x + (leaf_dx - half_leaves)

if in_chunk(leaf_x, chunk_pos):
air_height = world_gen['height'] - ground_height_feature(features, x)
leaf_height = air_height - tree_feature['height'] - len(leaf_slice) + tree_feature['trunk_depth']

for leaf_dy, leaf in enumerate(leaf_slice):
if (bool(DEBUG) and leaf_dy == 0) or (not bool(DEBUG) and leaf):
leaf_y = leaf_height + leaf_dy
chunk[leaf_x][leaf_y] = spawn_hierarchy(('@', chunk[leaf_x][leaf_y]))


def build_grass(chunk, chunk_pos, x, grass_feature, features):
""" Adds a grass feature at x to the chunk. """

if in_chunk(x, chunk_pos):
grass_y = world_gen['height'] - ground_height_feature(features, x) - 1
chunk[x][grass_y] = spawn_hierarchy(('v', chunk[x][grass_y]))


def build_ore(chunk, chunk_pos, x, ore_feature, ore, features):
""" Adds an ore feature at x to the chunk. """

for block_pos in range(ore['vain_size'] ** 2):
if ore_feature['vain_shape'][block_pos] < ore['vain_density']:

# Centre on root ore
block_dx = (block_pos % ore['vain_size']) - int((ore['vain_size'] - 1) / 2)
block_dy = int(block_pos / ore['vain_size']) - int((ore['vain_size'] - 1) / 2)

block_x = block_dx + x
block_y = block_dy + ore_feature['root_height']

if not in_chunk(block_x, chunk_pos):
continue

if not world_gen['height'] > block_y > world_gen['height'] - ground_height_feature(features, block_x):
continue

if chunk[block_x][block_y] is ' ':
continue

chunk[block_x][block_y] = spawn_hierarchy((ore['char'], chunk[block_x][block_y]))


def build_cave(chunk, chunk_pos, x, cave_feature, features):
""" Adds caves at x to the chunk. """

for (world_x, y) in cave_feature:
if in_chunk(world_x, chunk_pos):
chunk[world_x][int(y)] = ' '


def build_chunk(chunk_n, meta):
chunk_pos = chunk_n * world_gen['chunk_size']

chunk_features = terrain_gen.gen_chunk_features(chunk_n, meta)

chunk_ground_heights = {}
chunk = {}
for x in range(chunk_pos, chunk_pos + world_gen['chunk_size']):
slice_features = chunk_features.get(x)

chunk_ground_heights[x] = ground_height_feature(chunk_features, x)

chunk[x] = (
[' '] * (world_gen['height'] - chunk_ground_heights[x]) +
['-'] +
['#'] * (chunk_ground_heights[x] - 2) + # 2 for grass and bedrock
['_']
)

for x, slice_features in chunk_features.items():
x = int(x)

for feature_name, feature in slice_features.items():

if feature_name == 'tree':
build_tree(chunk, chunk_pos, x, feature, chunk_features)

elif feature_name == 'grass':
build_grass(chunk, chunk_pos, x, feature, chunk_features)

elif feature_name == 'cave':
build_cave(chunk, chunk_pos, x, feature, chunk_features)

else:
for name, ore in world_gen['ores'].items():
ore_name = name + '_ore_root'

if feature_name == ore_name:
build_ore(chunk, chunk_pos, x, feature, ore, chunk_features)
break

return chunk, chunk_ground_heights
2 changes: 1 addition & 1 deletion colours.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def round_to_palette(*colour):


def lightness(colour):
return 0.2126 * colour[0] + 0.7152 * colour[1] + 0.0722 * colour[2];
return 0.2126 * colour[0] + 0.7152 * colour[1] + 0.0722 * colour[2]


def grey(value):
Expand Down
16 changes: 15 additions & 1 deletion data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from colours import *
from colours import DARK, BOLD, DARK_GRAY, YELLOW, RED, WHITE
from console import supported_chars


Expand Down Expand Up @@ -470,6 +470,8 @@
'chance': 1,
'max_height': 8,
'min_height': 3,
'mean_height': 4,
'max_std_dev': 2,
'leaves': ((0, 1, 1),
(1, 1, 0),
(0, 1, 1))
Expand All @@ -478,6 +480,8 @@
'chance': 1,
'max_height': 12,
'min_height': 6,
'mean_height': 7,
'max_std_dev': 3,
'leaves': ((1, 1, 0, 0, 0, 1, 1),
(0, 1, 1, 0, 1, 1, 0),
(0, 0, 1, 1, 0, 0, 0),
Expand All @@ -488,6 +492,8 @@
'chance': .7,
'max_height': 10,
'min_height': 4,
'mean_height': 8,
'max_std_dev': 4,
'leaves': ((0, 0, 0, 0, 1),
(0, 0, 1, 1, 0),
(0, 1, 0, 0, 0),
Expand All @@ -503,6 +509,8 @@
'chance': 1,
'max_height': 8,
'min_height': 4,
'mean_height': 5,
'max_std_dev': 2,
'leaves': ((0, 0, 1),
(1, 0, 0),
(0, 1, 0))
Expand All @@ -511,6 +519,8 @@
'chance': .9,
'max_height': 16,
'min_height': 6,
'mean_height': 7,
'max_std_dev': 4,
'leaves': ((0, 0, 0, 1, 0, 1),
(0, 1, 1, 1, 1, 0),
(1, 1, 0, 0, 0, 0),
Expand All @@ -521,6 +531,8 @@
'chance': .8,
'max_height': 22,
'min_height': 8,
'mean_height': 7,
'max_std_dev': 3,
'leaves': ((0,0,1,1,0,1,1,0),
(0,1,1,1,1,1,1,1),
(1,1,1,1,1,1,1,1),
Expand All @@ -533,6 +545,8 @@
'chance': 1,
'max_height': 32,
'min_height': 12,
'mean_height': 10,
'max_std_dev': 4,
'leaves': ((0, 0, 0, 0, 0, 1, 1, 1, 0),
(0, 0, 0, 0, 1, 1, 1, 1, 0),
(0, 0, 0, 1, 1, 1, 1, 1, 0),
Expand Down
2 changes: 1 addition & 1 deletion events.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def boom(server, x, y):

server.splash_damage(x, y, radius*2, blast_strength/3)

return new_blocks
return new_blocks
52 changes: 52 additions & 0 deletions gravity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from data import world_gen
from terrain import is_solid


def apply_gravity(map_, edges):
start_pos = (sum(edges) / 2,
world_gen['height'] - 1)

new_blocks = {}
connected_to_ground = explore_map(map_, edges, start_pos, set())

for x, slice_ in map_.items():
if x not in range(*edges):
continue
for y in range(len(slice_)-3, -1, -1):
block = slice_[y]
if (is_solid(block) and (x, y) not in connected_to_ground):
new_blocks.setdefault(x, {})
new_blocks[x][y] = ' '
new_blocks[x][y+1] = block

return new_blocks


def explore_map(map_, edges, start_pos, connected_to_ground):
blocks_to_explore = set([start_pos])
visted_blocks = set()

while blocks_to_explore:
current_pos = blocks_to_explore.pop()
visted_blocks.add(current_pos)

if (current_pos[1] >= 0 and current_pos[1] < world_gen['height'] and
current_pos not in connected_to_ground and
current_pos[0] in range(edges[0]-1, edges[1]+1)):

try:
current_block = map_[current_pos[0]][current_pos[1]]
except (IndexError, KeyError):
current_block = None

if (current_block is not None and
is_solid(current_block)) or current_pos[0] in (edges[0]-1, edges[1]):

connected_to_ground.add(current_pos)
for (dx, dy) in ((x, y) for x in (-1, 0, 1) for y in (-1, 0, 1)):
pos = (current_pos[0] + dx, current_pos[1] + dy)

if pos not in visted_blocks:
blocks_to_explore.add(pos)

return connected_to_ground
2 changes: 1 addition & 1 deletion items.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ def items_to_render_objects(items, x, offset):

objects.append(object_)

return objects
return objects
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from items import items_to_render_objects
from events import process_events

import saves, ui, terrain, player, render, render_interface, server_interface, data
import saves, ui, terrain, gravity, player, render, render_interface, server_interface, data


def main():
Expand Down Expand Up @@ -80,7 +80,6 @@ def setdown():

def game(server, settings, benchmarks):
dt = 0 # Tick
df = 0 # Frame
dc = 0 # Cursor
ds = 0 # Selector
dpos = False
Expand Down Expand Up @@ -216,7 +215,7 @@ def game(server, settings, benchmarks):
server.redraw = True

if settings.get('gravity'):
blocks = terrain.apply_gravity(server.map_, extended_edges)
blocks = gravity.apply_gravity(server.map_, extended_edges)
if blocks: server.set_blocks(blocks)

## Crafting
Expand Down Expand Up @@ -286,14 +285,14 @@ def game(server, settings, benchmarks):
events += new_events

new_blocks = {}
for i in range(int(dt)):
for _ in range(int(dt)):
new_blocks.update(process_events(events, server))
if new_blocks:
server.set_blocks(new_blocks)

# If no block below, kill player
try:
block = server.map_[x][y+1]
_ = server.map_[x][y+1]
except IndexError:
alive = False

Expand Down
4 changes: 2 additions & 2 deletions mobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def update(mobs, players, map_, last_tick):
new_items = {}

for mob_id, mob in mobs.items():
mx, my, x_vel = mob['x'], mob['y'], mob['x_vel']
mx, my = mob['x'], mob['y']

if mob['health'] <= 0:
removed_mobs.append(mob_id)
Expand Down Expand Up @@ -64,7 +64,7 @@ def update(mobs, players, map_, last_tick):


def spawn(mobs, players, map_, x_start_range, y_start_range, x_end_range, y_end_range):
log("spawning", x_start_range, x_end_range, m='mobs');
log('spawning', x_start_range, x_end_range, m='mobs')

n_mobs_to_spawn = random.randint(0, 5) if random.random() < mob_rate else 0
new_mobs = {}
Expand Down
4 changes: 2 additions & 2 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def receive(sock):
d = bytes()

try:
for i in range(length // bufsize):
for _ in range(length // bufsize):
d += sock.recv(bufsize)
time.sleep(0.001)
d += sock.recv(length % bufsize)
Expand Down Expand Up @@ -107,7 +107,7 @@ def start(data_handler, port):
HOST, PORT = '0.0.0.0', int(port)

server = ThreadedTCPServer((HOST, PORT), requestHandlerFactory(data_handler))
ip, port = server.server_address
_, port = server.server_address

# Start a thread with the server -- that thread will then start one
# more thread for each request
Expand Down
2 changes: 1 addition & 1 deletion pathfinding.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def pathfind_towards_delta(entity, delta, map_):

updated = True

return updated, kill_entity
return updated, kill_entity
Loading