Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Monsieur OUXX on Tue 10/07/2007 08:29:19

Title: SOLVED : Algorithm problem
Post by: Monsieur OUXX on Tue 10/07/2007 08:29:19
this problem is solved

Hello, I know it's not nice to bother other people about his very own algorithm, but the problem I have looks very simple (and it is probably very simple) - But, you know, sometimes you have been trying to find the error for so long that you don't see a very obvious error anymore.

Here is the function :


static ADDR AGSH_Allocator::searchContiguousBlock(ADDR node,  ADDR contiguousToBlock) {
 
    ADDR return_node = node;
 
    AGSH_Console.DebugMsg(String.Format("searchContiguousBlock (%d, %d)", node, contiguousToBlock));

    if (contiguousToBlock < 0) {
      AGSH_Console.DebugMsg(String.Format("Cannot search block contiguous to an invalid node (@%d)", contiguousToBlock));
      return INVALID_ADDR;
    }

    if (node == INVALID_ADDR) {
      //node = INVALID_ADDR;
      //do nothing; return INVALID_ADDR
      AGSH_Console.DebugMsg("null"); //DEBUG
    } else {
 
      int whatSide = AGSH_Allocator.compareNodes(contiguousToBlock, node);

      if (whatSide == CMP_INTERSECT) {
        AGSH_Console.DebugMsg("CMP_INTERSECT"); //DEBUG
        //we are probably in the case where contiguousToblock == node; search 'node's sons
        return_node = AGSH_Allocator.searchContiguousBlock(AGSH_FreeNodesLifo.GetLeftSon(node),  contiguousToBlock);
        if (return_node == INVALID_ADDR)
          return_node = AGSH_Allocator.searchContiguousBlock(AGSH_FreeNodesLifo.GetRightSon(node),  contiguousToBlock);
      } else if (whatSide == CMP_LEFT) {
        AGSH_Console.DebugMsg("CMP_LEFT"); //DEBUG
        return_node = AGSH_Allocator.searchContiguousBlock(AGSH_FreeNodesLifo.GetLeftSon(node), contiguousToBlock);
      } else if (whatSide == CMP_RIGHT) {
        AGSH_Console.DebugMsg("CMP_RIGHT"); //DEBUG
        return_node = AGSH_Allocator.searchContiguousBlock(AGSH_FreeNodesLifo.GetRightSon(node), contiguousToBlock);
      } else if (whatSide == CMP_LEFT_CONTIGUOUS_RIGHT || whatSide == CMP_RIGHT_CONTIGUOUS_LEFT) {   
        //block 'contiguousToBlock' is contiguous to 'node'
        AGSH_Console.DebugMsg("CMP_LEFT_CONTIGUOUS_RIGHT || whatSide == CMP_RIGHT_CONTIGUOUS_LEFT"); //DEBUG
        //do nothing; we return 'node'
      } else {
        AGSH_Console.Error(String.Format("AGSH_Allocator::searchContiguousBlock : Forbidden value : whatSide=%d", whatSide));
      }
    }

    AGSH_Console.DebugMsg(String.Format("Return : %d", node));

    return return_node;
 
  }
}



and here is the output :


searchContiguousBlock (688, 688)
CMP_INTERSECT
searchContiguousBlock (-1, 688)
null
Return : -1
searchContiguousBlock (-1, 688)
null
Return : -1
Return : 688


As you can see, the execution enters the "CMP_INTERSECT" condition and tries searchContiguousBlock on leftSon and rightSon. Both return INVALID_ADDR (-1); But, strangely, the final return is 688 instead of -1...

Why so???





Note : i noticed that when i don't use a temporary variable (result_node) to modify the return value directly from a parameter (node), it causes bugs (the function returns random values). That means that if 'node' is a parameter, and if i write "node = ...;" and then "return node;", then the return value will be incorrect.
This has nothing to do with my problem explained above, and someone else probably noticed it before me, but it's quite confusing.
Title: Re: Algorithm problem
Post by: Khris on Tue 10/07/2007 09:23:44
Hmmm.
    AGSH_Console.DebugMsg(String.Format("Return : %d", return_node));
?

return_node is set to node in the beginning and not changed if the node is invalid; so in that case your line displays the correct return value although you've used the wrong variable.
Title: Re: Algorithm problem
Post by: Monsieur OUXX on Tue 10/07/2007 09:30:01
Quote from: KhrisMUC on Tue 10/07/2007 09:23:44
    AGSH_Console.DebugMsg(String.Format("Return : %d", return_node));
?

We found it at the same time. THANK YOU!
Grrr, i always put some "printf" to debug, and finally they are more bugged than the code to debug ;)