particle.py 53.5 KB
Newer Older
Ahmad Reza's avatar
Ahmad Reza committed
1001
1002
        Takes a particle that is in a given direction

1003
        :param direction: The direction on which the particle should be taken. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
1004
1005
1006
        :return: True: successful taken; False: unsuccessful taken
        """
        if self.carried_tile is None and self.carried_particle is None:
1007
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
1008
            if coords in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
1009
                logging.info("Take particle")
Ahmad Reza's avatar
Ahmad Reza committed
1010
                self.carried_particle = self.world.particle_map_coords[coords]
Ahmad Reza's avatar
Ahmad Reza committed
1011
1012
                if self.carried_particle.take_me(coords=self.coords):
                    logging.info("particle with particle id %s  has been taken", str(self.carried_particle.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
1013
                    self.world.csv_round.update_metrics(
Ahmad Reza's avatar
Ahmad Reza committed
1014
1015
1016
1017
1018
1019
1020
                                                               particles_taken=1)
                    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
1021
                logging.info("particl is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
1022
                return False
Ahmad Reza's avatar
Ahmad Reza committed
1023
1024
        else:
            logging.info("particle cannot be  taken")
Ahmad Reza's avatar
Ahmad Reza committed
1025
            return False
Ahmad Reza's avatar
Ahmad Reza committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035

    def take_particle_on(self, x=None, y=None):
        """
        Takes the particle on the given coordinate if it is not taken

        :param y:
        :param x:
        :return: True: Successful taken; False: Cannot be taken or wrong Coordinates
        """
        if self.carried_particle is None and self.carried_tile is None:
1036
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
1037
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
1038
1039
1040
                if coords in self.world.particle_map_coords:
                    self.carried_particle = self.world.particle_map_coords[coords]
                    logging.info("Particle with id %s is in the world", str(self.carried_particle.get_id()))
Ahmad Reza's avatar
Ahmad Reza committed
1041
                    if self.carried_particle.take_me(coords=self.coords):
Ahmad Reza's avatar
Ahmad Reza committed
1042
                        self.world.csv_round.update_metrics( particles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
1043
1044
1045
1046
1047
1048
1049
                        self.csv_particle_writer.write_particle(particles_taken=1)
                        logging.info("particle with tile id %s has been taken", str(self.carried_particle.get_id()))
                        return True
                    else:
                        logging.info("Particle with id %s could not be taken", str(self.carried_particle.get_id()))
                        return False
                else:
Ahmad Reza's avatar
Ahmad Reza committed
1050
                    logging.info("Particle is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
                    return False
            else:
                logging.info("Coordinates are wrong")
                return False
        else:
            logging.info("Particle cannot taken because particle is carrieng either a tile or a particle")
            return False

    def drop_particle(self):
        """
        Drops the taken particle on the particles actual position

        :return: None
        """
        if self.carried_particle is not None:
Ahmad Reza's avatar
Ahmad Reza committed
1066
            if self.coords not in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
1067
1068
1069
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_particle.drop_me(self.coords)
                except AttributeError:
Ahmad Reza's avatar
Ahmad Reza committed
1070
1071
                    logging.info("Dropped particle: Error while dropping")
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1072
            self.carried_particle = None
Ahmad Reza's avatar
Ahmad Reza committed
1073
            self.world.csv_round.update_metrics( particles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
1074
1075
1076
1077
1078
1079
1080
            self.csv_particle_writer.write_particle(particles_dropped=1)
            logging.info("Particle succesfull dropped")
            return True
        else:
            logging.info("No particle taken to drop")
            return False

1081
    def drop_particle_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
1082
1083
1084
        """
        Drops the particle tile in a given direction

1085
         :param direction: The direction on which the particle should be dropped. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
1086
1087
        """
        if self.carried_particle is not None:
1088
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
1089
            if coords not in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
1090
1091
1092
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_particle.drop_me(coords)
                except AttributeError:
Ahmad Reza's avatar
Ahmad Reza committed
1093
                    logging.info("Dropped particle in: Error while dropping")
Ahmad Reza's avatar
Ahmad Reza committed
1094
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1095
1096
1097
1098
1099
                self.carried_particle = None
                logging.info("Dropped particle on %s coordinate", str(coords))
                self.world.csv_round.update_metrics( particles_dropped=1)
                self.csv_particle_writer.write_particle(particles_dropped=1)
                return True
Ahmad Reza's avatar
Ahmad Reza committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
            else:
                logging.info("Is not possible to drop the particle on that position because it is occupied")
                return False
        else:
            logging.info("No particle taken to drop")
            return False

    def drop_particle_on(self, x=None, y=None):
        """
        Drops the particle tile on a given x and y coordination

        :param x: x coordinate
        :param y: y coordinate
        """
Ahmad Reza's avatar
Ahmad Reza committed
1114
1115
1116
1117
1118
1119
1120
        if self.carried_particle is not None and x is not None and y is not None and check_coords(x, y):
            coords = (x, y)
            if coords not in self.world.particle_map_coords:
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_particle.drop_me(coords)
                except AttributeError:
                    logging.info("Dropped particle on: Error while dropping")
Ahmad Reza's avatar
Ahmad Reza committed
1121
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1122
1123
1124
1125
1126
                self.carried_particle = None
                logging.info("Dropped particle on %s coordinate", str(coords))
                self.world.csv_round.update_metrics(particles_dropped=1)
                self.csv_particle_writer.write_particle(particles_dropped=1)
                return True
Ahmad Reza's avatar
Ahmad Reza committed
1127
            else:
Ahmad Reza's avatar
Ahmad Reza committed
1128
                logging.info("Is not possible to drop the particle on that position  because it is occupied")
Ahmad Reza's avatar
Ahmad Reza committed
1129
1130
                return False
        else:
Ahmad Reza's avatar
Ahmad Reza committed
1131
            logging.info("drop_particle_on: Wrong Inputs")
Ahmad Reza's avatar
Ahmad Reza committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
            return False

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

        :param particle: The particle object
        :param new_coords: new coorindation points
        :return: None
        """
        particle.coords = new_coords
        self.particle_map_coords[new_coords] = particle

1146
    def create_marker(self, color=black, alpha=1):
Ahmad Reza's avatar
Ahmad Reza committed
1147
        """
1148
         Creates a marker on the particles actual position
Ahmad Reza's avatar
Ahmad Reza committed
1149

1150
        :return: New marker or False
Ahmad Reza's avatar
Ahmad Reza committed
1151
1152
1153
        """

        logging.info("Going to create on position %s", str(self.coords))
Ahmad Reza's avatar
Ahmad Reza committed
1154
        new_marker=self.world.add_marker(self.coords[0], self.coords[1], color, alpha)
1155
1156
        if new_marker != False:
            self.csv_particle_writer.write_particle(marker_created=1)
Ahmad Reza's avatar
Ahmad Reza committed
1157
1158
            self.world.csv_round.update_markers_num(len(self.world.get_marker_list()))
            self.world.csv_round.update_metrics( marker_created=1)
1159
            return  new_marker
Ahmad Reza's avatar
Ahmad Reza committed
1160
1161
1162
        else:
            return False

1163
    def create_marker_in(self, direction=None, color=black, alpha=1):
Ahmad Reza's avatar
Ahmad Reza committed
1164
        """
1165
        Creates a marker either in a given direction
1166
        :param direction: The direction on which the marker should be created. Options: E, SE, SW, W, NW, NE,
1167
        :return: New marker or False
Ahmad Reza's avatar
Ahmad Reza committed
1168
1169
1170

        """
        coords = (0, 0)
1171
1172
1173
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Going to create a marker in %s on position %s", str(direction), str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1174
            new_marker = self.world.add_marker(coords[0], coords[1], color, alpha)
1175
1176
            if new_marker:
                logging.info("Created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1177
1178
                self.world.csv_round.update_markers_num(len(self.world.get_marker_list()))
                self.world.csv_round.update_metrics( marker_created=1)
1179
                return new_marker
Ahmad Reza's avatar
Ahmad Reza committed
1180
1181
1182
            else:
                return False
        else:
1183
            logging.info("Not created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1184
1185
            return False

1186
    def create_marker_on(self, x=None, y=None, color=black, alpha=1):
Ahmad Reza's avatar
Ahmad Reza committed
1187
        """
1188
        Creates a marker either on a given x,y coordinates
Ahmad Reza's avatar
Ahmad Reza committed
1189
1190
1191

        :param x: x coordinate
        :param y: y coordinate
1192
        :return: New marker or False
Ahmad Reza's avatar
Ahmad Reza committed
1193
1194
1195
1196

        """
        coords = (0, 0)
        if x is not None and y is not None:
1197
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
1198
                coords = (x, y)
1199
                logging.info("Going to create a marker on position %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1200
                new_marker =  self.world.add_marker(coords[0], coords[1], color, alpha)
1201
1202
                if new_marker:
                    logging.info("Created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1203
1204
                    self.world.csv_round.update_markers_num(len(self.world.get_marker_list()))
                    self.world.csv_round.update_metrics( marker_created=1)
1205
                    return new_marker
Ahmad Reza's avatar
Ahmad Reza committed
1206
1207
1208
            else:
                return False
        else:
1209
            logging.info("Not created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1210
1211
            return False

1212
    def delete_marker(self):
Ahmad Reza's avatar
Ahmad Reza committed
1213
        """
1214
        Deletes a marker on current position
Ahmad Reza's avatar
Ahmad Reza committed
1215
1216
1217
1218

        :return: True: Deleting successful; False: Deleting unsuccessful
        """
        logging.info("Particle %s is", self.get_id())
1219
        logging.info("is going to delete a marker on current position")
Ahmad Reza's avatar
Ahmad Reza committed
1220
1221
        if self.coords in self.world.get_marker_map_coords():
            if self.world.remove_marker_on(self.coords):
1222
                self.csv_particle_writer.write_particle(marker_deleted=1)
Ahmad Reza's avatar
Ahmad Reza committed
1223
1224
                return True
        else:
1225
            logging.info("Could not delet marker")
Ahmad Reza's avatar
Ahmad Reza committed
1226
1227
            return False

1228
    def delete_marker_with(self, marker_id):
Ahmad Reza's avatar
Ahmad Reza committed
1229
        """
1230
        Deletes a marker with a given marker-id
Ahmad Reza's avatar
Ahmad Reza committed
1231

1232
        :param marker_id: The id of the marker that should be deleted
Ahmad Reza's avatar
Ahmad Reza committed
1233
1234
1235
        :return: True: Deleting successful; False: Deleting unsuccessful
        """

1236
1237
        logging.info("marker %s is", self.get_id())
        logging.info("is going to delete a marker with id %s", str(marker_id))
Ahmad Reza's avatar
Ahmad Reza committed
1238
        if self.world.remove_marker(marker_id):
1239
            self.csv_particle_writer.write_particle(marker_deleted=1)
Ahmad Reza's avatar
Ahmad Reza committed
1240
1241
            return
        else:
1242
            logging.info("Could not delet marker with marker id %s", str(marker_id))
Ahmad Reza's avatar
Ahmad Reza committed
1243

1244
    def delete_marker_in(self, direction=None):
Ahmad Reza's avatar
Ahmad Reza committed
1245
        """
1246
        Deletes a marker either in a given direction or on a given x,y coordinates
Ahmad Reza's avatar
Ahmad Reza committed
1247

1248
        :param direction: The direction on which the marker should be deleted. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
1249
1250
1251
1252
1253
        :param x: x coordinate
        :param y: y coordinate
        :return: True: Deleting successful; False: Deleting unsuccessful
        """

1254
1255
1256
        if direction is not None:
            coords = get_coords_in_direction(self.coords, direction)
            logging.info("Deleting tile in %s direction", str(direction))
Ahmad Reza's avatar
Ahmad Reza committed
1257
            if self.world.remove_marker_on(coords):
1258
1259
                logging.info("Deleted marker with marker on coords %s", str(coords))
                self.csv_particle_writer.write_particle(marker_deleted=1)
Ahmad Reza's avatar
Ahmad Reza committed
1260
            else:
1261
                logging.info("Could not delet marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1262

1263
    def delete_marker_on(self, x=None, y=None):
Ahmad Reza's avatar
Ahmad Reza committed
1264
1265
1266
1267
1268
1269
1270
1271
        """
        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:
1272
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
1273
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
1274
                if self.world.remove_marker_on(coords):
1275
1276
                    logging.info("Deleted marker  oords %s", str(coords))
                    self.csv_particle_writer.write_particle(marker_deleted=1)
Ahmad Reza's avatar
Ahmad Reza committed
1277
1278
                    return True
                else:
1279
                    logging.info("Could not delet marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1280
1281
1282
1283
1284
1285
                    return False
            else:
                return False
        else:
            return False
For faster browsing, not all history is shown. View entire blame