home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2003 January / maximum-cd-2003-01.iso / Software / Games / AoM / mtrial.exe / AOM / AI / SCNLIB_1.XS < prev    next >
Encoding:
Text File  |  2002-08-27  |  14.8 KB  |  518 lines

  1. // *****************************************************************************
  2. //
  3. // scn lib.xs
  4. //
  5. // General library utilities for scenarios
  6. // Leaner versions of RM utilities, without escrow IDs and other complications
  7. //
  8. // *****************************************************************************
  9.  
  10.  
  11.  
  12. //==============================================================================
  13. // Failure
  14. // To be called in the event of an error. Breaks the script, preserves
  15. // the current call stack, etc.
  16. //==============================================================================
  17. void failure( string msg="" )
  18. {
  19.     aiEcho("Failure: " + msg);
  20. }
  21.  
  22.  
  23.  
  24. // *****************************************************************************
  25. //
  26. // configQuery
  27. //
  28. // Sets up all the non-default parameters so you can config a query on a single call.
  29. // Query must be created prior to calling, and the results reset and the query executed
  30. // after the call.
  31. //
  32. // ***************************************************************************** 
  33. bool  configQuery( int queryID = -1, int unitType = -1, int action = -1, int state = -1, int player = -1, vector center = vector(-1,-1,-1), bool sort = false, float radius = -1 )
  34. {
  35.  
  36.    if ( queryID == -1)
  37.    {
  38.       failure("Invalid query ID");
  39.       return(false);
  40.    }
  41.  
  42.    if (player != -1)
  43.       kbUnitQuerySetPlayerID(queryID, player);
  44.    
  45.    if (unitType != -1)
  46.       kbUnitQuerySetUnitType(queryID, unitType);
  47.  
  48.    if (action != -1)
  49.       kbUnitQuerySetActionType(queryID, action);
  50.  
  51.    if (state != -1)
  52.       kbUnitQuerySetState(queryID, state);
  53.  
  54.    if (center != vector(-1,-1,-1))
  55.    {
  56.       kbUnitQuerySetPosition(queryID, center);
  57.       if (sort == true)
  58.          kbUnitQuerySetAscendingSort(queryID, true);
  59.       if (radius != -1)
  60.          kbUnitQuerySetMaximumDistance(queryID, radius);
  61.    }
  62.    return(true);
  63. }
  64.  
  65. // *****************************************************************************
  66. //
  67. // configQueryRelation
  68. //
  69. // Sets up all the non-default parameters so you can config a query on a single call.
  70. // Query must be created prior to calling, and the results reset and the query executed
  71. // after the call.
  72. // Unlike configQuery(), this uses the PLAYER RELATION rather than the player number
  73. //
  74. // ***************************************************************************** 
  75. bool  configQueryRelation( int queryID = -1, int unitType = -1, int action = -1, int state = -1, int playerRelation = -1, vector center = vector(-1,-1,-1), bool sort = false, float radius = -1 )
  76. {
  77.  
  78.    if ( queryID == -1)
  79.    {
  80.       failure("Invalid query ID");
  81.       return(false);
  82.    }
  83.  
  84.    if (playerRelation != -1)
  85.       kbUnitQuerySetPlayerRelation(queryID, playerRelation);
  86.    
  87.    if (unitType != -1)
  88.       kbUnitQuerySetUnitType(queryID, unitType);
  89.  
  90.    if (action != -1)
  91.       kbUnitQuerySetActionType(queryID, action);
  92.  
  93.    if (state != -1)
  94.       kbUnitQuerySetState(queryID, state);
  95.  
  96.    if (center != vector(-1,-1,-1))
  97.    {
  98.       kbUnitQuerySetPosition(queryID, center);
  99.       if (sort == true)
  100.          kbUnitQuerySetAscendingSort(queryID, true);
  101.       if (radius != -1)
  102.          kbUnitQuerySetMaximumDistance(queryID, radius);
  103.    }
  104.    return(true);
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. //==============================================================================
  115. // getUnit( int unitType, int action, vector center)
  116. // 
  117. // Returns a unit of the specified type, doing the specified action.
  118. // Defaults = any unit, any action.
  119. // Searches units owned by this player only, can include buildings.
  120. // If a location is specified, the nearest matching unit is returned.
  121. //==============================================================================
  122.  
  123. int   getUnit( int unitType = -1, int action = -1, vector center = vector(-1,-1,-1) )
  124. {
  125.  
  126.       int   retVal = -1;
  127.    int   count = -1;
  128.     int   unitQueryID = kbUnitQueryCreate("unit");
  129.  
  130.     // Define a query to get all matching units
  131.     if (unitQueryID != -1)
  132.     {
  133.         kbUnitQuerySetPlayerID(unitQueryID, cMyID);         // only my units
  134.       if (unitType != -1)
  135.            kbUnitQuerySetUnitType(unitQueryID, unitType);   // only if specified
  136.       if (action != -1)
  137.            kbUnitQuerySetActionType(unitQueryID, action);   // only if specified
  138.       if (center != vector(-1,-1,-1))
  139.       {
  140.          kbUnitQuerySetPosition(unitQueryID, center);
  141.          kbUnitQuerySetAscendingSort(unitQueryID, true);
  142.       }
  143.         kbUnitQuerySetState(unitQueryID, cUnitStateAlive);
  144.     }
  145.     else
  146.    {
  147.         failure("Couldn't create unit query.");
  148.       return(-1);
  149.    }
  150.  
  151.     kbUnitQueryResetResults(unitQueryID);
  152.     count = kbUnitQueryExecute(unitQueryID);
  153.    kbUnitQuerySetState(unitQueryID, cUnitStateBuilding);     // Add buildings in process
  154.    count = kbUnitQueryExecute(unitQueryID);
  155.  
  156.     // Pick a unit and return its ID, or return -1.
  157.     if ( count > 0 )
  158.       if (center != vector(-1,-1,-1))
  159.          retVal = kbUnitQueryGetResult(unitQueryID, 0);   // closest unit
  160.       else
  161.            retVal = kbUnitQueryGetResult(unitQueryID, aiRandInt(count));    // get the ID of a random unit
  162.     else
  163.         retVal = -1;
  164.  
  165.     return(retVal);
  166. }
  167.  
  168.  
  169. // *****************************************************************************
  170. //
  171. // trainUnit
  172. //
  173. // Train qty units of type unitID, optionally gathering at gatherPoint and 
  174. // training at a minimum of interval seconds apart.  Returns the planID, or -1 on failure.
  175. // *****************************************************************************
  176. int   trainUnit( int unitID=-1, int qty=1, vector gatherPoint=vector(-1,-1,-1), int interval=-1)
  177. {
  178.  
  179.    if (unitID == -1)
  180.       return(-1);
  181.    if (qty < 1)
  182.       return(-1);
  183.    int planID = aiPlanCreate("Train "+qty+" "+kbGetProtoUnitName(unitID), cPlanTrain);
  184.     if (planID >= 0)
  185.     {
  186.         aiPlanSetVariableInt(planID, cTrainPlanUnitType, 0, unitID);
  187.         aiPlanSetVariableInt(planID, cTrainPlanNumberToTrain, 0, qty);
  188.       if (interval > 0)
  189.            aiPlanSetVariableInt(planID, cTrainPlanFrequency, 0, interval);
  190.       if (xsVectorGetX(gatherPoint) >= 0)
  191.            aiPlanSetVariableVector(planID, cTrainPlanGatherPoint, 0, gatherPoint);
  192.         aiPlanSetActive(planID);
  193.       return(planID);
  194.     }
  195.    else
  196.       return(-1);
  197. }
  198.  
  199.  
  200.  
  201.  
  202. // *****************************************************************************
  203. //
  204. // maintainUnit
  205. //
  206. // Maintain a total of qty units of type unitID, optionally gathering at gatherPoint and 
  207. // training at a minimum of interval seconds apart.  Returns the planID, or -1 on failure.
  208. // *****************************************************************************
  209. int   maintainUnit( int unitID=-1, int qty=1, vector gatherPoint=vector(-1,-1,-1), int interval=-1)
  210. {
  211.  
  212.    if (unitID == -1)
  213.       return(-1);
  214.    if (qty < 1)
  215.       return(-1);
  216.    int planID = aiPlanCreate("Maintain "+qty+" "+kbGetProtoUnitName(unitID), cPlanTrain);
  217.     if (planID >= 0)
  218.     {
  219.         aiPlanSetVariableInt(planID, cTrainPlanUnitType, 0, unitID);
  220.         aiPlanSetVariableInt(planID, cTrainPlanNumberToMaintain, 0, qty);
  221.       if (interval > 0)
  222.            aiPlanSetVariableInt(planID, cTrainPlanFrequency, 0, interval);
  223.       if (xsVectorGetX(gatherPoint) >= 0)
  224.            aiPlanSetVariableVector(planID, cTrainPlanGatherPoint, 0, gatherPoint);
  225.         aiPlanSetActive(planID);
  226.       return(planID);
  227.     }
  228.    else
  229.       return(-1);
  230. }
  231.  
  232. // *****************************************************************************
  233. //
  234. // researchTech
  235. // 
  236. // Creates a research plan to research the tech at an appropriate building
  237. //
  238. // *****************************************************************************
  239. int   researchTech(int techID=-1)
  240. {
  241.     int planID = aiPlanCreate("Research "+kbGetTechName(techID), cPlanProgression);
  242.     if(planID < 0)
  243.         return(-1);
  244.  
  245.     aiPlanSetVariableInt(planID, cProgressionPlanGoalTechID, 0, techID);
  246.     aiPlanSetActive(planID);
  247.    return(planID);
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256. // *****************************************************************************
  257. //
  258. // attackRoute()
  259. //
  260. // Makes an attack route from a series of block names.  Must have 2-5 block names.
  261. //
  262. // *****************************************************************************
  263. int   attackRoute(string name="default attack route", string block1="", string block2="", string block3="", string block4="", string block5="")
  264. {
  265.    string end="";
  266.    int numPoints=0;
  267.  
  268.    
  269.    // Start at block 5, find the last one
  270.    if (block5 != "")
  271.    {
  272.       numPoints=5;
  273.       end=block5;
  274.    }
  275.    
  276.    if ( (block4 != "") && (numPoints == 0) )
  277.    {
  278.       numPoints=4;
  279.       end=block4;
  280.    }
  281.   
  282.    if ( (block3 != "") && (numPoints == 0) )
  283.    {
  284.       numPoints=3;
  285.       end=block3;
  286.    }
  287.  
  288.    if ( (block2 != "") && (numPoints == 0) )
  289.    {
  290.       numPoints=2;
  291.       end=block2;
  292.    }
  293.    
  294.    if ( (block1 != "") && (numPoints == 0) )
  295.    {
  296.       numPoints=1;
  297.       end=block1;
  298.    }
  299.  
  300.    if (numPoints < 2)
  301.       return(-1);
  302.  
  303.    int pathID = kbPathCreate(name+" path");
  304.    if (pathID < 0)
  305.       return(-1);
  306.  
  307.    if (numPoints > 2)
  308.        kbPathAddWaypoint(pathID, kbGetBlockPosition(block2));
  309.    if (numPoints > 3)
  310.        kbPathAddWaypoint(pathID, kbGetBlockPosition(block3));
  311.    if (numPoints > 4)
  312.        kbPathAddWaypoint(pathID, kbGetBlockPosition(block4));
  313.  
  314.  
  315.    int attackRouteID = kbCreateAttackRouteWithPath(name, kbGetBlockPosition(block1), kbGetBlockPosition(end));
  316.    if (attackRouteID < 0)
  317.       return(-1);
  318.    if (numPoints > 2)
  319.    kbAttackRouteAddPath(attackRouteID, pathID);
  320.  
  321.    return(attackRouteID);
  322. }
  323.  
  324.  
  325. // *****************************************************************************
  326. //
  327. // int getUnassignedUnitCount(vector center, float radius, int player, int unitType)
  328. //
  329. // Counts the number of player's units of type unitType that don't belong to 
  330. // a plan.  Count is limited to a distance of radius around point center.
  331. //
  332. // Generally used to count newly spawned units in order to set appropriate want/max
  333. // levels for an attack plan.
  334. // *****************************************************************************
  335.  
  336. int getUnassignedUnitCount(vector center=vector(-1.0, -1.0, -1.0), float radius=25.0, int player=2, int unitType=cUnitTypeUnit)
  337. {
  338.    int unassigned=0;       // Number of unassigned units found by traversing the query results
  339.    int query=-1;           // Query to find the units of unitType within radius of center.
  340.    int count=-1;           // Number of units found by the query.
  341.    int i=-1;
  342.  
  343.    query = kbUnitQueryCreate("Unassigned units");
  344.    if (query < 0)
  345.       return(-1);
  346.  
  347.    configQuery(query, unitType, -1, cUnitStateAlive, player, center, true, radius);
  348.    kbUnitQueryResetResults(query);
  349.    count = kbUnitQueryExecute(query);
  350.  
  351.    for (i=0; <count)
  352.    {
  353.       if (kbUnitGetPlanID(kbUnitQueryGetResult(query,i)) == -1)      // if not a member of a plan...
  354.          unassigned = unassigned + 1;
  355.    }
  356.    return(unassigned);
  357. }
  358.  
  359.  
  360.  
  361.  
  362.  
  363. // *****************************************************************************
  364. //
  365. // string timeString(bool trimmed=true)
  366. // 
  367. // Returns the current time in h:mm:ss format.  If trimmed is true, it suppresses
  368. // leading spaces or zeros.  If false, string is always 7 characters.  
  369. // Not responible for games over 10 hours.  ;-)
  370. //
  371. // *****************************************************************************
  372. string timeString(bool trimmed=true)
  373. {
  374.    int hour = 0;
  375.    int min = 0;
  376.    int sec = 0;
  377.    int time = 0;
  378.    string retval = "";
  379.  
  380.    time = xsGetTime()/1000;   // Seconds
  381.    hour = time/3600;
  382.    time = time - (hour*3600);
  383.    min = time/60;
  384.    time = time -(min*60);
  385.    sec = time;
  386.  
  387.    if (trimmed == true)
  388.    {
  389.       if (hour > 0)     // start with h:
  390.       {
  391.          retval = hour+":";
  392.          if (min >= 10)
  393.             retval = retval+min+":";
  394.          else
  395.             retval = retval+0+min+":";
  396.          if (sec >=10)
  397.             retval = retval+sec;
  398.          else
  399.             retval = retval+0+sec;
  400.       }
  401.       else
  402.       {                 // start with min
  403.          retval = min+":";
  404.          if (sec >=10)
  405.             retval = retval+sec;
  406.          else
  407.             retval = retval+0+sec;
  408.       }
  409.    }
  410.    else  // non-trimmed
  411.    {
  412.       retval = hour+":";
  413.      if (min >= 10)
  414.          retval = retval+min+":";
  415.       else
  416.          retval = retval+0+min+":";
  417.       if (sec >=10)
  418.          retval = retval+sec;
  419.       else
  420.          retval = retval+0+sec;
  421.    }
  422.    return(retval);
  423. }
  424.  
  425.  
  426.  
  427. /*
  428. // *****************************************************************************
  429. //
  430. // build(int BuildingID, int areaID) 
  431. //
  432. // *****************************************************************************
  433.  
  434. int build(int building=-1, int escrow=0, int areaID=-1)
  435. {
  436.    int plan=aiPlanCreate("Build "+kbGetProtoUnitName(building), cPlanProgression);
  437.    if (plan < 0)
  438.       return(-1);
  439.  
  440.    //Set it for the building that we get our unit from.
  441.    aiPlanSetVariableInt(plan, cProgressionPlanGoalUnitID, 0, building);
  442.    //Build it in our town.
  443.    aiPlanSetVariableInt(plan, cProgressionPlanBuildAreaID, 0, areaID);
  444.    //Go.
  445.    aiPlanSetActive(plan);
  446.    return(plan);
  447. }
  448. */
  449.  
  450.  
  451.  
  452.  
  453. // *****************************************************************************
  454. //
  455. // echoQuery(int queryID)
  456. //
  457. // aiEchos the list of items in the query result space, with ID numbers and unit types.
  458. //
  459. // *****************************************************************************
  460. void  echoQuery(int queryID = -1)
  461. {
  462.    if (queryID < 0)
  463.    {
  464.       aiEcho("Invalid query");
  465.       return;
  466.    }
  467.  
  468.    int i = 0;
  469.    int id = 0;
  470.    for (i=0; < kbUnitQueryNumberResults(queryID))
  471.    {
  472.       id = kbUnitQueryGetResult(queryID, i);
  473.       aiEcho("    "+id+" ("+kbGetProtoUnitName(kbGetUnitBaseTypeID(id))+")");
  474.    }
  475.  
  476. }
  477.  
  478.  
  479.  
  480. //==============================================================================
  481. //createSimpleMaintainPlan:  DCP
  482. //==============================================================================
  483. bool createSimpleMaintainPlan(int puid=-1, int number=1, bool economy=true, int baseID=-1)
  484. {
  485.    //Create a the plan name.
  486.    string planName="Military";
  487.    if (economy == true)
  488.       planName="Economy";
  489.    planName=planName+" maintain "+number+" "+kbGetProtoUnitName(puid)+" ";// e.g. "Economy maintain 17 villager "
  490.    int planID=aiPlanCreate(planName, cPlanTrain);
  491.    if (planID < 0)
  492.       return(false);
  493.  
  494.    //Economy or Military.
  495.    if (economy == true)
  496.       aiPlanSetEconomy(planID, true);
  497.    else
  498.       aiPlanSetMilitary(planID, true);
  499.    //Unit type.
  500.    aiPlanSetVariableInt(planID, cTrainPlanUnitType, 0, puid);
  501.    //Number.
  502.    aiPlanSetVariableInt(planID, cTrainPlanNumberToMaintain, 0, number);
  503.  
  504.    //If we have a base ID, use it.
  505.    if (baseID >= 0)
  506.    {
  507.       aiPlanSetBaseID(planID, baseID);
  508.       if  (economy == false)
  509.          aiPlanSetVariableVector(planID, cTrainPlanGatherPoint, 0, kbBaseGetMilitaryGatherPoint(cMyID, baseID));
  510.    }
  511.  
  512.    aiPlanSetActive(planID);
  513.  
  514.    //Done.
  515.    return(true);
  516. }
  517.  
  518.