+def id_setter(id, category, id_store=False, start_at_1=False):
+ """Set ID of object of category to manipulate ID unused? Create new one.
+
+ The ID is stored as id_store.id (if id_store is set). If the integer of the
+ input is valid (if start_at_1, >= 0 and <= 255, else >= -32768 and <=
+ 32767), but <0 or (if start_at_1) <1, calculate new ID: lowest unused ID
+ >=0 or (if start_at_1) >= 1, and <= 255. None is always returned when no
+ new object is created, otherwise the new object's ID.
+ """
+ min = 0 if start_at_1 else -32768
+ max = 255 if start_at_1 else 32767
+ if str == type(id):
+ id = integer_test(id, min, max)
+ if None != id:
+ if id in world_db[category]:
+ if id_store:
+ id_store.id = id
+ return None
+ else:
+ if (start_at_1 and 0 == id) \
+ or ((not start_at_1) and (id < 0 or id > 255)):
+ id = -1
+ while 1:
+ id = id + 1
+ if id not in world_db[category]:
+ break
+ if id > 255:
+ print("Ignoring: "
+ "No unused ID available to add to ID list.")
+ return None
+ if id_store:
+ id_store.id = id
+ return id
+
+