Advertisement
Guest User

script.cpp

a guest
Apr 17th, 2021
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.56 KB | None | 0 0
  1. #include "script.h"
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <fstream>
  6. #include "keyboard.h"
  7. using namespace std;
  8.  
  9. //#define DEBUG_CONSOLE
  10.  
  11. void allocateConsole()
  12. {
  13.     AllocConsole();
  14.     SetConsoleTitleA("RDR2DebugConsole");
  15.  
  16.     FILE* pCout;
  17.     freopen_s(&pCout, "CONOUT$", "w", stdout);
  18.     freopen_s(&pCout, "CONOUT$", "w", stderr);
  19.     std::cout.clear();
  20.     std::clog.clear();
  21.     std::cerr.clear();
  22. }
  23.  
  24. void notifycomplex(const char* fmt, ...)
  25. {
  26. #ifdef DEBUG_CONSOLE
  27.     char buf[2048] = { 0 };
  28.     va_list va_alist;
  29.     va_start(va_alist, fmt);
  30.     vsprintf_s(buf, fmt, va_alist);
  31.     va_end(va_alist);
  32.     char buff2[2048] = { 0 };
  33.     sprintf_s(buff2, "%s", buf);
  34.     const char* literalString = MISC::VAR_STRING(10, "LITERAL_STRING", buff2);
  35.     std::cout << literalString << std::endl;
  36. #endif
  37. }
  38.  
  39. struct GunInfo
  40. {
  41.     Vehicle Gun;
  42.     Ped Gunner;
  43.     bool Available;
  44.     bool WaitingForPedToReach;
  45.     bool Occupied;
  46.     bool PedRecentlyKilled;
  47.     bool AssignedTarget;
  48.     Ped Target;
  49.     int CooldownTimer;
  50. };
  51.  
  52. Hash MaximHash;
  53.  
  54. const int maxguns = 4;
  55.  
  56. const int defaultcooldown = 400;
  57.  
  58. const float ConsiderRange = 20.0;
  59. const float MaxShootingRange = 60.0;
  60.  
  61. int GunAccuracy = 10;
  62.  
  63. GunInfo DefaultGunInfo = { NULL, NULL, false, false, false, false, false, NULL, 0 };
  64. GunInfo KnownGuns[maxguns];
  65.  
  66. void refreshConsole()
  67. {
  68. #ifdef DEBUG_CONSOLE
  69.     cout << string(100, '\n');
  70.    
  71.     cout << "----- TRACKING GUNS -----" << endl;
  72.    
  73.     for (int i = 0; i < maxguns; i++)
  74.     {
  75.         notifycomplex("GUN %i", i);
  76.         notifycomplex("    VEHICLE: %s", (KnownGuns[i].Gun == NULL) ? " " : "YES");
  77.         notifycomplex("       FREE: %s", KnownGuns[i].Available ? "TRUE" : "FALSE");
  78.         notifycomplex("   PED: %s", (KnownGuns[i].Gunner == NULL) ? " " : "YES");
  79.         notifycomplex("COMING: %s", KnownGuns[i].WaitingForPedToReach ? "TRUE" : "FALSE");
  80.         notifycomplex("MANNED: %s", KnownGuns[i].Occupied ? "TRUE" : "FALSE");
  81.         notifycomplex("KILLED: %s", KnownGuns[i].PedRecentlyKilled ? "TRUE" : "FALSE");
  82.         notifycomplex("TARGET: %s", KnownGuns[i].AssignedTarget ? "TRUE" : "FALSE");
  83.         notifycomplex("TARGET: %s", (KnownGuns[i].Target == NULL) ? " " : "YES");
  84.         notifycomplex(" TIMER: %i", KnownGuns[i].CooldownTimer);
  85.         cout << endl;
  86.     }
  87. #endif
  88. }
  89.  
  90. void AddGunToKnown(Vehicle g)
  91. {
  92.     bool GunExists = false;
  93.    
  94.     for (int i = 0; i < maxguns; i++)
  95.     {
  96.         if (KnownGuns[i].Gun == g)
  97.         {
  98.             GunExists = true;
  99.         }
  100.     }
  101.  
  102.     if (!GunExists)
  103.     {
  104.         for (int i = 0; i < maxguns; i++)
  105.         {
  106.             if (KnownGuns[i].Gun == NULL)
  107.             {
  108.                 KnownGuns[i].Gun = g;
  109.                 KnownGuns[i].Available = true;
  110.                 break;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. void RemoveGunFromKnown(Vehicle g)
  117. {
  118.     for (int i = 0; i < maxguns; i++)
  119.     {
  120.         if (KnownGuns[i].Gun == g)
  121.         {
  122.             KnownGuns[i] = DefaultGunInfo;
  123.         }
  124.     }
  125. }
  126.  
  127. bool WorldGunValid(Vehicle v)
  128. {
  129.     if (v == NULL)
  130.     {
  131.         return false;
  132.     }
  133.  
  134.     if (!VEHICLE::IS_VEHICLE_MODEL(v, MaximHash))
  135.     {
  136.         return false;
  137.     }
  138.  
  139.     if (!ENTITY::IS_ENTITY_UPRIGHT(v, 40.0))
  140.     {
  141.         return false;
  142.     }
  143.  
  144.     if (VEHICLE::IS_VEHICLE_WRECKED(v))
  145.     {
  146.         return false;
  147.     }
  148.  
  149.     return true;
  150. }
  151.  
  152. bool GunOccupiedByPlayer(Vehicle gun)
  153. {
  154.     return (PED::GET_VEHICLE_PED_IS_IN(PLAYER::PLAYER_PED_ID(), false) == gun);
  155. }
  156.  
  157. bool PedIsUsingKnownGun(Ped p)
  158. {
  159.     bool usingOurGuns = false;
  160.     for (int i = 0; i < maxguns; i++)
  161.     {
  162.         if (PED::GET_VEHICLE_PED_IS_IN(p, false) == KnownGuns[i].Gun)
  163.         {
  164.             usingOurGuns = true;
  165.         }
  166.     }
  167.  
  168.     return usingOurGuns;
  169. }
  170.  
  171. bool PedIsGunCapable(Ped p)
  172. {
  173.     if (!PED::IS_PED_HUMAN(p))
  174.     {
  175.         return false;
  176.     }
  177.  
  178.     if (p == PLAYER::PLAYER_PED_ID() || p == PLAYER::GET_PLAYER_PED(PLAYER::PLAYER_ID()) || PED::IS_PED_A_PLAYER(p))
  179.     {
  180.         return false;
  181.     }
  182.  
  183.     if (PED::IS_PED_FATALLY_INJURED(p) || PED::IS_PED_DEAD_OR_DYING(p, false))
  184.     {
  185.         return false;
  186.     }
  187.  
  188.     if (PED::IS_PED_LASSOED(p) || PED::IS_PED_BEING_DRAGGED(p) || PED::IS_PED_HOGTIED(p) || PED::IS_PED_INCAPACITATED(p) || PED::IS_PED_RAGDOLL(p))
  189.     {
  190.         return false;
  191.     }
  192.  
  193.     //if (!PED::IS_PED_ON_FOOT(p) && !PedIsUsingKnownGun(p))
  194.     //{
  195.     //  return false;
  196.     //}
  197.  
  198.     bool combat = false;
  199.     Ped potentialpeds[1024];
  200.     int pcount = worldGetAllPeds(potentialpeds, 1024);
  201.  
  202.     for (int i = 0; i < pcount; i++)
  203.     {
  204.         Ped other = potentialpeds[i];
  205.  
  206.         if (PED::IS_PED_IN_COMBAT(p, other))
  207.         {
  208.             combat = true;
  209.         }
  210.     }
  211.  
  212.     if (!combat)
  213.     {
  214.         return false;
  215.     }
  216.  
  217.     return true;
  218. }
  219.  
  220. bool PedInGunConsiderRange(Ped ped, Vehicle gun)
  221. {
  222.     Vector3 p = ENTITY::GET_ENTITY_COORDS(ped, true, true);
  223.     Vector3 g = ENTITY::GET_ENTITY_COORDS(gun, true, true);
  224.  
  225.     return MISC::GET_DISTANCE_BETWEEN_COORDS(p.x, p.y, p.z, g.x, g.y, g.z, true) <= ConsiderRange;
  226. }
  227.  
  228. bool PedAlreadyAssignedToGun(Ped p)
  229. {
  230.     for (int i = 0; i < maxguns; i++)
  231.     {
  232.         if (KnownGuns[i].Gunner == p)
  233.         {
  234.             return true;
  235.         }
  236.     }
  237.  
  238.     return false;
  239. }
  240.  
  241. void TellPedToManGun(Ped p, Vehicle gun)
  242. {
  243.     TASK::TASK_ENTER_VEHICLE(p, gun, 20000, -1, 2.0f, 1, 0);
  244. }
  245.  
  246. bool PedIsValidGunTarget(Ped target, Ped gunner)
  247. {
  248.     if (target == gunner)
  249.     {
  250.         return false;
  251.     }
  252.    
  253.     Vector3 tloc = ENTITY::GET_ENTITY_COORDS(target, true, true);
  254.     Vector3 gloc = ENTITY::GET_ENTITY_COORDS(gunner, true, true);
  255.  
  256.     if (MISC::GET_DISTANCE_BETWEEN_COORDS(tloc.x, tloc.y, tloc.z, gloc.x, gloc.y, gloc.z, true) > MaxShootingRange)
  257.     {
  258.         return false;
  259.     }
  260.  
  261.     // Figure out shooting at hostile animals later
  262.     if (!PED::IS_PED_HUMAN(target))
  263.     {
  264.         return false;
  265.     }
  266.    
  267.     if (!ENTITY::HAS_ENTITY_CLEAR_LOS_TO_ENTITY(gunner, target, 17))
  268.     {
  269.         return false;
  270.     }
  271.  
  272.     if (PED::IS_PED_DEAD_OR_DYING(target, false))
  273.     {
  274.         return false;
  275.     }
  276.  
  277.     if (PED::IS_PED_LASSOED(target) || PED::IS_PED_BEING_DRAGGED(target) || PED::IS_PED_HOGTIED(target) || PED::IS_PED_INCAPACITATED(target) || PED::IS_PED_RAGDOLL(target))
  278.     {
  279.         return false;
  280.     }
  281.  
  282.     //if (!PED::IS_PED_IN_COMBAT(gunner, target))
  283.     //{
  284.     //  return false;
  285.     //}
  286.  
  287.     enum RelationshipType
  288.     {
  289.         ACQUAINTANCE_TYPE_NONE = 0,
  290.         ACQUAINTANCE_TYPE_PED_RESPECT = 1,
  291.         ACQUAINTANCE_TYPE_PED_LIKE = 2,
  292.         ACQUAINTANCE_TYPE_PED_IGNORE = 3,
  293.         ACQUAINTANCE_TYPE_PED_DISLIKE = 4,
  294.         ACQUAINTANCE_TYPE_PED_HATE = 6
  295.     };
  296.  
  297.     int RelationToTarget = PED::GET_RELATIONSHIP_BETWEEN_PEDS(gunner, target);
  298.  
  299.     if (RelationToTarget == ACQUAINTANCE_TYPE_PED_LIKE ||
  300.         RelationToTarget == ACQUAINTANCE_TYPE_PED_RESPECT ||
  301.         RelationToTarget == ACQUAINTANCE_TYPE_PED_IGNORE)
  302.     {
  303.         return false;
  304.     }
  305.  
  306.     return true;
  307. }
  308.  
  309. Ped ChooseGunTarget(Ped gunner, Vehicle gun)
  310. {
  311.     Ped target = NULL;
  312.     Ped potentialpeds[1024];
  313.     int pcount = worldGetAllPeds(potentialpeds, 1024);
  314.  
  315.     for (int i = 0; i < pcount; i++)
  316.     {
  317.         Ped ped = potentialpeds[i];
  318.  
  319.         if (PedIsValidGunTarget(ped, gunner))
  320.         {
  321.             target = ped;
  322.         }
  323.     }
  324.  
  325.     return target;
  326. }
  327.  
  328. void TriggerGunCooldown(int i)
  329. {
  330.     KnownGuns[i].Available = false;
  331.     KnownGuns[i].Gunner = NULL;
  332.     KnownGuns[i].WaitingForPedToReach = false;
  333.     KnownGuns[i].Occupied = false;
  334.     KnownGuns[i].AssignedTarget = false;
  335.     KnownGuns[i].Target = NULL;
  336.     KnownGuns[i].PedRecentlyKilled = true;
  337.     KnownGuns[i].CooldownTimer = defaultcooldown;
  338. }
  339.  
  340. void ProcessGunsInWorld()
  341. {
  342.     Vehicle potentialguns[50];
  343.     int vcount = worldGetAllVehicles(potentialguns, 50);
  344.  
  345.     for (int i = 0; i < vcount; i++)
  346.     {
  347.         Vehicle gun = potentialguns[i];
  348.  
  349.         if (WorldGunValid(gun))
  350.         {
  351.             AddGunToKnown(gun);
  352.         }
  353.         else
  354.         {
  355.             RemoveGunFromKnown(gun);
  356.         }
  357.     }
  358. }
  359.  
  360. void ProcessKnownGuns()
  361. {
  362.     Ped potentialpeds[1024];
  363.     int pcount = worldGetAllPeds(potentialpeds, 1024);
  364.    
  365.     for (int i = 0; i < maxguns; i++)
  366.     {
  367.         if (KnownGuns[i].Gun == NULL)
  368.         {
  369.             KnownGuns[i] = DefaultGunInfo;
  370.             break;
  371.         }
  372.  
  373.         // If the player is on the gun then just set a cooldown, it'll start counting down as soon as they leave
  374.         if (GunOccupiedByPlayer(KnownGuns[i].Gun) && !KnownGuns[i].WaitingForPedToReach)
  375.         {
  376.             TriggerGunCooldown(i);
  377.         }
  378.         // If the gun is available and has nobody assigned to it, assign a valid ped and tell them to enter it
  379.         else if (KnownGuns[i].Available && KnownGuns[i].Gunner == NULL)
  380.         {
  381.             for (int j = 0; j < pcount; j++)
  382.             {
  383.                 Ped ped = potentialpeds[j];
  384.                
  385.                 if (PedIsGunCapable(ped) && PedInGunConsiderRange(ped, KnownGuns[i].Gun) && !PedAlreadyAssignedToGun(ped) && !KnownGuns[i].WaitingForPedToReach)
  386.                 {
  387.                     KnownGuns[i].Available = false;
  388.                     KnownGuns[i].Gunner = ped;
  389.                     KnownGuns[i].WaitingForPedToReach = true;
  390.  
  391.                     TellPedToManGun(KnownGuns[i].Gunner, KnownGuns[i].Gun);
  392.                 }
  393.             }
  394.         }
  395.         // If we have a ped, but they died or something, clear everything and start the cooldown
  396.         else if (KnownGuns[i].Gunner != NULL && !PedIsGunCapable(KnownGuns[i].Gunner))
  397.         {
  398.             TriggerGunCooldown(i);
  399.         }
  400.         // If we have a cooldown, decrement it
  401.         else if (KnownGuns[i].PedRecentlyKilled && KnownGuns[i].CooldownTimer > 0)
  402.         {
  403.             KnownGuns[i].CooldownTimer--;
  404.         }
  405.         // If the cooldown expired, reset
  406.         else if (KnownGuns[i].PedRecentlyKilled && KnownGuns[i].CooldownTimer <= 0)
  407.         {
  408.             KnownGuns[i].PedRecentlyKilled = false;
  409.             KnownGuns[i].Available = true;
  410.         }
  411.         // If we have a ped but they're still running to the gun, check if they've reached it and prepare to fire if they have
  412.         else if (KnownGuns[i].Gunner != NULL && KnownGuns[i].WaitingForPedToReach)
  413.         {
  414.             if (PED::GET_VEHICLE_PED_IS_IN(KnownGuns[i].Gunner, false) == KnownGuns[i].Gun)
  415.             {
  416.                 KnownGuns[i].WaitingForPedToReach = false;
  417.                 KnownGuns[i].Occupied = true;
  418.  
  419.                 // From ambush_exc_wagon_turret.c (Lemoyne Raiders Maxim Ambush)
  420.                 PED::SET_PED_COMBAT_MOVEMENT(KnownGuns[i].Gunner, 0);
  421.                 PED::SET_PED_COMBAT_ATTRIBUTES(KnownGuns[i].Gunner, 27, false);
  422.                 PED::SET_PED_COMBAT_ATTRIBUTES(KnownGuns[i].Gunner, 30, true);
  423.                 WEAPON::_0xA769D753922B031B(KnownGuns[i].Gunner, 0.5f, 0.5f);
  424.                 PED::SET_PED_FIRING_PATTERN(KnownGuns[i].Gunner, 1575766855);
  425.                 PED::SET_PED_CONFIG_FLAG(KnownGuns[i].Gunner, 22, true);
  426.                 PED::SET_PED_ACCURACY(KnownGuns[i].Gunner, GunAccuracy);
  427.             }
  428.         }
  429.         // If we have a ped, and they are manning the gun, then give them a target and start shooting at it
  430.         else if (KnownGuns[i].Gunner != NULL && KnownGuns[i].Occupied && !KnownGuns[i].AssignedTarget)
  431.         {
  432.             Ped potentialTarget = ChooseGunTarget(KnownGuns[i].Gunner, KnownGuns[i].Gun);
  433.  
  434.             if (potentialTarget != NULL)
  435.             {
  436.                 KnownGuns[i].AssignedTarget = true;
  437.                 KnownGuns[i].Target = potentialTarget;
  438.  
  439.                 TASK::TASK_VEHICLE_SHOOT_AT_PED(KnownGuns[i].Gunner, potentialTarget, 360.0f);
  440.             }
  441.             else
  442.             {
  443.                 TASK::CLEAR_PED_TASKS(KnownGuns[i].Gunner, false, false);
  444.             }
  445.         }
  446.         // If the target is down, clear the target so we can get a new one next time
  447.         else if (KnownGuns[i].AssignedTarget && !PedIsValidGunTarget(KnownGuns[i].Target, KnownGuns[i].Gunner))
  448.         {
  449.             KnownGuns[i].AssignedTarget = false;
  450.             KnownGuns[i].Target = NULL;
  451.         }
  452.         // If we have a target then shoot at it
  453.         else if (KnownGuns[i].AssignedTarget)
  454.         {
  455.             // The game handles the actual shooting
  456.         }
  457.         // If something strange happened, just reset
  458.         else if (KnownGuns[i].Gun != NULL && KnownGuns[i].Gunner != NULL)
  459.         {
  460.             TASK::TASK_EVERYONE_LEAVE_VEHICLE(KnownGuns[i].Gun, true);
  461.             TriggerGunCooldown(i);
  462.         }
  463.     }
  464. }
  465.  
  466. void ProcessOtherHeavyWeapons()
  467. {
  468.     Vehicle mountedguns[10];
  469.     int count = worldGetAllVehicles(mountedguns, 10);
  470.  
  471.     for (int i = 0; i < count; i++)
  472.     {
  473.         Vehicle gun = mountedguns[i];
  474.         Hash gunHash = ENTITY::GET_ENTITY_MODEL(gun);
  475.  
  476.         // These vehicles are hardcoded
  477.         //if (gunHash == MISC::GET_HASH_KEY("GATCHUCK_2") || gunHash == MISC::GET_HASH_KEY("policewagongatling01x"))
  478.         //{
  479.         //  WEAPON::_SET_VEHICLE_WEAPON_HEADING_LIMITS(gun, -1, -180.0, 180.0);
  480.         //}
  481.  
  482.         if (gun == PED::GET_VEHICLE_PED_IS_USING(PLAYER::PLAYER_PED_ID()))
  483.         {
  484.             if (gunHash == MISC::GET_HASH_KEY("hotchkiss_cannon") ||
  485.                 gunHash == MISC::GET_HASH_KEY("BREACH_CANNON") ||
  486.                 gunHash == MISC::GET_HASH_KEY("GATLING_GUN"))
  487.             {
  488.                 int ro = 0;
  489.                 float step = 0.1;
  490.                 Vector3 rot = ENTITY::GET_ENTITY_ROTATION(gun, ro);
  491.  
  492.                 if (IsKeyDown(0x41 /*A*/))
  493.                 {
  494.                     ENTITY::SET_ENTITY_ROTATION(gun, rot.x, rot.y, rot.z + step, ro, true);
  495.                 }
  496.                 else if (IsKeyDown(0x44 /*D*/))
  497.                 {
  498.                     ENTITY::SET_ENTITY_ROTATION(gun, rot.x, rot.y, rot.z - step, ro, true);
  499.                 }
  500.             }
  501.         }
  502.     }
  503. }
  504.  
  505. void update()
  506. {
  507.     ProcessGunsInWorld();
  508.     ProcessKnownGuns();
  509.     ProcessOtherHeavyWeapons();
  510. }
  511.  
  512. void main()
  513. {
  514. #ifdef DEBUG_CONSOLE
  515.     allocateConsole();
  516. #endif
  517.    
  518.     MaximHash = MISC::GET_HASH_KEY("GATLINGMAXIM02");
  519.  
  520.     for (int i = 0; i < maxguns; i++)
  521.     {
  522.         KnownGuns[i] = DefaultGunInfo;
  523.     }
  524.  
  525.     int refreshrate = 25;
  526.  
  527.     while (true)
  528.     {
  529. #ifdef DEBUG_CONSOLE
  530.         if (refreshrate <= 0)
  531.         {
  532.             refreshrate = 25;
  533.             refreshConsole();
  534.         }
  535.         else
  536.         {
  537.             refreshrate--;
  538.         }
  539. #endif
  540.         update();
  541.         WAIT(0);
  542.     }
  543. }
  544.  
  545. void ScriptMain()
  546. {
  547.     srand(GetTickCount());
  548.     main();
  549. }
  550.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement