particle.py 44.9 KB
Newer Older
1
2
3
4
5
6
7
8
"""
.. module:: particle
   :platform: Unix, Windows
   :synopsis: This module provides the interfaces of the robotics particle

.. moduleauthor:: Ahmad Reza Cheraghi

TODO: Erase Memory
Ahmad Reza's avatar
Ahmad Reza committed
9
10
11

"""

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

Ahmad Reza's avatar
Ahmad Reza committed
16

17
class Particle(matter.Matter):
Ahmad Reza's avatar
Ahmad Reza committed
18

19
20
21
22
23
24
25
26
27
28
    def __init__(self, world, coordinates, color, particle_counter=0):
        """Initializing the particle constructor"""
        super().__init__(world, coordinates, color,
                         type="particle", mm_size=world.config_data.particle_mm_size)
        self.number = particle_counter
        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)
Ahmad Reza's avatar
Ahmad Reza committed
29

30
31
32
33
34
    def has_tile(self):
        if self.carried_tile is None:
            return False
        else:
            return True
Ahmad Reza's avatar
Ahmad Reza committed
35

36
37
38
    def has_particle(self):
        if self.carried_particle is None:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
39
        else:
40
            return True
Ahmad Reza's avatar
Ahmad Reza committed
41

42
43
44
    def get_carried_status(self):
        """
        Get the status if it is taken or not
Ahmad Reza's avatar
Ahmad Reza committed
45

46
47
48
        :return: Tiles status
        """
        return self.__isCarried
Ahmad Reza's avatar
Ahmad Reza committed
49

50
51
52
    def check_on_tile(self):
        """
        Checks if the particle is on a tile
Ahmad Reza's avatar
Ahmad Reza committed
53

54
55
56
57
58
59
60
61
62
63
        :return: True: On a tile; False: Not on a Tile
        """
        if self.coordinates in self.world.tile_map_coordinates:
            return True
        else:
            return False

    def check_on_particle(self):
        """
        Checks if the particle is on a particle
Karol Actun's avatar
Karol Actun committed
64

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

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

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        :return: True: On a location; False: Not on a location
        """
        if self.coordinates in self.world.location_map_coordinates:
            return True
        else:
            return False

    def move_to(self, direction):
        """
        Moves the particle to the given direction

        :param direction: The direction is defined by loaded grid class
        :return: True: Success Moving;  False: Non moving
        """
        direction_coord = get_coordinates_in_direction(self.coordinates, direction)
        direction_coord = self.check_within_border(direction, direction_coord)
        if self.world.grid.are_valid_coordinates(direction_coord) \
        and direction_coord not in self.world.particle_map_coordinates:
            if self.coordinates in self.world.particle_map_coordinates:
                del self.world.particle_map_coordinates[self.coordinates]
            self.coordinates = direction_coord
            self.world.particle_map_coordinates[self.coordinates] = self
            if self.world.vis is not None:
                self.world.vis.particle_changed(self)
            logging.info("particle %s successfully moved to %s", str(self.get_id()), direction)
            self.world.csv_round.update_metrics(steps=1)
            self.csv_particle_writer.write_particle(steps=1)
            self.check_for_carried_tile_or_particle()
            return True

        return False

    def check_for_carried_tile_or_particle(self):
        if self.carried_tile is not None:
            self.carried_tile.coordinates = self.coordinates
            if self.world.vis is not None:
                self.world.vis.tile_changed(self.carried_tile)
        elif self.carried_particle is not None:
            self.carried_particle.coordinates = self.coordinates
            if self.world.vis is not None:
                self.world.vis.particle_changed(self.carried_particle)

    def check_within_border(self, direction, direction_coord):
        if self.world.config_data.border == 1:
            if self.world.config_data.type == 1:
                if abs(direction_coord[0]) > self.world.get_x_size():
                    direction_coord = (-1 * (self.coordinates[0] - direction[0]), direction_coord[1], direction_coord[2])
                if abs(direction_coord[1]) > self.world.get_y_size():
                    direction_coord = (direction_coord[0], -1* (self.coordinates[1] - direction[1]), direction_coord[2])
                if abs(direction_coord[2]) > self.world.get_z_size():
                    direction_coord = (direction_coord[0], direction_coord[1], -1* (self.coordinates[2] - direction[2]))
            else:
                if abs(direction_coord[0]) > self.world.get_x_size():
                    direction_coord = (self.coordinates[0], direction_coord[1], direction_coord[2])
                if abs(direction_coord[1]) > self.world.get_y_size():
                    direction_coord = (direction_coord[0], self.coordinates[1], direction_coord[2])
                if abs(direction_coord[2]) > self.world.get_z_size():
                    direction_coord = (direction_coord[0], direction_coord[1], self.coordinates[2])
        return direction_coord
Ahmad Reza's avatar
Ahmad Reza committed
135

136
    def read_from_with(self, target, key=None):
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
137
        """
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        Read the memories from the matters (particle, tile, or location object) memories with a given keyword

        :param target: The matter can be either a particle, tile, or location
        :param key: A string keyword to searcg for the data in the memory
        :return: The matters memory; None
        """
        if key is not None:
            tmp_memory = target.read_memory_with(key)
        else:
            tmp_memory = target.read_whole_memory()

        if tmp_memory is not None \
        and not (hasattr(tmp_memory, '__len__')) or len(tmp_memory) > 0:
            if target.type == "particle":
                self.world.csv_round.update_metrics(particle_read=1)
                self.csv_particle_writer.write_particle(particle_read=1)
            elif target.type == "tile":
                self.world.csv_round.update_metrics(tile_read=1)
                self.csv_particle_writer.write_particle(tile_read=1)
            elif target.type == "location":
                self.world.csv_round.update_metrics(location_read=1)
                self.csv_particle_writer.write_particle(location_read=1)
            return tmp_memory
        return None

    def matter_in(self, direction):
        """
        :param direction: the directionection to check if a matter is there
        :return: True: if a matter is there, False: if not
        """
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates() \
                or get_coordinates_in_direction(self.coordinates, direction) \
                in self.world.get_particle_map_coordinates() \
                or get_coordinates_in_direction(self.coordinates, direction) \
                in self.world.get_location_map_coordinates():
            return True
        else:
            return False

    def tile_in(self, direction):
        """
        :param direction: the direction to check if a tile is there
        :return: True: if a tile is there, False: if not
Ahmad Reza's avatar
Ahmad Reza committed
181
        """
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
            return True
        else:
            return False

    def particle_in(self, direction):
        """
        :param direction: the direction to check if a particle is there
        :return: True: if a particle is there, False: if not
        """
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
196

197
    def location_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
198
        """
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        :param direction: the direction to check if a location is there
        :return: True: if a location is there, False: if not
        """
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_location_map_coordinates():
            return True
        else:
            return False

    def get_matter_in(self, direction):
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        elif get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
            return self.world.get_particle_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        elif get_coordinates_in_direction(self.coordinates, direction) in self.world.get_location_map_coordinates():
            return self.world.get_location_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        else:
            return False

    def get_tile_in(self, direction):
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        else:
            return False

    def get_particle_in(self, direction):
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_particle_map_coordinates():
            return self.world.get_particle_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
228

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    def get_location_in(self, direction):
        if get_coordinates_in_direction(self.coordinates, direction) in self.world.get_location_map_coordinates():
            return self.world.get_location_map_coordinates()[get_coordinates_in_direction(self.coordinates, direction)]
        else:
            return False

    def get_location(self):
        if self.coordinates in self.world.location_map_coordinates:
            return self.world.get_location_map_coordinates()[self.coordinates]
        else:
            return False

    def get_tile(self):
        if self.coordinates in self.world.get_tile_map_coordinates():
            return self.world.get_tile_map_coordinates()[self.coordinates]
        else:
            return False

    def write_to_with(self, target, key=None, data=None):
Ahmad Reza's avatar
Ahmad Reza committed
248
        """
249
        Writes data with given a keyword direction on the matters (particle, tile, or location object) memory
Ahmad Reza's avatar
Ahmad Reza committed
250

251
252
253
254
        :param target: The matter can be either a particle, tile, or location
        :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
Ahmad Reza's avatar
Ahmad Reza committed
255
        """
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        if data is not None:
            if key is None:
                wrote = target.write_memory(data)
            else:
                wrote = target.write_memory_with(key, data)
            if wrote:
                if target.type == "particle":
                    self.world.csv_round.update_metrics(particle_write=1)
                    self.csv_particle_writer.write_particle(particle_write=1)
                elif target.type == "tile":
                    self.world.csv_round.update_metrics(tile_write=1)
                    self.csv_particle_writer.write_particle(tile_write=1)
                elif target.type == "location":
                    self.world.csv_round.update_metrics(location_write=1)
                    self.csv_particle_writer.write_particle(location_write=1)
                return True
            else:
                return False
        else:
            return False

    def scan_for_matters_within(self, matter_type='all', hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
278
        """
279
280
281
        Scans for particles, tiles, or locations on a given hop distance and all the matters within the hop distance

        :todo: If nothing then everything should be scanned
Ahmad Reza's avatar
Ahmad Reza committed
282

283
284
285
286
        :param matter_type: For what matter this method should scan for.
                            Can be either particles, tiles, locations, or (default) all
        :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
287
        """
288
289
290
291
292
293
294
295
296
297
298
299

        within_hop_list = []
        for i in range(hop + 1):
            in_list = self.scan_for_matters_in(matter_type, i)
            if in_list is not None:
                within_hop_list.extend(in_list)
        if len(within_hop_list) != 0:
            return within_hop_list
        else:
            return None

    def scan_for_matters_in(self, matter_type='all', hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
300
        """
301
302
303
304
305
306
307
         Scanning for particles, tiles, or locations on a given hop distance

         :param matter_type: For what matter this method should scan for.
                             Can be either particles, tiles, locations, or (default) all
         :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
308

309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
        logging.info("particle on %s is scanning for %s in %i hops", str(self.coordinates), matter_type, hop)

        if matter_type == "particles":
            scanned_list = scan_in(self.world.particle_map_coordinates, self.coordinates, hop, self.world.grid)
        elif matter_type == "tiles":
            scanned_list = scan_in(self.world.tile_map_coordinates, self.coordinates, hop, self.world.grid)
        elif matter_type == "locations":
            scanned_list = scan_in(self.world.location_map_coordinates, self.coordinates, hop, self.world.grid)
        else:
            scanned_list = []
            scanned_list.extend(scan_in(self.world.particle_map_coordinates, self.coordinates, hop, self.world.grid))
            scanned_list.extend(scan_in(self.world.tile_map_coordinates, self.coordinates, hop, self.world.grid))
            scanned_list.extend(scan_in(self.world.location_map_coordinates, self.coordinates, hop, self.world.grid))
        return scanned_list

    def scan_for_particles_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
325
        """
326
327
328
        Scans for particles on a given hop distance and all the matters within the hop distance

        :todo: If nothing then everything should be scanned
Ahmad Reza's avatar
Ahmad Reza committed
329

330
331
        :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
332
        """
333
        return scan_within(self.world.particle_map_coordinates, self.coordinates, hop, self.world.grid)
Ahmad Reza's avatar
Ahmad Reza committed
334

335
    def scan_for_particles_in(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
336
        """
337
        Scanning for particles on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
338

339
340
        :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
341
342
        """

343
344
345
        return scan_in(self.world.particle_map_coordinates, self.coordinates, hop, self.world.grid)

    def scan_for_tiles_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
346
        """
347
348
349
        Scans for tiles on a given hop distance and all the matters within the hop distance

        :todo: If nothing then everything should be scanned
Ahmad Reza's avatar
Ahmad Reza committed
350

351
352
        :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
353
354
        """

355
356
357
        return scan_within(self.world.tile_map_coordinates, self.coordinates, hop, self.world.grid)

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

361
362
        :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
363
        """
364
        return scan_in(self.world.tile_map_coordinates, self.coordinates, hop, self.world.grid)
Ahmad Reza's avatar
Ahmad Reza committed
365

366
    def scan_for_locations_within(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
367
        """
368
369
370
        Scans for particles, tiles, or location on a given hop distance and all the matters within the hop distance

        :todo: If nothing then everything should be scanned
Ahmad Reza's avatar
Ahmad Reza committed
371

372
373
        :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
374
375
        """

376
377
378
        return scan_within(self.world.location_map_coordinates, self.coordinates, hop, self.world.grid)

    def scan_for_locations_in(self, hop=1):
Ahmad Reza's avatar
Ahmad Reza committed
379
        """
380
        Scanning for particles, tiles, or location on a given hop distance
Ahmad Reza's avatar
Ahmad Reza committed
381

382
383
        :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
384
        """
385
        return scan_in(self.world.location_map_coordinates, self.coordinates, hop, self.world.grid)
Ahmad Reza's avatar
Ahmad Reza committed
386

387
    def take_me(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
388
        """
389
        The particle is getting taken from the the other particle on the given coordinate
Ahmad Reza's avatar
Ahmad Reza committed
390

391
392
        :param coordinates, the coordinates of the particle which takes this particle
        :return: True: Successful taken; False: Cannot be taken or wrong Coordinates
Ahmad Reza's avatar
Ahmad Reza committed
393
394
        """

395
396
397
398
399
400
401
402
403
404
405
406
        if not self.__isCarried:
            if self.coordinates in self.world.particle_map_coordinates:
                del self.world.particle_map_coordinates[self.coordinates]
            self.__isCarried = True
            self.coordinates = coordinates
            if self.world.vis is not None:
                self.world.vis.particle_changed(self)
            return True
        else:
            return False

    def drop_me(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
407
        """
408
        The actual particle is getting dropped
Ahmad Reza's avatar
Ahmad Reza committed
409

410
411
        :param coordinates: the given position
        :return: None
Ahmad Reza's avatar
Ahmad Reza committed
412
        """
413
414
415
416
417
        self.coordinates = coordinates
        self.world.particle_map_coordinates[coordinates] = self
        self.__isCarried = False
        if self.world.vis is not None:
            self.world.vis.particle_changed(self)
Ahmad Reza's avatar
Ahmad Reza committed
418

419
    def create_tile(self):
Ahmad Reza's avatar
Ahmad Reza committed
420
        """
421
        Creates a tile on the particles actual position
Ahmad Reza's avatar
Ahmad Reza committed
422

423
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
424
        """
425
426
427
428
429
430
431
432
433
434
        logging.info("Going to create a tile on position %s", str(self.coordinates))
        new_tile = self.world.add_tile(self.coordinates)
        if new_tile:
            self.world.tile_map_coordinates[self.coordinates].created = True
            self.csv_particle_writer.write_particle(tile_created=1)
            self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()))
            self.world.csv_round.update_metrics(tile_created=1)
            return new_tile
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
435

436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    def create_tile_in(self, direction=None):
        """
        Creates a tile either in a given direction

        :param direction: The direction on which the tile should be created.
        :return: New tile or False
        """
        logging.info("particle with id %s is" % self.get_id())
        logging.info("Going to create a tile in %s " % str(direction))
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            new_tile = self.world.add_tile(coordinates)
            if new_tile:
                self.world.tile_map_coordinates[coordinates].created = True
                logging.info("Tile is created")
                self.world.new_tile_flag = True
                self.csv_particle_writer.write_particle(tile_created=1)
                self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()))
                self.world.csv_round.update_metrics(tile_created=1)
                return new_tile
            else:
                return False
        else:
            logging.info("Not created tile ")
            return False

    def create_tile_on(self, coordinates=None):
Ahmad Reza's avatar
Ahmad Reza committed
463
        """
464
        Creates a tile either on a given x,y coordinates
Ahmad Reza's avatar
Ahmad Reza committed
465

466
467
        :param coordinates: the coordinates
        :return: New Tile or False
Ahmad Reza's avatar
Ahmad Reza committed
468
469
        """

470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
        logging.info("particle with id %s is", self.get_id())
        if coordinates is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                logging.info("Going to create a tile on position %s" % str(coordinates))
                if self.world.add_tile(coordinates):
                    self.world.tile_map_coordinates[coordinates].created = True
                    self.world.new_tile_flag = True
                    self.csv_particle_writer.write_particle(tile_created=1)
                    self.world.csv_round.update_tiles_num(len(self.world.get_tiles_list()))
                    self.world.csv_round.update_metrics(tile_created=1)
                    return True
                else:
                    logging.info("Not created tile on coordinates %s" % str(coordinates))
                    return False
            else:
                logging.info("Not created tile on coordinates %s" % str(coordinates))
                return False

    def delete_tile(self):
Ahmad Reza's avatar
Ahmad Reza committed
489
        """
490
        Deletes a tile on current position
Ahmad Reza's avatar
Ahmad Reza committed
491

492
        :return: True: Deleting successful; False: Deleting unsuccessful
Ahmad Reza's avatar
Ahmad Reza committed
493
        """
494
495
496
497
498
499
500
501
502
        logging.info("Particle %s is" % self.get_id())
        logging.info("is going to delete a tile on current position")
        if self.coordinates in self.world.get_tile_map_coordinates():
            if self.world.remove_tile_on(self.coordinates):
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
        else:
            logging.info("Could not delet tile")
            return False
Ahmad Reza's avatar
Ahmad Reza committed
503

504
    def delete_tile_with(self, tile_id):
Ahmad Reza's avatar
Ahmad Reza committed
505
        """
506
        Deletes a tile with a given tile-id
Ahmad Reza's avatar
Ahmad Reza committed
507

508
509
        :param tile_id: The id of the tile that should be deleted
        :return: True: Deleting successful; False: Deleting unsuccessful
Ahmad Reza's avatar
Ahmad Reza committed
510
        """
511
512
513
514
515
516
517
518
        logging.info("Particle %s is" % self.get_id())
        logging.info("is going to delete a tile with tile id %s" % str(tile_id))
        if self.world.remove_tile(tile_id):
            self.csv_particle_writer.write_particle(tile_deleted=1)
            return True
        else:
            logging.info("Could not delet tile with tile id %s" % str(tile_id))
            return False
Ahmad Reza's avatar
Ahmad Reza committed
519

520
    def delete_tile_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
521
        """
522
        Deletes a tile either in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
523

524
525
526
        :param direction: The directionection on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,

        :return: True: Deleting successful; False: Deleting unsuccessful
Ahmad Reza's avatar
Ahmad Reza committed
527
        """
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        coordinates = ()
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Deleting tile in %s directionection" % str(direction))
            if coordinates is not None:
                if self.world.remove_tile_on(coordinates):
                    logging.info("Deleted tile with tile on coordinates %s" % str(coordinates))
                    self.csv_particle_writer.write_particle(tile_deleted=1)
                    return True
                else:
                    logging.info("Could not delet tile on coordinates %s" % str(coordinates))
                    return False
        else:
            logging.info("Could not delet tile on coordinates %s" % str(coordinates))
            return False
Ahmad Reza's avatar
Ahmad Reza committed
543

544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    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
        """
        coordinates = ()
        if x is not None and y is not None:
            coordinates = (x, y)
            if self.world.remove_tile_on(coordinates):
                logging.info("Deleted tile with tile on coordinates %s" % str(coordinates))
                self.csv_particle_writer.write_particle(tile_deleted=1)
                return True
            else:
                logging.info("Could not delet tile on coordinates %s" % str(coordinates))
                return False
        else:
            logging.info("Could not delet tile on coordinates %s" % str(coordinates))
            return False

    def take_tile_with(self, tile_id):
Ahmad Reza's avatar
Ahmad Reza committed
567
        """
568
        Takes a tile with a given tile id
Ahmad Reza's avatar
Ahmad Reza committed
569

570
571
        :param tile_id:  The id of the tile that should be taken
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
572
        """
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
        if self.carried_particle is None and self.carried_tile is None:
            if tile_id in self.world.tile_map_id:
                self.carried_tile = self.world.tile_map_id[tile_id]
                if self.carried_tile.take():
                    logging.info("Tile with tile id %s  has been taken", str(tile_id))
                    self.carried_tile.coordinates = self.coordinates
                    if self.world.vis is not None:
                        self.world.vis.tile_changed(self.carried_tile)
                    self.world.csv_round.update_metrics(tiles_taken=1)
                    self.csv_particle_writer.write_particle(tiles_taken=1)
                    return True
                else:
                    self.carried_tile = None
                    logging.info("Tile with tile id %s could not be taken" % str(tile_id))
                    return False
            else:
                logging.info("Tile with tile id %s is not in the world" % str(tile_id))
                return False
        else:
Ahmad Reza's avatar
Ahmad Reza committed
592

593
594
595
596
597
            logging.info("Tile cannot taken because particle is carrying either a tile or a particle (%s, %s)"
                         % (str(self.carried_tile), str(self.carried_particle)))
            return False

    def take_tile_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
598
        """
599
        Takes a tile that is in a given direction
Ahmad Reza's avatar
Ahmad Reza committed
600

601
602
        :param direction: The direction on which the tile should be taken.
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
603
        """
604
605
        coordinates = get_coordinates_in_direction(self.coordinates, direction)
        return self.take_tile_on(coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
606

607
    def take_tile_on(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
608
        """
609
610
611
612
        Takes a tile on given coordinates

        :param coordinates of the tile
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
613
        """
614
615
616
617
618
619
620
621
622
        if self.world.grid.are_valid_coordinates(coordinates):
            if coordinates in self.world.tile_map_coordinates:
                return self.take_tile_with(self.world.tile_map_coordinates[coordinates].get_id())
            else:
                logging.info("There is no Tile at %s" % str(coordinates))
                return False
        else:
            logging.info("invalid coordinates %s" % str(coordinates))
            return False
Ahmad Reza's avatar
Ahmad Reza committed
623

624
    def take_tile(self):
Ahmad Reza's avatar
Ahmad Reza committed
625
        """
626
        Takes a tile on the actual position
Ahmad Reza's avatar
Ahmad Reza committed
627

628
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
629
        """
630
        return self.take_tile_on(self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
631

632
    def drop_tile(self):
Ahmad Reza's avatar
Ahmad Reza committed
633
        """
634
635
636
        Drops the taken tile on the particles actual position

        :return: None
Ahmad Reza's avatar
Ahmad Reza committed
637
        """
638
        return self.drop_tile_on(self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
639

640
641
642
    def drop_tile_in(self, direction):
        """
        Drops the taken tile on a given direction
Ahmad Reza's avatar
Ahmad Reza committed
643

644
645
646
         :param direction: The directionection on which the tile should be dropped. Options: E, SE, SW, W, NW, NE,
        """
        return self.drop_tile_on(get_coordinates_in_direction(self.coordinates, direction))
Ahmad Reza's avatar
Ahmad Reza committed
647

648
649
650
    def drop_tile_on(self, coordinates):
        """
        Drops the taken tile on a given direction
Ahmad Reza's avatar
Ahmad Reza committed
651

652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
        :param coordinates
        """
        if self.carried_tile is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                if coordinates not in self.world.get_tile_map_coordinates():
                    try:  # cher: insert so to overcome the AttributeError
                        self.carried_tile.drop_me(coordinates)
                    except AttributeError:
                        pass
                    self.carried_tile = None
                    self.world.csv_round.update_metrics(tiles_dropped=1)
                    self.csv_particle_writer.write_particle(tiles_dropped=1)
                    logging.info("Dropped tile on %s coordinate", str(coordinates))
                    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
Ahmad Reza's avatar
Ahmad Reza committed
675

676
677
678
    def create_particle(self):
        """
        Creates a particle on the particles actual position
Ahmad Reza's avatar
Ahmad Reza committed
679

680
681
682
683
684
685
686
687
688
689
690
691
        :return: New Particle or False
        """
        logging.info("Going to create on position %s", str(self.coordinates))
        new_particle = self.world.add_particle(self.coordinates)
        if new_particle:
            self.world.particle_map_coordinates[self.coordinates[0], self.coordinates[1]].created = True
            self.csv_particle_writer.write_particle(particle_created=1)
            self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
            self.world.csv_round.update_metrics(particle_created=1)
            return new_particle
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
692

693
    def create_particle_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
694
        """
695
        Creates a particle either in a given direction
Ahmad Reza's avatar
Ahmad Reza committed
696

697
698
699
700
        :toDo: separate the direction and coordinates and delete state

        :param direction: The direction on which the particle should be created. Options: E, SE, SW, W, NW, NE,
        :return: New Particle or False
Ahmad Reza's avatar
Ahmad Reza committed
701
        """
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Going to create a particle in %s on position %s", str(direction), str(coordinates))
            new_particle = self.world.add_particle(coordinates)
            if new_particle:
                self.world.particle_map_coordinates[coordinates].created = True
                logging.info("Created particle on coordinates %s", coordinates)
                self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
                self.world.csv_round.update_metrics(particle_created=1)
                self.csv_particle_writer.write_particle(particle_created=1)
                return new_particle
            else:
                return False
        else:
            logging.info("Particle not created. invalid direction (None)")
            return False
Ahmad Reza's avatar
Ahmad Reza committed
718

719
720
721
    def create_particle_on(self, coordinates):
        """
        Creates a particle either on the given coordinates
722

723
        :toDo: separate the direction and coordinates and delete state
Ahmad Reza's avatar
Ahmad Reza committed
724

725
726
727
728
729
730
731
732
733
734
735
736
737
738
        :param coordinates: the coordinates
        :return: New Particle or False
        """
        if coordinates is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                logging.info("Going to create a particle on position %s" % str(coordinates))
                new_particle = self.world.add_particle(coordinates)
                if new_particle:
                    self.world.particle_map_coordinates[coordinates].created = True
                    logging.info("Created particle on coordinates %s" % str(coordinates))
                    self.world.csv_round.update_particle_num(len(self.world.get_particle_list()))
                    self.world.csv_round.update_metrics(particle_created=1)
                    self.csv_particle_writer.write_particle(particle_created=1)
                    return new_particle
Ahmad Reza's avatar
Ahmad Reza committed
739
740
741
742
743
                else:
                    return False
            else:
                return False
        else:
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
            logging.info("Not created particle on coordinates %s" % str(coordinates))
            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")
        if self.coordinates in self.world.get_particle_map_coordinates():
            if self.world.remove_particle_on(self.coordinates):
                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, particle_id):
        """
        Deletes a particle with a given id

        :param particle_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(particle_id))
        if self.world.remove_particle(particle_id):
            self.csv_particle_writer.write_particle(particle_deleted=1)
            return True
        else:
            logging.info("Could not delet particle with particle id %s" % str(particle_id))
            return False

    def delete_particle_in(self, direction=None):
        """
        Deletes a particle either in a given directionection

        :param direction: The directionection on which the particle should be deleted. Options: E, SE, SW, W, NW, NE,
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Deleting tile in %s directionection" % str(direction))
            if self.world.remove_particle_on(coordinates):
                logging.info("Deleted particle with particle on coordinates %s" % str(coordinates))
                self.csv_particle_writer.write_particle(particle_deleted=1)
                return True
            else:
                logging.info("Could not delet particle on coordinates %s" % str(coordinates))
                return False

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

        :param coordinates
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        if coordinates is None:
            logging.info("coordinates are 'None'...")
            return False

        if not self.world.grid.are_valid_coordinates(coordinates):
            logging.info("invalid coordinates")
            return False

        if self.world.remove_particle_on(coordinates):
            logging.info("Deleted particle with particle on coordinates %s" % str(coordinates))
            self.csv_particle_writer.write_particle(particle_deleted=1)
            return True
        else:
            logging.info("Could not delete particle on coordinates %s" % str(coordinates))
Ahmad Reza's avatar
Ahmad Reza committed
818
819
            return False

820

821
822
823
    def take_particle_with(self, particle_id):
        """
        Takes a particle with a given tile id
Ahmad Reza's avatar
Ahmad Reza committed
824

825
826
        :param particle_id:  The id of the particle that should be taken
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
827
        """
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
        if self.carried_tile is not None or self.carried_particle is not None:
            logging.info("particle %s is already carrying a particle or a tile" % str(self.get_id()))
            return False

        if particle_id not in self.world.get_particle_map_id():
            logging.info("particle with particle id %s is not in the world" % str(particle_id))
            return False

        self.carried_particle = self.world.particle_map_id[particle_id]
        if self.carried_particle.take_me(self.coordinates):
            logging.info("particle with particle id %s  has been taken" % str(particle_id))
            self.carried_particle.coordinates = self.coordinates
            if self.world.vis is not None:
                self.world.vis.particle_changed(self.carried_particle)
            self.world.csv_round.update_metrics(particles_taken=1)
            self.csv_particle_writer.write_particle(particles_taken=1)
844
            return True
Ahmad Reza's avatar
Ahmad Reza committed
845
        else:
846
847
            self.carried_particle = None
            logging.info("particle with particle id %s could not be taken" % str(particle_id))
848
849
            return False

850
851

    def take_particle_on(self, coordinates):
Ahmad Reza's avatar
Ahmad Reza committed
852
        """
853
        Takes the particle on the given coordinates if it is not taken
Ahmad Reza's avatar
Ahmad Reza committed
854

855
856
        :param coordinates: the particle coordinates
        :return: True: Successful taken; False: Cannot be taken or wrong Coordinates
Ahmad Reza's avatar
Ahmad Reza committed
857
        """
858
859
860
861
862
863
864

        if not self.world.grid.are_valid_coordinates(coordinates):
            logging.info("Coordinates are invalid")
            return False

        if coordinates in self.world.particle_map_coordinates:
            return self.take_particle_with(self.world.particle_map_coordinates[coordinates].get_id())
Ahmad Reza's avatar
Ahmad Reza committed
865
        else:
866
            logging.info("There is no particle on %s" % str(coordinates))
Karol Actun's avatar
Karol Actun committed
867
            return False
Ahmad Reza's avatar
Ahmad Reza committed
868

869
870
871
872
873
874
    def take_particle_in(self, direction):
        """
        Takes a particle that is in a given direction

        :param direction: The direction on which the particle should be taken. Options: E, SE, SW, W, NW, NE,
        :return: True: successful taken; False: unsuccessful taken
Ahmad Reza's avatar
Ahmad Reza committed
875
        """
876
877
878
        return self.take_particle_on(get_coordinates_in_direction(self.coordinates, direction))

    def take_particle(self):
Ahmad Reza's avatar
Ahmad Reza committed
879
        """
880
        Takes a particle on the actual position
Ahmad Reza's avatar
Ahmad Reza committed
881

882
883
884
        :return: True: successful taken; False: unsuccessful taken
        """
        return self.take_particle_on(self.coordinates)
Ahmad Reza's avatar
Ahmad Reza committed
885

886
887
888
    def drop_particle(self):
        """
        Drops the taken particle on the particles actual position
Ahmad Reza's avatar
Ahmad Reza committed
889

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
        :return: None
        """
        return self.drop_particle_on(self.coordinates)

    def drop_particle_in(self, direction):
        """
        Drops the particle tile in a given direction

         :param direction: The direction on which the particle should be dropped.
        """
        return self.drop_particle_on(get_coordinates_in_direction(self.coordinates, direction))

    def drop_particle_on(self, coordinates=None):
        """
        Drops the particle tile on the given coordinates

        :param coordinates:
        """
        if self.carried_particle is not None and coordinates is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                if coordinates not in self.world.particle_map_coordinates:
                    try:  # cher: insert so to overcome the AttributeError
                        self.carried_particle.drop_me(coordinates)
                    except AttributeError:
                        logging.info("Dropped particle on: Error while dropping")
                        return False
                    self.carried_particle = None
                    logging.info("Dropped particle on %s coordinate", str(coordinates))
                    self.world.csv_round.update_metrics(particles_dropped=1)
                    self.csv_particle_writer.write_particle(particles_dropped=1)
                    return True
                else:
                    logging.info("Is not possible to drop the particle on that position because it is occupied")
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
924
            else:
925
                logging.info("invalid coordinates")
Ahmad Reza's avatar
Ahmad Reza committed
926
        else:
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
            logging.info("drop_particle_on: coordinates are 'None' or not carrying a particle")
            return False

    def update_particle_coordinates(self, particle, new_coordinates):
        """
        Upadting the particle with new coordinates
        Only necessary for taking and moving particles

        :param particle: The particle object
        :param new_coordinates: new coorindation points
        :return: None
        """
        if self.world.grid.are_valid_coordinates(new_coordinates):
            particle.coordinates = new_coordinates
            self.world.particle_map_coordinates[new_coordinates] = particle
            if self.world.vis is not None:
                self.world.vis.particle_changed(particle)
944
945
946
            return True
        else:
            return False
Ahmad Reza's avatar
Ahmad Reza committed
947

948
    def create_location(self):
Ahmad Reza's avatar
Ahmad Reza committed
949
        """
950
         Creates a location on the particles actual position
Ahmad Reza's avatar
Ahmad Reza committed
951

952
        :return: New location or False
Ahmad Reza's avatar
Ahmad Reza committed
953
954
        """

955
956
957
958
959
960
961
        logging.info("Going to create on position %s" % str(self.coordinates))
        new_location = self.world.add_location(self.coordinates)
        if new_location:
            self.csv_particle_writer.write_particle(location_created=1)
            self.world.csv_round.update_locations_num(len(self.world.get_location_list()))
            self.world.csv_round.update_metrics(location_created=1)
            return new_location
Ahmad Reza's avatar
Ahmad Reza committed
962
963
964
        else:
            return False

965
    def create_location_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
966
        """
967
968
969
        Creates a location either in a given direction
        :param direction: The direction on which the location should be created.
        :return: New location or False
Ahmad Reza's avatar
Ahmad Reza committed
970
971

        """
972
973
974
975
976
977
978
979
980
981
982
983
984
985
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Going to create a location in %s on position %s" % (str(direction), str(coordinates)))
            new_location = self.world.add_location(coordinates)
            if new_location:
                logging.info("Created location on coordinates %s" % str(coordinates))
                self.world.csv_round.update_locations_num(len(self.world.get_location_list()))
                self.world.csv_round.update_metrics(location_created=1)
                return new_location
            else:
                return False
        else:
            logging.info("Location not created. Invalid direction (None)")
            return False
Ahmad Reza's avatar
Ahmad Reza committed
986

987
988
989
    def create_location_on(self, coordinates=None):
        """
        Creates a location either on a given x,y coordinates
Ahmad Reza's avatar
Ahmad Reza committed
990

991
        :return: New location or False
Ahmad Reza's avatar
Ahmad Reza committed
992

993
994
995
996
997
998
999
1000
1001
1002
        """
        if coordinates is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                logging.info("Going to create a location on position %s", str(coordinates))
                new_location = self.world.add_location(coordinates)
                if new_location:
                    logging.info("Created location on coordinates %s", str(coordinates))
                    self.world.csv_round.update_locations_num(len(self.world.get_location_list()))
                    self.world.csv_round.update_metrics(location_created=1)
                    return new_location
Ahmad Reza's avatar
Ahmad Reza committed
1003
1004
1005
            else:
                return False
        else:
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
            logging.info("Location not created. invalid coordinates (None)")
            return False

    def delete_location_with(self, location_id):
        """
        Deletes a location with a given location-id
        :param location_id: The id of the location that should be deleted
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is going to delete location with location id %s" % (self.get_id(), location_id))
        if self.world.remove_location(location_id):
            self.csv_particle_writer.write_particle(location_deleted=1)
Karol Actun's avatar
Karol Actun committed
1018
            return True
Ahmad Reza's avatar
Ahmad Reza committed
1019
        else:
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
            logging.info("Could not delete location with location id %s", str(location_id))
            return False

    def delete_location(self):
        """
        Deletes a location on current position

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is going to delete a location on current position" % self.get_id())
        if self.coordinates in self.world.get_location_map_coordinates():
            if self.world.remove_location_on(self.coordinates):
                self.csv_particle_writer.write_particle(location_deleted=1)
                return True
        else:
            logging.info("Could not delete location")
1036
            return False
Ahmad Reza's avatar
Ahmad Reza committed
1037

1038
    def delete_location_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
1039
        """
1040
        Deletes a location in a given direction
Ahmad Reza's avatar
Ahmad Reza committed
1041

1042
1043
        :param direction: The direction on which the location should be deleted.
        :return: True: Deleting successful; False: Deleting unsuccessful
Ahmad Reza's avatar
Ahmad Reza committed
1044
        """
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
        if direction is not None:
            coordinates = get_coordinates_in_direction(self.coordinates, direction)
            logging.info("Deleting tile in %s direction", str(direction))
            if self.world.remove_location_on(coordinates):
                logging.info("Deleted location with location on coordinates %s", str(coordinates))
                self.csv_particle_writer.write_particle(location_deleted=1)
                return True
            else:
                logging.info("Could not delete location on coordinates %s", str(coordinates))
                return False
        else:
            logging.info("invalid direction %d", str(direction))

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

        :param coordinates: the coordinates
        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        if coordinates is not None:
            if self.world.grid.are_valid_coordinates(coordinates):
                if self.world.remove_location_on(coordinates):
                    logging.info("Deleted location on coordinates %s", str(coordinates))
                    self.csv_particle_writer.write_particle(location_deleted=1)
                    return True
                else:
                    logging.info("Could not delete location on coordinates %s", str(coordinates))
                    return False
            else:
                return False
Ahmad Reza's avatar
Ahmad Reza committed
1076
        else:
Karol Actun's avatar
Karol Actun committed
1077
            return False
1078
1079
1080
1081
1082

    def set_color(self, color):
        super().set_color(color)
        if self.world.vis is not None:
            self.world.vis.particle_changed(self)