Commit 6b638d6d authored by Ahmad Reza's avatar Ahmad Reza
Browse files

Renamed the attributes coords to coordinations and alpha to transparency

parent 2b269a47
"""
TODO:
1- Order the names based on particles, markers, and tiles and alphabetic
1- Order the names based on particles, markers, and tiles and transparencybetic
2- A new column called round_success
3- On demand extenstion of the metrics.
......
......@@ -6,8 +6,8 @@ from lib.swarm_sim_header import *
class Marker(matter.Matter):
"""In the classe marker all the methods for the characterstic of a marker is included"""
def __init__(self, world, x, y, color=black, alpha=1):
def __init__(self, world, x, y, color=black, transparency=1):
"""Initializing the marker constructor"""
super().__init__( world, (x, y), color, alpha, type="marker", mm_size=world.config_data.marker_mm_size)
super().__init__( world, (x, y), color, transparency, type="marker", mm_size=world.config_data.marker_mm_size)
......@@ -7,42 +7,42 @@ from lib.swarm_sim_header import *
class Matter:
"""In the classe marker all the methods for the characterstic of a marker is included"""
def __init__(self, world, coords, color=black, alpha=1, type=None, mm_size=100):
def __init__(self, world, coordinates, color=black, transparency=1, type=None, mm_size=100):
"""Initializing the marker constructor"""
self.coords = coords
self.coordinates = coordinates
self.color = color_map[color]
self.__id = str(uuid.uuid4())
self.world = world
self._memory={}
self.__alpha=alpha
self.__transparency=transparency
self.type = type
self.memory_limitation = world.config_data.memory_limitation
self.mm_size = mm_size
self.modified = False
self.created = False
def set_alpha(self, alpha):
def set_transparency(self, transparency):
"""
Set the alpha value of the particle
Set the transparency value of the particle
:param alpha: The alpha of the particle
:param transparency: The transparency of the particle
:return: None
"""
if (0 <= alpha <= 1):
self.__alpha = round(alpha,2)
elif alpha < 0:
self.__alpha = 0
elif alpha > 1:
self.__alpha = 1
if (0 <= transparency <= 1):
self.__transparency = round(transparency,2)
elif transparency < 0:
self.__transparency = 0
elif transparency > 1:
self.__transparency = 1
self.touch()
def get_alpha(self):
def get_transparency(self):
"""
Returns the alpha value of the particle
Returns the transparency value of the particle
:return: alpha
:return: transparency
"""
return round(self.__alpha,2)
return round(self.__transparency,2)
def read_memory_with(self, key):
"""
......
This diff is collapsed.
......@@ -85,78 +85,78 @@ def direction_in_range(direction):
return direction % 6
def check_values_are_coordinates(coords_x, coords_y):
def check_values_are_coordinates(coordinates_x, coordinates_y):
"""
Checks if the given coordinates are matching the
hexagon coordinates
:param coords_x: proposed x coordinate
:param coords_y: proposed y coordinate
:param coordinates_x: proposed x coordinate
:param coordinates_y: proposed y coordinate
:return: True: Correct x and y coordinates; False: Incorrect coordinates
"""
if (coords_x / 0.5) % 2 == 0:
if coords_y % 2 != 0:
if (coordinates_x / 0.5) % 2 == 0:
if coordinates_y % 2 != 0:
return False
else:
return True
else:
if coords_y % 2 == 0:
if coordinates_y % 2 == 0:
return False
else:
return True
def coords_to_sim(coords):
return coords[0], coords[1] * math.sqrt(3 / 4)
def coordinates_to_sim(coordinates):
return coordinates[0], coordinates[1] * math.sqrt(3 / 4)
def sim_to_coords(x, y):
def sim_to_coordinates(x, y):
return x, round(y / math.sqrt(3 / 4), 0)
def get_coords_in_direction(coords, direction):
def get_coordinates_in_direction(coordinates, direction):
"""
Returns the coordination data of the pointed directions
:param coords: particles actual staying coordination
:param coordinates: particles actual staying coordination
:param direction: The direction. Options: E, SE, SW, W, NW, or NE
:return: The coordinaiton of the pointed directions
"""
return coords[0] + x_offset[direction], coords[1] + y_offset[direction]
return coordinates[0] + x_offset[direction], coordinates[1] + y_offset[direction]
def global_scanning(matter_map_coords_dict, hop, starting_x, starting_y):
def global_scanning(matter_map_coordinates_dict, hop, starting_x, starting_y):
hop_list = []
if (hop / 2 + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop / 2 + starting_x, hop + starting_y)])
if (hop + starting_x, starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop + starting_x, starting_y)])
if (hop / 2 + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop / 2 + starting_x, -hop + starting_y)])
if (-hop / 2 + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + starting_x, -hop + starting_y)])
if (-hop + starting_x, starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop + starting_x, starting_y)])
if (-hop / 2 + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + starting_x, hop + starting_y)])
if (hop / 2 + starting_x, hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(hop / 2 + starting_x, hop + starting_y)])
if (hop + starting_x, starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(hop + starting_x, starting_y)])
if (hop / 2 + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(hop / 2 + starting_x, -hop + starting_y)])
if (-hop / 2 + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(-hop / 2 + starting_x, -hop + starting_y)])
if (-hop + starting_x, starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(-hop + starting_x, starting_y)])
if (-hop / 2 + starting_x, hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(-hop / 2 + starting_x, hop + starting_y)])
for i in range(1, hop):
if (-hop / 2 + i + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + i + starting_x, hop + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords_dict:
if (-hop / 2 + i + starting_x, hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(-hop / 2 + i + starting_x, hop + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coordinates_dict:
hop_list.append(
matter_map_coords_dict[(hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords_dict:
matter_map_coordinates_dict[(hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coordinates_dict:
hop_list.append(
matter_map_coords_dict[(hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 + i + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + i + starting_x, -hop + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords_dict:
matter_map_coordinates_dict[(hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 + i + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
hop_list.append(matter_map_coordinates_dict[(-hop / 2 + i + starting_x, -hop + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coordinates_dict:
hop_list.append(
matter_map_coords_dict[(-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords_dict:
matter_map_coordinates_dict[(-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coordinates_dict:
hop_list.append(
matter_map_coords_dict[(-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y)])
matter_map_coordinates_dict[(-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y)])
return hop_list
......@@ -169,28 +169,28 @@ def generating_random_spraded_particles (world, max_size_particle):
y = random.randrange(-world.get_world_y_size(), world.get_world_y_size())
if y % 2 == 1:
x = x + 0.5
if (x, y) not in world.tile_map_coords:
if (x, y) not in world.tile_map_coordinates:
world.add_particle(x, y)
else:
print(" x and y ", (x, y))
print("Max Size of created Particle", len(world.particles))
def create_particle_in_line(world, max_size_particle, start_coords):
if start_coords[0] % 1 != 0:
start_i = int(start_coords[0] - 0.5)
def create_particle_in_line(world, max_size_particle, start_coordinates):
if start_coordinates[0] % 1 != 0:
start_i = int(start_coordinates[0] - 0.5)
for i in range(start_i, start_i+max_size_particle):
world.add_particle(i + 1.5, start_coords[1])
world.add_particle(i + 1.5, start_coordinates[1])
else:
for i in range(int(start_coords[0] + 1), int(start_coords[0] + 1) + max_size_particle):
world.add_particle(i, start_coords[1])
for i in range(int(start_coordinates[0] + 1), int(start_coordinates[0] + 1) + max_size_particle):
world.add_particle(i, start_coordinates[1])
def create_particle_in_square(world, max_size_particle, start_coords):
def create_particle_in_square(world, max_size_particle, start_coordinates):
for y in range(start_coords[1], round(max_size_particle/2)):
for x in range(start_coords[0], round(max_size_particle/2)):
for y in range(start_coordinates[1], round(max_size_particle/2)):
for x in range(start_coordinates[0], round(max_size_particle/2)):
world.add_particle(x + 0.5, 2 * y + 1.0)
world.add_particle(-(x + 0.5), 2 * y + 1.0)
world.add_particle(x + 0.5, -(2 * y + 1.0))
......@@ -292,7 +292,7 @@ def move_to_dest_step_by_step(particle, destiny):
:param destiny:
:return: True if movement occured, False if not movment and a Matter if the next direction point has a matter on it
"""
next_dir = get_next_direction_to(particle.coords[0], particle.coords[1], destiny.coords[0], destiny.coords[1])
next_dir = get_next_direction_to(particle.coordinates[0], particle.coordinates[1], destiny.coordinates[0], destiny.coordinates[1])
if particle.matter_in(next_dir):
particle.get_matter_in(next_dir)
return particle.get_matter_in(next_dir)
......
......@@ -6,9 +6,9 @@ from lib.swarm_sim_header import *
class Tile(matter.Matter):
"""In the classe marker all the methods for the characterstic of a marker is included"""
def __init__(self, world, x, y, color=gray, alpha=1):
def __init__(self, world, x, y, color=gray, transparency=1):
"""Initializing the marker constructor"""
super().__init__( world, (x, y), color, alpha, type="tile", mm_size=world.config_data.tile_mm_size)
super().__init__( world, (x, y), color, transparency, type="tile", mm_size=world.config_data.tile_mm_size)
self.__isCarried = False
def get_tile_status(self):
......@@ -28,46 +28,46 @@ class Tile(matter.Matter):
"""
self.__isCarried = status
def take(self, coords=0):
def take(self, coordinates=0):
"""
Takes the tile on the given coordinate if it is not taken
:param coords: Coordination of tile that should be taken
:param coordinates: Coordination of tile that should be taken
:return: True: Successful taken; False: Cannot be taken or wrong Coordinates
"""
if coords==0:
if coordinates==0:
if self.__isCarried == False:
if self.coords in self.world.tile_map:
del self.world.tile_map_coords[self.coords]
if self.coordinates in self.world.tile_map:
del self.world.tile_map_coordinates[self.coordinates]
self.__isCarried = True
self.set_alpha(0.5)
self.set_transparency(0.5)
self.touch()
return True
else:
return False
else:
if self.__isCarried == False:
if self.coords in self.world.tile_map_coords:
del self.world.tile_map_coords[self.coords]
if self.coordinates in self.world.tile_map_coordinates:
del self.world.tile_map_coordinates[self.coordinates]
self.__isCarried = True
self.coords = coords
self.set_alpha(0.5)
self.coordinates = coordinates
self.set_transparency(0.5)
self.touch()
return True
else:
return False
def drop_me(self, coords):
def drop_me(self, coordinates):
"""
Drops the tile
:param coords: the given position
:param coordinates: the given position
:return: None
"""
self.world.tile_map_coords[coords] = self
self.coords = coords
self.world.tile_map_coordinates[coordinates] = self
self.coordinates = coordinates
self.__isCarried = False
self.set_alpha(1)
self.set_transparency(1)
self.touch()
def touch(self):
......
......@@ -32,17 +32,17 @@ print_frame_stats = False
# simulation parameters
rounds_per_second = 10
# tile_alpha = 0.6
particle_alpha = 1
# tile_transparency = 0.6
particle_transparency = 1
marker_alpha = 1
marker_transparency = 1
def coords_to_sim(coords):
return coords[0], coords[1] * math.sqrt(3/4)
def coordinates_to_sim(coordinates):
return coordinates[0], coordinates[1] * math.sqrt(3/4)
def sim_to_coords(x, y):
def sim_to_coordinates(x, y):
return x, round(y / math.sqrt(3/4), 0)
......@@ -142,7 +142,6 @@ class VisWindow(pyglet.window.Window):
self.video_mode = False
self.draw()
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if buttons & mouse.LEFT:
self.view.drag(dx, dy)
......@@ -155,16 +154,16 @@ class VisWindow(pyglet.window.Window):
def on_mouse_press(self, x, y, button, modifiers):
if modifiers & key.MOD_CTRL:
# get correct coordinates
sim_coords = window_to_sim(x, y, self.view)
coords_coords = sim_to_coords(sim_coords[0], sim_coords[1])
rounded_coords=0
if coords_coords[1]%2!=0:
rounded_coords = round(coords_coords[0],0) + 0.5
sim_coordinates = window_to_sim(x, y, self.view)
coordinates_coordinates = sim_to_coordinates(sim_coordinates[0], sim_coordinates[1])
rounded_coordinates=0
if coordinates_coordinates[1]%2!=0:
rounded_coordinates = round(coordinates_coordinates[0],0) + 0.5
else:
rounded_coords =round(coords_coords[0], 0)
if (rounded_coords,coords_coords[1]) not in self.world.tile_map_coords:
rounded_coordinates =round(coordinates_coordinates[0], 0)
if (rounded_coordinates,coordinates_coordinates[1]) not in self.world.tile_map_coordinates:
# add tile and vertices
if self.world.add_tile_vis(rounded_coords, coords_coords[1]):
if self.world.add_tile_vis(rounded_coordinates, coordinates_coordinates[1]):
self.tile_vertex_list.resize(4 * len(self.world.tiles), 4 * len(self.world.tiles))
#self.tile_vertex_list.resize(4 * len(self.world.tiles), 8 * len(self.world.tiles))
self.tile_vertex_list.indices[4 * (len(self.world.tiles) - 1) : 4 * (len(self.world.tiles) - 1) + 4] = range(4 * (len(self.world.tiles) - 1), 4 * (len(self.world.tiles) - 1) + 4)
......@@ -173,7 +172,7 @@ class VisWindow(pyglet.window.Window):
self.update_tiles(True)
else:
# delete tile
self.world.remove_tile_on((rounded_coords,coords_coords[1]))
self.world.remove_tile_on((rounded_coordinates,coordinates_coordinates[1]))
self.tile_vertex_list.resize(4 * len(self.world.tiles), 4 * len(self.world.tiles))
self.update_tiles(True)
......@@ -295,7 +294,7 @@ class VisWindow(pyglet.window.Window):
def update_tile(self, i, tile):
weird = 256 / 220
pos = coords_to_sim(tile.coords)
pos = coordinates_to_sim(tile.coordinates)
x = pos[0]
y = pos[1]
......@@ -307,18 +306,18 @@ class VisWindow(pyglet.window.Window):
texRight = 1 / 8
texBottom = 5 / 8
texTop = 6 / 8
#tile_alpha = 1
#tile_transparency = 1
else:
texLeft = 7 / 8
texRight = 1 # 8/8
texBottom = 4 / 8
texTop = 5 / 8
#tile_alpha = 0.5
#tile_transparency = 0.5
self.tile_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom, texRight, texTop,
texLeft, texTop]
self.tile_vertex_list.colors[16 * i: 16 * i + 16] = (tile.color + [tile.get_alpha()]) * 4
self.tile_vertex_list.colors[16 * i: 16 * i + 16] = (tile.color + [tile.get_transparency()]) * 4
def init_particle_vertex_list(self):
self.particle_vertex_list = self.particle_vertex_list = pyglet.graphics.vertex_list \
......@@ -344,7 +343,7 @@ class VisWindow(pyglet.window.Window):
def update_particle(self, i, particle):
weird = 256 / 220
pos = coords_to_sim(particle.coords)
pos = coordinates_to_sim(particle.coordinates)
x = pos[0]
y = pos[1]
......@@ -357,17 +356,17 @@ class VisWindow(pyglet.window.Window):
texRight = 1 / 8
texBottom = 7 / 8
texTop = 6 / 8
#particle.set_alpha(0.5)
#particle.set_transparency(0.5)
else:
texLeft = 0 / 8
texRight = 1 / 8
texBottom = 0 / 8
texTop = 1 / 8
#particle.set_alpha(1)
#particle.set_transparency(1)
self.particle_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom,
texRight, texTop, texLeft, texTop]
self.particle_vertex_list.colors[16 * i: 16 * i + 16] = (particle.color + [particle.get_alpha()]) * 4
self.particle_vertex_list.colors[16 * i: 16 * i + 16] = (particle.color + [particle.get_transparency()]) * 4
def init_marker_vertex_list(self):
self.marker_vertex_list = self.marker_vertex_list = pyglet.graphics.vertex_list \
......@@ -393,7 +392,7 @@ class VisWindow(pyglet.window.Window):
def update_marker(self, i, marker):
weird = 256 / 220
pos = coords_to_sim(marker.coords)
pos = coordinates_to_sim(marker.coordinates)
x = pos[0]
y = pos[1]
......@@ -407,7 +406,7 @@ class VisWindow(pyglet.window.Window):
self.marker_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom,
texRight, texTop, texLeft, texTop]
self.marker_vertex_list.colors[16 * i: 16 * i + 16] = (marker.color + [marker.get_alpha()]) * 4
self.marker_vertex_list.colors[16 * i: 16 * i + 16] = (marker.color + [marker.get_transparency()]) * 4
def draw_world(self, round_start_timestamp):
while not self.simulation_running:
......
......@@ -34,21 +34,21 @@ class World:
self.particles = []
self.particles_created = []
self.particle_rm = []
self.particle_map_coords = {}
self.particle_map_coordinates = {}
self.particle_map_id = {}
self.__particle_deleted=False
self.tiles = []
self.tiles_created = []
self.tiles_rm = []
self.tile_map_coords = {}
self.tile_map_coordinates = {}
self.tile_map_id = {}
self.__tile_deleted=False
self.new_tile = None
self.markers = []
self.markers_created = []
self.marker_map_coords = {}
self.marker_map_coordinates = {}
self.marker_map_id = {}
self.markers_rm = []
self.__marker_deleted = False
......@@ -148,13 +148,13 @@ class World:
"""
return self.particles
def get_particle_map_coords(self):
def get_particle_map_coordinates(self):
"""
Get a dictionary with all particles mapped with their actual coordinates
:return: a dictionary with particles and their coordinates
"""
return self.particle_map_coords
return self.particle_map_coordinates
def get_particle_map_id(self):
"""
......@@ -180,13 +180,13 @@ class World:
"""
return self.tiles
def get_tile_map_coords(self):
def get_tile_map_coordinates(self):
"""
Get a dictionary with all tiles mapped with their actual coordinates
:return: a dictionary with particles and their coordinates
"""
return self.tile_map_coords
return self.tile_map_coordinates
def get_tile_map_id(self):
"""
......@@ -212,13 +212,13 @@ class World:
"""
return self.markers
def get_marker_map_coords(self):
def get_marker_map_coordinates(self):
"""
Get a dictionary with all markers mapped with their actual coordinates
:return: a dictionary with markers and their coordinates
"""
return self.marker_map_coords
return self.marker_map_coordinates
def get_marker_map_id(self):
"""
......@@ -265,7 +265,7 @@ class World:
def set_marker_deleted(self):
self.__marker_deleted = False
def add_particle(self, x, y, color=black, alpha=1):
def add_particle(self, x, y, color=black, transparency=1):
"""
Add a particle to the world database
......@@ -276,23 +276,23 @@ class World:
:param color: The color of the particle. Coloroptions: black, gray, red, green, or blue
:return: Added Matter; False: Unsuccsessful
"""
if alpha < 0 or alpha >1:
alpha = 1
if transparency < 0 or transparency >1:
transparency = 1
if len(self.particles) < self.config_data.max_particles:
if check_values_are_coordinates(x,y) == True:
if (x,y) not in self.get_particle_map_coords():
if (x,y) not in self.get_particle_map_coordinates():
self.particle_id_counter += 1
new_particle = particle.Particle(self, x, y, color, alpha, self.particle_id_counter)
new_particle = particle.Particle(self, x, y, color, transparency, self.particle_id_counter)
print(new_particle.number)
self.particles_created.append(new_particle)
self.particle_map_coords[new_particle.coords] = new_particle
self.particle_map_coordinates[new_particle.coordinates] = new_particle
self.particle_map_id[new_particle.get_id()] = new_particle
self.particles.append(new_particle)
new_particle.touch()
self.csv_round.update_particle_num(len(self.particles))
self.init_particles.append(new_particle)
new_particle.created=True
logging.info("Created particle at %s", new_particle.coords)
logging.info("Created particle at %s", new_particle.coordinates)
return new_particle
else:
print("for x %f and y %f not not possible because Particle exist ", x, y)
......@@ -315,7 +315,7 @@ class World:
if rm_particle:
self.particles.remove(rm_particle)
try:
del self.particle_map_coords[rm_particle.coords]
del self.particle_map_coordinates[rm_particle.coordinates]
del self.particle_map_id[id]
except:
pass
......@@ -327,22 +327,22 @@ class World:
else:
return False
def remove_particle_on(self, coords):
def remove_particle_on(self, coordinates):
"""
Removes a particle on a give coordinat from to the world database
:param coords: A tupel that includes the x and y coorindates
:param coordinates: A tupel that includes the x and y coorindates
:return: True: Successful removed; False: Unsuccessful
"""
if coords in self.particle_map_coords:
self.particles.remove(self.particle_map_coords[coords])
self.particle_rm.append(self.particle_map_coords[coords])
if coordinates in self.particle_map_coordinates: