Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CppProto.h"
- #include "SBMain.h"
- #include "ObjMachine.h"
- #include "ObjBotNet.h"
- #define DCOM_WALLRAY 1
- #define DCOM_EMSCANNER 2
- struct SGDroneCom
- {
- UINT32 ID;
- FLOAT Health;
- mVec2 Off;
- FLOAT Size;
- FLOAT Sens;//-itivity
- };
- typedef TArray< SGDroneCom > DroneComVec;
- /*
- Component damage:
- = Sens / MAX( EPSILON, len( World(Off) - DmgPoint ) - Size ) * len( Velocity ) * MC
- - MC: magic constant (0.01?)
- */
- SGDroneCom DCP_WallRay = { DCOM_WALLRAY, 10.0f, mVec2( 0, 12.0f ), 2.0f, 4.0f };
- SGDroneCom DCP_EMScanner = { DCOM_EMSCANNER, 4.0f, mVec2( 0, 0 ), 5.0f, 10.0f };
- struct SGDroneSB : SGMachine
- {
- SGDroneSB( PVarVec& data, SGMap* map ) : SGMachine( data, map ), SpawnPos( data[ 1 ].Vec2() )
- {
- Radius = 12;
- ShowLaser = FALSE;
- Mtl.LoadMaterial( "Materials/Chars/Drone.mtx" );
- CurAng = PrevAng = gAtan2( data[ 2 ].Vec2().y, data[ 2 ].Vec2().x );
- Init();
- Map->AddLight( &Light );
- // Light.Color = mFColor( 0.7f, 0.1f, 0.05f );
- Components.PushBack( DCP_WallRay );
- Components.PushBack( DCP_EMScanner );
- }
- ~SGDroneSB()
- {
- Map->RemoveLight( &Light );
- }
- void Tick( FLOAT delta )
- {
- GTickDebugInfo += TString().Printf( 32, "\nDRONE [%p]\n", this );
- SGMachine::Tick( delta );
- if( !Dead )
- {
- // Hardware
- UpdateComponents();
- // Software
- UpdateFirmware( delta );
- }
- }
- /*
- Component system
- */
- UBOOL HasComponent( INT32 id )
- {
- for( UINT32 i = 0; i < Components.Size(); ++i )
- {
- if( Components[ i ].ID == id )
- return Components[ i ].Health > 0.0f;
- }
- return FALSE;
- }
- FLOAT DC_WallRayOutput; // distance to wall
- FLOAT DC_EMSTimeout; // source-to-confirmation timeout (resets on disappearance of source)
- mVec3 DC_EMSSources[5]; // source information on first 5 visible items
- mVec2 DC_EMSConfPos; // confirmed position of enemy
- FLOAT DC_EMSConfEnergy; // confirmed energy of enemy (0 if none confirmed)
- void UpdateComponents()
- {
- // create a connection
- SGBotNet* botnet = GetBotNet( Map );
- for( UINT32 i = 0; i < Components.Size(); ++i )
- {
- if( Components[ i ].Health <= 0.0f )
- continue;
- switch( Components[ i ].ID )
- {
- case DCOM_WALLRAY:
- DC_WallRayOutput = Map->RaycastPhysics2( CurPos, CurPos + mVec2( gCos( CurAng ), gSin( CurAng ) ) * 50, MC_RAYCAST );
- GTickDebugInfo += TString().Printf( 256, "WALLRAY: %f\n", DC_WallRayOutput );
- break;
- case DCOM_EMSCANNER:
- {
- UINT32 count = 0, which = 0;
- count = botnet->GetEMFSources( DC_EMSSources, 5, CurPos, mVec2( gCos( CurAng ), gSin( CurAng ) ), 0.5f, 0.1f );
- if( !count )
- DC_EMSConfEnergy = 0;
- else
- {
- which = botnet->CheckEMFSources( DC_EMSSources, count, CurPos );
- if( which == count )
- DC_EMSConfEnergy = 0;
- else
- {
- DC_EMSConfPos = mVec2( DC_EMSSources[ which ] );
- DC_EMSConfEnergy = DC_EMSSources[ which ].z;
- }
- }
- GTickDebugInfo += TString().Printf( 256, "EMSCANNER: %d sources, #%d hostile\n", count, which == count ? -1 : which + 1 );
- }
- break;
- }
- }
- }
- /*
- End of Component system
- */
- PVar FWData;
- void UpdateFirmware( FLOAT delta );
- DroneComVec Components;
- mVec2 SpawnPos;
- };
- ENT_BEGIN( SGDroneSB )
- PVAR_INHERIT( SGEntity )
- PVAR_VEC2_I( "Position", mVec2( 0.0f ) )
- PVAR_VEC2_I( "Direction", mVec2( 1, 0 ) )
- ENT_END( "NPC_Drone", SGDroneSB );
- #define DACT_PATROL 1
- #define DACT_VERIFY 2
- #define DACT_ATTACK 3
- void SGDroneSB::UpdateFirmware( FLOAT delta )
- {
- // PROTOTYPE CODE
- // initialize
- if( !FWData.Get( "ready" ).GetInt() )
- {
- FWData.Get( "action" ).SetInt( DACT_PATROL );
- FWData.Get( "movedir" ).SetVec2( mVec2( 1, 0 ) );
- FWData.Get( "turndir" ).SetInt( 1 );
- FWData.Get( "turntimeout" ).SetReal( 5 + M_RandF() * 5 );
- FWData.Get( "verifytimeout" ).SetReal( 0 );
- FWData.Get( "attacktime" ).SetReal( 0 );
- FWData.Get( "ready" ).SetInt( 1 );
- }
- INT32 action = FWData.Get( "action" ).GetInt();
- mVec2 movedir = FWData.Get( "movedir" ).GetVec2();
- INT32 turndir = FWData.Get( "turndir" ).GetInt();
- FLOAT turntimeout = FWData.Get( "turntimeout" ).GetReal();
- FLOAT verifytimeout = FWData.Get( "verifytimeout" ).GetReal();
- FLOAT attacktime = FWData.Get( "attacktime" ).GetReal();
- mVec2 target = FWData.Get( "target" ).GetVec2();
- movedir.Normalize();
- turntimeout -= delta;
- if( turntimeout <= 0 )
- {
- turndir = !turndir;
- turntimeout = M_RandF() * 13 + 8;
- }
- switch( action )
- {
- case DACT_PATROL:
- Light.Color = mFColor( 0.9f, 0.8f, 0.7f );
- if( DC_WallRayOutput < 2 )
- {
- IMoveX = 0;
- IMoveY = 0;
- movedir = movedir.Rotated( delta * ( turndir * 2 - 1 ) * 3 );
- }
- else
- {
- FLOAT spd = 0.2f;
- IMoveX = movedir.x * spd;
- IMoveY = movedir.y * spd;
- }
- if( DC_EMSConfEnergy > 0 )
- {
- // target found!
- target = DC_EMSConfPos;
- movedir = ( target - CurPos ).Normalized();
- verifytimeout = 0.83f;
- action = DACT_VERIFY;
- }
- RotateTo( movedir );
- break;
- case DACT_VERIFY:
- IMoveX = 0;
- IMoveY = 0;
- verifytimeout -= delta;
- if( verifytimeout <= 0 )
- {
- if( DC_EMSConfEnergy > 0 )
- {
- // target confirmed!
- target = DC_EMSConfPos;
- action = DACT_ATTACK;
- attacktime = 0;
- }
- else
- action = DACT_PATROL;
- }
- RotateTo( movedir, 0.1f, 1 );
- break;
- case DACT_ATTACK:
- Light.Color = mFColor( 0.7f, 0.1f, 0.05f );
- if( DC_EMSConfEnergy > 0 && ( CurPos - target ).Length() < 300 && gMod( attacktime, 1.2f ) < 0.6f )
- {
- target = DC_EMSConfPos;
- movedir = target - CurPos;
- FireTurret();
- }
- else if( ( CurPos - target ).Length() > 64 )
- {
- FLOAT spd = 0.4f;
- IMoveX = target.x > CurPos.x ? spd : -spd;
- IMoveY = target.y > CurPos.y ? spd : -spd;
- movedir = target - CurPos;
- verifytimeout = 4 + M_RandF();
- }
- else
- {
- IMoveX = 0;
- IMoveY = 0;
- movedir = movedir.Rotated( delta * 2 );
- verifytimeout -= delta;
- if( verifytimeout <= 0 )
- {
- // target lost!
- action = DACT_PATROL;
- }
- }
- RotateTo( movedir );
- attacktime += delta;
- break;
- }
- ShowLaser = action != DACT_PATROL;
- FWData.Get( "action" ).SetInt( action );
- FWData.Get( "movedir" ).SetVec2( movedir );
- FWData.Get( "turndir" ).SetInt( turndir );
- FWData.Get( "turntimeout" ).SetReal( turntimeout );
- FWData.Get( "verifytimeout" ).SetReal( verifytimeout );
- FWData.Get( "attacktime" ).SetReal( attacktime );
- FWData.Get( "target" ).SetVec2( target );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement