particle.py 55.3 KB
Newer Older
Ahmad Reza's avatar
Ahmad Reza committed
1
2
3
4
5
6
7
8
9
10
11
12
13
"""
.. module:: particle
   :platform: Unix, Windows
   :synopsis: This module provides the interfaces of the robotics particle

.. moduleauthor:: Ahmad Reza Cheraghi

TODO: Erase Memory

"""

import logging, math
from lib import csv_generator, matter
14
from lib.swarm_sim_header import *
Ahmad Reza's avatar
Ahmad Reza committed
15

Ahmad Reza's avatar
Ahmad Reza committed
16

Ahmad Reza's avatar
Ahmad Reza committed
17
class Particle(matter.Matter):
18
    """In the classe marker all the methods for the characterstic of a marker is included"""
Karol Actun's avatar
Karol Actun committed
19
    def __init__(self, world, coordinates, color=black, transparency=1, particle_counter=0):
20
        """Initializing the marker constructor"""
Karol Actun's avatar
Karol Actun committed
21
        super().__init__( world, coordinates, color, transparency,
22
23
                          type="particle", mm_size=world.config_data.particle_mm_size)
        self.number = particle_counter
Ahmad Reza's avatar
Ahmad Reza committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        self.__isCarried = False
        self.carried_tile = None
        self.carried_particle = None
        self.steps = 0
        self.csv_particle_writer = csv_generator.CsvParticleData( self.get_id(), self.number)

    def has_tile(self):
        if self.carried_tile == None:
            return False
        else:
            return True

    def has_particle(self):
        if self.carried_particle == None:
38
            return False 
Ahmad Reza's avatar
Ahmad Reza committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        else:
            return True

    def get_carried_status(self):
        """
        Get the status if it is taken or not

        :return: Tiles status
        """
        return self.__isCarried

    def check_on_tile(self):
        """
        Checks if the particle is on a tile

        :return: True: On a tile; False: Not on a Tile
        """
56
        if self.coordinates in self.world.tile_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
57
58
59
60
61
62
63
64
65
66
            return True
        else:
            return False

    def check_on_particle(self):
        """
        Checks if the particle is on a particle

        :return: True: On a particle; False: Not on a particle
        """
67
        if self.coordinates in self.world.particle_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
68
69
70
71
            return True
        else:
            return False

72
    def check_on_marker(self):
Ahmad Reza's avatar
Ahmad Reza committed
73
        """
74
        Checks if the particle is on a marker
Ahmad Reza's avatar
Ahmad Reza committed
75

76
        :return: True: On a marker; False: Not on a marker
Ahmad Reza's avatar
Ahmad Reza committed
77
        """
78
        if self.coordinates in self.world.marker_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
79
80
81
82
            return True
        else:
            return False

83
    def move_to(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
84
        """
85
        Moves the particle to the given directionection
Ahmad Reza's avatar
Ahmad Reza committed
86

Karol Actun's avatar
Karol Actun committed
87
        :param direction: The direction is defined by loaded grid class
Ahmad Reza's avatar
Ahmad Reza committed
88
89
        :return: True: Success Moving;  False: Non moving
        """
90
        direction_coord = get_coordinates_in_direction(self.coordinates, direction)
91
        direction, direction_coord = self.check_within_border(direction, direction_coord)
Karol Actun's avatar
Karol Actun committed
92
        if grid.is_valid_location(direction_coord):
Ahmad Reza's avatar
Ahmad Reza committed
93

94
95
            if self.coordinates in self.world.particle_map_coordinates:
                del self.world.particle_map_coordinates[self.coordinates]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
96

97
98
99
            if not direction_coord in self.world.particle_map_coordinates:
                self.coordinates = direction_coord
                self.world.particle_map_coordinates[self.coordinates] = self
100
                logging.info("particle %s successfully moved to %s", str(self.get_id()), direction)
Ahmad Reza's avatar
Ahmad Reza committed
101
                self.world.csv_round.update_metrics( steps=1)
Ahmad Reza's avatar
Ahmad Reza committed
102
103
                self.csv_particle_writer.write_particle(steps=1)
                self.touch()
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
104
                self.check_for_carried_tile_or_particle()
Ahmad Reza's avatar
Ahmad Reza committed
105
                return True
106
        return False
Ahmad Reza's avatar
Ahmad Reza committed
107

Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
108
109
    def check_for_carried_tile_or_particle(self):
        if self.carried_tile is not None:
110
            self.carried_tile.coordinates = self.coordinates
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
111
112
            self.carried_tile.touch()
        elif self.carried_particle is not None:
113
            self.carried_particle.coordinates = self.coordinates
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
114
115
            self.carried_particle.touch()

116
    def check_within_border(self, direction, direction_coord):
Ahmad Reza's avatar
Ahmad Reza committed
117
        if self.world.config_data.border == 1 and \
118
119
                (abs(direction_coord[0]) > self.world.get_sim_x_size() or abs(direction_coord[1]) > self.world.get_sim_y_size()):
            direction = direction - 3 if direction > 2 else direction + 3
120
            direction_coord = get_coordinates_in_direction(self.coordinates, direction)
121
        return direction, direction_coord
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
122

123
    def move_to_in_bounds(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
124
        """
125
            Moves the particle to the given directionection if it would remain in bounds.
Ahmad Reza's avatar
Ahmad Reza committed
126

127
            :param direction: The directionection must be either: E, SE, SW, W, NW, or NE
Ahmad Reza's avatar
Ahmad Reza committed
128
129
            :return: True: Success Moving;  False: Non moving
        """
130
131
        direction_coord = get_coordinates_in_direction(self.coordinates, direction)
        sim_coord = coordinates_to_sim(direction_coord)
Ahmad Reza's avatar
Ahmad Reza committed
132
133
        if self.world.get_sim_x_size() >=  abs(sim_coord[0]) and \
                        self.world.get_sim_y_size() >=  abs(sim_coord[1]):
134
            return self.move_to(direction)
Ahmad Reza's avatar
Ahmad Reza committed
135
136
        else:
            # 'bounce' off the wall
137
138
            n_direction = direction - 3 if direction > 2 else direction + 3
            self.move_to(n_direction)
Ahmad Reza's avatar
Ahmad Reza committed
139
140
141

    def read_from_with(self, matter, key=None):
        """
142
        Read the memories from the matters (paricle, tile, or marker object) memories with a given keyword
Ahmad Reza's avatar
Ahmad Reza committed
143

144
        :param matter: The matter can be either a particle, tile, or marker
Ahmad Reza's avatar
Ahmad Reza committed
145
146
147
148
149
150
151
152
        :param key: A string keyword to searcg for the data in the memory
        :return: The matters memory; None
        """
        if key != None:
            tmp_memory = matter.read_memory_with(key)
        else:
            tmp_memory =  matter.read_whole_memory()

153
154
155
156
157
158
159
160
161
162
163
164
165
        if tmp_memory != None:
            if not(hasattr(tmp_memory, '__len__')) or len(tmp_memory) > 0:
                if matter.type == "particle":
                    self.world.csv_round.update_metrics( particle_read=1)
                    self.csv_particle_writer.write_particle(particle_read=1)
                elif matter.type == "tile":
                    self.world.csv_round.update_metrics( tile_read=1)
                    self.csv_particle_writer.write_particle(tile_read=1)
                elif matter.type == "marker":
                    self.world.csv_round.update_metrics( marker_read=1)
                    self.csv_particle_writer.write_particle(marker_read=1)
                return tmp_memory
        return None
Ahmad Reza's avatar
Ahmad Reza committed
166

167
    def matter_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
168
        """
169
        :param direction: the directionection to check if a matter is there
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
170
171
        :return: True: if a matter is there, False: if not
        """
172
173
174
        if  get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates() \
            or get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates() \
            or get_coordinates_in_direction(self.coordinates, direction) in self.world.get_marker_map_coordinates():
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
175
176
177
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
178

179
    def tile_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
180
        """
181
        :param direction: the directionection to check if a tile is there
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
182
183
        :return: True: if a tile is there, False: if not
        """
184
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
185
186
187
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
188

189
    def particle_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
190
        """
191
        :param direction: the directionection to check if a particle is there
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
192
193
        :return: True: if a particle is there, False: if not
        """
194
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
195
196
197
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
198

199
    def marker_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
200
        """
201
        :param direction: the directionection to check if a marker is there
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
202
203
        :return: True: if a marker is there, False: if not
        """
204
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_marker_map_coordinates():
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
205
206
207
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
208

209
    def get_matter_in(self, direction=E):
210
211
212
213
214
215
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        elif get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
            return self.world.get_particle_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        elif get_coordinates_in_direction(self.coordinates, direction) in self.world.get_marker_map_coordinates():
            return self.world.get_marker_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
216
217
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
218

219
    def get_tile_in(self, direction=E):
220
221
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
222
223
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
224

225
    def get_particle_in(self, direction=E):
226
227
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
            return self.world.get_particle_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
228
229
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
230

231
    def get_marker_in(self, direction=E):
232
233
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_marker_map_coordinates():
            return self.world.get_marker_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
234
235
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
236

237
    def get_marker(self):
238
239
        if self.coordinates in self.world.marker_map_coordinates:
            return self.world.get_marker_map_coordinates()[self.coordinates]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
240
241
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
242

Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
243
    def get_tile(self):
244
245
        if self.self.coordinates in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[self.coordinates]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
246
247
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
248
249
250

    def write_to_with(self, matter, key=None, data=None):
        """
251
        Writes data with given a keyword directionectly on the matters (paricle, tile, or marker object) memory
Ahmad Reza's avatar
Ahmad Reza committed
252

253
        :param matter: The matter can be either a particle, tile, or marker
Ahmad Reza's avatar
Ahmad Reza committed
254
255
256
257
258
259
260
261
262
263
264
265
266
        :param key: A string keyword so to order the data that is written into the memory
        :param data: The data that should be stored into the memory
        :return: True: Successful written into the memory; False: Unsuccessful
        """
        wrote=False
        if data != None:
            wrote=False
            if key==None:
                wrote=matter.write_memory(data)
            else:
                wrote= matter.write_memory_with(key, data)
            if  wrote==True:
                if matter.type == "particle":
Ahmad Reza's avatar
Ahmad Reza committed
267
                    self.world.csv_round.update_metrics( particle_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
268
269
                    self.csv_particle_writer.write_particle(particle_write=1)
                elif matter.type == "tile":
Ahmad Reza's avatar
Ahmad Reza committed
270
                    self.world.csv_round.update_metrics( tile_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
271
                    self.csv_particle_writer.write_particle(tile_write=1)
272
                elif matter.type == "marker":
Ahmad Reza's avatar
Ahmad Reza committed
273
                    self.world.csv_round.update_metrics( marker_write=1)
274
                    self.csv_particle_writer.write_particle(marker_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
275
276
277
278
279
280
                return True
            else:
                return False
        else:
            return False

Ahmad Reza's avatar
Ahmad Reza committed
281
    def scan_for_matters_within(self, matter='all', hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
282
        """
283
        Scans for particles, tiles, or marker on a given hop distance and all the matters within the hop distance
Ahmad Reza's avatar
Ahmad Reza committed
284
285
286

        :todo: If nothing then everything should be scanned

287
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
288
289
290
291
        :param hop: The hop distance from the actual position of the scanning particle
        :return: A list of the founded matters
        """

Ahmad Reza's avatar
Ahmad Reza committed
292
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
293
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
294
295
296
297
298
            in_list = self.scan_for_matters_in(matter, i)
            if in_list is not None:
                within_hop_list.extend(in_list)
        if len(within_hop_list) != 0:
            return within_hop_list
Ahmad Reza's avatar
Ahmad Reza committed
299
300
301
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
302
    def scan_for_matters_in(self, matter='all', hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
303
        """
Ahmad Reza's avatar
Ahmad Reza committed
304
         Scanning for particles, tiles, or marker on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
305

Ahmad Reza's avatar
Ahmad Reza committed
306
307
308
309
         :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
         :param hop: The hop distance from thee actual position of the scanning particle
         :return: A list of the founded matters
         """
310
311
        starting_x = self.coordinates[0]
        starting_y = self.coordinates[1]
Ahmad Reza's avatar
Ahmad Reza committed
312
        scanned_list = []
313
        logging.info("particle on %s is scanning for %s in %i hops", str(self.coordinates), matter, hop)
Ahmad Reza's avatar
Ahmad Reza committed
314

Ahmad Reza's avatar
Ahmad Reza committed
315
        if matter == "particles":
Karol Actun's avatar
Karol Actun committed
316
            scanned_list = global_scanning(self.world.particle_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
317
        elif matter == "tiles":
Karol Actun's avatar
Karol Actun committed
318
            scanned_list = global_scanning(self.world.tile_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
319
        elif matter == "markers":
Karol Actun's avatar
Karol Actun committed
320
            scanned_list = global_scanning(self.world.marker_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
321
        else:
Karol Actun's avatar
Karol Actun committed
322
            scanned_list = global_scanning(self.world.particle_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
323
            if scanned_list is not None:
Karol Actun's avatar
Karol Actun committed
324
325
                scanned_list.extend(global_scanning(self.world.tile_map_coordinates, hop, self.coordinates))
                scanned_list.extend(global_scanning(self.world.marker_map_coordinates, hop, self.coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
326
            else:
Karol Actun's avatar
Karol Actun committed
327
                scanned_list = global_scanning(self.world.tile_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
328
                if scanned_list is not None:
Karol Actun's avatar
Karol Actun committed
329
                    scanned_list.extend(global_scanning(self.world.marker_map_coordinates, hop, self.coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
330
                else:
Karol Actun's avatar
Karol Actun committed
331
                    scanned_list = global_scanning(self.world.marker_map_coordinates, hop, self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
332
333
        if scanned_list is not None:
            return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
334
335
336
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
337
    def scan_for_particles_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
338
        """
339
        Scans for particles, tiles, or marker on a given hop distance and all the matters within the hop distance
Ahmad Reza's avatar
Ahmad Reza committed
340
341
342

        :todo: If nothing then everything should be scanned

343
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
344
345
346
347
        :param hop: The hop distance from the actual position of the scanning particle
        :return: A list of the founded matters
        """

Ahmad Reza's avatar
Ahmad Reza committed
348
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
349
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
350
351
352
353
354
            in_list = self.scan_for_particles_in(hop=i)
            if in_list is not None:
                within_hop_list.extend(in_list)
        if len(within_hop_list) != 0:
            return within_hop_list
Ahmad Reza's avatar
Ahmad Reza committed
355
356
357
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
358
    def scan_for_particles_in(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
359
        """
360
        Scanning for particles, tiles, or marker on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
361

362
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
363
364
365
366
        :param hop: The hop distance from thee actual position of the scanning particle
        :return: A list of the founded matters
        """

Ahmad Reza's avatar
Ahmad Reza committed
367
368
369
370
        scanned_list = self.scan_for_matters_in(matter='particles', hop=hop)
        return scanned_list

    def scan_for_tiles_within(self,  hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
371
        """
372
        Scans for particles, tiles, or marker on a given hop distance and all the matters within the hop distance
Ahmad Reza's avatar
Ahmad Reza committed
373
374
375

        :todo: If nothing then everything should be scanned

376
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
377
378
379
380
        :param hop: The hop distance from the actual position of the scanning particle
        :return: A list of the founded matters
        """

Ahmad Reza's avatar
Ahmad Reza committed
381
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
382
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
383
384
385
386
387
            in_list = self.scan_for_tiles_in( hop=i)
            if in_list is not None:
                within_hop_list.extend(in_list)
        if len(within_hop_list) != 0:
            return within_hop_list
Ahmad Reza's avatar
Ahmad Reza committed
388
389
390
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
391
    def scan_for_tiles_in(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
392
        """
393
        Scanning for particles, tiles, or marker on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
394

395
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
396
397
398
        :param hop: The hop distance from thee actual position of the scanning particle
        :return: A list of the founded matters
        """
Ahmad Reza's avatar
Ahmad Reza committed
399
400
        scanned_list = self.scan_for_matters_in(matter='tiles', hop=hop)
        return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
401

Ahmad Reza's avatar
Ahmad Reza committed
402
    def scan_for_markers_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
403
        """
404
        Scans for particles, tiles, or marker on a given hop distance and all the matters within the hop distance
Ahmad Reza's avatar
Ahmad Reza committed
405
406
407

        :todo: If nothing then everything should be scanned

408
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
409
410
411
412
        :param hop: The hop distance from the actual position of the scanning particle
        :return: A list of the founded matters
        """

Ahmad Reza's avatar
Ahmad Reza committed
413
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
414
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
415
416
417
418
419
            in_list = self.scan_for_markers_in(hop=i)
            if in_list is not None:
                within_hop_list.extend(in_list)
        if len(within_hop_list) != 0:
            return within_hop_list
Ahmad Reza's avatar
Ahmad Reza committed
420
421
422
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
423
    def scan_for_markers_in(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
424
        """
425
        Scanning for particles, tiles, or marker on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
426

427
        :param matter: For what matter this method should scan for. Can be either particles, tiles, markers, or (default)all
Ahmad Reza's avatar
Ahmad Reza committed
428
429
430
        :param hop: The hop distance from thee actual position of the scanning particle
        :return: A list of the founded matters
        """
Ahmad Reza's avatar
Ahmad Reza committed
431
432
        scanned_list = self.scan_for_matters_in(matter='markers', hop=hop)
        return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
433

434
    def take_me(self, coordinates=0):
Ahmad Reza's avatar
Ahmad Reza committed
435
436
437
        """
        The particle is getting taken from the the other particle on the given coordinate

438
        :param coordinates: Coordination of particle that should be taken
Ahmad Reza's avatar
Ahmad Reza committed
439
440
441
442
        :return: True: Successful taken; False: Cannot be taken or wrong Coordinates
        """

        if not self.__isCarried:
443
444
            if self.coordinates in self.world.particle_map_coordinates:
                del self.world.particle_map_coordinates[self.coordinates]
Ahmad Reza's avatar
Ahmad Reza committed
445
            self.__isCarried = True
446
447
            self.coordinates = coordinates
            self.set_transparency(0.5)
Ahmad Reza's avatar
Ahmad Reza committed
448
449
450
451
452
            self.touch()
            return True
        else:
            return False

453
    def drop_me(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
454
455
456
        """
        The actual particle is getting dropped

457
        :param coordinates: the given position
Ahmad Reza's avatar
Ahmad Reza committed
458
459
        :return: None
        """
460
461
        self.world.particle_map_coordinates[coordinates] = self
        self.coordinates = coordinates
Ahmad Reza's avatar
Ahmad Reza committed
462
        self.__isCarried = False
463
        self.set_transparency(1)
Ahmad Reza's avatar
Ahmad Reza committed
464
465
        self.touch()

466
    def create_tile(self, color=gray, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
467
468
469
        """
        Creates a tile on the particles actual position

Ahmad Reza's avatar
Ahmad Reza committed
470
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
471
        """
472
        logging.info("Going to create a tile on position %s", str(self.coordinates))
Karol Actun's avatar
Karol Actun committed
473
        new_tile = self.world.add_tile(self.coordinates, color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
474
        if new_tile:
Karol Actun's avatar
Karol Actun committed
475
            self.world.tile_map_coordinates[self.coordinates].created = True
Ahmad Reza's avatar
Ahmad Reza committed
476
            self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
477
478
            self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()))
            self.world.csv_round.update_metrics( tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
479
480
481
            return new_tile
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
482

483
    def create_tile_in(self, direction=None, color=gray, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
484
        """
485
        Creates a tile either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
486

487
        :param direction: The directionection on which the tile should be created. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
488
        :return: New tile or False
Ahmad Reza's avatar
Ahmad Reza committed
489
490
        """
        logging.info("particle with id %s is", self.get_id())
491
        logging.info("Going to create a tile in %s ", str(direction) )
Karol Actun's avatar
Karol Actun committed
492
        if direction is not None:
493
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
Karol Actun's avatar
Karol Actun committed
494
            new_tile = self.world.add_tile(coordinates, color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
495
            if new_tile:
Karol Actun's avatar
Karol Actun committed
496
                self.world.tile_map_coordinates[coordinates].created = True
Ahmad Reza's avatar
Ahmad Reza committed
497
                logging.info("Tile is created")
Ahmad Reza's avatar
Ahmad Reza committed
498
                self.world.new_tile_flag = True
Ahmad Reza's avatar
Ahmad Reza committed
499
                self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
500
501
                self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()))
                self.world.csv_round.update_metrics( tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
502
503
504
                return new_tile
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
505
506
        else:
            logging.info("Not created tile ")
Ahmad Reza's avatar
Ahmad Reza committed
507
            return False
Ahmad Reza's avatar
Ahmad Reza committed
508

Karol Actun's avatar
Karol Actun committed
509
    def create_tile_on(self, coordinates=None, color=gray, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
510
511
512
        """
        Creates a tile either on a given x,y coordinates

Karol Actun's avatar
Karol Actun committed
513
        :param coordinates: the coordinates
Ahmad Reza's avatar
Ahmad Reza committed
514
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
515
516
517
        """

        logging.info("particle with id %s is", self.get_id())
Karol Actun's avatar
Karol Actun committed
518
519
        if coordinates is not None:
            if grid.is_valid_location(coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
520
                logging.info("Going to create a tile on position \(%i , %i\)", x,y )
Karol Actun's avatar
Karol Actun committed
521
522
                if self.world.add_tile(coordinates, color, transparency):
                    self.world.tile_map_coordinates[coordinates, coordinates[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
523
                    self.world.new_tile_flag = True
Ahmad Reza's avatar
Ahmad Reza committed
524
                    self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
525
526
                    self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()) )
                    self.world.csv_round.update_metrics( tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
527
528
                    return True
                else:
529
                    logging.info("Not created tile on coordinates  \(%i , %i\)", y,x )
Ahmad Reza's avatar
Ahmad Reza committed
530
531
                    return False
            else:
532
                logging.info("Not created tile on coordinates   \(%i , %i\)", y,x )
Ahmad Reza's avatar
Ahmad Reza committed
533
                return False
Ahmad Reza's avatar
Ahmad Reza committed
534
535
536
537
538
539
540
541
542

    def delete_tile(self):
        """
        Deletes a tile on current position

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is", self.get_id())
        logging.info("is going to delete a tile on current position")
543
544
        if self.coordinates in self.world.get_tile_map_coordinates():
            if self.world.remove_tile_on(self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
        else:
            logging.info("Could not delet tile")
            return False

    def delete_tile_with(self, id):
        """
        Deletes a tile with a given tile-id

        :param tile_id: The id of the tile that should be deleted
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is", self.get_id())
        logging.info("is going to delete a tile with tile id %s", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
560
        if self.world.remove_tile(id):
Ahmad Reza's avatar
Ahmad Reza committed
561
562
563
564
565
566
            self.csv_particle_writer.write_particle(tile_deleted=1)
            return True
        else:
            logging.info("Could not delet tile with tile id %s", str(id))
            return False

567
    def delete_tile_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
568
        """
569
        Deletes a tile either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
570

571
        :param direction: The directionection on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
572
573
574

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
575
        coordinates = ()
576
        if direction is not None:
577
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
578
            logging.info("Deleting tile in %s directionection", str(direction))
579
580
581
            if coordinates is not None:
                if self.world.remove_tile_on(coordinates):
                    logging.info("Deleted tile with tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
582
583
584
                    self.csv_particle_writer.write_particle(tile_deleted=1)
                    return True
                else:
585
                    logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
586
587
                    return False
        else:
588
            logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
589
590
591
592
593
594
595
596
597
598
            return False

    def delete_tile_on(self, x=None, y=None):
        """
        Deletes a tile either on a given x,y coordinates
,
        :param x: x coordinate
        :param y: y coordinate
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
599
        coordinates = ()
Ahmad Reza's avatar
Ahmad Reza committed
600
        if x is not None and y is not None:
601
602
603
            coordinates = (x, y)
            if self.world.remove_tile_on(coordinates):
                logging.info("Deleted tile with tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
604
605
606
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
            else:
607
                logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
608
609
                return False
        else:
610
            logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
611
612
613
614
615
616
617
618
619
620
            return False

    def take_tile(self):
        """
        Takes a tile on the actual position

        :param id:  The id of the tile that should be taken
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
621
622
623
            if self.coordinates in self.world.tile_map_coordinates:
                self.carried_tile = self.world.tile_map_coordinates[self.coordinates]
                if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
624
                    logging.info("Tile has been taken")
Ahmad Reza's avatar
Ahmad Reza committed
625
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
626
627
628
629
630
631
                    self.csv_particle_writer.write_particle(tiles_taken=1)
                    return True
                else:
                    logging.info("Tile could not be taken")
                    return False
            else:
Ahmad Reza's avatar
Ahmad Reza committed
632
                logging.info("No tile on the actual position not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
633
634
635
636
637
638
639
640
641
642
643
644
645
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
            return False

    def take_tile_with(self, id):
        """
        Takes a tile with a given tile id

        :param id:  The id of the tile that should be taken
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
Ahmad Reza's avatar
Ahmad Reza committed
646
647
648
            if id in self.world.tile_map_id:
                logging.info("Tile with tile id %s is in the world", str(id))
                self.carried_tile = self.world.tile_map_id[id]
649
                if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
650
                    logging.info("Tile with tile id %s  has been taken", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
651
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
652
653
654
655
656
657
                    self.csv_particle_writer.write_particle(tiles_taken=1)
                    return True
                else:
                    logging.info("Tile with tile id %s could not be taken", str(id))
                    return False
            else:
Ahmad Reza's avatar
Ahmad Reza committed
658
                logging.info("Tile with tile id %s is not in the world", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
659
660
661
662
663
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
            return False

664
    def take_tile_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
665
        """
666
        Takes a tile that is in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
667

Karol Actun's avatar
Karol Actun committed
668
        :param direction: The direction on which the tile should be taken.
Ahmad Reza's avatar
Ahmad Reza committed
669
670
671
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
672
673
674
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            if coordinates in self.world.tile_map_coordinates:
                self.carried_tile = self.world.tile_map_coordinates[coordinates]
Ahmad Reza's avatar
Ahmad Reza committed
675
                logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
676
                if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
677
                    logging.info("Tile with tile id %s  has been taken", str(self.carried_tile.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
678
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
679
680
681
682
683
684
                    self.csv_particle_writer.write_particle(tiles_taken=1)
                    return True
                else:
                    logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
                    return False
            else:
Ahmad Reza's avatar
Ahmad Reza committed
685
                logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
686
687
688
689
690
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
            return False

Karol Actun's avatar
Karol Actun committed
691
    def take_tile_on(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
692
        """
693
        Takes a tile that is in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
694

Karol Actun's avatar
Karol Actun committed
695
        :param coordinates
Ahmad Reza's avatar
Ahmad Reza committed
696
697
698
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
Karol Actun's avatar
Karol Actun committed
699
700
            if grid.is_valid_location(coordinates):

701
702
                if coordinates in self.world.tile_map_coordinates:
                    self.carried_tile = self.world.tile_map_coordinates[coordinates]
Ahmad Reza's avatar
Ahmad Reza committed
703
                    logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
704
                    if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
705
                        self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
706
707
708
709
710
711
712
                        self.csv_particle_writer.write_particle(tiles_taken=1)
                        logging.info("Tile with tile id %s  has been taken", str(self.carried_tile.get_id()))
                        return True
                    else:
                        logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
                        return False
                else:
Ahmad Reza's avatar
Ahmad Reza committed
713
                    logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
                    return False
            else:
                logging.info("Coordinates are wrong")
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
            return False

    def drop_tile(self):
        """
        Drops the taken tile on the particles actual position

        :return: None
        """
        if self.carried_tile is not None:
729
            if self.coordinates not in self.world.tile_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
730
                try:  # cher: insert so to overcome the AttributeError
731
                    self.carried_tile.drop_me(self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
732
733
734
735
                except AttributeError:
                    pass
                self.carried_tile = None
                logging.info("Tile has been dropped on the actual position")
Ahmad Reza's avatar
Ahmad Reza committed
736
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
737
738
739
740
741
742
743
744
                self.csv_particle_writer.write_particle(tiles_dropped=1)
                return True
            else:
                logging.info("Is not possible to drop the tile on that position because it is occupied")
                return False
        else:
            return False

745
    def drop_tile_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
746
        """
747
        Drops the taken tile on a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
748

749
         :param direction: The directionection on which the tile should be dropped. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
750
751
        """
        if self.carried_tile is not None:
752
753
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            if coordinates not in self.world.tile_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
754
                try:  # cher: insert so to overcome the AttributeError
755
                    self.carried_tile.drop_me(coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
756
757
758
                except AttributeError:
                    pass
                self.carried_tile = None
Ahmad Reza's avatar
Ahmad Reza committed
759
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
760
                self.csv_particle_writer.write_particle(tiles_dropped=1)
761
                logging.info("Dropped tile on %s coordinate", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
762
763
764
765
766
767
768
769
770
                return True
            else:
                logging.info("Is not possible to drop the tile on that position")
                return False

        else:
            logging.info("No tile taken for dropping")
            return False

Karol Actun's avatar
Karol Actun committed
771
    def drop_tile_on(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
772
        """
773
        Drops the taken tile on a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
774

Karol Actun's avatar
Karol Actun committed
775
        :param coordinates
Ahmad Reza's avatar
Ahmad Reza committed
776
777
        """
        if self.carried_tile is not None:
Karol Actun's avatar
Karol Actun committed
778
            if grid.is_valid_location(coordinates):
779
                if coordinates not in self.world.get_tile_map_coordinates():
Ahmad Reza's avatar
Ahmad Reza committed
780
                    try:  # cher: insert so to overcome the AttributeError
781
                        self.carried_tile.drop_me(coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
782
783
784
                    except AttributeError:
                        pass
                    self.carried_tile = None
Ahmad Reza's avatar
Ahmad Reza committed
785
                    self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
786
                    self.csv_particle_writer.write_particle(tiles_dropped=1)
787
                    logging.info("Dropped tile on %s coordinate", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
788
789
790
791
792
793
794
795
796
797
798
                    return True
                else:
                    logging.info("Is not possible to drop the tile on that position because it is occupied")
                    return False
            else:
                logging.info("Wrong coordinates for dropping the tile")
                return False
        else:
            logging.info("No tile is taken for dropping")
            return False

799
    def create_particle(self, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
800
801
802
        """
        Creates a particle on the particles actual position

Ahmad Reza's avatar
Ahmad Reza committed
803
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
804
        """
805
806
        logging.info("Going to create on position %s", str(self.coordinates))
        new_particle = self.world.add_particle(self.coordinates[0], self.coordinates[1], color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
807
        if new_particle:
808
            self.world.particle_map_coordinates[self.coordinates[0], self.coordinates[1]].created=True
Ahmad Reza's avatar
Ahmad Reza committed
809
            self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
810
811
            self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
            self.world.csv_round.update_metrics( particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
812
813
814
            return new_particle
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
815

816
    def create_particle_in(self, direction=None, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
817
        """
818
        Creates a particle either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
819

820
        :toDo: seperate the directionection and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
821

822
        :param direction: The directionection on which the particle should be created. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
823
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
824
        """
825
        coordinates = (0, 0)
826
        if direction is not None:
827
828
829
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Going to create a particle in %s on position %s", str(direction), str(coordinates))
            new_particle= self.world.add_particle(coordinates[0], coordinates[1], color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
830
            if new_particle:
831
832
                self.world.particle_map_coordinates[coordinates[0], coordinates[1]].created = True
                logging.info("Created particle on coordinates %s", coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
833
834
                self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
                self.world.csv_round.update_metrics( particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
835
                self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
836
837
838
                return new_particle
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
839
        else:
840
            logging.info("Not created particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
841
            return False
Ahmad Reza's avatar
Ahmad Reza committed
842

843
    def create_particle_on(self, x=None, y=None, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
844
845
846
        """
        Creates a particle either on a given x,y coordinates

847
        :toDo: seperate the directionection and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
848
849
850

        :param x: x coordinate
        :param y: y coordinate
Ahmad Reza's avatar
Ahmad Reza committed
851
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
852
        """
853
        coordinates = (0, 0)
Ahmad Reza's avatar
Ahmad Reza committed
854
        if x is not None and y is not None:
Karol Actun's avatar
Karol Actun committed
855
            if grid.is_valid_location(coordinates):
856
857
                logging.info("Going to create a particle on position %s", str(coordinates))
                new_particle = self.world.add_particle(coordinates[0], coordinates[1], color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
858
                if new_particle:
859
860
                    self.world.particle_map_coordinates[coordinates[0], coordinates[1]].created = True
                    logging.info("Created particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
861
862
                    self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
                    self.world.csv_round.update_metrics( particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
863
                    self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
864
                    return new_particle
Ahmad Reza's avatar
Ahmad Reza committed
865
866
867
868
869
                else:
                    return False
            else:
                return False
        else:
870
            logging.info("Not created particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
871
872
873
874
875
876
877
878
879
880
            return False

    def delete_particle(self):
        """
        Deletes a tile on current position

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is", self.get_id())
        logging.info("is going to delete a particle on current position")
881
882
        if self.coordinates in self.world.get_particle_map_coordinates():
            if self.world.remove_particle_on(self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
                self.csv_particle_writer.write_particle(particle_deleted=1)
                return True
        else:
            logging.info("Could not delet particle")
            return False

    def delete_particle_with(self, id):
        """
        Deletes a particle with a given id

        :param id: The id of the particle that should be deleted
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is", self.get_id())
        logging.info("is going to delete a particle with id %s", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
898
        if self.world.remove_particle(id):
Ahmad Reza's avatar
Ahmad Reza committed
899
            self.csv_particle_writer.write_particle(particle_deleted=1)
900
            return True
Ahmad Reza's avatar
Ahmad Reza committed
901
902
        else:
            logging.info("Could not delet particle with particle id %s", str(id))
903
            return False
Ahmad Reza's avatar
Ahmad Reza committed
904

905
    def delete_particle_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
906
        """
907
        Deletes a particle either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
908

909
        :param direction: The directionection on which the particle should be deleted. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
910
911
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
912
        if direction is not None:
913
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
914
            logging.info("Deleting tile in %s directionection", str(direction))