particle.py 56.1 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"""
19
    def __init__(self, world, x, y, color=black, transparency=1, particle_counter=0):
20
        """Initializing the marker constructor"""
21
        super().__init__( world, (x, y), 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

87
        :param direction: The directionection must be either: E, SE, SW, W, NW, or NE
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)
92
        if check_values_are_coordinates(direction_coord[0], direction_coord[1]):
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":
316
            scanned_list = global_scanning(self.world.particle_map_coordinates, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
317
        elif matter == "tiles":
318
            scanned_list = global_scanning(self.world.tile_map_coordinates, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
319
        elif matter == "markers":
320
            scanned_list = global_scanning(self.world.marker_map_coordinates, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
321
        else:
322
            scanned_list = global_scanning(self.world.particle_map_coordinates, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
323
            if scanned_list is not None:
324
325
                scanned_list.extend(global_scanning(self.world.tile_map_coordinates, hop, starting_x, starting_y))
                scanned_list.extend(global_scanning(self.world.marker_map_coordinates, hop, starting_x, starting_y))
Ahmad Reza's avatar
Ahmad Reza committed
326
            else:
327
                scanned_list = global_scanning(self.world.tile_map_coordinates, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
328
                if scanned_list is not None:
329
                    scanned_list.extend(global_scanning(self.world.marker_map_coordinates, hop, starting_x, starting_y))
Ahmad Reza's avatar
Ahmad Reza committed
330
                else:
331
                    scanned_list = global_scanning(self.world.marker_map_coordinates, hop, starting_x, starting_y)
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
473
        logging.info("Going to create a tile on position %s", str(self.coordinates))
        new_tile = self.world.add_tile(self.coordinates[0], self.coordinates[1], color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
474
        if new_tile:
475
            self.world.tile_map_coordinates[self.coordinates[0], self.coordinates[1]].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
492
        logging.info("Going to create a tile in %s ", str(direction) )
        if direction != None:
493
494
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            new_tile = self.world.add_tile(coordinates[0], coordinates[1], color, transparency)
Ahmad Reza's avatar
Ahmad Reza committed
495
            if new_tile:
496
                self.world.tile_map_coordinates[coordinates[0], coordinates[1]].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

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

        :param x: x coordinate
        :param y: y coordinate
Ahmad Reza's avatar
Ahmad Reza committed
515
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
516
517
518
519
        """

        logging.info("particle with id %s is", self.get_id())
        if x is not None and y is not None:
520
            coordinates = (x, y)
521
            if check_values_are_coordinates(x,y):
Ahmad Reza's avatar
Ahmad Reza committed
522
                logging.info("Going to create a tile on position \(%i , %i\)", x,y )
523
524
                if self.world.add_tile(coordinates[0], coordinates[1], color, transparency) == True:
                    self.world.tile_map_coordinates[coordinates[0], coordinates[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
525
                    self.world.new_tile_flag = True
Ahmad Reza's avatar
Ahmad Reza committed
526
                    self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
527
528
                    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
529
530
                    return True
                else:
531
                    logging.info("Not created tile on coordinates  \(%i , %i\)", y,x )
Ahmad Reza's avatar
Ahmad Reza committed
532
533
                    return False
            else:
534
                logging.info("Not created tile on coordinates   \(%i , %i\)", y,x )
Ahmad Reza's avatar
Ahmad Reza committed
535
                return False
Ahmad Reza's avatar
Ahmad Reza committed
536
537
538
539
540
541
542
543
544

    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")
545
546
        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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
                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
562
        if self.world.remove_tile(id):
Ahmad Reza's avatar
Ahmad Reza committed
563
564
565
566
567
568
            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

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

573
        :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
574
575
576

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
577
        coordinates = ()
578
        if direction is not None:
579
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
580
            logging.info("Deleting tile in %s directionection", str(direction))
581
582
583
            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
584
585
586
                    self.csv_particle_writer.write_particle(tile_deleted=1)
                    return True
                else:
587
                    logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
588
589
                    return False
        else:
590
            logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
591
592
593
594
595
596
597
598
599
600
            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
        """
601
        coordinates = ()
Ahmad Reza's avatar
Ahmad Reza committed
602
        if x is not None and y is not None:
603
604
605
            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
606
607
608
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
            else:
609
                logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
610
611
                return False
        else:
612
            logging.info("Could not delet tile on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
613
614
615
616
617
618
619
620
621
622
            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:
623
624
625
            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
626
                    logging.info("Tile has been taken")
Ahmad Reza's avatar
Ahmad Reza committed
627
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
628
629
630
631
632
633
                    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
634
                logging.info("No tile on the actual position not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
635
636
637
638
639
640
641
642
643
644
645
646
647
                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
648
649
650
            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]
651
                if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
652
                    logging.info("Tile with tile id %s  has been taken", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
653
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
654
655
656
657
658
659
                    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
660
                logging.info("Tile with tile id %s is not in the world", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
661
662
663
664
665
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
            return False

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

670
        :param direction: The directionection on which the tile should be taken. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
671
672
673
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
674
675
676
            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
677
                logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
678
                if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
679
                    logging.info("Tile with tile id %s  has been taken", str(self.carried_tile.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
680
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
681
682
683
684
685
686
                    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
687
                logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
688
689
690
691
692
693
694
                return False
        else:
            logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
            return False

    def take_tile_on(self, x=None, y=None):
        """
695
        Takes a tile that is in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
696
697
698
699
700
701

        :param x: x coordinate
        :param y: y coordinate
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
702
            if check_values_are_coordinates(x, y):
703
704
705
                coordinates = (x, y)
                if coordinates in self.world.tile_map_coordinates:
                    self.carried_tile = self.world.tile_map_coordinates[coordinates]
Ahmad Reza's avatar
Ahmad Reza committed
706
                    logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
707
                    if self.carried_tile.take(coordinates=self.coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
708
                        self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
709
710
711
712
713
714
715
                        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
716
                    logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
                    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:
732
            if self.coordinates not in self.world.tile_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
733
                try:  # cher: insert so to overcome the AttributeError
734
                    self.carried_tile.drop_me(self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
735
736
737
738
                except AttributeError:
                    pass
                self.carried_tile = None
                logging.info("Tile has been dropped on the actual position")
Ahmad Reza's avatar
Ahmad Reza committed
739
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
740
741
742
743
744
745
746
747
                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

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

752
         :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
753
754
        """
        if self.carried_tile is not None:
755
756
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            if coordinates not in self.world.tile_map_coordinates:
Ahmad Reza's avatar
Ahmad Reza committed
757
                try:  # cher: insert so to overcome the AttributeError
758
                    self.carried_tile.drop_me(coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
759
760
761
                except AttributeError:
                    pass
                self.carried_tile = None
Ahmad Reza's avatar
Ahmad Reza committed
762
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
763
                self.csv_particle_writer.write_particle(tiles_dropped=1)
764
                logging.info("Dropped tile on %s coordinate", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
765
766
767
768
769
770
771
772
773
774
775
                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

    def drop_tile_on(self, x=None, y=None):
        """
776
        Drops the taken tile on a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
777
778
779
780
781

        :param x: x coordinate
        :param y: y coordinate
        """
        if self.carried_tile is not None:
782
            if check_values_are_coordinates(x, y):
783
784
                coordinates = (x, y)
                if coordinates not in self.world.get_tile_map_coordinates():
Ahmad Reza's avatar
Ahmad Reza committed
785
                    try:  # cher: insert so to overcome the AttributeError
786
                        self.carried_tile.drop_me(coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
787
788
789
                    except AttributeError:
                        pass
                    self.carried_tile = None
Ahmad Reza's avatar
Ahmad Reza committed
790
                    self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
791
                    self.csv_particle_writer.write_particle(tiles_dropped=1)
792
                    logging.info("Dropped tile on %s coordinate", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
793
794
795
796
797
798
799
800
801
802
803
                    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

804
    def create_particle(self, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
805
806
807
        """
        Creates a particle on the particles actual position

Ahmad Reza's avatar
Ahmad Reza committed
808
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
809
        """
810
811
        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
812
        if new_particle:
813
            self.world.particle_map_coordinates[self.coordinates[0], self.coordinates[1]].created=True
Ahmad Reza's avatar
Ahmad Reza committed
814
            self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
815
816
            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
817
818
819
            return new_particle
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
820

821
    def create_particle_in(self, direction=None, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
822
        """
823
        Creates a particle either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
824

825
        :toDo: seperate the directionection and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
826

827
        :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
828
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
829
        """
830
        coordinates = (0, 0)
831
        if direction is not None:
832
833
834
            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
835
            if new_particle:
836
837
                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
838
839
                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
840
                self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
841
842
843
                return new_particle
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
844
        else:
845
            logging.info("Not created particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
846
            return False
Ahmad Reza's avatar
Ahmad Reza committed
847

848
    def create_particle_on(self, x=None, y=None, color=black, transparency=1):
Ahmad Reza's avatar
Ahmad Reza committed
849
850
851
        """
        Creates a particle either on a given x,y coordinates

852
        :toDo: seperate the directionection and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
853
854
855

        :param x: x coordinate
        :param y: y coordinate
Ahmad Reza's avatar
Ahmad Reza committed
856
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
857
        """
858
        coordinates = (0, 0)
Ahmad Reza's avatar
Ahmad Reza committed
859
        if x is not None and y is not None:
860
            if check_values_are_coordinates(x, y):
861
862
863
                coordinates = (x, y)
                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
864
                if new_particle:
865
866
                    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
867
868
                    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
869
                    self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
870
                    return new_particle
Ahmad Reza's avatar
Ahmad Reza committed
871
872
873
874
875
                else:
                    return False
            else:
                return False
        else:
876
            logging.info("Not created particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
877
878
879
880
881
882
883
884
885
886
            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")
887
888
        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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
                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
904
        if self.world.remove_particle(id):
Ahmad Reza's avatar
Ahmad Reza committed
905
            self.csv_particle_writer.write_particle(particle_deleted=1)
906
            return True
Ahmad Reza's avatar
Ahmad Reza committed
907
908
        else:
            logging.info("Could not delet particle with particle id %s", str(id))
909
            return False
Ahmad Reza's avatar
Ahmad Reza committed
910

911
    def delete_particle_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
912
        """
913
        Deletes a particle either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
914

915
        :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
916
917
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
918
        if direction is not None:
919
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
920
            logging.info("Deleting tile in %s directionection", str(direction))
921
922
            if self.world.remove_particle_on(coordinates):
                logging.info("Deleted particle with particle on coordinates %s", str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
923
                self.csv_particle_writer.write_particle(particle_deleted=1)
924
                return True
Ahmad Reza's avatar
Ahmad Reza committed
925
            else:
926
                logging.info("Could not delet particle on coordinates %s", str(coordinates))