Guest

s0beit

By: a guest on May 19th, 2010  |  syntax: C++  |  size: 12.73 KB  |  hits: 309  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include "CustomFiberThread.h"
  2. #include "Scripting.h"
  3. #include "../ScriptHook/Log.h"
  4.  
  5. #include <windows.h>
  6. #include <stdio.h>
  7.  
  8. using namespace Scripting;
  9.  
  10. // ASSEMBLY FUNCTIONS, MAYBE CAN NOT BE A CLASS FUNCTION
  11.  
  12. __declspec( noinline ) u32 GTA4_GetHandleFromPed( CPool< void* >* pPool, void *Ped )
  13. {
  14.         _asm mov        ecx, pPool;
  15.         _asm mov        eax, Ped;
  16.         _asm sub        eax, [ecx];
  17.         _asm cdq;
  18.         _asm idiv       dword ptr [ecx+12];
  19.         _asm mov        edx, eax;
  20.         _asm mov        eax, [ecx+4];
  21.         _asm movzx      eax, byte ptr [eax+edx];
  22.         _asm shl        edx, 8;
  23.         _asm add        eax, edx;
  24. }
  25.  
  26. // ASSEMBLY FUNCTIONS, MAYBE CAN NOT BE A CLASS FUNCTION
  27.  
  28. RiotThread::RiotThread()
  29. {
  30.         SetName( "RiotModeNative100" );
  31. }
  32.  
  33. CPool< void* >* RiotThread::GetPedPoolNative()
  34. {
  35.         return reinterpret_cast< CPool< void* >* >( *reinterpret_cast< DWORD* >( Game::GetBase() + 0x18A72BC ) );
  36. }
  37.  
  38. u32 RiotThread::GetPedCount()
  39. {
  40.         if( GetPedPoolNative() == NULL ) return 0;
  41.  
  42.         return GetPedPoolNative()->Count();
  43. }
  44.  
  45. b8 RiotThread::GetPedByIndex( int idx, Ped *Out )
  46. {
  47.         if( !Out ) return false;
  48.  
  49.         Out->Set( 0 );
  50.  
  51.         if( GetPedCount() == 0 )
  52.                 return false;
  53.  
  54.         void *CurrentPedIdx = GetPedPoolNative()->at( idx );
  55.  
  56.         if( CurrentPedIdx == NULL )
  57.                 return false;
  58.  
  59.         Out->Set( GTA4_GetHandleFromPed( GetPedPoolNative(), CurrentPedIdx ) );
  60.  
  61.         return Out->IsValid();
  62. }
  63.  
  64. b8 RiotThread::GetClosestPedFromPed( Scripting::Ped *Searcher, Scripting::Ped *Seek )
  65. {
  66.         if( Searcher == NULL || Seek == NULL )
  67.                 return false;
  68.  
  69.         if( GetPedCount() == 0 )
  70.                 return false;
  71.  
  72.         Vector3 SearcherVector = GetPedVector( Searcher );
  73.  
  74.         float LowestDistance = -1.f;
  75.  
  76.         for( u32 i = 0; i < GetPedCount(); i++ )
  77.         {
  78.                 Ped Search;
  79.  
  80.                 if( GetPedByIndex( i, &Search ) == false )
  81.                         continue;
  82.  
  83.                 Vector3 SeekVector = GetPedVector( Seek );
  84.  
  85.                 if( LowestDistance == -1.f )
  86.                 {
  87.                         LowestDistance = SeekVector.Distance( &SearcherVector );
  88.  
  89.                         continue;
  90.                 }
  91.                 else
  92.                 {
  93.                         float Dist = SeekVector.Distance( &SearcherVector );
  94.  
  95.                         if( Dist < LowestDistance )
  96.                         {
  97.                                 Seek->Set( Search.Get() );
  98.  
  99.                                 LowestDistance = Dist;
  100.  
  101.                                 continue;
  102.                         }
  103.                 }
  104.         }
  105.  
  106.         if( Seek->IsValid() )
  107.         {
  108.                 return DoesCharExist( *Seek );
  109.         }
  110.  
  111.         return false;
  112. }
  113.  
  114. Scripting::Vector3 RiotThread::GetPedVector( Scripting::Ped *In )
  115. {
  116.         float x, y, z;
  117.         Scripting::GetCharCoordinates( *In, &x, &y, &z );
  118.         return Vector3( x, y, z );
  119. }
  120.  
  121. Player RiotThread::GetPlayer()
  122. {
  123.         Player playerIndex = ConvertIntToPlayerIndex(GetPlayerId());
  124.         return playerIndex;
  125. }
  126.  
  127. Scripting::Ped RiotThread::GetPlayerPed()
  128. {
  129.         Ped ped;
  130.         GetPlayerChar( GetPlayer(), &ped );
  131.         return ped;
  132. }
  133.  
  134. Scripting::eWeapon RiotThread::GetRandomWeapon( int RandMax )
  135. {
  136.         // Include melee as the last three, so if melee weapons is enabled, randmax += 3;
  137.         u32 WeaponsAlright[ 16 ] = { 4, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 1, 2, 3 };
  138.  
  139.         srand( GetTickCount() );
  140.  
  141.         //RandMax == 13, Firing weapons only
  142.         //RandMax == 16, Melee and firing weapons included
  143.  
  144.         return static_cast< Scripting::eWeapon >( WeaponsAlright[ rand() % RandMax ] );
  145. }
  146.  
  147. u32 RiotThread::GetPedGroup( Scripting::Ped *In )
  148. {
  149.         if( In == NULL ) return 0;
  150.  
  151.         u32 GroupIdx;
  152.         Scripting::GetPedGroupIndex( *In, &GroupIdx );
  153.         return GroupIdx;
  154. }
  155.  
  156. f32 RiotThread::RandomFloat()
  157. {
  158.         srand( GetTickCount() % 1000 );
  159.  
  160.         float Scale = RAND_MAX + 1;
  161.         float BaseF = rand() / Scale;
  162.         float FineF = rand() / Scale;
  163.         return BaseF + FineF / Scale;
  164. }
  165.  
  166. void RiotThread::SetupRelationships()
  167. {
  168.         // Make everyone just hate everyone, lol
  169.  
  170.         SetRelationship( 5, 8, 8 );                                             // Rioters hate themselves
  171.  
  172.         if( m_bTargetPlayer )
  173.         {
  174.                 SetRelationship( 5, 8, 0 );                                     // Rioters hate player (you)
  175.  
  176.                 SetPlayerCanBeHassledByGangs( this->GetPlayer(), true );
  177.  
  178.                 SetRelationship( 5, 0, 8 );                                     // Player hate rioters
  179.         }
  180.         else
  181.         {
  182.                 SetRelationship( 1, 8, 0 );                                     // Rioters ignore player (you)
  183.         }
  184.  
  185.         SetRelationship( 5, 8, 3 );                                             // Rioters hate cops
  186.         SetRelationship( 5, 3, 8 );                                             // Cops hate rioters
  187.        
  188.         SetRelationship( 5, 8, 2 );                                             // Rioters hate CIVFEMALE
  189.         SetRelationship( 5, 8, 1 );                                             // Rioters hate CIVMALE
  190.  
  191.         SetRelationship( 5, 2, 8 );                                             // CIVFEMALE hate rioters
  192.         SetRelationship( 5, 1, 8 );                                             // CIVMALE hate rioters
  193. }
  194.  
  195. /*
  196. LOAD_CHAR_DECISION_MAKER(3, ZcharDec)
  197. LOAD_COMBAT_DECISION_MAKER(3, ZcombatDec)
  198. SET_DECISION_MAKER_ATTRIBUTE_CAUTION(ZcharDec.a, 2)
  199. SET_DECISION_MAKER_ATTRIBUTE_CAUTION(ZcombatDec.a, 2)
  200. */
  201.  
  202. b8 RiotThread::SetupDecisionMaker()
  203. {
  204.         // DM:
  205.         // [2,2] Seems to be the most aggressive setting in terms of weaponry
  206.         // [9,5] Seems to be agressive also
  207.         // [2,9] Anarchy Script..
  208.         // [3,3] From zombie script
  209.  
  210.         // Cation:
  211.         // [2,2] From zombie script
  212.         // [10,10] From my script
  213.  
  214.         Log::Debug( "Loading decision makers" );
  215.  
  216.         LoadCharDecisionMaker( 2, &m_CharDecisionMaker );
  217.  
  218.         LoadCombatDecisionMaker( 9, &m_CombatDecisionMaker );
  219.  
  220.         bool bAlright = ( DoesDecisionMakerExist( m_CharDecisionMaker ) && DoesDecisionMakerExist( m_CombatDecisionMaker ) );
  221.  
  222.         if( bAlright == false )
  223.         {
  224.                 Log::Debug( "Error loading decision makers..." );
  225.  
  226.                 return false;
  227.         }
  228.  
  229. //      SetDecisionMakerAttributeCaution( m_CombatDecisionMaker, 10 );
  230. //      SetDecisionMakerAttributeRetreatingBehaviour( m_CombatDecisionMaker, 4 );
  231.  
  232. //      SetDecisionMakerAttributeCaution( m_CharDecisionMaker, 10 );
  233. //      SetDecisionMakerAttributeRetreatingBehaviour( m_CharDecisionMaker, 4 );
  234.  
  235.         Log::Debug( "Loaded decision makers" );
  236.  
  237.         return true;
  238. }
  239.  
  240. // TODO: Turn fist-fights into GUN FIGHTS, WRAAAA
  241. // TODO: Peds will not offensively attack you with the menu option disabled, however may defend themselves
  242.  
  243. void RiotThread::RunScript()
  244. {
  245.         m_bEnabled                                              = true;
  246.         m_bTargetPlayer                                 = false;
  247.         m_bCopsGoCrazy                                  = true;
  248.         m_bPedsUseCars                                  = true;
  249.         m_bUseDecisionMaker                             = false;
  250.         m_bPedsWanted                                   = true;
  251.         m_bMadDrivers                                   = true;
  252.         m_bEmergancyServicesDisabled    = true;
  253.  
  254.         CreateKeyboard();
  255.  
  256.         CreateMenu();
  257.  
  258.         bool bSetupDMSuccess = SetupDecisionMaker();
  259.  
  260.         while( IsThreadAlive() )
  261.         {
  262.                 if( m_bEnabled && ( !NetworkIsSessionStarted() ) )
  263.                 {
  264.                         Ped LocalPed = GetPlayerPed();
  265.  
  266.                         SetPedDensityMultiplier( 70.f );                //More peds! (was 80.f)
  267.                        
  268.                         SwitchMadDrivers( m_bMadDrivers );
  269.  
  270.                         AllowEmergencyServices( !m_bEmergancyServicesDisabled );
  271.  
  272.                         AllowGangRelationshipsToBeChangedByNextCommand( true );
  273.  
  274.                         SetupRelationships();
  275.  
  276.                         for( u32 i = 0; i < GetPedCount(); i++ )
  277.                         {
  278.                                 Ped Index;
  279.  
  280.                                 if( GetPedByIndex( i, &Index ) )
  281.                                 {
  282.                                         if( IsCharDead( Index )
  283.                                                 || IsCharFatallyInjured( Index )
  284.                                                 || IsPedAMissionPed( Index ) )
  285.                                                 continue;
  286.  
  287.                                         u32 IndexGroup = GetPedGroup( &Index );
  288.  
  289.                                         if( m_bCopsGoCrazy == false && IndexGroup == 3 )
  290.                                                 continue; //Cops
  291.  
  292.                                         SetCharAsMissionChar( Index ); // This is what makes them commit acts of hair-trigger violence
  293.  
  294.                                         if( m_bUseDecisionMaker && bSetupDMSuccess )
  295.                                         {
  296.                                                 SetCharDecisionMaker( Index, m_CharDecisionMaker );
  297.  
  298.                                                 SetCombatDecisionMaker( Index, m_CombatDecisionMaker );
  299.                                         }
  300.  
  301.                                         if( HasCharGotWeapon( Index, eWeapon::WEAPON_ANYWEAPON ) == false )
  302.                                         {
  303.                                                 AllowTargetWhenInjured( Index, 1 );
  304.  
  305.                                                 SetCharWillMoveWhenInjured( Index, 1 );
  306.  
  307.                                                 SetCharWillDoDrivebys( Index, 1 );
  308.  
  309.                                                 SetCharWillUseCarsInCombat( Index, m_bPedsUseCars );
  310.  
  311.                                                 SetCharCanBeShotInVehicle( Index, 1 );
  312.  
  313.                                                 SetCharCanBeKnockedOffBike( Index, 1 );
  314.  
  315.                                                 SetCharCantBeDraggedOut( Index, 0 );
  316.  
  317.                                                 SetCharWantedByPolice( Index, ( m_bPedsWanted && IndexGroup != 3 ) );
  318.  
  319.                                                 CoverPoint cp;
  320.  
  321.                                                 cp.Set( 1 );
  322.  
  323.                                                 SetCharWillUseCover( Index, cp );
  324.  
  325.                                                 SetPedWontAttackPlayerWithoutWantedLevel( Index, 0 );
  326.  
  327.                                                 SetCharDropsWeaponsWhenDead( Index, false ); //TODO: Menu var
  328.                                                
  329.                                                 SetSenseRange( Index, 100.f );
  330.                                                                        
  331.                                                 SetCharAccuracy( Index, 100 );
  332.  
  333.                                                 SetCharRelationshipGroup( Index, 8 );
  334.  
  335.                                                 if( IndexGroup != 3 )
  336.                                                 {
  337.                                                         eWeapon RandomWeapon = GetRandomWeapon( m_bPedsUseMelee ? 16 : 13 );
  338.  
  339.                                                         GiveWeaponToChar( Index, RandomWeapon, 9999, 0 );
  340.  
  341.                                                         // GiveDelayedWeaponToChar( Index, RandomWeapon, 9999, 1 );
  342.  
  343.                                                         SetCurrentCharWeapon( Index, RandomWeapon, true );
  344.  
  345.                                                         BlockPedWeaponSwitching( Index, true );
  346.                                                 }
  347.  
  348.                                                 TaskCombatHatedTargetsAroundChar( Index, 100.f );
  349.  
  350.                                                 SetCharKeepTask( Index, true );
  351.                                         }
  352.                                 }
  353.                         }
  354.  
  355.                         Wait( 100 );
  356.                 }
  357.         }
  358. }
  359.  
  360. void RiotThread::OnStart()
  361. {
  362.         //Init
  363.  
  364.         m_pMenu = NULL;
  365.  
  366.         Log::Debug( "OnStart Executed" );
  367. }
  368.  
  369. void RiotThread::OnKill()
  370. {
  371.         // Destroy
  372.  
  373.         IKeyboardHookService *kbhService = ScriptHookManager::RequestService<IKeyboardHookService>( "KeyboardHook" );
  374.        
  375.         kbhService->RemoveHandler( this );
  376.  
  377.         if( m_pMenu )
  378.         {
  379.                 m_pMenu->Release();
  380.                 m_pMenu = NULL;
  381.         }
  382.  
  383.         ScriptThread::OnKill();
  384.  
  385.         Log::Debug( "OnKill Executed" );
  386. }
  387.  
  388. void RiotThread::CreateKeyboard()
  389. {
  390.         IKeyboardHookService *kbhService = ScriptHookManager::RequestService<IKeyboardHookService>( "KeyboardHook" );
  391.  
  392.         kbhService->AddHandler( this );
  393. }
  394.  
  395. void RiotThread::CreateMenu()
  396. {
  397.         IMenuService *menuService = ScriptHookManager::RequestService< IMenuService >( "Menu" );
  398.  
  399.         m_pMenu = menuService->CreateMenu();
  400.  
  401.         // Normal Menu
  402.         m_pMenu->SetTitle( "Riot mode" );
  403.         AddMenuItemCustom( 0, m_bEnabled ? "Disable Riot Mode" : "Enable Riot Mode" );
  404.         AddMenuItemCustom( 1, m_bTargetPlayer ? "Disable peds targetting player" : "Enable peds targetting player" );
  405.         AddMenuItemCustom( 2, m_bCopsGoCrazy ? "Disable cops rioting" : "Enable cops rioting" );
  406.         AddMenuItemCustom( 3, m_bPedsUseCars ? "Disable peds use cars in combat" : "Enable peds use cars in combat" );
  407.         AddMenuItemCustom( 4, m_bPedsUseMelee ? "Disable peds use melee weapons" : "Enable peds use melee weapons" );
  408.         AddMenuItemCustom( 5, m_bUseDecisionMaker ? "Disable decision maker" : "Enable decision maker" );
  409.         AddMenuItemCustom( 6, m_bPedsWanted ? "Disable peds wanted" : "Enable peds wanted" );
  410.         AddMenuItemCustom( 7, m_bMadDrivers ? "Disable mad drivers" : "Enable mad drives" );
  411.         AddMenuItemCustom( 8, m_bEmergancyServicesDisabled ? "Enable emergancy services" : "Disable emergancy services" );
  412.  
  413.         m_pMenu->SetEventHandler( this );
  414. }
  415.  
  416. void RiotThread::AddMenuItemCustom( int idx, char *pszFormat, ... )
  417. {
  418.         char FormattedBuffer[ 1024 ] = { 0 };
  419.  
  420.         va_list va_alist;
  421.  
  422.         va_start( va_alist, pszFormat );
  423.  
  424.         _vsnprintf(
  425.                 FormattedBuffer + strlen( FormattedBuffer ),
  426.                 sizeof( FormattedBuffer ) - strlen( FormattedBuffer ),
  427.                 pszFormat, va_alist );
  428.  
  429.         va_end( va_alist );
  430.  
  431.         m_pMenu->AddItem( idx, FormattedBuffer );
  432. }
  433.  
  434. void RiotThread::SetMenuItemCustom( int idx, char *pszFormat, ... )
  435. {
  436.         char FormattedBuffer[ 1024 ] = { 0 };
  437.  
  438.         va_list va_alist;
  439.  
  440.         va_start( va_alist, pszFormat );
  441.  
  442.         _vsnprintf(
  443.                 FormattedBuffer + strlen( FormattedBuffer ),
  444.                 sizeof( FormattedBuffer ) - strlen( FormattedBuffer ),
  445.                 pszFormat, va_alist );
  446.  
  447.         va_end( va_alist );
  448.  
  449.         m_pMenu->SetItem( idx, FormattedBuffer );
  450. }
  451.  
  452. void RiotThread::OnMenuSelectionChanged( IMenu *menu, u32 id )
  453. {
  454.         // None?
  455. }
  456.  
  457. void RiotThread::OnMenuSelected( IMenu *menu, u32 id )
  458. {
  459.         switch( id )
  460.         {
  461.         case 0:
  462.                 {
  463.                         m_bEnabled = !m_bEnabled;
  464.  
  465.                         SetMenuItemCustom( 0, m_bEnabled ? "Disable Riot Mode" : "Enable Riot Mode" );
  466.  
  467.                         break;
  468.                 }
  469.         case 1:
  470.                 {
  471.                         m_bTargetPlayer = !m_bTargetPlayer;
  472.  
  473.                         SetMenuItemCustom( 1, m_bTargetPlayer ? "Disable peds targetting player" : "Enable peds targetting player" );
  474.  
  475.                         break;
  476.                 }
  477.         case 2:
  478.                 {
  479.                         m_bCopsGoCrazy = !m_bCopsGoCrazy;
  480.  
  481.                         SetMenuItemCustom( 2, m_bCopsGoCrazy ? "Disable cops rioting" : "Enable cops rioting" );
  482.  
  483.                         break;
  484.                 }
  485.         case 3:
  486.                 {
  487.                         m_bPedsUseCars = !m_bPedsUseCars;
  488.  
  489.                         SetMenuItemCustom( 3, m_bPedsUseCars ? "Disable peds use cars in combat" : "Enable peds use cars in combat" );
  490.  
  491.                         break;
  492.                 }
  493.         case 4:
  494.                 {
  495.                         m_bPedsUseMelee = !m_bPedsUseMelee;
  496.  
  497.                         SetMenuItemCustom( 4, m_bPedsUseMelee ? "Disable peds use melee weapons" : "Enable peds use melee weapons" );
  498.  
  499.                         break;
  500.                 }
  501.         case 5:
  502.                 {
  503.                         m_bUseDecisionMaker = !m_bUseDecisionMaker;
  504.  
  505.                         SetMenuItemCustom( 5, m_bUseDecisionMaker ? "Disable decision maker" : "Enable decision maker" );
  506.  
  507.                         break;
  508.                 }
  509.         case 6:
  510.                 {
  511.                         m_bPedsWanted = !m_bPedsWanted;
  512.  
  513.                         SetMenuItemCustom( 6, m_bPedsWanted ? "Disable peds wanted" : "Enable peds wanted" );
  514.  
  515.                         break;
  516.                 }
  517.         case 7:
  518.                 {
  519.                         m_bMadDrivers = !m_bMadDrivers;
  520.  
  521.                         SetMenuItemCustom( 7, m_bMadDrivers ? "Disable mad drivers" : "Enable mad drives" );
  522.  
  523.                         break;
  524.                 }
  525.         case 8:
  526.                 {
  527.                         m_bEmergancyServicesDisabled = !m_bEmergancyServicesDisabled;
  528.  
  529.                         SetMenuItemCustom( 8, m_bEmergancyServicesDisabled ? "Enable emergancy services" : "Disable emergancy services" );
  530.  
  531.                         break;
  532.                 }
  533.         }
  534. }
  535.  
  536. void RiotThread::OnKeyboardHookEvent( const IKeyboardHookHandler::KeyEventArgs &args )
  537. {
  538.         if( args.VirtualKey == VK_INSERT && !args.WasKeyDownBefore )
  539.         {
  540.                 if( m_pMenu )
  541.                 {
  542.                         m_pMenu->Show();
  543.                 }
  544.         }
  545. }