Advertisement
Guest User

Roamer spider v1.3

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.81 KB | None | 0 0
  1. // ROAMER SCRIPT FOR SPIDERS, VER. 1.3
  2. extern void object::Roam( )
  3. {
  4.     int     list[], i;
  5.     object  target;
  6.     object  targets[];
  7.     point       center[];
  8.     point       lastPos = position, lastSafePos = position, lastUnsafePos, probSafePoint;
  9.     int         part = 0, apart = 0;
  10.     int         approach = 0;
  11.     int         cgo[];
  12.     float       safeDir;
  13.     float       roamDist[];
  14.     float       guardDist[];
  15.     float       attackDist[];
  16.     float       absDetectRange = 10000;
  17.     float       r;
  18.     float       mp = 1, tp = 1;
  19.     float       wc = 8;
  20.     float       wcd0, wcd1, wcd2, wcd3, wcd4; // directions of primary water check for checking around
  21.     float       sfsmin = 8, sfsmax = 12; // distance of search for safe point, in case if current one is not as safe
  22.     float       frange[];
  23.     float       ci = 0, cli = 0, icli = 0; // center counter, cmdline counter, internal cmdline counter
  24.     float       waiter[];
  25.     float       ftime = abstime();
  26.     float       lastOri = orientation;
  27.  
  28.     errmode(0);
  29.     while ( ismovie() != 0 )  wait(1);
  30.    
  31.     float       stime = abstime();
  32.  
  33.     i = 0;
  34.     list[i++] = WingedGrabber;
  35.     list[i++] = TrackedGrabber;
  36.     list[i++] = WheeledGrabber;
  37.     list[i++] = LeggedGrabber;
  38.     list[i++] = WingedShooter;
  39.     list[i++] = TrackedShooter;
  40.     list[i++] = WheeledShooter;
  41.     list[i++] = LeggedShooter;
  42.     list[i++] = WingedOrgaShooter;
  43.     list[i++] = TrackedOrgaShooter;
  44.     list[i++] = WheeledOrgaShooter;
  45.     list[i++] = LeggedOrgaShooter;
  46.     list[i++] = WingedSniffer;
  47.     list[i++] = TrackedSniffer;
  48.     list[i++] = WheeledSniffer;
  49.     list[i++] = LeggedSniffer;
  50.     list[i++] = Thumper;
  51.     list[i++] = PhazerShooter;
  52.     list[i++] = Recycler;
  53.     list[i++] = Shielder;
  54.     list[i++] = Subber;
  55.     list[i++] = Me;
  56.     list[i++] = Derrick;
  57.     list[i++] = BotFactory;
  58.     list[i++] = PowerStation;
  59.     list[i++] = Converter;
  60.     list[i++] = RepairCenter;
  61.     list[i++] = DefenseTower;
  62.     list[i++] = ResearchCenter;
  63.     list[i++] = RadarStation;
  64.     list[i++] = ExchangePost;
  65.     list[i++] = PowerPlant;
  66.     list[i++] = AutoLab;
  67.     list[i++] = NuclearPlant;
  68.    
  69.    
  70.     // BEGIN OF COMMAND LINE VALUES PROCESSING
  71.     // EXAMINE FOR BETTER UNDERSTANDING
  72.     // These are five simple values but with them you can create very, very different behaviors.
  73.    
  74.     center[ci] = position;
  75.     roamDist[ci] = 0;
  76.     guardDist[ci] = 0;
  77.     attackDist[ci] = 45;
  78.     waiter[ci] = 0;
  79.     frange[ci] = 8;
  80.     cgo[ci] = 1;
  81.     while(cmdline(cli) != 0)
  82.     {
  83.         if(cmdline(cli) < 1000)
  84.         {
  85.             ci++;
  86.            
  87.             // These are default values. They will be replaced with specified values or values based on other specified values if there will be any.
  88.             // Central point.
  89.             center[ci] = position;
  90.             // Range within which insect will roam around central point.
  91.             // If 0 then there is no range and insect will roam everywhere.
  92.             roamDist[ci] = 0;
  93.             // Timer after expiration of which central point changes to next one. Timer is absolute, not relative.
  94.             // By default there is no need in timer.
  95.             waiter[ci] = 0;
  96.             // If there is any enemy closer to central point than that value then insect will go and attack this enemy.
  97.             guardDist[ci] = 0;
  98.             // If there is any enemy closer to insect itself than that value then insect will go and attack this enemy.
  99.             // But insect will NOT go towards enemy in these cases:
  100.             // 1) both roam range and guard range are greater than 0 and insect is about to get out of both.
  101.             // 2) only roam range is greater than 0 and insect is about to get out of it.
  102.             attackDist[ci] = 45;
  103.             // If there is enemy then insect will start shooting at this enemy from this distance.
  104.             frange[ci] = 8;
  105.             // If insect is allowed to get out of both roam and guard ranges while moving towards enemy. 0 - now allowed, 1 - allowed.
  106.             cgo[ci] = 1;
  107.            
  108.             center[ci].x = cmdline(cli);
  109.             center[ci].y = 0;
  110.             icli = 0;
  111.             if(cmdline(cli+1) < 1000)
  112.             {
  113.                 center[ci].y = cmdline(cli+1);
  114.                 cli++;
  115.             }
  116.         }
  117.         if(cmdline(cli) >= 1000)
  118.         {
  119.             if(icli == 0)
  120.             {
  121.                 roamDist[ci] = cmdline(cli) - 1000;
  122.                 guardDist[ci] = cmdline(cli) - 1000;
  123.             }
  124.             else if(icli == 1)
  125.             {
  126.                 waiter[ci] = cmdline(cli) - 1000;
  127.             }
  128.             else if(icli == 2)
  129.             {
  130.                 guardDist[ci] = cmdline(cli) - 1000;
  131.             }
  132.             else if(icli == 3)
  133.             {
  134.                 attackDist[ci] = cmdline(cli) - 1000;
  135.             }
  136.             else if(icli == 4)
  137.             {
  138.                 if(cmdline(cli-icli+3) == 0 and attackDist[ci] > 0)
  139.                 {
  140.                     // DEFAULT: if attack dist was not specified and thus was by default equaled to default fire range... Equal it to specified fire range instead.
  141.                     attackDist[ci] = attackDist[ci] - frange[ci];
  142.                     attackDist[ci] = attackDist[ci] + cmdline(cli) - 1000;
  143.                 }
  144.                 frange[ci] = cmdline(cli) - 1000;
  145.             }
  146.             else if(icli == 5)
  147.             {
  148.                 cgo[ci] = cmdline(cli) - 1000;
  149.             }
  150.             icli++;
  151.         }
  152.         cli++;
  153.     }
  154.     // END OF COMMAND LINE VALUES PROCESSING
  155.    
  156.     ci = 0;
  157.     cli = 0;
  158.     icli = 0;
  159.    
  160.     lastPos = position;
  161.     lastSafePos = position;
  162.     lastUnsafePos.x = 9999;
  163.     lastUnsafePos.y = 9999;
  164.     lastUnsafePos.z = 0;
  165.     motor(1,1);
  166.     ftime = abstime();
  167.     while ( true )
  168.     {
  169.         if(distance2d(lastUnsafePos,position) > wc)
  170.         {
  171.             wcd0 = orientation;
  172.             wcd1 = orientation + 65;
  173.             wcd2 = orientation - 65;
  174.             wcd3 = orientation + 130;
  175.             wcd4 = orientation - 130;
  176.             if(wcd1 > 360){ wcd1 = wcd1 - 360; }
  177.             if(wcd2 < 0){ wcd2 = wcd2 + 360; }
  178.             if(wcd3 > 360){ wcd3 = wcd3 - 360; }
  179.             if(wcd4 < 0){ wcd4 = wcd4 + 360; }
  180.             if(CheckWater(position,wcd0,wc,0.5) > -1 or CheckWater(position,wcd1,wc,0.5) > -1 or CheckWater(position,wcd2,wc,0.5) > -1 or CheckWater(position,wcd3,wc,0.5) > -1 or CheckWater(position,wcd4,wc,0.5) > -1)
  181.             {
  182.                 lastUnsafePos = position;
  183.             }
  184.             else
  185.             {
  186.                 lastSafePos = position;
  187.             }
  188.         }
  189.         if(ftime > abstime()) // because before 0.1.11 Colobot was losing abstime() and lifetime()
  190.         {
  191.             i = 0;
  192.             while(i < sizeof(waiter))
  193.             {
  194.                 waiter[i] = waiter[i] - (ftime - abstime());
  195.                 if(waiter[i] < 0)
  196.                 {
  197.                     waiter[i] = 0;
  198.                 }
  199.                 i++;
  200.             }
  201.             ftime = abstime();
  202.         }
  203.         else
  204.         {
  205.             ftime = abstime();
  206.         }
  207.         if(ci < sizeof(center) - 1 and abstime() > waiter[ci] + stime and ((roamDist[ci] != 0 and distance2d(position,center[ci]) <= roamDist[ci]) or distance2d(position,center[ci]) <= 5 or (roamDist[ci] == 0 and waiter[ci] != 0)))
  208.         {
  209.             ci++;
  210.             if(roamDist[ci] - 1 > 1.5)
  211.             {
  212.                 turn(rand()*360-180);
  213.                 motor(1,1);
  214.                 wait(1);
  215.             }
  216.         }
  217.         target = null;
  218.         targets = radarall(list);
  219.         i = 0;
  220.         approach = 0;
  221.         while(i < sizeof(targets))
  222.         {
  223.             // Detect enemy only if it is not underwater
  224.             if(targets[i].altitude + topo(targets[i].position) > -2.0)
  225.             {
  226.                 if(guardDist[ci] != 0 and distance(targets[i].position,center[ci]) <= guardDist[ci] + frange[ci] and distance(position,center[ci]) <= guardDist[ci])
  227.                 {
  228.                     target = targets[i];
  229.                     approach = 1;
  230.                 }
  231.                 else if(distance(targets[i].position,position) <= attackDist[ci])
  232.                 {
  233.                     if(guardDist[ci] <= 0 and roamDist[ci] > 0)
  234.                     {
  235.                         if(cgo[ci] > 0 or distance(targets[i].position,position) <= frange[ci] or distance(targets[i].position,center[ci]) <= roamDist[ci] + frange[ci] or (distance(targets[i].position,position) > frange[ci] and distance(targets[i].position,position) <= attackDist[ci]))
  236.                         {
  237.                             approach = 2;
  238.                             if(cgo[ci] > 0 or (distance(targets[i].position,position) > frange[ci] and distance(targets[i].position,position) <= attackDist[ci]))
  239.                             {
  240.                                 approach = 1;
  241.                             }
  242.                             target = targets[i];
  243.                         }
  244.                     }
  245.                     else if(guardDist[ci] > 0 and roamDist[ci] > 0)
  246.                     {
  247.                         if(cgo[ci] > 0 or distance(targets[i].position,center[ci]) <= guardDist[ci] + frange[ci] or distance(targets[i].position,center[ci]) <= roamDist[ci] + frange[ci] or (distance(targets[i].position,position) > frange[ci] and distance(targets[i].position,position) <= attackDist[ci]))
  248.                         {
  249.                             approach = 2;
  250.                             if(cgo[ci] > 0 or (distance(targets[i].position,position) > frange[ci] and distance(targets[i].position,position) <= attackDist[ci]))
  251.                             {
  252.                                 approach = 1;
  253.                             }
  254.                             target = targets[i];
  255.                         }
  256.                     }
  257.                     else
  258.                     {
  259.                         approach = 1;
  260.                         target = targets[i];
  261.                     }
  262.                 }
  263.                 if(approach == 1 and (guardDist[ci] != 0 and guardDist[ci] < 1.5) and (roamDist[ci] != 0 and roamDist[ci] < 1.5))
  264.                 {
  265.                     approach = 2;
  266.                 }
  267.             }
  268.             if(approach > 0)
  269.             {
  270.                 break;
  271.             }
  272.             i++;
  273.         }
  274.         // if there is no enemy or (we are in danger of submersion and safe point isn't nearby already)
  275.         if (target == null or topo(position)<=-0.05 or (distance2d(lastUnsafePos,position) <= 2 and distance2d(lastSafePos,position) > 2))
  276.         {
  277.             apart = 0;
  278.             // if we are in danger of submersion
  279.             if(topo(position)<-0.05 or (distance2d(lastUnsafePos,position) <= 2 and distance2d(lastSafePos,position) > 2))
  280.             {
  281.                 if(distance(position,lastSafePos) > 1)
  282.                 {
  283.                     turn(direction(lastSafePos));
  284.                 }
  285.                 while(distance(position,lastSafePos) > 1)
  286.                 {
  287.                     motor (1, 1);
  288.                     wait(0.1);
  289.                 }
  290.                 if(target != null and distance(target.position,position) <= frange[ci])
  291.                 {
  292.                     motor (0, 0);
  293.                     fire();
  294.                 }
  295.             }
  296.             // or if we have a home and we are outside of it
  297.             else if ((roamDist[ci] > 0 and distance2d(position, center[ci]) > roamDist[ci]) or (((roamDist[ci] == 0 and waiter[ci] == 0) or roamDist[ci] < 0) and ci < sizeof(center) - 1 and distance2d(position, center[ci]) > 5))
  298.             {
  299.                 mp = 1;
  300.                 if(roamDist[ci] > 0 and roamDist[ci] <= 5 and distance2d(position, center[ci]) < 9)
  301.                 {
  302.                     mp = roamDist[ci] / 4;
  303.                 }
  304.                 if(direction(center[ci]) > 2)
  305.                 {
  306.                     tp = direction(center[ci]) / 60;
  307.                     motor (mp-0.2*tp, mp+0.2*tp);
  308.                 }
  309.                 else if(direction(center[ci]) < -2)
  310.                 {
  311.                     tp = -direction(center[ci]) / 60;
  312.                     motor (mp+0.2*tp, mp-0.2*tp);
  313.                 }
  314.                 else
  315.                 {
  316.                     motor (mp, mp);
  317.                 }
  318.                 while(target != null and distance(target.position,position) <= frange[ci])
  319.                 {
  320.                     motor (0, 0);
  321.                     fire();
  322.                 }
  323.                 // try to walk around if we got stuck
  324.                 lastPos = position;
  325.                 wait(0.5);
  326.                 if (distance2d(lastPos, position) < 0.2)
  327.                 {
  328.                     turn(rand()*360-180);
  329.                     motor(mp,mp);
  330.                     wait(1);
  331.                     if(direction(center[ci]) > 2 or direction(center[ci]) < -2)
  332.                     {
  333.                         turn(direction(center[ci]));
  334.                     }
  335.                 }
  336.             }
  337.             // if either we are homeless or we are at home already then we roam around
  338.             else if((roamDist[ci] >= 1.5 or roamDist[ci] == 0) and CheckWater(position,orientation,wc,0.5) == -1)
  339.             {
  340.                 mp = 1;
  341.                 if(roamDist[ci] > 0 and roamDist[ci] <= 9)
  342.                 {
  343.                     mp = roamDist[ci] / 6;
  344.                 }
  345.                 if((roamDist[ci] <= 5 and roamDist[ci] != 0) or distance2d(position,lastUnsafePos) <= 2)
  346.                 {
  347.                     if(roamDist[ci] > 5 or roamDist[ci] == 0)
  348.                     {
  349.                         mp = 0.45;
  350.                     }
  351.                     if(part <= 5)
  352.                     {
  353.                         mp = mp / 5 * part;
  354.                         if(rand()<0.1)
  355.                         {
  356.                             r = rand();
  357.                             if (r > 0.6)
  358.                             {
  359.                                 motor(mp*(rand()/4),mp);
  360.                             }
  361.                             else if (r < 0.4)
  362.                             {
  363.                                 motor(mp,mp*(rand()/4));
  364.                             }
  365.                             else
  366.                             {
  367.                                 r = 10;
  368.                                 while(r > 0)
  369.                                 {
  370.                                     motor(mp/10*r,mp/10*r);
  371.                                     wait(0.1);
  372.                                     r = r - 1;
  373.                                 }
  374.                                 motor(0,0);
  375.                                 wait(0.1);
  376.                                 part = 6;
  377.                             }
  378.                             lastPos = position;
  379.                         }
  380.                         else
  381.                         {
  382.                             if(direction(center[ci]) < -20)
  383.                             {
  384.                                 motor(mp,mp*rand());
  385.                             }
  386.                             else if(direction(center[ci]) > 20)
  387.                             {
  388.                                 motor(mp*rand(),mp);
  389.                             }
  390.                             else
  391.                             {
  392.                                 motor(mp,mp);
  393.                             }
  394.                             wait(0.25);
  395.                             lastPos = position;
  396.                         }
  397.                         if(part < 5)
  398.                         {
  399.                             part++;
  400.                         }
  401.                     }
  402.                     else if(part > 5)
  403.                     {
  404.                         motor(0,0);
  405.                         wait(0.3+rand()/3);
  406.                         part++;
  407.                         if(part > 13 or (part > 9 and rand() <= 0.1))
  408.                         {
  409.                             part = 0;
  410.                         }
  411.                     }
  412.                     if(part > 13)
  413.                     {
  414.                         part = 0;
  415.                     }
  416.                 }
  417.                 else
  418.                 {
  419.                     if(part == 0)
  420.                     {
  421.                         if (distance2d(lastPos, position) < 0.2)
  422.                         {
  423.                             turn(rand()*360-180);
  424.                         }
  425.                         lastPos = position;
  426.                     }
  427.                     if(part == 1)
  428.                     {
  429.                         motor(mp,mp);
  430.                         wait(0.3);
  431.                         lastPos = position;
  432.                     }
  433.                     if(part == 2)
  434.                     {
  435.                         r = rand();
  436.                         if (r > 0.6)
  437.                         {
  438.                             motor (mp, mp*0.7);
  439.                             wait(rand()*1);
  440.                             motor (mp, mp);
  441.                         }
  442.                         if (r < 0.4)
  443.                         {
  444.                             motor (mp*0.7, mp);
  445.                             wait(rand()*1);
  446.                             motor (mp, mp);
  447.                         }
  448.                         wait(0.45);
  449.                         lastPos = position;
  450.                     }
  451.                     part++;
  452.                     if(part > 2)
  453.                     {
  454.                         part = 0;
  455.                     }
  456.                 }
  457.             }
  458.             else
  459.             {
  460.                 // I don't like to call CheckWater too often, so this is fine too
  461.                 if(roamDist[ci] >= 1.5 or roamDist[ci] == 0)
  462.                 {
  463.                     r = rand();
  464.                     if(rand() < 0.3)
  465.                     {
  466.                         motor(r,-r);
  467.                     }
  468.                     else if(rand() < 0.3)
  469.                     {
  470.                         motor(-r,r);
  471.                     }
  472.                     wait(0.45);
  473.                 }
  474.                 else
  475.                 {
  476.                     r = rand() * 0.2;
  477.                     if(rand() < 0.2 and rand() < 0.2)
  478.                     {
  479.                         motor(r,-r);
  480.                     }
  481.                     else if(rand() < 0.2 and rand() < 0.2)
  482.                     {
  483.                         motor(-r,r);
  484.                     }
  485.                     else
  486.                     {
  487.                         motor(0,0);
  488.                     }
  489.                     wait(0.45);
  490.                 }
  491.             }
  492.         }
  493.         // we have a target
  494.         else if(target != null)
  495.         {
  496.             part = 0;
  497.             if(false and apart == 0)
  498.             {
  499.                 if(target != null and distance(target.position,position) <= frange[ci])
  500.                 {
  501.                     fire();
  502.                 }
  503.                 else if(direction(target.position) > 2 or direction(target.position) < -2)
  504.                 {
  505.                     turn(direction(target.position));
  506.                 }
  507.                 apart++;
  508.             }
  509.             else if(true or apart == 1)
  510.             {
  511.                 if(target != null and distance(target.position,position) <= frange[ci])
  512.                 {
  513.                     fire();
  514.                 }
  515.                 else if(ismovie() == 0 and approach == 1 and target != null and topo(position)>=-0.5 and distance2d(position,lastUnsafePos) > 2 and CheckWater(position,orientation,wc,0.5) == -1)
  516.                 {
  517.                     mp = 1;
  518.                     if(direction(target.position) > 2)
  519.                     {
  520.                         tp = direction(target.position) / 60;
  521.                         motor (mp-0.2*tp, mp+0.2*tp);
  522.                     }
  523.                     else if(direction(target.position) < -2)
  524.                     {
  525.                         tp = -direction(target.position) / 60;
  526.                         motor (mp+0.2*tp, mp-0.2*tp);
  527.                     }
  528.                     else
  529.                     {
  530.                         motor (mp, mp);
  531.                     }
  532.                     // try to walk around if we got stuck
  533.                     lastPos = position;
  534.                     wait(0.5);
  535.                     if (distance2d(lastPos, position) < 0.2)
  536.                     {
  537.                         turn(rand()*360-180);
  538.                         motor(1,1);
  539.                         wait(1);
  540.                         turn(direction(center[ci]));
  541.                     }
  542.                 }
  543.                 else
  544.                 {
  545.                     motor(0,0);
  546.                 }
  547.                 apart = 0;
  548.             }
  549.         }
  550.     }
  551. }
  552.  
  553. float CheckWater(point pos, float ori, float i, float start)
  554. {
  555.     float orix,oriy,c;
  556.     point p;
  557.     if(ori < 180)
  558.     {
  559.         if(ori < 90)
  560.         {
  561.             orix = 1-(ori/90);
  562.             oriy = ori/90;
  563.         }
  564.         else
  565.         {
  566.             orix = -((ori-90)/90);
  567.             oriy = 1-(ori-90)/90;
  568.         }
  569.     }
  570.     else
  571.     {
  572.         if(ori < 270)
  573.         {
  574.             orix = -1+((ori-180)/90);
  575.             oriy = -((ori-180)/90);
  576.         }
  577.         else
  578.         {
  579.             orix = (ori-270)/90;
  580.             oriy = -1+(ori-270)/90;
  581.         }
  582.     }
  583.     c = start;
  584.     while(c <= i)
  585.     {
  586.         p.x = pos.x+(c*orix);
  587.         p.y = pos.y+(c*oriy);
  588.         if(topo(p) <= -0.05)
  589.         {
  590.             return c;
  591.         }
  592.         c = c + 2.5;
  593.     }
  594.     return -1;
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement