Beispiel #1
0
	/**
	 * Select the first available object.
	 * @param change_class If true, change the class if no object in the current
	 *   class is available.
	 */
	void SelectFirstAvailableObject(bool change_class)
	{
		/* First try to select an object in the selected class. */
		ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
		for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
			const ObjectSpec *spec = sel_objclass->GetSpec(i);
			if (spec->IsAvailable()) {
				this->SelectOtherObject(i);
				return;
			}
		}
		if (change_class) {
			/* If that fails, select the first available object
			 * from a random class. */
			for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
				ObjectClass *objclass = ObjectClass::Get(j);
				for (uint i = 0; i < objclass->GetSpecCount(); i++) {
					const ObjectSpec *spec = objclass->GetSpec(i);
					if (spec->IsAvailable()) {
						this->SelectOtherClass(j);
						this->SelectOtherObject(i);
						return;
					}
				}
			}
		}
		/* If all objects are unavailable, select nothing... */
		if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
			/* ... but make sure that the class is not empty. */
			for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
				if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
					this->SelectOtherClass(j);
					break;
				}
			}
		}
		this->SelectOtherObject(-1);
	}