Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.99 KB | None | 0 0
  1. #include "BlockCommon.as"
  2. #include "IslandsCommon.as"
  3.  
  4. const f32 BULLET_SPEED = 12.0f;
  5. const f32 BULLET_SPREAD = 1.0f;
  6. const f32 BULLET_LIFETIME = 0.75f;
  7. const f32 MIN_FIRE_PAUSE = 3.2f;//min wait between shots
  8. const f32 MAX_FIRE_PAUSE = 8.0f;//max wait between shots
  9. //todo: should be done with 1 factor value
  10. const f32 FIRE_PAUSE_DECREASE_FACTOR = 0.85f;//how fast it recuperates
  11. const f32 FIRE_PAUSE_INCREASE_FACTOR = 1.09f;//how quick it slows down
  12.  
  13. Random _shotspreadrandom(0x11598); //clientside
  14.  
  15. void onInit( CBlob@ this )
  16. {
  17.     this.getCurrentScript().tickFrequency = 10;
  18.  
  19.     this.Tag("turret");
  20.     this.Tag("fixed_gun");
  21.     this.addCommandID("fire");
  22.     this.addCommandID("disable");
  23.     this.set_string("barrel", "left");
  24.     if ( getNet().isServer() )
  25.     {
  26.         this.set_f32("fire pause",MIN_FIRE_PAUSE);
  27.         this.Sync("fire pause", true );
  28.     }
  29.    
  30.     CSprite@ sprite = this.getSprite();
  31.     CSpriteLayer@ layer = sprite.addSpriteLayer( "turret", 16, 16 );
  32.     if (layer !is null)
  33.     {
  34.         layer.SetRelativeZ(2);
  35.         layer.SetLighting( false );
  36.         Animation@ anim = layer.addAnimation( "fire left", MIN_FIRE_PAUSE/2.0f, false );
  37.         anim.AddFrame(Block::AUTOCANNON_F2);
  38.         anim.AddFrame(Block::AUTOCANNON_F1);
  39.                
  40.         Animation@ anim2 = layer.addAnimation( "fire right", MIN_FIRE_PAUSE/2.0f, false );
  41.         anim2.AddFrame(Block::AUTOCANNON_F3);
  42.         anim2.AddFrame(Block::AUTOCANNON_F1);
  43.                
  44.         Animation@ anim3 = layer.addAnimation( "default", 1.0f, false );
  45.         anim3.AddFrame(Block::AUTOCANNON_F1);
  46.         layer.SetAnimation("default");  
  47.     }
  48.  
  49.         this.set_u32("fire time", 0);
  50. }
  51.  
  52. void onTick( CBlob@ this )
  53. {      
  54.     if (this.getShape().getVars().customData <= 0)//not placed yet
  55.         return;
  56.        
  57.     f32 currentFirePause = this.get_f32("fire pause");
  58.     if (currentFirePause > MIN_FIRE_PAUSE)
  59.         this.set_f32("fire pause", currentFirePause * FIRE_PAUSE_DECREASE_FACTOR);
  60.        
  61.     //print( "Fire pause: " + currentFirePause );
  62. }
  63.  
  64. bool canShoot( CBlob@ this )
  65. {
  66.     return ( this.get_u32("fire time") + this.get_f32("fire pause") < getGameTime() ) && getRules().get_u16( "projectiles" ) < 6;
  67. }
  68.  
  69.  
  70. void onCommand( CBlob@ this, u8 cmd, CBitStream @params )
  71. {
  72.     if (cmd == this.getCommandID("fire"))
  73.     {
  74.         if (canShoot(this))    
  75.         {
  76.             u8 teamNum = params.read_u8();
  77.             CSprite@ sprite = this.getSprite();
  78.             CSpriteLayer@ layer = sprite.getSpriteLayer( "turret" );
  79.             layer.SetAnimation( "default" );
  80.            
  81.             if (this.get_string("barrel") == "left")
  82.                     layer.SetAnimation( "fire left" );
  83.             if (this.get_string("barrel") == "right")
  84.                     layer.SetAnimation( "fire right" );
  85.  
  86.             Vec2f offset(_shotspreadrandom.NextFloat() * BULLET_SPREAD,0);
  87.             offset.RotateBy(_shotspreadrandom.NextFloat() * 360.0f, Vec2f());
  88.            
  89.             Vec2f aimvector = Vec2f(1, 0).RotateBy(this.getAngleDegrees());
  90.             Vec2f velocity = (aimvector * BULLET_SPEED) + offset;
  91.             f32 time = BULLET_LIFETIME;
  92.             Vec2f pos = this.getPosition() + aimvector*9;
  93.            
  94.             this.set_u32("fire time", getGameTime());
  95.            
  96.             Vec2f barrelOffset = Vec2f(0, 0);
  97.             if (this.get_string("barrel") == "left")
  98.             {
  99.                     barrelOffset = Vec2f(0, -3.0).RotateBy(-velocity.Angle());
  100.                     this.set_string("barrel", "right");
  101.             }
  102.             else
  103.             {
  104.                     barrelOffset = Vec2f(0, 3.0).RotateBy(-velocity.Angle());
  105.                     this.set_string("barrel", "left");
  106.             }
  107.             pos = pos + barrelOffset;
  108.             shotParticles(pos, velocity.Angle(), true );
  109.  
  110.             if (getNet().isServer() && 1 == 2)
  111.             {
  112.                 CBlob@ bullet = server_CreateBlob( "bullet", teamNum, pos );
  113.                 if (bullet !is null)
  114.                 {
  115.                     bullet.setVelocity( velocity + ((getIsland(this) !is null) ? getIsland( this ).vel : Vec2f(0, 0)) );
  116.                     bullet.server_SetTimeToDie( time );
  117.                 }
  118.             }
  119.            
  120.             if( 1 == 1 )
  121.             {
  122.                 HitInfo@[] hitInfos;
  123.                 CMap@ map = this.getMap();
  124.                 if( map.getHitInfosFromRay( pos, -aimvector.Angle(), 180.0f, this, @hitInfos ) )
  125.                 {
  126.                     for (uint i = 0; i < hitInfos.length; i++)
  127.                     {
  128.                         HitInfo@ hi = hitInfos[i];
  129.                         CBlob@ b = hi.blob;  
  130.                         if(b is null || b is this) continue;
  131.  
  132.                         const int color = b.getShape().getVars().customData;
  133.                         const int blockType = b.getSprite().getFrame();
  134.                         const bool isBlock = b.getName() == "block";
  135.  
  136.                         bool killed = false;
  137.                         if ( !b.hasTag( "booty" ) &&  (color > 0 || !isBlock) )
  138.                         {
  139.                             if (isBlock )
  140.                             {
  141.                                 if ( ( ( Block::isCore(blockType) || b.hasTag("turret") || blockType == Block::BOMB || blockType == Block::SEAT ) && b.getTeamNum() != this.getTeamNum() ) || Block::isSolid(blockType) )//hit these and die
  142.                                     killed = true;
  143.                                 else
  144.                                     continue;
  145.                             }
  146.                             else
  147.                             {
  148.                                 if ( b.getTeamNum() == this.getTeamNum() || ( b.hasTag("player") && b.isAttached() ) )
  149.                                     continue;
  150.                             }
  151.                            
  152.                             shotParticles( hi.hitpos, -aimvector.Angle(), false );
  153.  
  154.                             this.server_Hit( b, pos,
  155.                                              Vec2f_zero, 0.1f,
  156.                                              0, true);                 
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.            
  162.             if (XORRandom(1) == 0)
  163.                 sprite.PlaySound("Gunshot2.ogg");
  164.             else
  165.                 sprite.PlaySound("Gunshot3.ogg");
  166.            
  167.             f32 currentFirePause = this.get_f32("fire pause");
  168.             if (currentFirePause < MAX_FIRE_PAUSE)
  169.                 this.set_f32("fire pause", Maths::Pow(currentFirePause, FIRE_PAUSE_INCREASE_FACTOR) );
  170.         }                  
  171.     }
  172. }
  173.  
  174. Random _shotrandom(0x15125); //clientside
  175.  
  176. void shotParticles(Vec2f pos, float angle, bool muzzle )
  177. {
  178.     //muzzle flash
  179.     {
  180.         string particle = "Entities/Block/turret_muzzle_flash.png";
  181.        
  182.         if( !muzzle )
  183.         {
  184.             particle = "Entities/Block/turret_smoke.png";
  185.         }
  186.             CParticle@ p = ParticleAnimated( particle,
  187.                                                                                               pos, Vec2f(),
  188.                                                                                               -angle, //angle
  189.                                                                                               1.0f, //scale
  190.                                                                                               3, //animtime
  191.                                                                                               0.0f, //gravity
  192.                                                                                               true ); //selflit
  193.             if(p !is null)
  194.                     p.Z = 10.0f;
  195.     }
  196.  
  197.     Vec2f shot_vel = Vec2f(0.5f,0);
  198.     shot_vel.RotateBy(-angle);    
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement