//See http://zdoom.org/wiki/Classes:RandomSpawner if you want to define spawn chance
Actor BerryRandomizer: RandomSpawner {
    DropItem "BlueberryPlant"
    DropItem "StrawberryPlant"
    DropItem "CranberryPlant"
}

//Make your plant inherit from this.
//arg[0] used as boolean : harvested
Actor BerryPlant : SwitchingDecoration {
   +FLOORCLIP
   +USESPECIAL
   +SHOOTABLE
   +NOBLOOD

   Radius 8
   Height 38
   Health 50
   Mass 0x7FFFFFFF
   Alpha 0.0                        //Enable use of A_FadeOut/A_FadeIn
   Activation THINGSPEC_Activate

   States {
      MaybeDie:                     //Activated by script
         TNT1 A 0 A_Jump(12,"Death")      //5% chance of it dying
         TNT1 A 0 A_Jump(256,"Idle")
         Stop
   }
}

//Make your berry inherit from this.
Actor berryItem : CustomInventory {
   +SHOOTABLE
   +NOBLOOD
   +CANPASS
   +WINDTHRUST

   +INVENTORY.INVBAR
   Inventory.MaxAmount 20
   Health 30
}

// ------------- //
// --- ITEMS --- //
// ------------- //

ACTOR BlueberryItem : berryItem {
   Tag "Blueberries"
   Inventory.Icon "ZSEIA"
   Inventory.PickupMessage "Picked up a handful of ripe blueberries."
   Scale 0.5

   States {
      Spawn:
         ZSEP A 0
         ZSEP A 0 ACS_ExecuteAlways(953,0,0)         //Set our TID to FOOD_TID so we can spoil
         ZSEP A 10500                        //Disappear after 5 minutes
         Stop
      Use:
         TNT1 A 0 A_GiveInventory("Health",3)
         TNT1 A 0 A_TakeInventory("HungerLevel",2)
         TNT1 A 0 A_PlaySound("eat/crunch")
         TNT1 A 0 ACS_ExecuteAlways(850,0,0)         //Green screen flash - PLAYER.ACS
         Stop
   }
}

ACTOR StrawberryItem : berryItem {
   Tag "Strawberries"
   Inventory.Icon "ZSEIB"
   Inventory.PickupMessage "Picked up a handful of red strawberries."

   States {
      Spawn:
         ZSEP B 0
         ZSEP B 0 ACS_ExecuteAlways(953,0,0)         //Set our TID to FOOD_TID so we can spoil
         ZSEP B 10500                        //Disappear after 5 minutes
         Stop
      Use:
         TNT1 A 0 A_GiveInventory("Health",4)
         TNT1 A 0 A_TakeInventory("HungerLevel",4)
         TNT1 A 0 A_PlaySound("eat/crunch")
         TNT1 A 0 ACS_ExecuteAlways(850,0,0)         //Green screen flash - PLAYER.ACS
         Stop
   }
}

ACTOR CranberryItem : berryItem {
   Tag "Cranberries"
   Inventory.Icon "ZSEIC"
   Inventory.PickupMessage "Picked up a handful of cranberries."

   States {
      Spawn:
         ZSEP C 0
         ZSEP C 0 ACS_ExecuteAlways(953,0,0)         //Set our TID to FOOD_TID so we can spoil
         ZSEP C 10500                        //Disappear after 5 minutes
         Stop
      Use:
         TNT1 A 0 A_GiveInventory("Health",5)
         TNT1 A 0 A_TakeInventory("HungerLevel",5)
         TNT1 A 0 A_PlaySound("eat/crunch")
         TNT1 A 0 ACS_ExecuteAlways(850,0,0)         //Green screen flash - PLAYER.ACS
         Stop
   }
}

// -------------- //
// --- PLANTS --- //
// -------------- //

ACTOR BlueberryPlant : BerryPlant {
   States {
      Spawn:
         TNT1 A 0
         BPLN AAAAAAAAAA 1 A_FadeIn(0.1)
         BPLN A 0 ACS_ExecuteAlways(963,0,0)      //Set our TID to BERRY_PLANT_TID
      Idle:
         BPLN A 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN A -1
         Wait
      Harvested:
        TNT1 A 0
        goto Death
      Active:
         BPLN A 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN A 0 A_SetArg(0,1)
      Harvest:
         BPLN A 1 A_SpawnItemEx("BlueberryItem",random(-2,2),random(-2,2),random(5,25),random(-4,4),random(-4,4),random(5,12))
         BPLN A 0 A_Jump(96,"Harvested")
         Loop
      Death:
         BPLN B 0 Thing_ChangeTID(0,0)         //Clear our TID to avoid an another use of "MaybeDie" from script
         BPLN B 25 A_NoBlocking
         BPLN BBBBBBBBBB 1 A_FadeOut(0.1,false)
         Stop
   }
}

ACTOR StrawberryPlant : BerryPlant {
   States {
      Spawn:
         TNT1 A 0
         BPLN CCCCCCCCCC 1 A_FadeIn(0.1)
         BPLN C 0 ACS_ExecuteAlways(963,0,0)      //Set our TID to BERRY_PLANT_TID
      Idle:
         BPLN C 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN C -1
         Wait
     Harvested:
       TNT1 A 0
       goto Death
      Active:
         BPLN C 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN C 0 A_SetArg(0,1)
      Harvest:
         BPLN C 1 A_SpawnItemEx("StrawberryItem",random(-2,2),random(-2,2),random(5,25),random(-4,4),random(-4,4),random(5,12))
         BPLN C 0 A_Jump(96,"Harvested")
         Loop
      Death:
         BPLN D 0 Thing_ChangeTID(0,0)         //Clear our TID to avoid an another use of "MaybeDie" from script
         BPLN D 25 A_NoBlocking
         BPLN DDDDDDDDDD 1 A_FadeOut(0.1,false)
         Stop
   }
}

ACTOR CranberryPlant : BerryPlant {
   States {
      Spawn:
         TNT1 A 0
         BPLN EEEEEEEEEE 1 A_FadeIn(0.1)
         BPLN E 0 ACS_ExecuteAlways(963,0,0)      //Set our TID to BERRY_PLANT_TID
      Idle:
         BPLN E 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN E -1
         Wait
     Harvested:
       TNT1 A 0
       goto Death
      Active:
         BPLN E 0 A_JumpIf(args[0]==1,"Harvested")
         BPLN E 0 A_SetArg(0,1)
      Harvest:
         BPLN E 1 A_SpawnItemEx("CranberryItem",random(-2,2),random(-2,2),random(5,25),random(-4,4),random(-4,4),random(5,12))
         BPLN E 0 A_Jump(96,"Harvested")
         Loop
      Death:
         BPLN F 0 Thing_ChangeTID(0,0)         //Clear our TID to avoid an another use of "MaybeDie" from script
         BPLN F 25 A_NoBlocking
         BPLN FFFFFFFFFF 1 A_FadeOut(0.1,false)
         Stop
   }
}
