// Bomb object script
//  by Pteromys of Melon Island... http://melonisland.net
//  second object I've edited
//  based on a cross between the bear trap and the fireball wand
// Usage: Tap attack button once quickly to set a bomb
//        Or, hold down key for a while, then release to throw
// Guide to object states:
//   0  Lit and just set down
//   1  Lit and to be thrown
//   2  Idle or held
//   3  Held and waiting to be thrown
//  4,5 Land mine!
// Content variable usage:
//  Records how long the button has been held down to see if the
//  attack should be considered a throw.

IfSpawned
  JoinNullTeam
  ReaffirmCharacter
  IfStateIs2				// If not lit, I shouldn't have a little sparkie
    DisaffirmCharacter
  Else
    IfStateIs1				// Thrown
      tmpx = 0
      tmpy = 0
      tmpturn = ownerturn
      tmpdistance = targetstr > 7
      tmpdistance = tmpdistance * 20
      tmpdistance = rand % 20 + tmpdistance    //Strength increases range
      Compass
      SetTargetToSelf
      AccelerateTarget
      tmpargument = rand & 511 + 1200
      AccelerateUp
      tmpargument = 1
      PlaySound
    Else				// Possibility of land mine
      tmpargument = [TRAP]
      tmpdistance = BLAHITEMS
      SetTargetToNearestBlahID
        tmpargument = [BEAR]
        IfTargetHasID
          tmpx = targetdistance
          tmpy = 45
          IfXIsLessThanY
            tmpx = targetx
            tmpy = targety
            Teleport
            SetTargetToOwner
            tmpargument = 7
            SendMessageNear
            tmpargument = 4
            SetState
  MakeCrushValid
  MakeAmmoKnown
  IfStateIs4				// If land mine
    tmpargument = ACTIONMK		// Change shape
    DoActionOverride
    KeepAction
    DisaffirmCharacter
    tmpargument = 100			// Delay for land mine to activate
    SetTime
  Else					// or not...
    tmpargument = ACTIONJB
    DoAction
    KeepAction
    // Set the fuse time
    tmpargument = 210
    SetTime

IfReaffirmed				// If we lit this item...
  IfHeld
    DisaffirmCharacter
  Else
    IfStateIs2
      tmpargument = 210			// fuse time
      SetTime
      tmpargument = 0
      SetState
    GetState
    tmpx = tmpargument
    tmpy = 3
    IfXIsMoreThanY
      DisaffirmCharacter
IfDisaffirmed				// Go out if hits water when it's lit
  GetState
  tmpx = tmpargument
  tmpy = 2
  IfXIsLessThanY
    tmpargument = 2
    SetState

IfHitGround
  IfStateIs1
    SetTargetToSelf
    tmpargument = 1024
    DamageTarget

IfTimeOut				// When the fuse goes, the bomb blows
  tmpx = selfstate
  tmpy = 2
  IfXIsLessThanY
    SetTargetToSelf			// Call explosion by dropping life below threshold
    tmpargument = 1024
    DamageTarget
  tmpy = 3
  IfXIsMoreThanY			// Somebody stepped on a land mine!
    SetTargetToNearestLifeform
      SetTargetToLowestTarget
      IfTargetIsFlying
        DoNothing
      Else
        // Does it have legs?
        tmpargument = [XLEG]
        IfTargetHasAnyID
          DoNothing
        Else
          tmpx = targetdistance
          tmpy = 35
          IfXIsLessThanY
            tmpx = selfz + 10
            tmpy = targetz
            IfXIsMoreThanY
              tmpargument = 8
              SendMessageNear
              tmpargument = 5
              SetState
              SetTargetToSelf
              tmpargument = 1024
              DamageTarget
    tmpargument = 6
    SetTime

IfCrushed				// Surprise when you leave bombs in doorways!
  tmpargument = 2
  SendMessageNear
  tmpx = selfx
  tmpy = selfy
  tmpdistance = selfz
  tmpargument = 0
  SpawnExactParticle
  GoPoof

// Don't attack bombs... it's not safe!
tmpx = selflife
tmpy = 25560
IfXIsLessThanY
  IfHeld				// Don't die
    tmpargument = 25599 - selflife
    HealSelf
  Else
    IfStateIs4				// Not as a land mine...
      tmpargument = 25599 - selflife
      HealSelf
      tmpargument = 5
      SetState
    Else
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 0
      SpawnExactParticle
      GoPoof

IfUsed
  SetTargetToWhoeverIsHolding
  tmpargument = [TECH]
  IfTargetHasSkillID
    GetContent
    tmpargument = tmpargument + 9
    tmpx = 200				// max charge
    tmpy = tmpargument
    IfXIsLessThanY
      tmpargument = 200			// Charge cannot go above max
    SetContent
    IfStateIs2
      tmpargument = 3
      SetState
  Else
    IfTimeOut
      tmpargument = 4			//Cant use this..
      SendMessageNear
      tmpargument = 50
      SetTime
Else
  IfStateIs3
    tmpargument = ACTIONMG
    TargetDoAction
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturn
      tmpdistance = 30
      Compass
      tmpdistance = targetz + 15
      tmpturn = rand
      SpawnCharacterXYZ
        tmpargument = 1			// Make the new one have 1 ammo
        SetChildAmmo
        CostAmmo			// Down the ammo
        IfAmmoOut			// If I'm out of ammo, I shouldn't have a bomb in my hand
          GoPoof
        tmpargument = 200
        IfContentIs			// If you've charged up a throw
          tmpargument = 1
          SetChildState
        Else
          tmpargument = 0
          SetChildState
      Else
        tmpargument = 6
        SendMessageNear
      tmpargument = 50
      SetReloadTime
      tmpargument = 2			// Become just held again
      SetState
      tmpargument = 0			// Reset charge
      SetContent

IfTakenOut
  SetTargetToWhoeverIsHolding
  SetOwnerToTarget
  IfTargetIsAPlayer
    tmpargument = 3
    SendMessageNear
IfDropped				// Make it lie on floor
  tmpargument = 2
  SetState
  KeepAction
IfGrabbed
  SetTargetToWhoeverIsHolding
  SetOwnerToTarget
  GetState
  tmpdistance = 0
  tmpturn = 0
  tmpx = tmpargument
  tmpy = 2
  IfXIsLessThanY
    tmpdistance = 1
  tmpy = 3
  IfXIsMoreThanY
    tmpdistance = 1
  IfDistanceIsMoreThanTurn
    // Grabbing a lit bomb or a land mine
    tmpx = 0
    tmpy = 1
    tmpargument = [TECH]
    IfTargetHasSkillID
      tmpx = 1
    tmpargument = [DISA]
    IfTargetHasSkillID
      tmpx = 1
    IfXIsEqualToY			// If expert in either technology or disarming traps...
      tmpargument = 1
      SendMessageNear
      tmpargument = 2			// ...defuse the bomb.
      SetState
      DisaffirmCharacter
    Else				// If not, inadvertently detonate
      IfTargetIsAPlayer
        tmpargument = 5
        SendMessageNear
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 0
      SpawnExactParticle
      GoPoof
  Else
    IfTargetIsAPlayer
      tmpargument = 0
      SendMessageNear
    // Idle state
    tmpargument = 2
    SetState

End					// Finished
