Example #1
0
/* virtual */
void
Attack::operator()()
{
	if (fObject == NULL)
		std::cerr << "NULL OBJECT" << std::endl;
	Actor* actor = dynamic_cast<Actor*>(fObject);
	if (actor == NULL)
		return;
	
	Actor* target = dynamic_cast<Actor*>(Script::FindTargetObject(fObject, fActionParams));
	if (target == NULL){
		SetCompleted();
		return;
	}

	if (!Initiated()) {
		IE::point point = target->NearestPoint(actor->Position());
		if (!PointSufficientlyClose(actor->Position(), point))
			actor->SetDestination(point);
		SetInitiated();
	}

	if (actor->Position() != actor->Destination()) {
		actor->SetAnimationAction(ACT_WALKING);
		actor->MoveToNextPointInPath(actor->IsFlying());
	} else {
		actor->SetAnimationAction(ACT_ATTACKING);
		actor->AttackTarget(target);
		SetCompleted();
	}
}
Example #2
0
IE::point
PathFinder::_GeneratePath(const IE::point& start, const IE::point& end)
{
	fPoints.clear();

	IE::point maxReachableDirectly = start;//_CreateDirectPath(start, end);

	if (PointSufficientlyClose(maxReachableDirectly, end)
			|| !_IsPassable(end))
		return maxReachableDirectly;

	std::list<point_node*> openList;
	std::list<point_node*> closedList;

	point_node* currentNode = new point_node(maxReachableDirectly, NULL, 0);
	openList.push_back(currentNode);

	uint32 tries = 4000;
	bool notFound = false;
	for (;;) {
		// If adiacent nodes are passable, add them to the list
		_AddIfPassable(offset_point(currentNode->point, fStep, 0), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, fStep, fStep), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, 0, fStep), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, -fStep, fStep), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, -fStep, 0), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, -fStep, -fStep), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, fStep, -fStep), *currentNode, openList, closedList);
		_AddIfPassable(offset_point(currentNode->point, 0, -fStep), *currentNode, openList, closedList);

		openList.remove(currentNode);
		closedList.push_back(currentNode);

		if (PointSufficientlyClose(currentNode->point, end))
			break;

		currentNode = _GetCheapestNode(openList, end);
		if (currentNode == NULL || --tries == 0) {
			notFound = true;
			break;
		}
	}

	//std::list<point_node*>::const_iterator i;
	if (notFound) {
		// TODO: Destination is unreachable.
		// Try to find a reachable point near destination
		std::cout << "Path not found" << std::endl;

		EmptyList(closedList);
		EmptyList(openList);
		return start;
	}

	EmptyList(openList);
	std::list<point_node*>::reverse_iterator r = closedList.rbegin();
	point_node* walkNode = *r;
	for (;;) {
		//fPoints.insert(directRouteEnd, walkNode->point);
		//directRouteEnd--;
		fPoints.push_front(walkNode->point);
		const point_node* parent = walkNode->parent;
		if (parent == NULL)
			break;
		walkNode = const_cast<point_node*>(parent);
	}

	EmptyList(closedList);
	assert (PointSufficientlyClose(fPoints.back(), end));
	return fPoints.back();
}