particle.py 53.5 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, alpha=1, particle_counter=0):
20
        """Initializing the marker constructor"""
21
22
23
        super().__init__( world, (x, y), color, alpha,
                          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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        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:
            return False
        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
        """
Ahmad Reza's avatar
Ahmad Reza committed
56
        if self.coords in self.world.tile_map_coords:
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
        """
Ahmad Reza's avatar
Ahmad Reza committed
67
        if self.coords in self.world.particle_map_coords:
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
        """
Ahmad Reza's avatar
Ahmad Reza committed
78
        if self.coords in self.world.marker_map_coords:
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
86
        """
        Moves the particle to the given direction

87
        :param direction: The direction 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
91
92
        direction_coord = get_coords_in_direction(self.coords, direction)
        direction, direction_coord = self.check_within_border(direction, direction_coord)
        if check_coords(direction_coord[0], direction_coord[1]):
Ahmad Reza's avatar
Ahmad Reza committed
93

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

97
98
            if not direction_coord in self.world.particle_map_coords:
                self.coords = direction_coord
Ahmad Reza's avatar
Ahmad Reza committed
99
                self.world.particle_map_coords[self.coords] = 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
110
111
112
113
114
115
    def check_for_carried_tile_or_particle(self):
        if self.carried_tile is not None:
            self.carried_tile.coords = self.coords
            self.carried_tile.touch()
        elif self.carried_particle is not None:
            self.carried_particle.coords = self.coords
            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
120
121
                (abs(direction_coord[0]) > self.world.get_world_x_size() or abs(direction_coord[1]) > self.world.get_world_y_size()):
            direction = direction - 3 if direction > 2 else direction + 3
            direction_coord = get_coords_in_direction(self.coords, direction)
        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
126
        """
            Moves the particle to the given direction if it would remain in bounds.

127
            :param direction: The direction 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
132
133
134
        direction_coord = get_coords_in_direction(self.coords, direction)
        sim_coord = coords_to_sim(direction_coord)
        if self.world.get_world_x_size() >=  abs(sim_coord[0]) and \
                        self.world.get_world_y_size() >=  abs(sim_coord[1]):
            return self.move_to(direction)
Ahmad Reza's avatar
Ahmad Reza committed
135
136
        else:
            # 'bounce' off the wall
137
            n_dir = direction - 3 if direction > 2 else direction + 3
Ahmad Reza's avatar
Ahmad Reza committed
138
139
140
141
            self.move_to(n_dir)

    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
153
154
        :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()

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

167
    def matter_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
168
        """
169
        :param direction: the direction 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_coords_in_direction(self.coords, direction) in self.world.get_tile_map_coords() \
            or get_coords_in_direction(self.coords, direction) in self.world.get_particle_map_coords() \
            or get_coords_in_direction(self.coords, direction) in self.world.get_marker_map_coords():
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 direction 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_coords_in_direction(self.coords, direction) in self.world.get_tile_map_coords():
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 direction 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_coords_in_direction(self.coords, direction) in self.world.get_particle_map_coords():
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 direction 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_coords_in_direction(self.coords, direction) in self.world.get_marker_map_coords():
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
210
211
212
213
214
215
    def get_matter_in(self, direction=E):
        if get_coords_in_direction(self.coords, direction) in self.world.get_tile_map_coords():
            return self.world.get_tile_map_coords()[get_coords_in_direction(self.coords, direction)]
        elif get_coords_in_direction(self.coords, direction) in self.world.get_particle_map_coords():
            return self.world.get_particle_map_coords()[get_coords_in_direction(self.coords, direction)]
        elif get_coords_in_direction(self.coords, direction) in self.world.get_marker_map_coords():
            return self.world.get_marker_map_coords()[get_coords_in_direction(self.coords, direction)]
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
216
217
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
218

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

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

231
232
233
    def get_marker_in(self, direction=E):
        if get_coords_in_direction(self.coords, direction) in self.world.get_marker_map_coords():
            return self.world.get_marker_map_coords()[get_coords_in_direction(self.coords, 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):
Ahmad Reza's avatar
Ahmad Reza committed
238
239
        if self.coords in self.world.marker_map_coords:
            return self.world.get_marker_map_coords()[self.coords]
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):
Ahmad Reza's avatar
Ahmad Reza committed
244
245
        if self.self.coords in self.world.get_tile_map_coords():
            return self.world.get_tile_map_coords()[self.coords]
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 directly 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
310
311
312
         :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
         """
        starting_x = self.coords[0]
        starting_y = self.coords[1]
        scanned_list = []
Ahmad Reza's avatar
Ahmad Reza committed
313
314
        logging.info("particle on %s is scanning for %s in %i hops", str(self.coords), matter, hop)

Ahmad Reza's avatar
Ahmad Reza committed
315
316
317
318
319
320
321
322
323
324
325
        if matter == "particles":
            scanned_list = scan(self.world.particle_map_coords, hop, starting_x, starting_y)
        elif matter == "tiles":
            scanned_list = scan(self.world.tile_map_coords, hop, starting_x, starting_y)
        elif matter == "markers":
            scanned_list = scan(self.world.marker_map_coords, hop, starting_x, starting_y)
        else:
            scanned_list = scan(self.world.particle_map_coords, hop, starting_x, starting_y)
            if scanned_list is not None:
                scanned_list.extend(scan(self.world.tile_map_coords, hop, starting_x, starting_y))
                scanned_list.extend(scan(self.world.marker_map_coords, hop, starting_x, starting_y))
Ahmad Reza's avatar
Ahmad Reza committed
326
            else:
Ahmad Reza's avatar
Ahmad Reza committed
327
328
329
330
331
332
333
                scanned_list = scan(self.world.tile_map_coords, hop, starting_x, starting_y)
                if scanned_list is not None:
                    scanned_list.extend(scan(self.world.marker_map_coords, hop, starting_x, starting_y))
                else:
                    scanned_list = scan(self.world.marker_map_coords, hop, starting_x, starting_y)
        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
435
436
437
438
439
440
441
442

    def take_me(self, coords=0):
        """
        The particle is getting taken from the the other particle on the given coordinate

        :param coords: Coordination of particle that should be taken
        :return: True: Successful taken; False: Cannot be taken or wrong Coordinates
        """

        if not self.__isCarried:
Ahmad Reza's avatar
Ahmad Reza committed
443
444
            if self.coords in self.world.particle_map_coords:
                del self.world.particle_map_coords[self.coords]
Ahmad Reza's avatar
Ahmad Reza committed
445
446
            self.__isCarried = True
            self.coords = coords
447
            self.set_alpha(0.5)
Ahmad Reza's avatar
Ahmad Reza committed
448
449
450
451
452
453
454
455
456
457
458
459
            self.touch()
            return True
        else:
            return False

    def drop_me(self, coords):
        """
        The actual particle is getting dropped

        :param coords: the given position
        :return: None
        """
Ahmad Reza's avatar
Ahmad Reza committed
460
        self.world.particle_map_coords[coords] = self
Ahmad Reza's avatar
Ahmad Reza committed
461
462
        self.coords = coords
        self.__isCarried = False
463
        self.set_alpha(1)
Ahmad Reza's avatar
Ahmad Reza committed
464
465
466
467
468
469
        self.touch()

    def create_tile(self, color=gray, alpha=1):
        """
        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.coords))
Ahmad Reza's avatar
Ahmad Reza committed
473
        new_tile = self.world.add_tile(self.coords[0], self.coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
474
        if new_tile:
Ahmad Reza's avatar
Ahmad Reza committed
475
            self.world.tile_map_coords[self.coords[0], self.coords[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, alpha=1):
Ahmad Reza's avatar
Ahmad Reza committed
484
485
486
        """
        Creates a tile either in a given direction

487
        :param direction: The direction 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
493
        logging.info("Going to create a tile in %s ", str(direction) )
        if direction != None:
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
494
            new_tile = self.world.add_tile(coords[0], coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
495
            if new_tile:
Ahmad Reza's avatar
Ahmad Reza committed
496
                self.world.tile_map_coords[coords[0], coords[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
510
511
512
513
514

    def create_tile_on(self, x=None, y=None, color=gray, alpha=1):
        """
        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
520
        """

        logging.info("particle with id %s is", self.get_id())
        if x is not None and y is not None:
            coords = (x, y)
521
            if check_coords(x,y):
Ahmad Reza's avatar
Ahmad Reza committed
522
                logging.info("Going to create a tile on position \(%i , %i\)", x,y )
Ahmad Reza's avatar
Ahmad Reza committed
523
524
525
                if self.world.add_tile(coords[0], coords[1], color, alpha) == True:
                    self.world.tile_map_coords[coords[0], coords[1]].created = True
                    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
531
532
533
534
                    return True
                else:
                    logging.info("Not created tile on coords  \(%i , %i\)", y,x )
                    return False
            else:
                logging.info("Not created tile on coords   \(%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")
Ahmad Reza's avatar
Ahmad Reza committed
545
546
        if self.coords in self.world.get_tile_map_coords():
            if self.world.remove_tile_on(self.coords):
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
572
        """
        Deletes a tile either in a given direction

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

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        coords = ()
578
579
580
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Deleting tile in %s direction", str(direction))
Ahmad Reza's avatar
Ahmad Reza committed
581
            if coords is not None:
Ahmad Reza's avatar
Ahmad Reza committed
582
                if self.world.remove_tile_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
                    logging.info("Deleted tile with tile on coords %s", str(coords))
                    self.csv_particle_writer.write_particle(tile_deleted=1)
                    return True
                else:
                    logging.info("Could not delet tile on coords %s", str(coords))
                    return False
        else:
            logging.info("Could not delet tile on coords %s", str(coords))
            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
        """
        coords = ()
        if x is not None and y is not None:
            coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
604
            if self.world.remove_tile_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
                logging.info("Deleted tile with tile on coords %s", str(coords))
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
            else:
                logging.info("Could not delet tile on coords %s", str(coords))
                return False
        else:
            logging.info("Could not delet tile on coords %s", str(coords))
            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:
Ahmad Reza's avatar
Ahmad Reza committed
623
624
            if self.coords in self.world.tile_map_coords:
                self.carried_tile = self.world.tile_map_coords[self.coords]
Ahmad Reza's avatar
Ahmad Reza committed
625
626
                if self.carried_tile.take(coords=self.coords):
                    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]
Ahmad Reza's avatar
Ahmad Reza committed
651
652
                if self.carried_tile.take(coords=self.coords):
                    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
669
        """
        Takes a tile that is in a given direction

670
        :param direction: The direction 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
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
675
676
677
            if coords in self.world.tile_map_coords:
                self.carried_tile = self.world.tile_map_coords[coords]
                logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
678
679
                if self.carried_tile.take(coords=self.coords):
                    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
695
696
697
698
699
700
701
                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):
        """
        Takes a tile that is in a given direction

        :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_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
703
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
704
705
706
                if coords in self.world.tile_map_coords:
                    self.carried_tile = self.world.tile_map_coords[coords]
                    logging.info("Tile with tile id %s is in the world", str(self.carried_tile.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
707
                    if self.carried_tile.take(coords=self.coords):
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:
Ahmad Reza's avatar
Ahmad Reza committed
732
            if self.coords not in self.world.tile_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
733
734
735
736
737
738
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_tile.drop_me(self.coords)
                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
751
        """
        Drops the taken tile on a given direction

752
         :param direction: The direction 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
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
756
            if coords not in self.world.tile_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
757
758
759
760
761
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_tile.drop_me(coords)
                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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
                self.csv_particle_writer.write_particle(tiles_dropped=1)
                logging.info("Dropped tile on %s coordinate", str(coords))
                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):
        """
        Drops the taken tile on a given direction

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

    def create_particle(self, color=black, alpha=1):
        """
        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
        """
        logging.info("Going to create on position %s", str(self.coords))
Ahmad Reza's avatar
Ahmad Reza committed
811
        new_particle = self.world.add_particle(self.coords[0], self.coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
812
        if new_particle:
Ahmad Reza's avatar
Ahmad Reza committed
813
            self.world.particle_map_coords[self.coords[0], self.coords[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, alpha=1):
Ahmad Reza's avatar
Ahmad Reza committed
822
823
824
825
826
        """
        Creates a particle either in a given direction

        :toDo: seperate the direction and coordinates and delete state

827
        :param direction: The direction 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
        """
        coords = (0, 0)
831
832
833
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Going to create a particle in %s on position %s", str(direction), str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
834
            new_particle= self.world.add_particle(coords[0], coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
835
            if new_particle:
Ahmad Reza's avatar
Ahmad Reza committed
836
                self.world.particle_map_coords[coords[0], coords[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
837
                logging.info("Created particle on coords %s", coords)
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
845
        else:
            logging.info("Not created particle on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
846
            return False
Ahmad Reza's avatar
Ahmad Reza committed
847
848
849
850
851
852
853
854
855

    def create_particle_on(self, x=None, y=None, color=black, alpha=1):
        """
        Creates a particle either on a given x,y coordinates

        :toDo: seperate the direction and coordinates and delete state

        :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
859
        """
        coords = (0, 0)
        if x is not None and y is not None:
860
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
861
862
                coords = (x, y)
                logging.info("Going to create a particle on position %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
863
                new_particle = self.world.add_particle(coords[0], coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
864
                if new_particle:
Ahmad Reza's avatar
Ahmad Reza committed
865
                    self.world.particle_map_coords[coords[0], coords[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
866
                    logging.info("Created particle on coords %s", str(coords))
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
876
877
878
879
880
881
882
883
884
885
886
                else:
                    return False
            else:
                return False
        else:
            logging.info("Not created particle on coords %s", str(coords))
            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")
Ahmad Reza's avatar
Ahmad Reza committed
887
888
        if self.coords in self.world.get_particle_map_coords():
            if self.world.remove_particle_on(self.coords):
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
906
907
908
909
            self.csv_particle_writer.write_particle(particle_deleted=1)
            return
        else:
            logging.info("Could not delet particle with particle id %s", str(id))

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

914
        :param direction: The direction on which the particle should be deleted. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
915
916
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
917
918
919
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Deleting tile in %s direction", str(direction))
Ahmad Reza's avatar
Ahmad Reza committed
920
            if self.world.remove_particle_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
921
922
923
924
                logging.info("Deleted particle with particle on coords %s", str(coords))
                self.csv_particle_writer.write_particle(particle_deleted=1)
            else:
                logging.info("Could not delet particle on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
925
926
927
928
929
930
931
932
933
934

    def delete_particle_on(self, x=None, y=None):
        """
        Deletes a particle either on a given x,y coordinates

        :param x: x coordinate
        :param y: y coordinate
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        if x is not None and y is not None:
935
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
936
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
937
                if self.world.remove_particle_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
                    logging.info("Deleted particle with particle on coords %s", str(coords))
                    self.csv_particle_writer.write_particle(particle_deleted=1)
                    return True
                else:
                    logging.info("Could not delet particle on coords %s", str(coords))
                    return False
            else:
                return False
        else:
            return False

    def take_particle(self):
        """
        Takes a particle on the actual position

        :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
956
957
            if self.coords in self.world.particle_map_coords:
                self.carried_particle = self.world.particle_map_coords[self.coords]
Ahmad Reza's avatar
Ahmad Reza committed
958
959
                if self.carried_particle.take_me(coords=self.coords):
                    logging.info("particle has been taken")
Ahmad Reza's avatar
Ahmad Reza committed
960
                    self.world.csv_round.update_metrics( particles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
961
962
963
964
965
966
                    self.csv_particle_writer.write_particle(particles_taken=1)
                    return True
                else:
                    logging.info("particle could not be taken")
                    return False
            else:
Ahmad Reza's avatar
Ahmad Reza committed
967
                logging.info("No particle on the actual position not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
968
969
970
971
972
973
974
975
976
977
978
979
980
                return False
        else:
            logging.info("particle cannot taken because particle is carrieng either a particle or a particle")
            return False

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

        :param id:  The id of the particle that should be taken
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_tile is None and self.carried_particle is None:
Ahmad Reza's avatar
Ahmad Reza committed
981
982
983
            if id in self.world.get_particle_map_id():
                logging.info("particle with particle id %s is in the world", str(id))
                self.carried_particle = self.world.particle_map_id[id]
Ahmad Reza's avatar
Ahmad Reza committed
984
985
986
                if self.carried_particle.take_me(self.coords):
                    logging.info("particle with particle id %s  has been taken", str(id))

Ahmad Reza's avatar
Ahmad Reza committed
987
                    self.world.csv_round.update_metrics(
Ahmad Reza's avatar
Ahmad Reza committed
988
989
990
991
992
993
994
                                                               particles_taken=1)
                    self.csv_particle_writer.write_particle(particles_taken=1)
                    return True
                else:
                    logging.info("particle with particle id %s could not be taken", str(id))
                    return False
            else:
Ahmad Reza's avatar
Ahmad Reza committed
995
                logging.info("particle with particle id %s is not in the world", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
996
997
998
        else:
            logging.info("particle cannot taken because particle is carrieng either a particle or a particle", str(id))

999
    def take_particle_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
1000
        """