Example #1
0
void Behavior::doAction(bool directlyExecuted) {
	if (agent->isDead() || agent->isIncapacitated() || (agent->getZone() == NULL)) {
		agent->setFollowObject(NULL);
		return;
	}

	//agent->info(id, true);
	agent->setCurrentBehavior(id);

	if (!started())
		this->start();
	else if (!this->checkConditions())
		endWithFailure();

	if (finished()) {
		if (parent == NULL) {
			this->end();
			result = AiMap::SUSPEND;
			agent->activateMovementEvent(); // this is an automatic recycle decorator for the root node
		} else if (directlyExecuted) {
			agent->setCurrentBehavior(parent->id);
			parent->doAction(true);
		}

		return;
	}

	int res = AiMap::INVALID;
	if (interface != NULL)
		res = interface->doAction(agent);

	switch(res) {
	case AiMap::SUCCESS:
		endWithSuccess();
		break;
	case AiMap::FAILURE:
		endWithFailure();
		break;
	case AiMap::INVALID:
		if (agent == NULL)
			return;

		agent->resetBehaviorList();
		break;
	default:
		if (agent == NULL)
			return;

		break;
	}

	if (!finished() || parent == NULL)
		agent->activateMovementEvent();
	else if (directlyExecuted) {
		agent->setCurrentBehavior(parent->id);

		parent->doAction(true);
	}
}
void SequenceBehavior::childSucceeded() {
	if (currentPos == children.size() - 1)
		endWithSuccess();
	else {
		currentPos++;
		Behavior* currentChild = children.get(currentPos);
		if (currentChild == NULL || !currentChild->checkConditions())
			endWithFailure();
	}
}
void ParallelSequenceBehavior::finish() {
	if (numFailed > 0)
		endWithFailure();
	else
		endWithSuccess();
}