1. void RTSSearch::search() {
  2.  
  3. // create the initial state
  4. GameStateNode g(true, this, data);
  5.  
  6. LOOP_BITS (g.getLegalActions(), action) {
  7.  
  8. printf("%s\n", data->action_strings[action].c_str());
  9. }
  10.  
  11.  
  12. open_list.push_back(g);
  13.  
  14. int nodes = 0;
  15.  
  16. bool solutionFound = false;
  17. int winningFrame = 1000000;
  18. GameStateNode * winner;
  19.  
  20. while (true) {
  21.  
  22. if (++nodes % 1000 == 0) printf("Nodes searched: %d\n", nodes);
  23.  
  24. // if we have met the goal, we're good
  25. if (g.meetsGoal()) {
  26.  
  27. solutionFound = true;
  28.  
  29. //printf("\n\nGOAL MET!\n\n");
  30. //g.printData();
  31.  
  32. if (g.currentFrame < winningFrame) {
  33. winningFrame = g.currentFrame;
  34. winner = &g;
  35.  
  36. g.printData();
  37. }
  38.  
  39. }
  40.  
  41. if (g.currentFrame < winningFrame) {
  42.  
  43. // otherwise, generate the children and add them to the open list
  44.  
  45. // for each of the bits in the bitmask
  46. LOOP_BITS(g.getLegalActions(), nextAction) {
  47.  
  48. // copy the current state to a child state
  49. GameStateNode child(g);
  50. child.parent = &g;
  51.  
  52. // perform the current action
  53. child.doAction((ZergData::zerg_action)nextAction);
  54.  
  55. // search the child state
  56. open_list.push_back(child);
  57. }
  58. }
  59. if (++search_index < open_list.size()) g = open_list[search_index];
  60. else break;
  61. }
  62.  
  63. if (solutionFound) {
  64.  
  65. winner->printData();
  66. }
  67.  
  68. }