
void saveQueue(Object@ obj, string@ queueName) {
	if (obj is null || queueName.length() == 0)
		return;

	XMLWriter@ xml = XMLWriter("Queues/" + queueName);
	
	if(xml is null)
		return;
	
	xml.createHeader();
	
	xml.addElement("queue", false, "name", queueName, "repeat", obj.getRepeatQueue() ? "yes" : "no");
	xml.addLineBreak();

	for (uint i = 0; i < obj.getConstructionQueueSize(); ++i) {
		string@ name = obj.getConstructionName(i);
		string@ type = obj.getConstructionType(i);

		if (type == "ship" || type == "structure") {
			xml.addElement(type, true, "name", name);
			xml.addLineBreak();
		}
	}

	xml.closeTag("queue");
}

void loadQueue(Object@ obj, string@ queueName) {
	if (obj is null || queueName.length() == 0)
		return;

	XMLReader@ xml = XMLReader("Queues/" + queueName);
	
	if(xml is null)
		return;
	
	while (xml.advance()) {
		if (xml.getNodeType() == XN_Element) {
			string@ nodename = xml.getNodeName();

			if (nodename == "queue") {
				if (xml.hasAttribute("repeat")) {
					obj.setRepeatQueue(xml.getAttributeValue("repeat") == "yes");
				}
			}
			else if (nodename == "ship") {
				string@ buildname = xml.getAttributeValue("name");
				const HullLayout@ temp = obj.getOwner().getShipLayout(buildname);

				if (@temp != null)
					obj.makeShip(temp);
			}
			else if (nodename == "structure") {
				Planet@ pl = obj.toPlanet();

				if (pl !is null) {
					string@ buildname = xml.getAttributeValue("name");
					uint subsystemcnt = obj.getOwner().getSubSysDataCnt();

					// Find correct subsys id
					for (uint i = 0; i < subsystemcnt; ++i) {
						const subSystemDef@ temp = obj.getOwner().getSubSysData(i).type;

						if (@temp != null && temp.getName() == buildname && temp.canBuildOn(pl)) {
							pl.buildStructure(temp);
							break;
						}
					}
				}
			}
		}
	}
}
