void DiscoveryDan::Update()
{
	if (!_hasGoal)
	{
		for (int nodeX = 0; nodeX < X_NODES; nodeX += 1)
		{
			if (GetFogState(nodeX, _playerPositionYNode, NODE_COORDS) == 1)
			{
				_targetX = nodeX * PIXELS_IN_NODES;
				_targetY = _playerPositionY;
				_hasGoal = true;
				CalculatePathFromXYtoXY(_playerPositionX, _playerPositionY, _targetX, _targetY, PIXEL_COORDS);
				return;
			}
		}
		for (int nodeX = 0; nodeX < X_NODES; nodeX += 1)
		{
			for (int nodeY = 0; nodeY < Y_NODES; nodeY += 1)
			{
				if (GetNodeState(nodeX, nodeY, NODE_COORDS) == spExit)
				{
					_targetX = nodeX * PIXELS_IN_NODES;
					_targetY = nodeY * PIXELS_IN_NODES;
					_hasGoal = true;
					CalculatePathFromXYtoXY(_playerPositionX, _playerPositionY, _targetX, _targetY, PIXEL_COORDS);
					return;
				}
			}
		}
	}
	else
	{
		if (_pathCount > GetPathCount())
		{
			_pathCount = 0;
			_hasGoal = false;
		}
		_pathCount += 1;

		if (_playerPositionXNode < GetNextPathXPos(_playerPositionXNode, _playerPositionYNode, NODE_COORDS))
		{
			_goRight = true;
		}
		else
		{
			_goLeft = true;
		}

		if ((_playerPositionYNode - 1) > GetNextPathYPos(_playerPositionXNode, _playerPositionYNode, NODE_COORDS))
		{
			_jump = true;
		}
	}
}
bool BattlegroundIC::IsSpellAllowed(uint32 spellId, Player const* player) const
{
    switch (spellId)
    {
        case SPELL_OIL_REFINERY:
        case SPELL_QUARRY:
        {
            uint32 team = player->GetTeamId();
            uint8 nodeType = spellId = SPELL_OIL_REFINERY ? NODE_TYPE_REFINERY : NODE_TYPE_QUARRY;
            uint8 nodeState = team == TEAM_ALLIANCE ? NODE_STATE_CONTROLLED_A : NODE_STATE_CONTROLLED_H;
            return GetNodeState(nodeType) == nodeState;
        }
        default:
           break;
    }

    return true;
}
void JordanBot::Update()
{
	_canGoRight = IsNodePassable(_playerPositionXNode + 1, _playerPositionYNode, NODE_COORDS);
	_canGoLeft = IsNodePassable(_playerPositionXNode - 1, _playerPositionYNode, NODE_COORDS);

	_canJumpRight = IsNodePassable(_playerPositionXNode + 1, _playerPositionYNode - 1, NODE_COORDS);
	_canJumpLeft = IsNodePassable(_playerPositionXNode - 1, _playerPositionYNode - 1, NODE_COORDS);

	_canJumpGrabRight = IsNodePassable(_playerPositionXNode + 1, _playerPositionYNode - 2, NODE_COORDS);
	_canJumpGrabLeft = IsNodePassable(_playerPositionXNode - 1, _playerPositionYNode - 2, NODE_COORDS);

	if (!_hasGoal)
	{
		for (unsigned nodeY = 0; nodeY < Y_NODES; nodeY++)
		{
			for (unsigned nodeX = 0; nodeX < X_NODES; nodeX++)
			{
				if (GetNodeState(nodeX, nodeY, NODE_COORDS) == spExit)
				{
					_hasGoal = true;
					_targetX = nodeX * PIXELS_IN_NODES;
					_targetY = nodeY * PIXELS_IN_NODES;
					CalculatePathFromXYtoXY(_playerPositionX, _playerPositionY, _targetX, _targetY, PIXEL_COORDS);
					return;
				}
			}
		}

		if (_headingRight && (_canGoRight || _canJumpRight || _canJumpGrabRight))
		{
			if (!_canGoRight)
			{
				_jump = true;
			}

			_goRight = true;
			_headingRight = true;
			_headingLeft = false;
		}
		else if (_headingLeft && (_canGoLeft || _canJumpLeft || _canJumpGrabLeft))
		{
			if (!_canGoLeft)
			{
				_jump = true;
			}

			_goLeft = true;
			_headingLeft = true;
			_headingRight = false;
		}
		else if (_headingRight && (!_canGoRight && !_canJumpRight || !_canJumpGrabRight))
		{
			_goLeft = true;
			_headingLeft = true;
			_headingRight = false;
		}
		else
		{
			_goRight = true;
			_headingRight = true;
			_headingLeft = false;
		}
	}
	else
	{
		if (_pathCount > 60)
		{
			_pathCount = 0;
			CalculatePathFromXYtoXY(_playerPositionX, _playerPositionY, _targetX, _targetY, PIXEL_COORDS);
		}
		_pathCount++;

		if (_playerPositionXNode < GetNextPathXPos(_playerPositionXNode, _playerPositionYNode, NODE_COORDS))
		{
			_goRight = true;
		}
		else
		{
			_goLeft = true;
		}

		if ((_playerPositionYNode - 1) > GetNextPathYPos(_playerPositionXNode, _playerPositionYNode, NODE_COORDS))
		{
			if (_goRight && (_canJumpRight || _canJumpGrabRight))
			{
				_jump = true;
			}
			else if (_canJumpRight || _canJumpGrabLeft)
			{
				_jump = true;
			}
		}
	}

	if (_headingRight)
	{
		_attack = IsEnemyInNode(_playerPositionXNode + 1, _playerPositionYNode, NODE_COORDS);
	}
	else
	{
		_attack = IsEnemyInNode(_playerPositionX - 1, _playerPositionYNode, NODE_COORDS);
	}

	if (_attack)
	{
		_goLeft = false;
		_goRight = false;
	}
}
nsresult
nsXFormsMDGEngine::SetNodeContent(nsIDOMNode       *aContextNode,
                                  nsIDOMNode       *aContentEnvelope)
{
  NS_ENSURE_ARG(aContextNode);
  NS_ENSURE_ARG(aContentEnvelope);

  // ok, this is tricky.  This function will REPLACE the contents of
  // aContextNode with the a clone of the contents of aContentEnvelope.  If
  // aContentEnvelope has no contents, then any contents that aContextNode
  // has will still be removed.  

  const nsXFormsNodeState* ns = GetNodeState(aContextNode);
  NS_ENSURE_TRUE(ns, NS_ERROR_FAILURE);

  // If the node is read-only and not set by a @calculate MIP,
  // ignore the call
  if (ns->IsReadonly()) {
    ///
    /// @todo Better feedback for readonly nodes? (XXX)
    return NS_OK;
  }

  PRUint16 nodeType;
  nsresult rv = aContextNode->GetNodeType(&nodeType);
  NS_ENSURE_SUCCESS(rv, rv);

  if (nodeType != nsIDOMNode::ELEMENT_NODE) {
    // got to return something pretty unique that we can check down the road in
    // order to dispatch any error events
    return NS_ERROR_DOM_WRONG_TYPE_ERR;
  }

  // Need to determine if the contents of the context node and content envelope
  // are already the same.  If so, we can avoid some unnecessary work.

  PRBool hasChildren1, hasChildren2, contentsEqual = PR_FALSE;
  nsresult rv1 = aContextNode->HasChildNodes(&hasChildren1);
  nsresult rv2 = aContentEnvelope->HasChildNodes(&hasChildren2);
  if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2) && hasChildren1 == hasChildren2) {
    // First test passed.  Both have the same number of children nodes.
    if (hasChildren1) {
      nsCOMPtr<nsIDOMNodeList> children1, children2;
      PRUint32 childrenLength1, childrenLength2;
  
      rv1 = aContextNode->GetChildNodes(getter_AddRefs(children1));
      rv2 = aContentEnvelope->GetChildNodes(getter_AddRefs(children2));

      if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2) && children1 && children2) {

        // Both have child nodes.
        rv1 = children1->GetLength(&childrenLength1);
        rv2 = children2->GetLength(&childrenLength2);
        if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2) && 
            (childrenLength1 == childrenLength2)) {

          // both have the same number of child nodes.  Now checking to see if
          // each of the children are equal.
          for (PRUint32 i = 0; i < childrenLength1; ++i) {
            nsCOMPtr<nsIDOMNode> child1, child2;
      
            rv1 = children1->Item(i, getter_AddRefs(child1));
            rv2 = children2->Item(i, getter_AddRefs(child2));
            if (NS_FAILED(rv1) || NS_FAILED(rv2)) {
              // Unexpected error.  Not as many children in the list as we
              // were told.
              return NS_ERROR_UNEXPECTED;
            }
      
            contentsEqual = nsXFormsUtils::AreNodesEqual(child1, child2, PR_TRUE);
            if (!contentsEqual) {
              break;
            }
          }
        }
      }
    } else {
      // neither have children
      contentsEqual = PR_TRUE;
    }
  }

  if (contentsEqual) {
    return NS_OK;
  }

  // remove any child nodes that aContextNode already contains
  nsCOMPtr<nsIDOMNode> resultNode;
  nsCOMPtr<nsIDOMNodeList> childList;
  rv = aContextNode->GetChildNodes(getter_AddRefs(childList));
  NS_ENSURE_SUCCESS(rv, rv);
  if (childList) {
    PRUint32 length;
    rv = childList->GetLength(&length);
    NS_ENSURE_SUCCESS(rv, rv);

    for (PRInt32 i = length-1; i >= 0; i--) {
      nsCOMPtr<nsIDOMNode> childNode;
      rv = childList->Item(i, getter_AddRefs(childNode));
      NS_ENSURE_SUCCESS(rv, rv);

      rv = aContextNode->RemoveChild(childNode, getter_AddRefs(resultNode));
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  // add contents of the envelope under aContextNode
  nsCOMPtr<nsIDOMNode> childNode;
  rv = aContentEnvelope->GetFirstChild(getter_AddRefs(childNode));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIDOMDocument> document;
  rv = aContextNode->GetOwnerDocument(getter_AddRefs(document));
  NS_ENSURE_STATE(document);

  while (childNode) {
    nsCOMPtr<nsIDOMNode> importedNode;
    rv = document->ImportNode(childNode, PR_TRUE, getter_AddRefs(importedNode));
    NS_ENSURE_STATE(importedNode);
    rv = aContextNode->AppendChild(importedNode, getter_AddRefs(resultNode));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = childNode->GetNextSibling(getter_AddRefs(resultNode));
    NS_ENSURE_SUCCESS(rv, rv);

    resultNode.swap(childNode);
  }

  // We already know that the contents have changed.  Mark the node so that
  // a xforms-value-changed can be dispatched.
  MarkNodeAsChanged(aContextNode);

  return NS_OK;
}
nsresult
nsXFormsMDGEngine::SetNodeValueInternal(nsIDOMNode       *aContextNode,
                                        const nsAString  &aNodeValue,
                                        PRBool            aMarkNode,
                                        PRBool            aIsCalculate,
                                        PRBool           *aNodeChanged)
{
  if (aNodeChanged) {
    *aNodeChanged = PR_FALSE;
  }

  const nsXFormsNodeState* ns = GetNodeState(aContextNode);
  NS_ENSURE_TRUE(ns, NS_ERROR_FAILURE);

  // If the node is read-only and not set by a @calculate MIP,
  // ignore the call
  if (ns->IsReadonly() && !aIsCalculate) {
    ///
    /// @todo Better feedback for readonly nodes? (XXX)
    return NS_OK;
  }

  nsCOMPtr<nsIDOMNode> childNode;
  PRUint16 nodeType;
  nsresult rv = aContextNode->GetNodeType(&nodeType);
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoString oldValue;
  nsXFormsUtils::GetNodeValue(aContextNode, oldValue);
  if (oldValue.Equals(aNodeValue)) {
    return NS_OK;
  }

  switch(nodeType) {
  case nsIDOMNode::ATTRIBUTE_NODE:
  case nsIDOMNode::TEXT_NODE:
  case nsIDOMNode::CDATA_SECTION_NODE:
  case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
  case nsIDOMNode::COMMENT_NODE:
    rv = aContextNode->SetNodeValue(aNodeValue);
    NS_ENSURE_SUCCESS(rv, rv);

    break;

  case nsIDOMNode::ELEMENT_NODE:

    rv = aContextNode->GetFirstChild(getter_AddRefs(childNode));
    NS_ENSURE_SUCCESS(rv, rv);

    if (!childNode) {
      rv = CreateNewChild(aContextNode, aNodeValue);
      NS_ENSURE_SUCCESS(rv, rv);
    } else {
      PRUint16 childType;
      rv = childNode->GetNodeType(&childType);
      NS_ENSURE_SUCCESS(rv, rv);

      if (childType == nsIDOMNode::TEXT_NODE ||
          childType == nsIDOMNode::CDATA_SECTION_NODE) {
        rv = childNode->SetNodeValue(aNodeValue);
        NS_ENSURE_SUCCESS(rv, rv);

        // Remove all leading text child nodes except first one (see
        // nsXFormsUtils::GetNodeValue method for motivation).
        nsCOMPtr<nsIDOMNode> siblingNode;
        while (true) {
          rv = childNode->GetNextSibling(getter_AddRefs(siblingNode));
          NS_ENSURE_SUCCESS(rv, rv);
          if (!siblingNode)
            break;

          rv = siblingNode->GetNodeType(&childType);
          NS_ENSURE_SUCCESS(rv, rv);
          if (childType != nsIDOMNode::TEXT_NODE &&
              childType != nsIDOMNode::CDATA_SECTION_NODE) {
            break;
          }
          nsCOMPtr<nsIDOMNode> stubNode;
          rv = aContextNode->RemoveChild(siblingNode,
                                         getter_AddRefs(stubNode));
          NS_ENSURE_SUCCESS(rv, rv);
        }
      } else {
        // Not a text child, create a new one
        rv = CreateNewChild(aContextNode, aNodeValue, childNode);
        NS_ENSURE_SUCCESS(rv, rv);
      }
    }

    break;

  default:
    /// Unsupported nodeType
    /// @todo Should return more specific error? (XXX)
    return NS_ERROR_ILLEGAL_VALUE;
    break;
  }
  
  // NB: Never reached for Readonly nodes.  
  if (aNodeChanged) {
    *aNodeChanged = PR_TRUE;
  }
  if (aMarkNode) {
      MarkNodeAsChanged(aContextNode);
  }
  
  return NS_OK;
}