Ich sende dir hier eine ganze reihe von codesnippets welche alle durch die zeile in der !!!BREAK!!! steht getrennt sind. Alle codesnippets stammen aus einem gesprächsverlauf indem eine anwendung schritt für schritt entwickelt wurde. Nun soll aus diesen codesnippet verlauf (gesprächsverlauf) als resultat das vollständige skript der erstellten anwendung mit dem gesamten erstellten codes aus den snippets zusammengefügt werden um ein einziges skript als resultat ihrer entwicklung während des gesprächs zu erhalten. Erstelle also dieses skript anhand aller codesnippets, beachte dabei das die anwendung auch unter verwendung aller snippets noch nicht vollständig fertig entwickelt ist. Erstelle trotzdem das soweit die anwendung bisher entwickelt wurde, das vollständige skript der gesamten anwendung unter berücksichtung aller snippets. achte darauf keine inhalte zu vergessen.
Snippets:
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
Environment parameters
AIR = 0
SOIL = 1
FOOD = 2
NEST_MATERIAL = 3
Ant parameters
ANT_SIZE = 0.5 # Size of an ant (adjust as needed)
LEG_LENGTH = ANT_SIZE / 3
ANTENNA_LENGTH = ANT_SIZE / 2
NUM_LEGS = 6
NUM_ANTENNA_SEGMENTS = 3 # This parameter is not directly used anymore, but let's keep it for potential use.
NUM_BODY_SEGMENTS = 3
MAX_LEG_STRETCH = LEG_LENGTH * 1.2 # Maximum leg stretch
Detail culling distance
CULL_DISTANCE = 50
Gravity (not used directly, but kept for potential future use)
GRAVITY = np.array([0, 0, -0.1])
class Voxel:
def init(self, voxel_type):
self.type = voxel_type
self.food_pheromone = 0.0 # Initialize pheromone level to 0
class Ant:
def init(self, position, colony):
... (other attributes)
self.body_segments = [np.array(position) + np.array([i * ANT_SIZE / 3, 0, 0]) for i in range(NUM_BODY_SEGMENTS)]
self.body_rotations = [0.0] * (NUM_BODY_SEGMENTS - 1) # Rotation angles between body segments
self.leg_angles = [0.0] * NUM_LEGS # For leg animation
self.antenna_angles = [0.0] * NUM_ANTENNA_SEGMENTS * 2 # for antenna animation (2 antennae)
self.leg_targets = [np.array(position) for _ in range(NUM_LEGS)] # initialize leg targets
self.antenna_targets = [np.zeros(3) for _ in range(2)]
self.leg_positions = [[]] * NUM_LEGS # Clear leg positions to be recalculated dynamically in the ant's move method
self.antenna_positions = [np.zeros(3) for _ in range(2)] # set antenna positions before movement logic is applied
self.has_food = False
self.direction = np.random.rand(3) - 0.5 # Random initial direction
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
def move(self, environment):
Pheromone influence on movement
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
pheromone_direction = np.zeros(3)
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0,1]: # consider 3D Pheromone
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
pheromone_strength = environment[nx, ny, nz].food_pheromone
if pheromone_strength > FOOD_PHEROMONE_THRESHOLD and not self.has_food: # Ants with food don't follow food pheromones.
pheromone_direction += np.array([dx, dy, dz]) * pheromone_strength
Combine random walk with pheromone direction
if 0 <= new_pos[0] < ENV_SIZE[0] and 0 <= new_pos[1] < ENV_SIZE[1] and 0 <= new_pos[2] < ENV_SIZE[2]:
if environment[int(new_pos[0]), int(new_pos[1]), int(new_pos[2])].type != SOIL:
self.position = new_pos
Update body segment positions based on movement
for i in range(1, NUM_BODY_SEGMENTS):
self.body_segments[i] = self.body_segments[0] + np.array([i * ANT_SIZE / 3 * math.cos(self.direction[0]), i * ANT_SIZE/3 * math.sin(self.direction[1]), 0]) # Simple segment rotation for now
Inverse Kinematics for legs with 3D collision detection
for i in range(NUM_LEGS):
... (calculate start_pos)
Check if current target is still reachable
if np.linalg.norm(start_pos - self.leg_targets[i]) > MAX_LEG_STRETCH:
Find new target with 3D collision detection
Define search directions around the leg (more directions mean better coverage but worse performance):
for direction in search_directions:
x,y,z = int(start_pos[0]), int(start_pos[1]), int(start_pos[2])
for j in range(int(MAX_LEG_STRETCH)): # loop through voxels in direction
nx,ny,nz = x + int(j * direction[0]), y + int(j * direction[1]), z + int(j * direction[2])
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx,ny,nz].type == SOIL:
if closest_surface is not None:
self.leg_targets[i] = closest_surface
else:
self.leg_targets[i] = start_pos + self.direction * LEG_LENGTH # move target forward if no ground is found
Simplified IK calculation for two leg segments:
target = self.leg_targets[i]
dist_to_target = np.linalg.norm(start_pos-target)
if dist_to_target > 0:
mid_pos = start_pos + (target - start_pos) / 2 + np.array([0,0,LEG_LENGTH/2 * (1-(dist_to_target / MAX_LEG_STRETCH)) ])
Adjust mid point height dynamically based on leg stretch
self.leg_positions[i] = [start_pos, mid_pos, target]
3D body rotation (simplified):
for i in range(NUM_BODY_SEGMENTS - 1):
segment_direction = self.body_segments[i+1] - self.body_segments[i]
angle = math.atan2(segment_direction[1], segment_direction[0]) # Calculate angle in x-y plane
self.body_rotations[i] = angle # apply angle
Advanced antenna animation (exploration behavior):
for i in range(2): # 2 antennae
Simplified exploration by random target around the ant:
self.antenna_targets[i] = self.body_segments[-1] + np.random.rand(3) * ANT_SIZE - ANT_SIZE/2 # Target somewhere around the ant's head.
simple "dampened" movement of antennae toward target point
def forage(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if environment[x, y, z].type == FOOD:
self.has_food = True
environment[x, y, z].type = AIR # consume food
elif self.has_food and (x, y, z) == self.colony.nest_entrance:
self.has_food = False
self.colony.food_count += 1
Deposit pheromone at the nest entrance:
environment[x,y,z].food_pheromone = 1.0
Pheromone Deposit for food trail:
if self.has_food:
environment[x, y, z].food_pheromone = 1.0
def draw(self, screen):
Detail culling
distance_to_camera = np.linalg.norm(self.body_segments[0] - camera_pos)
if distance_to_camera > CULL_DISTANCE:
Draw simplified representation (e.g., only body)
for i in range(2):
screen_x1, screen_y1 = project_to_screen(self.body_segments[-1])
screen_x2, screen_y2 = project_to_screen(self.body_segments[-1] + self.antenna_positions[i])
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
environment[x,y,0] = Voxel(SOIL)
Simplified object generation (example: stick)
stick_start = np.array([random.randint(0, ENV_SIZE[0]-1), random.randint(0, ENV_SIZE[1]-1),1]) # put it a little bit above the ground
stick_end = stick_start + np.array([random.randint(-5,5),random.randint(-5,5),random.randint(5,10)]) # create a stick that can be placed in different positions
place stick voxels
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x,y,z] = SOIL
... (generate other objects: stones, leaves, etc.)
grid.point_data["voxel_types"] = voxel_types.flatten() # Update pyvista with new data
while running:
Pygame event handling (for user input)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
add camera mouse controls here later
... (ant logic)
Camera zoom and panning:
keys = pygame.key.get_pressed()
if keys[pygame.K_EQUALS]: # zoom in with +
zoom += 0.1
if keys[pygame.K_MINUS]: # zoom out with -
zoom -= 0.1
zoom = max(0.1, zoom) # Prevent zooming too far out
Clear screen
screen.fill((0, 0, 0))
draw_environment(grid) # draw the environment
for ant in colony.ants:
ant.forage(environment)
ant.leg_positions = [[]] * NUM_LEGS # Clear leg positions to be recalculated dynamically in the ant's move method
ant.move(environment)
ant.draw(screen) # Draw the ant
AIR = 0
SOIL = 1
FOOD = 2
NEST_MATERIAL = 3
Ant parameters
ANT_SIZE = 0.5 # Size of an ant (adjust as needed)
LEG_LENGTH = ANT_SIZE / 3
ANTENNA_LENGTH = ANT_SIZE / 2
NUM_LEGS = 6
NUM_ANTENNA_SEGMENTS = 3 # This parameter is not directly used anymore, but let's keep it for potential use.
NUM_BODY_SEGMENTS = 3
MAX_LEG_STRETCH = LEG_LENGTH * 1.2 # Maximum leg stretch
Detail culling distance
CULL_DISTANCE = 50
Gravity (not used directly, but kept for potential future use)
GRAVITY = np.array([0, 0, -0.1])
class Voxel:
def init(self, voxel_type):
self.type = voxel_type
self.food_pheromone = 0.0 # Initialize pheromone level to 0
class Ant:
def init(self, position, colony):
... (other attributes)
self.body_segments = [np.array(position) + np.array([i * ANT_SIZE / 3, 0, 0]) for i in
range(NUM_BODY_SEGMENTS)]
self.body_rotations = [0.0] * (NUM_BODY_SEGMENTS - 1) # Rotation angles between body segments
self.leg_angles = [0.0] * NUM_LEGS # For leg animation
self.antenna_angles = [0.0] * NUM_ANTENNA_SEGMENTS * 2 # for antenna animation (2 antennae)
self.leg_targets = [np.array(position) for _ in range(NUM_LEGS)] # initialize leg targets
self.antenna_targets = [np.zeros(3) for _ in range(2)]
self.leg_positions = [[]] * NUM_LEGS # Clear leg positions to be recalculated dynamically in the ant's move method
self.antenna_positions = [np.zeros(3) for _ in range(2)] # set antenna positions before movement logic is applied
self.has_food = False
self.direction = np.random.rand(3) - 0.5 # Random initial direction
def move(self, environment):
Pheromone influence on movement
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
pheromone_direction = np.zeros(3)
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]: # consider 3D Pheromone
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
pheromone_strength = environment[nx, ny, nz].food_pheromone
if pheromone_strength > FOOD_PHEROMONE_THRESHOLD and not self.has_food: # Ants with food don't follow food pheromones.
pheromone_direction += np.array([dx, dy, dz]) * pheromone_strength
Combine random walk with pheromone direction
if 0 <= new_pos[0] < ENV_SIZE[0] and 0 <= new_pos[1] < ENV_SIZE[1] and 0 <= new_pos[2] < ENV_SIZE[2]:
if environment[int(new_pos[0]), int(new_pos[1]), int(new_pos[2])].type != SOIL:
self.position = new_pos
Update body segment positions based on movement
for i in range(1, NUM_BODY_SEGMENTS):
self.body_segments[i] = self.body_segments[0] + np.array(
[i * ANT_SIZE / 3 * math.cos(self.direction[0]), i * ANT_SIZE / 3 * math.sin(self.direction[1]), 0]) # Simple segment rotation for now
Inverse Kinematics for legs with 3D collision detection
for i in range(NUM_LEGS):
start_pos = self.body_segments[i // (NUM_LEGS // NUM_BODY_SEGMENTS)]
if np.linalg.norm(start_pos - self.leg_targets[i]) > MAX_LEG_STRETCH:
Find new target with 3D collision detection
x, y, z = int(start_pos[0]), int(start_pos[1]), int(start_pos[2])
ground_z = -1
for dz in range(int(start_pos[2]), -1, -1):
nx, ny, nz = x, y, dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == SOIL:
ground_z = nz
self.leg_targets[i] = np.array([nx, ny, ground_z]) # Place leg on colliding surface
break # stop loop to prevent placing the target position below a colliding voxel
if ground_z == -1:
self.leg_targets[i] = start_pos + self.direction * LEG_LENGTH
Simplified IK calculation for two leg segments:
target = self.leg_targets[i]
dist_to_target = np.linalg.norm(start_pos - target)
if dist_to_target > 0:
mid_pos = start_pos + (target - start_pos) / 2 + np.array(
[0, 0, LEG_LENGTH / 2 * (1 - (dist_to_target / MAX_LEG_STRETCH))])
Adjust mid point height dynamically based on leg stretch
self.leg_positions[i] = [start_pos, mid_pos, target]
3D body rotation (simplified):
for i in range(NUM_BODY_SEGMENTS - 1):
segment_direction = self.body_segments[i + 1] - self.body_segments[i]
angle = math.atan2(segment_direction[1], segment_direction[0]) # Calculate angle in x-y plane
self.body_rotations[i] = angle # apply angle
Advanced antenna animation (exploration behavior):
for i in range(2): # 2 antennae
Simplified exploration by random target around the ant:
self.antenna_targets[i] = self.body_segments[-1] + np.random.rand(3) * ANT_SIZE - ANT_SIZE / 2 # Target somewhere around the ant's head.
simple "dampened" movement of antennae toward target point
def forage(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if environment[x, y, z].type == FOOD:
self.has_food = True
environment[x, y, z].type = AIR # consume food
elif self.has_food and (x, y, z) == self.colony.nest_entrance:
self.has_food = False
self.colony.food_count += 1
Deposit pheromone at the nest entrance:
environment[x, y, z].food_pheromone = 1.0
Pheromone Deposit for food trail:
if self.has_food:
environment[x, y, z].food_pheromone = 1.0
def draw(self, screen):
Detail culling
distance_to_camera = np.linalg.norm(self.body_segments[0] - camera_pos)
if distance_to_camera > CULL_DISTANCE:
Draw simplified representation (e.g., only body)
for i in range(2):
screen_x1, screen_y1 = project_to_screen(self.body_segments[-1])
screen_x2, screen_y2 = project_to_screen(self.body_segments[-1] + self.antenna_positions[i])
def generate_environment(grid, voxel_types):
Use OpenSimplex for more varied terrain
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
height = int(opensimplex.noise2(x / 10, y / 10) * 5 + ENV_SIZE[2] / 4)
for z in range(height):
voxel_types[x, y, z] = SOIL
Generate objects (example: stick, stone, leaf)
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x, y, z] = material
Generate various objects at random locations and angles.
for _ in range(10): # Number of sticks
start_pos = np.array(
[random.randint(0, ENV_SIZE[0] - 1), random.randint(0, ENV_SIZE[1] - 1), random.randint(1, ENV_SIZE[2] - 1)])
end_pos = start_pos + np.array(
[random.randint(-5, 5), random.randint(-5, 5), random.randint(-10, 10)]) # Allow sticks at any angle
generate_object(start_pos, end_pos, SOIL) # Use SOIL material for stick
for _ in range(5): # Number of stones
center = np.array(
[random.randint(0, ENV_SIZE[0] - 1), random.randint(0, ENV_SIZE[1] - 1), random.randint(1, ENV_SIZE[2] - 1)])
radius = random.randint(1, 3)
for x in range(center[0] - radius, center[0] + radius + 1):
for y in range(center[1] - radius, center[1] + radius + 1):
for z in range(center[2] - radius, center[2] + radius + 1): # make stones roughly spherical
dist_to_center = np.linalg.norm(np.array([x, y, z]) - center)
if dist_to_center <= radius:
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x, y, z] = SOIL
camera_pos = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, ENV_SIZE[2] / 2]) # Start in the middle of the environment.
camera_target = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, 0])
zoom = 1.0
camera_rotation = np.array([0.0, 0.0]) # rotation around x and y axis
PyVista initialization
pv.set_plot_theme("document") # simpler theme
plotter = pv.Plotter(off_screen=True) # for faster rendering. change to False, if you want to see the scene
grid = pv.UniformGrid() # use UniformGrid to store voxels
grid.dimensions = np.array(ENV_SIZE) + 1 # add 1 for correct dimensions
grid.spacing = (VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE)
grid.origin = (0, 0, 0)
Add a scalar array to hold voxel types. Initially all air.
plotter.add_mesh(grid, name="environment") # add grid to plotter
Initialize environment and colony
grid, voxel_types = generate_environment(grid, voxel_types)
environment = np.zeros(ENV_SIZE, dtype=object)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x, y, z] = Voxel(voxel_types[x, y, z]) # Initialize environment voxels
nest_entrance = (ENV_SIZE[0] // 2, ENV_SIZE[1] // 2, 0) # Nest entrance at center ground level
colony = Colony(nest_entrance)
Create initial ants
for _ in range(INITIAL_ANTS):
x, y = random.randint(0, ENV_SIZE[0] - 1), random.randint(0, ENV_SIZE[1] - 1)
colony.ants.append(Ant((x, y, 0), colony)) # Start ants at ground level
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
camera_controls(plotter, event) # call camera control function.
Rotate Camera:
plotter.camera_position = [(camera_pos[0] * VOXEL_SIZE, camera_pos[1] * VOXEL_SIZE, camera_pos[2] * VOXEL_SIZE),
(camera_target[0] * VOXEL_SIZE, camera_target[1] * VOXEL_SIZE,
camera_target[2] * VOXEL_SIZE),
(0, 0, 1)]
for ant in colony.ants:
ant.forage(environment)
ant.leg_positions = [[]] * NUM_LEGS # Clear leg positions to be recalculated dynamically in the ant's move method
ant.move(environment)
ant.draw(screen)
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (environment parameters, voxel types, etc.)
... (Ant class, Colony class, Voxel class)
... (functions: project_to_screen, generate_environment, draw_environment, camera_controls)
def main():
... (Pygame initialization, camera parameters, PyVista initialization, environment and colony initialization)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
camera_controls(plotter, event)
Rotate Camera:
ant.move(environment)
3. Leg and antenna animation:
3a. Legs
for i in range(NUM_LEGS):
ant.leg_positions[i] = [] # Reset leg positions (important to recalculate for each step)
ant.move(environment) # Recalculate leg positions
3b. Antennae
for i in range(2):
Simplified exploration by random target around the ant:
ant.antenna_targets[i] = ant.body_segments[-1] + np.random.rand(3) * ANT_SIZE - ANT_SIZE / 2
simple "dampened" movement of antennae toward target point
draw_environment(grid)
... (rest of the main loop: pygame.display.flip(), clock.tick(30))
pygame.quit()
if name == "main":
main()
---!!!BREAK!!!---
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
Check if close to nest:
if np.linalg.norm(self.position - self.colony.nest_entrance) < 5: # Adjust distance as needed
Check if carrying food:
if not self.has_food:
Try to pick up material:
if environment[x, y, z].type == SOIL: # Check for a soil voxel
if random.random() < 0.2: # Probability of picking up material
environment[x, y, z].type = AIR
self.carrying_material = True
Try to place material:
if self.carrying_material:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]: # Look for a nearby voxel
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == AIR: # Check for an air voxel
if random.random() < 0.5: # Probability of placing material
environment[nx, ny, nz].type = NEST_MATERIAL
self.carrying_material = False
return
---!!!BREAK!!!---
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
Check if close to nest:
if np.linalg.norm(self.position - self.colony.nest_entrance) < 5:
Check if carrying food:
if not self.has_food:
Try to pick up material:
if environment[x, y, z].type == SOIL:
if random.random() < 0.2:
environment[x, y, z].type = AIR
self.carrying_material = True
Try to place material:
if self.carrying_material:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == AIR and (
environment[nx, ny, nz - 1].type == NEST_MATERIAL or random.random() < 0.1):
Place material on top of existing nest or randomly inside
environment[nx, ny, nz].type = NEST_MATERIAL
self.carrying_material = False
return
Try to dig a tunnel (less likely):
if random.random() < 0.05:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == SOIL and (
environment[nx, ny, nz - 1].type == NEST_MATERIAL or random.random() < 0.1):
environment[nx, ny, nz].type = AIR
return
---!!!BREAK!!!---
... (Voxel class with alpha value)
def draw_environment(grid, tunnel_mode):
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
voxel = environment[x, y, z]
Set alpha for tunnel mode
if tunnel_mode and voxel.type == NEST_MATERIAL:
... (Implement raycasting logic to determine visibility)
if voxel is visible_from_inside:
voxel.alpha = 255 # Opaque if inside the tunnel
else:
voxel.alpha = 128 # Transparent if outside the tunnel
Draw voxel with alpha
AIR = 0
SOIL = 1
FOOD = 2
NEST_MATERIAL = 3
Ant parameters
ANT_SIZE = 0.5 # Size of an ant (adjust as needed)
LEG_LENGTH = ANT_SIZE / 3
ANTENNA_LENGTH = ANT_SIZE / 2
NUM_LEGS = 6
NUM_ANTENNA_SEGMENTS = 3 # This parameter is not directly used anymore, but let's keep it for potential use.
NUM_BODY_SEGMENTS = 3
MAX_LEG_STRETCH = LEG_LENGTH * 1.2 # Maximum leg stretch
Detail culling distance
CULL_DISTANCE = 50
Gravity (not used directly, but kept for potential future use)
camera_pos = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, ENV_SIZE[2] / 2]) # Start in the middle of the environment.
camera_target = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, 0])
zoom = 1.0
camera_rotation = np.array([0.0, 0.0]) # rotation around x and y axis
PyVista initialization
pv.set_plot_theme("document") # simpler theme
plotter = pv.Plotter(off_screen=True) # for faster rendering. change to False, if you want to see the scene
grid = pv.UniformGrid() # use UniformGrid to store voxels
grid.dimensions = np.array(ENV_SIZE) + 1 # add 1 for correct dimensions
grid.spacing = (VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE)
grid.origin = (0, 0, 0)
Add a scalar array to hold voxel types. Initially all air.
plotter.add_mesh(grid, name="environment") # add grid to plotter
... (generate_environment function - same as before)
def draw_environment(grid, tunnel_mode, camera_pos, colony): # Pass camera position and colony to the function
global environment
Update PyVista grid with current voxel data
grid.point_data["voxel_types"] = [voxel.type for voxel in environment.flatten()]
plotter.update()
plotter.screenshot("screenshot.png")
screenshot = pygame.image.load("screenshot.png").convert() # load screenshot
Get the current image on the plotter and display it in Pygame
screen.blit(screenshot, (0, 0))
... (camera_controls function - same as before)
def project_to_screen(position):
... (same as before)
def main():
global grid, voxel_types, camera_pos, camera_target, zoom, environment, tunnel_mode
... (Initialize environment and colony - same as before)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
camera_controls(plotter, event)
Toggle tunnel mode with 'T' key
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
tunnel_mode = not tunnel_mode
Rotate Camera:
for ant in colony.ants:
ant.forage(environment)
ant.leg_positions = [[]] * NUM_LEGS # Reset leg positions
ant.move(environment)
ant.build_nest(environment)
ant.draw(screen)
Apply tunnel view after ant logic to show updated nest structure
if tunnel_mode:
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
voxel = environment[x, y, z]
if voxel.type == NEST_MATERIAL:
voxel_center = np.array([x, y, z]) + VOXEL_SIZE / 2 # Calculate center of voxel
camera_to_voxel = voxel_center - camera_pos
distance_to_nest_center = np.linalg.norm(
camera_pos - colony.nest_entrance) # Assuming nest is centered at nest entrance
if distance_to_nest_center < 10: # Adjust this value based on nest size
voxel.alpha = 255 # Opaque inside the nest
else:
voxel.alpha = 128 # Transparent outside the nest
draw_environment(grid, tunnel_mode, camera_pos, colony) # Pass tunnel mode, camera position, and colony
pygame.display.flip()
clock.tick(30) # Limit framerate
pygame.quit()
if name == "main":
main()
---!!!BREAK!!!---
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if self.carrying_material:
Search for suitable place to deposit material (e.g., next to existing nest material)
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == AIR:
current_neighbors = 0
for nnx in [-1,0,1]:
for nny in [-1,0,1]:
for nnz in [-1,0,1]:
annx, anny, annz = nx + nnx, ny+nny, nz+nnz
if 0 <= annx < ENV_SIZE[0] and 0 <= anny < ENV_SIZE[1] and 0 <= annz < ENV_SIZE[2]:
if environment[annx, anny, annz].type == NEST_MATERIAL:
current_neighbors+=1
if best_nx != -1:
environment[best_nx,best_ny,best_nz].type = NEST_MATERIAL
self.carrying_material = False
Try to dig tunnel (with small probability):
elif random.random() < 0.02: # Reduced probability
... (Digging logic - similar as before)
---!!!BREAK!!!---
def generate_environment(grid, voxel_types):
... (terrain generation - similar as before)
def generate_object(material, min_size, max_size): # Function for object generation with more detailed arguments
size = random.randint(min_size, max_size)
... (logic for placing the object with varied sizes, shapes and distributions)
generate_object(SOIL, 2, 5) # Generate small stones
generate_object(SOIL, 5, 10) # Generate larger stones
... (generate other objects)
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (other code)
def is_voxel_supported(environment, x, y, z):
"""Checks if a voxel is supported by other voxels below or beside it."""
if z == 0: # Ground level is always supported
return True
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0,1]: # check neighbors below and beside.
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type != AIR:
return True # Supported if a neighboring voxel exists
return False # Not supported
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if self.carrying_material:
... (Find best place to deposit material using neighboring voxel count as before)
elif random.random() < 0.02: # tunnel digging
... (Digging logic - similar as before)
physics check: (only check nest material)
if environment[x,y,z].type == NEST_MATERIAL: # Only check nest voxels
if not is_voxel_supported(environment, x, y, z):
environment[x, y, z].type = AIR # Remove unsupported voxel
In the main loop:
def main():
... (initialization)
while running:
... (event handling, camera, ant movement, drawing)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x,y,z].evaporate_pheromone() # evaporate pheromones
ant.build_nest(environment) # Let the ant potentially build or modify the nest
Apply tunnel view... (same as before)
...
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (other code)
def apply_physics(environment):
"""Applies simple physics to the environment (falling voxels)."""
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[1]-1, 0, -1): # Iterate from top to bottom
if environment[x, y, z].type != AIR and not is_voxel_supported(environment, x, y, z):
Find the lowest empty spot below
nz = z - 1
while nz >= 0 and environment[x, y, nz].type != AIR:
nz -=1
if nz >=0:
move to empty spot
environment[x,y,nz].type = environment[x,y,z].type #copy type
environment[x, y, z].type = AIR # Remove original voxel
def is_voxel_supported(environment, x, y, z):
"""Checks if a voxel is supported by neighbors below or beside."""
if z == 0:
return True
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z - 1 # Only check voxels below and beside
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type != AIR:
return True
return False
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if self.carrying_material:
... (Find best place to deposit material using neighboring voxel count as before)
elif random.random() < 0.02: # tunnel digging
if environment[x,y,z].type == SOIL: # only remove soil (not other nest materials)
if random.random() < 0.2: # reduce possibility of digging for larger chambers
environment[x, y, z].type = AIR
... other functions
In the main loop:
def main():
... (initialization)
while running:
... (event handling, camera, ant movement, drawing)
Apply physics:
apply_physics(environment)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x,y,z].evaporate_pheromones() # evaporate pheromones
Apply tunnel view... (same as before)
...
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (other code)
def apply_physics(environment, x, y, z): # Only apply physics around the changed voxel
"""Applies simple physics to the environment (falling voxels) around a specific location."""
for dx in [-2, -1, 0, 1, 2]: # expand range a little bit
for dy in [-2, -1, 0, 1, 2]:
for dz in range(z + 2, max(0,z-3),-1): # only check area above the changed voxel (2 voxels) and area below (3 Voxel) the changed voxel
nx, ny, nz = x + dx, y + dy, dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type != AIR and not is_voxel_supported(environment, nx, ny, nz):
Find the lowest empty spot below
lnz = nz - 1
while lnz >= 0 and environment[nx, ny, lnz].type != AIR:
lnz -= 1
if lnz >= 0:
environment[nx, ny, lnz].type = environment[nx, ny, nz].type # Copy type
environment[nx, ny, nz].type = AIR # Remove original voxel
def is_voxel_supported(environment, x, y, z):
... (same as before)
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if self.carrying_material:
Find best place to deposit material using neighboring voxel count
... same as before
if random.random() < 0.2:
environment[x, y, z].type = AIR
apply_physics(environment, x, y, z) # apply physics after removing material
... other functions
In the main loop:
def main():
... (initialization)
while running:
... (event handling, camera, ant movement, drawing)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x,y,z].evaporate_pheromones()
Apply tunnel view... (same as before)
...
---!!!BREAK!!!---
def generate_environment(grid, voxel_types):
Terrain generation with OpenSimplex and varying heights
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
height = int(opensimplex.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4) # Adjust scale and height as needed
for z in range(height):
voxel_types[x, y, z] = SOIL
Generate objects with varying sizes and materials
def generate_object(material, min_size, max_size):
size = random.randint(min_size, max_size)
x = random.randint(0, ENV_SIZE[0] - size)
y = random.randint(0, ENV_SIZE[1] - size)
z = int(opensimplex.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4) # Place objects on the terrain
for dx in range(size):
for dy in range(size):
if 0 <= x + dx < ENV_SIZE[0] and 0 <= y + dy < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x + dx, y + dy, z] = material
Example object generation:
for _ in range(5): # Generate a few larger stones
generate_object(SOIL, 5, 15)
for _ in range(20): # Generate more smaller stones
generate_object(SOIL, 2, 5)
... generate other objects (leaves, sticks, etc.) with varying parameters
height_noise = opensimplex.OpenSimplex(seed=random.randint(0,10000))
moisture_noise = opensimplex.OpenSimplex(seed=random.randint(0,10000)) # Separate noise for moisture
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
height = int(height_noise.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4)
moisture = moisture_noise.noise2(x / 40, y / 40) # Lower frequency for larger moisture variations
for z in range(height):
if moisture < -0.5: # Example: Drier areas get different soil
voxel_types[x, y, z] = SOIL # SAND (you'll need to define this type)
else:
voxel_types[x, y, z] = SOIL
2. Procedural object generation (example: simple plant with L-system like logic):
if moisture > 0 and random.random() < 0.1: # plant grows at higher moisture and a given chance:
plant_height = random.randint(2, 5)
for pz in range(height, height + plant_height):
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= pz < ENV_SIZE[2]:
voxel_types[x, y, pz] = FOOD # Or a PLANT type, if you define one
3. Generate rocks (example with random shape):
for _ in range(10): # Number of rocks
center_x = random.randint(0, ENV_SIZE[0] - 1)
center_y = random.randint(0, ENV_SIZE[1] - 1)
center_z = int(height_noise.noise2(center_x / 20, center_y / 20) * 10 + ENV_SIZE[2] / 4) # height according to terrain
radius = random.randint(2, 5)
irregularity = random.uniform(0.5, 1.5) # For more natural shape
for x in range(center_x - radius, center_x + radius + 1):
for y in range(center_y - radius, center_y + radius + 1):
for z in range(center_z, center_z + radius + 1):
distance = np.linalg.norm(np.array([x, y, z]) - np.array([center_x, center_y, center_z]))
if distance <= radius * irregularity * random.random(): # Add irregularity
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x,y,z] = SOIL
def generate_environment(grid, voxel_types):
... (terrain and moisture generation - same as before)
def place_object(obj, x, y, z, scale=1, rotation=0): # Add parameters for scale and rotation
Implement rotation and scaling here (using matrix transformations if needed) and then place voxels
...
Place grass blades (denser for small grass)
for _ in range(200): # Number of small grass blades
x = random.randint(0, ENV_SIZE[0] - 1)
y = random.randint(0, ENV_SIZE[1] - 1)
z = # Height based on the terrain (same as before)
place_object(small_grass, x, y, z, scale=random.uniform(0.8, 1.2), rotation=random.randint(0, 360))
for _ in range(50): # Number of larger grass blades (less dense)
... (similar placement logic)
Place dandelions (less frequent)
for _ in range(20): # number of dandelions
... (place dandelion with variations in height using scaling).
... (generate rocks, etc. - similar as before)
for dz in range(int(new_pos[2])+1, int(new_pos[2])-5, -1): # Search for a voxel to stand on
x, y = int(new_pos[0]), int(new_pos[1])
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= dz < ENV_SIZE[2]:
if environment[x, y, dz].type != AIR: # Use type != AIR to find suitable surface
target_height = dz # set Height
if environment[x,y,dz].type == FOOD: # climb food (simplified)
new_pos[2] += 0.1 # move upwards
break # voxel to stand on found, so stop searching
new_pos[2] = target_height
... (check environment bounds and set position as before).
Inverse Kinematics for legs with 3D collision detection
... (same as before)
---!!!BREAK!!!---
class Ant:
... other methods
def move(self, environment):
Local exploration:
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
Check for pheromones in the vicinity
pheromone_direction = np.zeros(3)
search_radius = 3 # Adjust search radius
for dx in range(-search_radius, search_radius+1):
for dy in range(-search_radius, search_radius+1):
nx, ny = x + dx, y + dy
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1]:
pheromone_strength = environment[nx, ny, z].food_pheromone
if pheromone_strength > FOOD_PHEROMONE_THRESHOLD: # adjust threshold if needed
pheromone_direction += (np.array([nx,ny,z]) - self.position) * pheromone_strength # move towards the source
2. Random movement with direction changes
elif random.random() < 0.2: # Adjust probability for direction changes
self.direction = np.random.rand(3) - 0.5 # random direction change
self.direction[2] = 0 # Prevent vertical movement for now
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == AIR:
4. Height adjustment and gravity (simplified):
target_height = 0
for dz in range(nz+1, nz-5, -1):
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= dz < ENV_SIZE[2]:
if environment[nx,ny,dz].type != AIR:
target_height = dz
break
elif environment[nx,ny,nz].type == FOOD: # climb if possible
new_pos[2]+=0.1 # climbing logic (adjust speed as needed)
self.position = new_pos
... (rest of the movement logic)
... (rest of the ant class - same as before)
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (other code - same as before)
class Ant:
... (other methods)
def move(self, environment):
... (pheromone influence - same as before)
if np.linalg.norm(pheromone_direction) == 0 and random.random() < 0.1: # Low probability if no pheromones and random chance
sun_direction = np.array([1, 0, 0]) # Replace with actual sun direction if available
self.direction += sun_direction * 0.1 # Move slightly towards the sun
... (Inverse Kinematics for legs - same as before)
... (rest of ant class)
---!!!BREAK!!!---
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
... (Environment parameters, voxel types, etc.)
... (Ant class, Colony class, Voxel class)
Day/Night cycle parameters
DAY_LENGTH = 1000 # Number of simulation steps for a full day/night cycle
current_time = 0
Pheromone visualization
def generate_environment(grid, voxel_types):
... (same as before, using OpenSimplex, microhabitats, object placement with variations)
def draw_environment(grid, tunnel_mode, camera_pos, colony, show_pheromones): # Add show_pheromones parameter
global environment
... (same as before, rendering terrain and objects)
if show_pheromones:
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
pheromone_strength = environment[x, y, z].food_pheromone
if pheromone_strength > 0:
pheromone_color = (0, 255, 0, int(pheromone_strength* 255)) # green, adjust alpha based on strength.
Draw pheromone visualization (e.g., a small square or circle at the voxel position).
sun_direction = np.array([sun_x, sun_y, 0.5]) # Simulate sun direction (adjust z as needed)
sun_direction = sun_direction / np.linalg.norm(sun_direction)
if sun_x < 0: # if sun is "below" horizon
sun_direction *= 0.1 # moon light, low intensity
... (Rotate Camera - same as before)
Ant logic
for ant in colony.ants:
ant.forage(environment)
Sun-based orientation (using the calculated sun_direction):
if np.linalg.norm(ant.pheromone_direction) == 0 and random.random() < 0.1:
ant.direction += sun_direction * 0.1 # Adjust influence as needed
ant.leg_positions = [[]] * NUM_LEGS # Reset leg positions
ant.move(environment)
ant.build_nest(environment)
ant.draw(screen)
... (Apply tunnel view if enabled - same as before)
class Voxel: # Add pheromone types to voxel
def init(self, voxel_type):
... other attributes
self.pheromones = [0.0] * 2 # food and home pheromones
def evaporate_pheromones(self):
self.pheromones = [max(0, p - PHEROMONE_EVAPORATION_RATE) for p in self.pheromones]
def generate_environment(grid, voxel_types):
... (same as before, using OpenSimplex, microhabitats, object placement with variations)
def draw_environment(grid, tunnel_mode, camera_pos, colony, show_pheromones):
... (same as before, rendering terrain and objects)
if show_pheromones:
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
for pheromone_type in [FOOD_PHEROMONE, HOME_PHEROMONE]:
pheromone_strength = environment[x, y, z].pheromones[pheromone_type]
if pheromone_strength > 0:
pheromone_color = (0, 255, 0) if pheromone_type == FOOD_PHEROMONE else (0, 0, 255) # green for food, blue for home
screen_x, screen_y = project_to_screen(np.array([x, y, z]))
def main():
... (initialization of pygame, pyvista, environment, colony)
running = True
while running:
... (event handling)
Mouse click handling for pheromone button
if event.type == pygame.MOUSEBUTTONDOWN:
if show_pheromones_button.collidepoint(event.pos):
show_pheromones = not show_pheromones
Day/Night Cycle and Sun/Moon position:
... (same as before)
Render the button
for ant in colony.ants:
... (foraging - same as before)
Pheromone handling (dynamic and by type):
x,y,z = int(ant.position[0]), int(ant.position[1]), int(ant.position[2])
Home pheromone: drop near nest entrance
if np.linalg.norm(ant.position - colony.nest_entrance) < 5:
environment[x,y,z].pheromones[HOME_PHEROMONE] = 1.0
Food pheromone: drop when carrying food
if ant.has_food:
environment[x, y, z].pheromones[FOOD_PHEROMONE] = max(environment[x,y,z].pheromones[FOOD_PHEROMONE], 1 - (np.linalg.norm(ant.position - colony.nest_entrance) / 50)) # Pheromone strength decreases with distance from nest
Sun-based orientation (only during daytime):
if sun_direction[2] > 0: # is sun above the horizon?
if np.linalg.norm(ant.pheromone_direction) == 0 and random.random() < 0.1: # Use sun for orientation only if no pheromones detected.
ant.direction += sun_direction * 0.1 # slightly turn towards sun
... (rest of ant.move(), 3d climbing - same logic as before or as modified, integrate the climbing logic for all objects if needed)
ant.leg_positions = [[]] * NUM_LEGS # Reset leg positions
ant.move(environment)
ant.build_nest(environment)
ant.draw(screen)
... (Apply tunnel view if enabled - same as before)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x, y, z].evaporate_pheromones()
while running:
... (event handling, camera, etc.)
Ant logic
for ant in colony.ants:
ant.forage(environment)
Pheromone handling (dynamic and by type):
x, y, z = int(ant.position[0]), int(ant.position[1]), int(ant.position[2])
Home pheromone: drop near nest entrance
if np.linalg.norm(ant.position - colony.nest_entrance) < 5:
environment[x, y, z].pheromones[HOME_PHEROMONE] = 1.0
Food pheromone: drop when carrying food
if ant.has_food:
environment[x, y, z].pheromones[FOOD_PHEROMONE] = 1.0 # Constant strength
... (rest of ant logic, movement, drawing)
Pheromone evaporation (after all ants have moved)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x, y, z].evaporate_pheromones()
... (draw environment, etc.)
---!!!BREAK!!!---
... (other code)
class Voxel:
def init(self, voxel_type):
... (other attributes)
self.pheromones = [0.0] * 2 # food and home pheromones
def evaporate_pheromones(self):
self.pheromones = [max(0, p - (PHEROMONE_EVAPORATION_RATE / (1+p))) for p in self.pheromones] # slower evaporation for stronger trails
... (other code)
def main():
... (initialization)
while running:
... (event handling, camera, etc.)
for ant in colony.ants:
... (foraging, etc.)
Pheromone handling (dynamic and accumulating):
x, y, z = int(ant.position[0]), int(ant.position[1]), int(ant.position[2])
if np.linalg.norm(ant.position - colony.nest_entrance) < 5:
environment[x, y, z].pheromones[HOME_PHEROMONE] = min(environment[x,y,z].pheromones[HOME_PHEROMONE] +1, 10) # accumulate up to a value (e.g., 10)
if ant.has_food:
environment[x, y, z].pheromones[FOOD_PHEROMONE] = min(environment[x,y,z].pheromones[FOOD_PHEROMONE]+1, 10) # accumulate up to a value (e.g., 10)
... (rest of ant logic, movement, drawing)
... (draw environment, apply tunnel view, etc.)
Hier ist das vollständige Python-Skript, das alle bereitgestellten Code-Snippets integriert. Dieses Skript stellt eine Simulation dar, in der Ameisen eine Kolonie bilden, Nahrung suchen und Nester bauen. Die Umgebung wird mit Voxeln dargestellt und visuell mit Pygame und PyVista gerendert. Zusätzlich umfasst die Simulation Pheromonspuren, einen Tag-Nacht-Zyklus und einfache physikalische Effekte.
python
import pygame
import random
import numpy as np
import opensimplex
import math
import pyvista as pv
# -----------------------
# Umgebungseinstellungen
# -----------------------
ENV_SIZE = (100, 100, 50) # Größe der Umgebung (Breite, Höhe, Tiefe)
VOXEL_SIZE = 0.2 # Größe eines Voxels
INITIAL_ANTS = 20 # Anfangszahl der Ameisen
FOOD_SPAWN_RATE = 0.05 # Rate, mit der Nahrung erscheint
PHEROMONE_EVAPORATION_RATE = 0.01 # Rate, mit der Pheromone verdunsten
FOOD_PHEROMONE_THRESHOLD = 0.5 # Schwellenwert für Ameisen, um Nahrungspfeiferomone zu folgen
# Voxeltypen
AIR = 0
SOIL = 1
FOOD = 2
NEST_MATERIAL = 3
# Ameisenparameter
ANT_SIZE = 0.5 # Größe einer Ameise
LEG_LENGTH = ANT_SIZE / 3
ANTENNA_LENGTH = ANT_SIZE / 2
NUM_LEGS = 6
NUM_ANTENNA_SEGMENTS = 3
NUM_BODY_SEGMENTS = 3
MAX_LEG_STRETCH = LEG_LENGTH * 1.2
# Detailabschneidungsdistanz
CULL_DISTANCE = 50
# Gravitation (für zukünftige Verwendung)
GRAVITY = np.array([0, 0, -0.1])
# Tunnelmodus
tunnel_mode = False
# Tag/Nacht-Zyklusparameter
DAY_LENGTH = 1000 # Anzahl der Simulationsschritte für einen vollständigen Tag/Nacht-Zyklus
current_time = 0
# Pheromonarten
FOOD_PHEROMONE = 0
HOME_PHEROMONE = 1
# Pheromonvisualisierung
PHEROMONE_VISUALIZATION_SCALE = 20
# UI-Elemente
button_font = pygame.font.SysFont(None, 30)
# -----------------------
# Klassen
# -----------------------
class Voxel:
def __init__(self, voxel_type):
self.type = voxel_type
self.pheromones = [0.0] * 2 # [Food Pheromone, Home Pheromone]
self.alpha = 255 # Standard: Opaque
def evaporate_pheromones(self):
self.pheromones = [
max(0, p - (PHEROMONE_EVAPORATION_RATE / (1 + p)))
for p in self.pheromones
]
class Ant:
def __init__(self, position, colony):
self.position = np.array(position, dtype=float)
self.colony = colony
self.body_segments = [
np.array(position) + np.array([i * ANT_SIZE / 3, 0, 0])
for i in range(NUM_BODY_SEGMENTS)
]
self.body_rotations = [0.0] * (NUM_BODY_SEGMENTS - 1)
self.leg_angles = [0.0] * NUM_LEGS
self.antenna_angles = [0.0] * NUM_ANTENNA_SEGMENTS * 2
self.leg_targets = [np.array(position) for _ in range(NUM_LEGS)]
self.antenna_targets = [np.zeros(3) for _ in range(2)]
self.leg_positions = [[] for _ in range(NUM_LEGS)]
self.antenna_positions = [np.zeros(3) for _ in range(2)]
self.has_food = False
self.carrying_material = False
self.direction = np.random.rand(3) - 0.5
self.direction[2] = 0 # Verhindert vertikale Bewegung
if np.linalg.norm(self.direction) > 0:
self.direction /= np.linalg.norm(self.direction)
self.speed = 0.1 # Bewegungsgeschwindigkeit
def move(self, environment, sun_direction):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
pheromone_direction = np.zeros(3)
# Pheromone-Einfluss auf die Bewegung
search_radius = 3
for dx in range(-search_radius, search_radius + 1):
for dy in range(-search_radius, search_radius + 1):
nx, ny = x + dx, y + dy
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1]:
pheromone_strength = environment[nx, ny, z].pheromones[FOOD_PHEROMONE]
if pheromone_strength > FOOD_PHEROMONE_THRESHOLD and not self.has_food:
pheromone_direction += (np.array([nx, ny, z]) - self.position) * pheromone_strength
# Kombination aus zufälligem Lauf und Pheromoneinfluss
if np.linalg.norm(pheromone_direction) > 0:
self.direction = pheromone_direction / np.linalg.norm(pheromone_direction)
elif random.random() < 0.2:
self.direction = np.random.rand(3) - 0.5
self.direction[2] = 0
if np.linalg.norm(self.direction) > 0:
self.direction /= np.linalg.norm(self.direction)
new_pos = self.position + self.direction * self.speed
nx, ny, nz = int(new_pos[0]), int(new_pos[1]), int(new_pos[2])
# Kollisions- und Höhenanpassung
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
voxel = environment[nx, ny, nz]
if voxel.type == AIR:
# Höhenanpassung
target_height = -1
for dz in range(nz + 1, nz - 5, -1):
if 0 <= dz < ENV_SIZE[2]:
if environment[nx, ny, dz].type != AIR:
target_height = dz
break
if target_height != -1:
new_pos[2] = target_height
self.position = new_pos
elif voxel.type == FOOD:
new_pos[2] += 0.1
self.position = new_pos
elif voxel.type == FOOD:
new_pos[2] += 0.1
self.position = new_pos
else:
# Hindernis erkannt: Untersuchung mit Antennen
for i in range(2):
self.antenna_targets[i] = np.array([nx, ny, nz]) + np.random.rand(3) * ANT_SIZE - ANT_SIZE / 2
antenna_base = self.body_segments[-1]
self.antenna_positions[i] = 0.9 * self.antenna_positions[i] + 0.1 * (self.antenna_targets[i] - antenna_base)
if random.random() < 0.5:
# 50% Chance zum Klettern
new_pos[2] += 0.1
else:
# 50% Chance zur zufälligen Drehung
self.direction = np.random.rand(3) - 0.5
self.direction[2] = 0
if np.linalg.norm(self.direction) > 0:
self.direction /= np.linalg.norm(self.direction)
else:
# Neue Position außerhalb der Umgebung: Anpassen innerhalb der Grenzen
self.position = np.clip(new_pos, [0, 0, 0], [ENV_SIZE[0]-1, ENV_SIZE[1]-1, ENV_SIZE[2]-1])
# Körpersegmentpositionen basierend auf Bewegung aktualisieren
for i in range(1, NUM_BODY_SEGMENTS):
self.body_segments[i] = self.body_segments[0] + np.array([
i * ANT_SIZE / 3 * math.cos(self.direction[0]),
i * ANT_SIZE / 3 * math.sin(self.direction[1]),
0
])
# Inverse Kinematik für Beine mit 3D-Kollisionsdetektion
for i in range(NUM_LEGS):
start_pos = self.body_segments[i // (NUM_LEGS // NUM_BODY_SEGMENTS)]
if np.linalg.norm(start_pos - self.leg_targets[i]) > MAX_LEG_STRETCH:
# Neue Zielposition mit Kollisionsdetektion finden
ground_z = -1
for dz in range(int(start_pos[2]), -1, -1):
if 0 <= start_pos[0] < ENV_SIZE[0] and 0 <= start_pos[1] < ENV_SIZE[1] and 0 <= dz < ENV_SIZE[2]:
if environment[int(start_pos[0]), int(start_pos[1]), dz].type == SOIL:
ground_z = dz
break
if ground_z != -1:
self.leg_targets[i] = np.array([int(start_pos[0]), int(start_pos[1]), ground_z])
else:
self.leg_targets[i] = start_pos + self.direction * LEG_LENGTH
# Vereinfachte IK-Berechnung für zwei Beinsegmente
target = self.leg_targets[i]
dist_to_target = np.linalg.norm(start_pos - target)
if dist_to_target > 0:
mid_pos = start_pos + (target - start_pos) / 2 + np.array([
0, 0, LEG_LENGTH / 2 * (1 - (dist_to_target / MAX_LEG_STRETCH))
])
self.leg_positions[i] = [start_pos, mid_pos, target]
# 3D-Körperrotation (vereinfachte Berechnung)
for i in range(NUM_BODY_SEGMENTS - 1):
segment_direction = self.body_segments[i + 1] - self.body_segments[i]
angle = math.atan2(segment_direction[1], segment_direction[0])
self.body_rotations[i] = angle
# Erweiterte Antennenanimation (Erkundungsverhalten)
for i in range(2):
# Vereinfachte Erkundung durch zufälliges Ziel um die Ameise herum
self.antenna_targets[i] = self.body_segments[-1] + np.random.rand(3) * ANT_SIZE - ANT_SIZE / 2
# "Dämpfte" Bewegung der Antennen zum Zielpunkt
antenna_base = self.body_segments[-1]
self.antenna_positions[i] = 0.9 * self.antenna_positions[i] + 0.1 * (self.antenna_targets[i] - antenna_base)
# Sonnenausrichtung (nur während des Tages)
if sun_direction[2] > 0:
if np.linalg.norm(pheromone_direction) == 0 and random.random() < 0.1:
self.direction += sun_direction * 0.1
if np.linalg.norm(self.direction) > 0:
self.direction /= np.linalg.norm(self.direction)
def forage(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if environment[x, y, z].type == FOOD:
self.has_food = True
environment[x, y, z].type = AIR # Nahrung verbrauchen
elif self.has_food and (x, y, z) == self.colony.nest_entrance:
self.has_food = False
self.colony.food_count += 1
# Pheromon am Nisteneingang ablegen
environment[x, y, z].pheromones[HOME_PHEROMONE] = min(
environment[x, y, z].pheromones[HOME_PHEROMONE] + 1, 10
)
# Pheromone auf Nahrungspfad ablegen
if self.has_food:
environment[x, y, z].pheromones[FOOD_PHEROMONE] = min(
environment[x, y, z].pheromones[FOOD_PHEROMONE] + 1, 10
)
def build_nest(self, environment):
x, y, z = int(self.position[0]), int(self.position[1]), int(self.position[2])
if self.carrying_material:
# Geeigneten Platz zum Ablegen des Materials suchen
best_nx, best_ny, best_nz = -1, -1, -1
best_neighbors = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type == AIR:
current_neighbors = 0
for nnx in [-1, 0, 1]:
for nny in [-1, 0, 1]:
for nnz in [-1, 0, 1]:
annx, anny, annz = nx + nnx, ny + nny, nz + nnz
if 0 <= annx < ENV_SIZE[0] and 0 <= anny < ENV_SIZE[1] and 0 <= annz < ENV_SIZE[2]:
if environment[annx, anny, annz].type == NEST_MATERIAL:
current_neighbors += 1
if current_neighbors > best_neighbors:
best_neighbors = current_neighbors
best_nx, best_ny, best_nz = nx, ny, nz
if best_nx != -1:
environment[best_nx, best_ny, best_nz].type = NEST_MATERIAL
self.carrying_material = False
# Physik nach dem Ablegen anwenden
apply_physics(environment, best_nx, best_ny, best_nz)
elif random.random() < 0.02:
# Tunnel graben mit geringer Wahrscheinlichkeit
if environment[x, y, z].type == SOIL:
if random.random() < 0.2:
environment[x, y, z].type = AIR
apply_physics(environment, x, y, z)
def draw(self, screen, camera_pos, zoom):
# Detailabschneidung
distance_to_camera = np.linalg.norm(self.body_segments[0] - camera_pos)
if distance_to_camera > CULL_DISTANCE:
# Vereinfachte Darstellung (z.B. nur Körper)
screen_x, screen_y = project_to_screen(self.body_segments[0], zoom)
pygame.draw.circle(screen, (255, 0, 0), (int(screen_x), int(screen_y)), int(ANT_SIZE / zoom))
return
# Gedrehte Körpersegmente zeichnen
for i in range(NUM_BODY_SEGMENTS):
screen_x, screen_y = project_to_screen(self.body_segments[i], zoom)
segment_size = ANT_SIZE / 2 / zoom * (1 - (i / NUM_BODY_SEGMENTS) * 0.5)
pygame.draw.circle(screen, (255, 0, 0), (int(screen_x), int(screen_y)), int(segment_size))
if i > 0:
prev_screen_x, prev_screen_y = project_to_screen(self.body_segments[i - 1], zoom)
pygame.draw.line(
screen,
(255, 0, 0),
(int(prev_screen_x), int(prev_screen_y)),
(int(screen_x), int(screen_y)),
int(ANT_SIZE / 3 / zoom)
)
# Beine (IK-basiert) zeichnen
for i in range(NUM_LEGS):
if len(self.leg_positions[i]) > 0:
start_pos, mid_pos, target = self.leg_positions[i]
screen_x1, screen_y1 = project_to_screen(start_pos, zoom)
screen_x2, screen_y2 = project_to_screen(mid_pos, zoom)
screen_x3, screen_y3 = project_to_screen(target, zoom)
pygame.draw.line(screen, (0, 0, 0), (int(screen_x1), int(screen_y1)), (int(screen_x2), int(screen_y2)), int(ANT_SIZE / 6 / zoom))
pygame.draw.line(screen, (0, 0, 0), (int(screen_x2), int(screen_y2)), (int(screen_x3), int(screen_y3)), int(ANT_SIZE / 6 / zoom))
# Antennen zeichnen
for i in range(2):
screen_x1, screen_y1 = project_to_screen(self.body_segments[-1], zoom)
antenna_tip = self.body_segments[-1] + self.antenna_positions[i]
screen_x2, screen_y2 = project_to_screen(antenna_tip, zoom)
pygame.draw.line(
screen,
(0, 0, 0),
(int(screen_x1), int(screen_y1)),
(int(screen_x2), int(screen_y2)),
int(ANT_SIZE / 6 / zoom)
)
class Colony:
def __init__(self, nest_entrance):
self.nest_entrance = nest_entrance # Tupel (x, y, z)
self.ants = []
self.food_count = 0
# -----------------------
# Funktionen
# -----------------------
def is_voxel_supported(environment, x, y, z):
"""Überprüft, ob ein Voxel von anderen Voxeln darunter oder daneben unterstützt wird."""
if z == 0:
return True # Der Boden ist immer unterstützt
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
for dz in [-1, 0, 1]:
nx, ny, nz = x + dx, y + dy, z + dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type != AIR:
return True # Unterstützt durch einen Nachbar-Voxel
return False # Nicht unterstützt
def apply_physics(environment, x, y, z):
"""Wendet einfache Physik auf die Umgebung an (fallende Voxels) um eine spezifische Position."""
for dx in [-2, -1, 0, 1, 2]:
for dy in [-2, -1, 0, 1, 2]:
for dz in range(z + 2, max(-1, z - 3), -1): # Bereich oberhalb und unterhalb der geänderten Position
nx, ny, nz = x + dx, y + dy, dz
if 0 <= nx < ENV_SIZE[0] and 0 <= ny < ENV_SIZE[1] and 0 <= nz < ENV_SIZE[2]:
if environment[nx, ny, nz].type != AIR and not is_voxel_supported(environment, nx, ny, nz):
# Niedrigste freie Position finden
lnz = nz - 1
while lnz >= 0 and environment[nx, ny, lnz].type != AIR:
lnz -= 1
if lnz >= 0:
environment[nx, ny, lnz].type = environment[nx, ny, nz].type
environment[nx, ny, nz].type = AIR
def generate_environment(grid, voxel_types):
"""Generiert die Umgebung mit Gelände, Feuchtigkeitsverteilungen und Objekten."""
height_noise = opensimplex.OpenSimplex(seed=random.randint(0, 10000))
moisture_noise = opensimplex.OpenSimplex(seed=random.randint(0, 10000)) # Separater Lärm für Feuchtigkeit
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
height = int(height_noise.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4)
moisture = moisture_noise.noise2(x / 40, y / 40) # Niedrigere Frequenz für größere Feuchtigkeitsvariationen
for z in range(height):
if moisture < -0.5:
voxel_types[x, y, z] = SOIL # Beispiel: Trockenere Gebiete
else:
voxel_types[x, y, z] = SOIL
# Procedurale Objektgenerierung (z.B. Pflanzen)
if moisture > 0 and random.random() < 0.1:
plant_height = random.randint(2, 5)
for pz in range(height, min(height + plant_height, ENV_SIZE[2])):
voxel_types[x, y, pz] = FOOD # Beispiel: Pflanzen als FOOD definiert
# Felsen generieren (unregelmäßige Formen)
for _ in range(10): # Anzahl der Felsen
center_x = random.randint(0, ENV_SIZE[0] - 1)
center_y = random.randint(0, ENV_SIZE[1] - 1)
center_z = int(height_noise.noise2(center_x / 20, center_y / 20) * 10 + ENV_SIZE[2] / 4)
radius = random.randint(2, 5)
irregularity = random.uniform(0.5, 1.5)
for x in range(center_x - radius, center_x + radius + 1):
for y in range(center_y - radius, center_y + radius + 1):
for z in range(center_z, center_z + radius + 1):
distance = np.linalg.norm(np.array([x, y, z]) - np.array([center_x, center_y, center_z]))
if distance <= radius * irregularity * random.random():
if 0 <= x < ENV_SIZE[0] and 0 <= y < ENV_SIZE[1] and 0 <= z < ENV_SIZE[2]:
voxel_types[x, y, z] = SOIL
# Gras und Dandelions platzieren
small_grass = np.array([
[[0, 0, 1]],
[[0, 0, 1]],
[[0, 0, 1]]
]) # 3 Voxels hoch
large_grass = np.array([
[[0, 0, 1]],
[[0, 0, 1]],
[[0, 0, 1]],
[[0, 0, 1]],
[[0, 0, 1]]
]) # 5 Voxels hoch
dandelion = np.array([
[[0, 1, 0]], # Blatt
[[0, 1, 0]], # Blatt
[[0, 0, 2]], # Gelbe Blume
[[0, 0, 2]]
]) # Leicht höhere Blume
def place_object(obj, x, y, z, scale=1, rotation=0):
"""Platziert ein Objekt mit Skalierung und Rotation."""
for dx in range(obj.shape[0]):
for dy in range(obj.shape[1]):
for dz in range(obj.shape[2]):
vx, vy, vz = obj[dx, dy, dz]
voxel_x = x + dx
voxel_y = y + dy
voxel_z = z + dz
if 0 <= voxel_x < ENV_SIZE[0] and 0 <= voxel_y < ENV_SIZE[1] and 0 <= voxel_z < ENV_SIZE[2]:
voxel_types[voxel_x, voxel_y, voxel_z] = voxel_z
# Gras platzieren
for _ in range(200): # Anzahl kleiner Grasbüschel
x = random.randint(0, ENV_SIZE[0] - 1)
y = random.randint(0, ENV_SIZE[1] - 1)
z = int(height_noise.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4)
place_object(small_grass, x, y, z, scale=random.uniform(0.8, 1.2), rotation=random.randint(0, 360))
for _ in range(50): # Anzahl großer Grasbüschel
x = random.randint(0, ENV_SIZE[0] - 1)
y = random.randint(0, ENV_SIZE[1] - 1)
z = int(height_noise.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4)
place_object(large_grass, x, y, z, scale=random.uniform(0.8, 1.2), rotation=random.randint(0, 360))
# Dandelions platzieren
for _ in range(20): # Anzahl Dandelions
x = random.randint(0, ENV_SIZE[0] - 1)
y = random.randint(0, ENV_SIZE[1] - 1)
z = int(height_noise.noise2(x / 20, y / 20) * 10 + ENV_SIZE[2] / 4)
place_object(dandelion, x, y, z, scale=random.uniform(0.8, 1.2), rotation=random.randint(0, 360))
grid.point_data["voxel_types"] = voxel_types.flatten()
return grid, voxel_types
def project_to_screen(position, zoom):
"""Einfache Projektion von 3D-Positionen auf den 2D-Bildschirm."""
relative_pos = position - camera_target
rotated_x = relative_pos[0] * math.cos(camera_rotation[1]) + relative_pos[2] * math.sin(camera_rotation[1])
rotated_z = -relative_pos[0] * math.sin(camera_rotation[1]) + relative_pos[2] * math.cos(camera_rotation[1])
screen_x = width / 2 + rotated_x * zoom * 10
screen_y = height / 2 + (relative_pos[1] * zoom * 10 - rotated_z * zoom * 5)
return screen_x, screen_y
def draw_voxel(voxel, position, zoom):
"""Zeichnet einen einzelnen Voxel auf dem Bildschirm."""
screen_x, screen_y = project_to_screen(position + VOXEL_SIZE / 2, zoom)
color = (255, 255, 255) # Standardfarbe
if voxel.type == SOIL:
color = (139, 69, 19) # Braun für Erde
elif voxel.type == FOOD:
color = (255, 255, 0) # Gelb für Nahrung
elif voxel.type == NEST_MATERIAL:
color = (128, 0, 128) # Violett für Nestmaterial
pygame.draw.rect(
screen,
color,
(int(screen_x), int(screen_y), int(VOXEL_SIZE * zoom), int(VOXEL_SIZE * zoom)),
0
)
def draw_environment(grid, tunnel_mode, camera_pos, colony, show_pheromones):
"""Zeichnet die gesamte Umgebung."""
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
voxel = environment[x, y, z]
if tunnel_mode and voxel.type == NEST_MATERIAL:
voxel_center = np.array([x, y, z]) + VOXEL_SIZE / 2
camera_to_voxel = voxel_center - camera_pos
distance_to_nest_center = np.linalg.norm(camera_pos - colony.nest_entrance)
if distance_to_nest_center < 10:
voxel.alpha = 255 # Opaque inside the nest
else:
voxel.alpha = 128 # Transparent outside the nest
draw_voxel(voxel, np.array([x, y, z]) * VOXEL_SIZE, zoom)
if show_pheromones:
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
voxel = environment[x, y, z]
for pheromone_type in [FOOD_PHEROMONE, HOME_PHEROMONE]:
pheromone_strength = voxel.pheromones[pheromone_type]
if pheromone_strength > 0:
if pheromone_type == FOOD_PHEROMONE:
pheromone_color = (0, 255, 0) # Grün für Nahrung
else:
pheromone_color = (0, 0, 255) # Blau für Heim
screen_x, screen_y = project_to_screen(np.array([x, y, z]) * VOXEL_SIZE, zoom)
pygame.draw.circle(
screen,
pheromone_color,
(int(screen_x), int(screen_y)),
int(PHEROMONE_VISUALIZATION_SCALE * pheromone_strength / zoom)
)
def camera_controls(plotter, event):
"""Steuert die Kamerabewegung basierend auf Benutzereingaben."""
global zoom, camera_pos, camera_target
if event.type == pygame.MOUSEWHEEL:
if event.y > 0: # Scrollen nach oben (hineinzoomen)
zoom *= 1.2
else:
zoom /= 1.2
zoom = max(0.1, zoom)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[2]: # Rechte Maustaste
dx, dy = event.rel
camera_pos[0] += dx / zoom * 0.1
camera_pos[1] += dy / zoom * 0.1
camera_target[0] += dx / zoom * 0.1
camera_target[1] += dy / zoom * 0.1
def main():
global grid, voxel_types, camera_pos, camera_target, zoom, camera_rotation, environment, tunnel_mode, width, height
# Pygame-Initialisierung
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
# Kamera-Parameter
camera_pos = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, ENV_SIZE[2] / 2])
camera_target = np.array([ENV_SIZE[0] / 2, ENV_SIZE[1] / 2, 0])
zoom = 1.0
camera_rotation = np.array([0.0, 0.0]) # Rotation um x- und y-Achse
# PyVista-Initialisierung
pv.set_plot_theme("document")
plotter = pv.Plotter(off_screen=True)
grid = pv.UniformGrid()
grid.dimensions = np.array(ENV_SIZE) + 1
grid.spacing = (VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE)
grid.origin = (0, 0, 0)
voxel_types = np.zeros(ENV_SIZE, dtype=int)
grid.point_data["voxel_types"] = voxel_types.flatten()
plotter.add_mesh(grid, name="environment")
# Umgebung und Kolonie initialisieren
grid, voxel_types = generate_environment(grid, voxel_types)
environment = np.empty(ENV_SIZE, dtype=object)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x, y, z] = Voxel(voxel_types[x, y, z])
nest_entrance = (ENV_SIZE[0] // 2, ENV_SIZE[1] // 2, 0)
colony = Colony(nest_entrance)
# Anfangs-Ameisen erstellen
for _ in range(INITIAL_ANTS):
x, y = random.randint(0, ENV_SIZE[0] - 1), random.randint(0, ENV_SIZE[1] - 1)
colony.ants.append(Ant((x, y, 0), colony))
# UI-Button für Pheromonanzeige
show_pheromones_button = pygame.Rect(width - 150, 10, 140, 40)
show_pheromones = False
running = True
while running:
# Ereignisbehandlung
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
camera_controls(plotter, event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
tunnel_mode = not tunnel_mode
if event.key == pygame.K_p:
show_pheromones = not show_pheromones
if event.type == pygame.MOUSEBUTTONDOWN:
if show_pheromones_button.collidepoint(event.pos):
show_pheromones = not show_pheromones
# Tag/Nacht-Zyklus und Sonnenposition
current_time = (current_time + 1) % DAY_LENGTH
sun_angle = (current_time / DAY_LENGTH) * 2 * math.pi
sun_x = math.sin(sun_angle)
sun_y = math.cos(sun_angle)
sun_direction = np.array([sun_x, sun_y, 0.5])
sun_direction = sun_direction / np.linalg.norm(sun_direction)
if sun_x < 0:
sun_direction *= 0.1 # Mondlicht, geringe Intensität
# Bildschirm löschen
screen.fill((0, 0, 0))
# Kameraposition aktualisieren
plotter.camera_position = [
(camera_pos[0] * VOXEL_SIZE, camera_pos[1] * VOXEL_SIZE, camera_pos[2] * VOXEL_SIZE),
(camera_target[0] * VOXEL_SIZE, camera_target[1] * VOXEL_SIZE, camera_target[2] * VOXEL_SIZE),
(0, 0, 1)
]
# Ameisenlogik
for ant in colony.ants:
ant.forage(environment)
ant.leg_positions = [[] for _ in range(NUM_LEGS)]
ant.move(environment, sun_direction)
ant.build_nest(environment)
# Pheromonhandling
x, y, z = int(ant.position[0]), int(ant.position[1]), int(ant.position[2])
if np.linalg.norm(ant.position - colony.nest_entrance) < 5:
environment[x, y, z].pheromones[HOME_PHEROMONE] = min(
environment[x, y, z].pheromones[HOME_PHEROMONE] + 1, 10
)
if ant.has_food:
environment[x, y, z].pheromones[FOOD_PHEROMONE] = min(
environment[x, y, z].pheromones[FOOD_PHEROMONE] + 1, 10
)
ant.draw(screen, camera_pos, zoom)
# Physik anwenden und Pheromone verdunsten
apply_physics(environment, 0, 0, 0) # Beispiel: Physik um (0,0,0)
for x in range(ENV_SIZE[0]):
for y in range(ENV_SIZE[1]):
for z in range(ENV_SIZE[2]):
environment[x, y, z].evaporate_pheromones()
# Umgebung zeichnen
draw_environment(grid, tunnel_mode, camera_pos, colony, show_pheromones)
# UI-Button zeichnen
pygame.draw.rect(screen, (100, 100, 100), show_pheromones_button)
text = button_font.render("Show Pheromones", True, (255, 255, 255))
screen.blit(text, (show_pheromones_button.x + 10, show_pheromones_button.y + 10))
# Bildschirmausgabe aktualisieren
pygame.display.flip()
clock.tick(30) # Framerate begrenzen
pygame.quit()
if __name__ == "__main__":
main()
Erklärung der Hauptkomponenten:
Imports: Die notwendigen Bibliotheken pygame, random, numpy, opensimplex, math und pyvista werden importiert.
Konstanten und Einstellungen: Definiert die Größe der Umgebung, Voxelgröße, Anzahl der Ameisen, Pheromon-Parameter, Ameisen- und Physikparameter sowie Vorlieben für die Detailabschneidung.
Voxel-Klasse: Repräsentiert einzelne Voxels in der Umgebung, speichert ihren Typ und verschiedene Arten von Pheromonen. Enthält Methoden zur Verdunstung von Pheromonen.
Ant-Klasse: Repräsentiert eine einzelne Ameise, enthält Attribute für Position, Richtung, Status (ob sie Nahrung trägt), und Methoden für Bewegung, Nahrungssuche, Nestbau und Darstellung.
Colony-Klasse: Repräsentiert eine Kolonie von Ameisen, speichert den Nisteneingang, Liste der Ameisen und die gezählte Nahrung.
Funktionen:
is_voxel_supported: Überprüft, ob ein Voxel von anderen Voxeln unterstützt wird.
apply_physics: Wendet einfache Physik auf die Umgebung an, um fallende Voxels zu simulieren.
generate_environment: Generiert die Umgebung mit Gelände, Feuchtigkeitsverteilungen, Pflanzen, Felsen und anderen Objekten.
project_to_screen: Projektionsfunktion, um 3D-Positionen auf den 2D-Bildschirm zu übertragen.
draw_voxel: Zeichnet einen einzelnen Voxel auf dem Bildschirm.
draw_environment: Zeichnet die gesamte Umgebung und die Pheromonspuren, falls aktiviert.
camera_controls: Steuerung der Kamera basierend auf Benutzereingaben.
Main-Funktion:
Initialisiert Pygame und PyVista.
Generiert die Umgebung und initialisiert die Kolonie und die Ameisen.
Enthält den Hauptspiel-Loop, der Ereignisse verarbeitet, den Tag-Nacht-Zyklus aktualisiert, Ameisenbewegungen und Nestbau durchführt, Physik anwendet, Pheromone verdunsten lässt und die Umgebung sowie die Ameisen zeichnet.
Zeichnet einen UI-Button zur Steuerung der Pheromonvisualisierung.
Hinweise:
Fehlende Implementierungen: Einige Methoden, insbesondere zur Detaildarstellung und komplexeren Physik, sind stark vereinfacht und sollten weiterentwickelt werden, um realistischere Simulationen zu ermöglichen.
Performance: Da die Umgebung in 3D mit Voxel-Renderings dargestellt wird, kann die Performance bei größeren Umgebungen und vielen Ameisen leiden. Optimierungen wie Voxels-Culling und effizientere Datenstrukturen können die Performance verbessern.
Erweiterungen: Weitere Funktionen wie eine komplexere Physik, realistischere Pheromonfallen, oder erweiterte Ameisenverhaltensweisen können hinzugefügt werden, um die Simulation realistischer und interessanter zu gestalten.
Dieses Skript bildet eine solide Grundlage für eine Ameisenkolonien-Simulation mit Visualisierung und grundlegenden Interaktionen. Es kann weiter verfeinert und erweitert werden, um komplexere Verhaltensweisen und realistischere Umgebungen zu integrieren.