particle.py 53.8 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

16
particle_counter=0
Ahmad Reza's avatar
Ahmad Reza committed
17

Ahmad Reza's avatar
Ahmad Reza committed
18

Ahmad Reza's avatar
Ahmad Reza committed
19
class Particle(matter.Matter):
20
    """In the classe marker all the methods for the characterstic of a marker is included"""
21
    def __init__(self, world, x, y, color=black, alpha=1, particle_counter = particle_counter):
22
        """Initializing the marker constructor"""
23
24
25
        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
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
56
57
        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
58
        if self.coords in self.world.tile_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
59
60
61
62
63
64
65
66
67
68
            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
69
        if self.coords in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
70
71
72
73
            return True
        else:
            return False

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

78
        :return: True: On a marker; False: Not on a marker
Ahmad Reza's avatar
Ahmad Reza committed
79
        """
Ahmad Reza's avatar
Ahmad Reza committed
80
        if self.coords in self.world.marker_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
81
82
83
84
            return True
        else:
            return False

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

89
        :param direction: The directionection must be either: E, SE, SW, W, NW, or NE
Ahmad Reza's avatar
Ahmad Reza committed
90
91
        :return: True: Success Moving;  False: Non moving
        """
92
93
94
        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
95

Ahmad Reza's avatar
Ahmad Reza committed
96
97
            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
98

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

Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
110
111
112
113
114
115
116
117
    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()

118
    def check_within_border(self, direction, direction_coord):
Ahmad Reza's avatar
Ahmad Reza committed
119
        if self.world.config_data.border == 1 and \
120
121
122
123
                (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
            direction_coord = get_coords_in_direction(self.coords, direction)
        return direction, direction_coord
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
124

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

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

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

146
        :param matter: The matter can be either a particle, tile, or marker
Ahmad Reza's avatar
Ahmad Reza committed
147
148
149
150
151
152
153
154
155
156
        :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
157
                self.world.csv_round.update_metrics( particle_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
158
159
                self.csv_particle_writer.write_particle(particle_read=1)
            elif matter.type == "tile":
Ahmad Reza's avatar
Ahmad Reza committed
160
                self.world.csv_round.update_metrics( tile_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
161
                self.csv_particle_writer.write_particle(tile_read=1)
162
            elif matter.type == "marker":
Ahmad Reza's avatar
Ahmad Reza committed
163
                self.world.csv_round.update_metrics( marker_read=1)
164
                self.csv_particle_writer.write_particle(marker_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
165
166
167
168
            return tmp_memory
        else:
            return None

169
    def matter_in(self, direction=E):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
170
        """
171
        :param direction: the directionection to check if a matter is there
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
172
173
        :return: True: if a matter is there, False: if not
        """
174
175
176
        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
177
178
179
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
180

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

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

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

211
212
213
214
215
216
217
    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
218
219
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
220

221
222
223
    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
224
225
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
226

227
228
229
    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
230
231
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
232

233
234
235
    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
236
237
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
238

239
    def get_marker(self):
Ahmad Reza's avatar
Ahmad Reza committed
240
241
        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
242
243
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
244

Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
245
    def get_tile(self):
Ahmad Reza's avatar
Ahmad Reza committed
246
247
        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
248
249
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
250
251
252

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

255
        :param matter: The matter can be either a particle, tile, or marker
Ahmad Reza's avatar
Ahmad Reza committed
256
257
258
259
260
261
262
263
264
265
266
267
268
        :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
269
                    self.world.csv_round.update_metrics( particle_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
270
271
                    self.csv_particle_writer.write_particle(particle_write=1)
                elif matter.type == "tile":
Ahmad Reza's avatar
Ahmad Reza committed
272
                    self.world.csv_round.update_metrics( tile_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
273
                    self.csv_particle_writer.write_particle(tile_write=1)
274
                elif matter.type == "marker":
Ahmad Reza's avatar
Ahmad Reza committed
275
                    self.world.csv_round.update_metrics( marker_write=1)
276
                    self.csv_particle_writer.write_particle(marker_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
277
278
279
280
281
282
                return True
            else:
                return False
        else:
            return False

Ahmad Reza's avatar
Ahmad Reza committed
283
    def scan_for_matters_within(self, matter='all', hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
284
        """
285
        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
286
287
288

        :todo: If nothing then everything should be scanned

289
        :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
290
291
292
293
        :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
294
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
295
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
296
297
298
299
300
            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
301
302
303
        else:
            return None

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

Ahmad Reza's avatar
Ahmad Reza committed
308
309
310
311
312
313
314
         :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
315
316
        logging.info("particle on %s is scanning for %s in %i hops", str(self.coords), matter, hop)

Ahmad Reza's avatar
Ahmad Reza committed
317
        if matter == "particles":
318
            scanned_list = global_scanning(self.world.particle_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
319
        elif matter == "tiles":
320
            scanned_list = global_scanning(self.world.tile_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
321
        elif matter == "markers":
322
            scanned_list = global_scanning(self.world.marker_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
323
        else:
324
            scanned_list = global_scanning(self.world.particle_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
325
            if scanned_list is not None:
326
327
                scanned_list.extend(global_scanning(self.world.tile_map_coords, hop, starting_x, starting_y))
                scanned_list.extend(global_scanning(self.world.marker_map_coords, hop, starting_x, starting_y))
Ahmad Reza's avatar
Ahmad Reza committed
328
            else:
329
                scanned_list = global_scanning(self.world.tile_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
330
                if scanned_list is not None:
331
                    scanned_list.extend(global_scanning(self.world.marker_map_coords, hop, starting_x, starting_y))
Ahmad Reza's avatar
Ahmad Reza committed
332
                else:
333
                    scanned_list = global_scanning(self.world.marker_map_coords, hop, starting_x, starting_y)
Ahmad Reza's avatar
Ahmad Reza committed
334
335
        if scanned_list is not None:
            return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
336
337
338
        else:
            return None

Ahmad Reza's avatar
Ahmad Reza committed
339
    def scan_for_particles_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
340
        """
341
        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
342
343
344

        :todo: If nothing then everything should be scanned

345
        :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
346
347
348
349
        :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
350
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
351
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
352
353
354
355
356
            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
357
358
359
        else:
            return None

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

364
        :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
365
366
367
368
        :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
369
370
371
372
        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
373
        """
374
        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
375
376
377

        :todo: If nothing then everything should be scanned

378
        :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
379
380
381
382
        :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
383
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
384
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
385
386
387
388
389
            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
390
391
392
        else:
            return None

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

397
        :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
398
399
400
        :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
401
402
        scanned_list = self.scan_for_matters_in(matter='tiles', hop=hop)
        return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
403

Ahmad Reza's avatar
Ahmad Reza committed
404
    def scan_for_markers_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
405
        """
406
        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
407
408
409

        :todo: If nothing then everything should be scanned

410
        :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
411
412
413
414
        :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
415
        within_hop_list = []
Ahmad Reza's avatar
Ahmad Reza committed
416
        for i in range(1, hop + 1):
Ahmad Reza's avatar
Ahmad Reza committed
417
418
419
420
421
            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
422
423
424
        else:
            return None

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

429
        :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
430
431
432
        :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
433
434
        scanned_list = self.scan_for_matters_in(matter='markers', hop=hop)
        return scanned_list
Ahmad Reza's avatar
Ahmad Reza committed
435
436
437
438
439
440
441
442
443
444

    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
445
446
            if self.coords in self.world.particle_map_coords:
                del self.world.particle_map_coords[self.coords]
Ahmad Reza's avatar
Ahmad Reza committed
447
448
            self.__isCarried = True
            self.coords = coords
449
            self.set_alpha(0.5)
Ahmad Reza's avatar
Ahmad Reza committed
450
451
452
453
454
455
456
457
458
459
460
461
            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
462
        self.world.particle_map_coords[coords] = self
Ahmad Reza's avatar
Ahmad Reza committed
463
464
        self.coords = coords
        self.__isCarried = False
465
        self.set_alpha(1)
Ahmad Reza's avatar
Ahmad Reza committed
466
467
468
469
470
471
        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
472
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
473
474
        """
        logging.info("Going to create a tile on position %s", str(self.coords))
Ahmad Reza's avatar
Ahmad Reza committed
475
        new_tile = self.world.add_tile(self.coords[0], self.coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
476
        if new_tile:
Ahmad Reza's avatar
Ahmad Reza committed
477
            self.world.tile_map_coords[self.coords[0], self.coords[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
478
            self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
479
480
            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
481
482
483
            return new_tile
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
484

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

489
        :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
490
        :return: New tile or False
Ahmad Reza's avatar
Ahmad Reza committed
491
492
        """
        logging.info("particle with id %s is", self.get_id())
493
494
495
        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
496
            new_tile = self.world.add_tile(coords[0], coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
497
            if new_tile:
Ahmad Reza's avatar
Ahmad Reza committed
498
                self.world.tile_map_coords[coords[0], coords[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
499
                logging.info("Tile is created")
Ahmad Reza's avatar
Ahmad Reza committed
500
                self.world.new_tile_flag = True
Ahmad Reza's avatar
Ahmad Reza committed
501
                self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
502
503
                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
504
505
506
                return new_tile
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
507
508
        else:
            logging.info("Not created tile ")
Ahmad Reza's avatar
Ahmad Reza committed
509
            return False
Ahmad Reza's avatar
Ahmad Reza committed
510
511
512
513
514
515
516

    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
517
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
518
519
520
521
522
        """

        logging.info("particle with id %s is", self.get_id())
        if x is not None and y is not None:
            coords = (x, y)
523
            if check_coords(x,y):
Ahmad Reza's avatar
Ahmad Reza committed
524
                logging.info("Going to create a tile on position \(%i , %i\)", x,y )
Ahmad Reza's avatar
Ahmad Reza committed
525
526
527
                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
528
                    self.csv_particle_writer.write_particle(tile_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
529
530
                    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
531
532
533
534
535
536
                    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
537
                return False
Ahmad Reza's avatar
Ahmad Reza committed
538
539
540
541
542
543
544
545
546

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

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

575
        :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
576
577
578
579

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

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

672
        :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
673
674
675
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_particle is None and self.carried_tile is None:
676
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
677
678
679
            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
680
681
                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
682
                    self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
683
684
685
686
687
688
                    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
689
                logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
690
691
692
693
694
695
696
                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):
        """
697
        Takes a tile that is in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
698
699
700
701
702
703

        :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:
704
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
705
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
706
707
708
                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
709
                    if self.carried_tile.take(coords=self.coords):
Ahmad Reza's avatar
Ahmad Reza committed
710
                        self.world.csv_round.update_metrics( tiles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
711
712
713
714
715
716
717
                        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
718
                    logging.info("Tile is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
                    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
734
            if self.coords not in self.world.tile_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
735
736
737
738
739
740
                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
741
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
742
743
744
745
746
747
748
749
                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

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

754
         :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
755
756
        """
        if self.carried_tile is not None:
757
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
758
            if coords not in self.world.tile_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
759
760
761
762
763
                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
764
                self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
765
766
767
768
769
770
771
772
773
774
775
776
777
                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):
        """
778
        Drops the taken tile on a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
779
780
781
782
783

        :param x: x coordinate
        :param y: y coordinate
        """
        if self.carried_tile is not None:
784
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
785
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
786
                if coords not in self.world.get_tile_map_coords():
Ahmad Reza's avatar
Ahmad Reza committed
787
788
789
790
791
                    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
792
                    self.world.csv_round.update_metrics( tiles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
                    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
810
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
811
812
        """
        logging.info("Going to create on position %s", str(self.coords))
Ahmad Reza's avatar
Ahmad Reza committed
813
        new_particle = self.world.add_particle(self.coords[0], self.coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
814
        if new_particle:
Ahmad Reza's avatar
Ahmad Reza committed
815
            self.world.particle_map_coords[self.coords[0], self.coords[1]].created=True
Ahmad Reza's avatar
Ahmad Reza committed
816
            self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
817
818
            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
819
820
821
            return new_particle
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
822

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

827
        :toDo: seperate the directionection and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
828

829
        :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
830
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
831
832
        """
        coords = (0, 0)
833
834
835
        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
836
            new_particle= self.world.add_particle(coords[0], coords[1], color, alpha)
Ahmad Reza's avatar
Ahmad Reza committed
837
            if new_particle:
Ahmad Reza's avatar
Ahmad Reza committed
838
                self.world.particle_map_coords[coords[0], coords[1]].created = True
Ahmad Reza's avatar
Ahmad Reza committed
839
                logging.info("Created particle on coords %s", coords)
Ahmad Reza's avatar
Ahmad Reza committed
840
841
                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
842
                self.csv_particle_writer.write_particle(particle_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
843
844
845
                return new_particle
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
846
847
        else:
            logging.info("Not created particle on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
848
            return False
Ahmad Reza's avatar
Ahmad Reza committed
849
850
851
852
853

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

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

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

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

916
        :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
917
918
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
919
920
921
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Deleting tile in %s directionection", str(direction))
Ahmad Reza's avatar
Ahmad Reza committed
922
            if self.world.remove_particle_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
923
924
925
926
                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
927
928
929
930
931
932
933
934
935
936

    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:
937
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
938
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
939
                if self.world.remove_particle_on(coords):
Ahmad Reza's avatar
Ahmad Reza committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
                    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
958
959
            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
960
961
                if self.carried_particle.take_me(coords=self.coords):
                    logging.info("particle has been taken")
Ahmad Reza's avatar
Ahmad Reza committed
962
                    self.world.csv_round.update_metrics( particles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
963
964
965
966
967
968
                    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
969
                logging.info("No particle on the actual position not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
970
971
972
973
974
975
976
977
978
979
980
981
982
                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
983
984
985
            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
986
987
988
                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
989
                    self.world.csv_round.update_metrics(
Ahmad Reza's avatar
Ahmad Reza committed
990
991
992
993
994
995
996
                                                               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
997
                logging.info("particle with particle id %s is not in the world", str(id))
Ahmad Reza's avatar
Ahmad Reza committed
998
999
1000
        else:
            logging.info("particle cannot taken because particle is carrieng either a particle or a particle", str(id))

For faster browsing, not all history is shown. View entire blame