particle.py 53.8 KB
Newer Older
1001
    def take_particle_in(self, direction):
Ahmad Reza's avatar
Ahmad Reza committed
1002
        """
1003
        Takes a particle that is in a given directionection
Ahmad Reza's avatar
Ahmad Reza committed
1004

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

    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:
1038
            if check_coords(x, y):
Ahmad Reza's avatar
Ahmad Reza committed
1039
                coords = (x, y)
Ahmad Reza's avatar
Ahmad Reza committed
1040
1041
1042
                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
1043
                    if self.carried_particle.take_me(coords=self.coords):
Ahmad Reza's avatar
Ahmad Reza committed
1044
                        self.world.csv_round.update_metrics( particles_taken=1)
Ahmad Reza's avatar
Ahmad Reza committed
1045
1046
1047
1048
1049
1050
1051
                        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
1052
                    logging.info("Particle is not in the world")
Ahmad Reza's avatar
Ahmad Reza committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
                    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
1068
            if self.coords not in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
1069
1070
1071
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_particle.drop_me(self.coords)
                except AttributeError:
Ahmad Reza's avatar
Ahmad Reza committed
1072
1073
                    logging.info("Dropped particle: Error while dropping")
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1074
            self.carried_particle = None
Ahmad Reza's avatar
Ahmad Reza committed
1075
            self.world.csv_round.update_metrics( particles_dropped=1)
Ahmad Reza's avatar
Ahmad Reza committed
1076
1077
1078
1079
1080
1081
1082
            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

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

1087
         :param direction: The directionection on which the particle should be dropped. Options: E, SE, SW, W, NW, NE,
Ahmad Reza's avatar
Ahmad Reza committed
1088
1089
        """
        if self.carried_particle is not None:
1090
            coords = get_coords_in_direction(self.coords, direction)
Ahmad Reza's avatar
Ahmad Reza committed
1091
            if coords not in self.world.particle_map_coords:
Ahmad Reza's avatar
Ahmad Reza committed
1092
1093
1094
                try:  # cher: insert so to overcome the AttributeError
                    self.carried_particle.drop_me(coords)
                except AttributeError:
Ahmad Reza's avatar
Ahmad Reza committed
1095
                    logging.info("Dropped particle in: Error while dropping")
Ahmad Reza's avatar
Ahmad Reza committed
1096
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1097
1098
1099
1100
1101
                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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
            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
1116
1117
1118
1119
1120
1121
1122
        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
1123
                    return False
Ahmad Reza's avatar
Ahmad Reza committed
1124
1125
1126
1127
1128
                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
1129
            else:
Ahmad Reza's avatar
Ahmad Reza committed
1130
                logging.info("Is not possible to drop the particle on that position  because it is occupied")
Ahmad Reza's avatar
Ahmad Reza committed
1131
1132
                return False
        else:
Ahmad Reza's avatar
Ahmad Reza committed
1133
            logging.info("drop_particle_on: Wrong Inputs")
Ahmad Reza's avatar
Ahmad Reza committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
            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

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

1152
        :return: New marker or False
Ahmad Reza's avatar
Ahmad Reza committed
1153
1154
1155
        """

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

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

        """
        coords = (0, 0)
1173
1174
1175
        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
1176
            new_marker = self.world.add_marker(coords[0], coords[1], color, alpha)
1177
1178
            if new_marker:
                logging.info("Created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1179
1180
                self.world.csv_round.update_markers_num(len(self.world.get_marker_list()))
                self.world.csv_round.update_metrics( marker_created=1)
1181
                return new_marker
Ahmad Reza's avatar
Ahmad Reza committed
1182
1183
1184
            else:
                return False
        else:
1185
            logging.info("Not created marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1186
1187
            return False

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

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

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

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

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

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

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

1238
1239
        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
1240
        if self.world.remove_marker(marker_id):
1241
            self.csv_particle_writer.write_particle(marker_deleted=1)
Ahmad Reza's avatar
Ahmad Reza committed
1242
1243
            return
        else:
1244
            logging.info("Could not delet marker with marker id %s", str(marker_id))
Ahmad Reza's avatar
Ahmad Reza committed
1245

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

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

1256
1257
1258
        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
1259
            if self.world.remove_marker_on(coords):
1260
1261
                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
1262
            else:
1263
                logging.info("Could not delet marker on coords %s", str(coords))
Ahmad Reza's avatar
Ahmad Reza committed
1264

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