/* This was originally an alternate version of ChoppableTree that called a script
   when it dies in order to respawn itself. The script doesn't work
   in Zandronum 1.3 though, but I'm keeping the code around in case it works
   in Zandronum 2.0. The reason this approach is preferred is so that:
     A) We won't be affected by non-solid actors being buggy/jittery in multiplayer.
	 B) Trees won't be respawned if they're blocked (eg. by buildings)

   Because we can't make this approach work (and work around the Zandronum bugs),
   I've added TreeSeeds to the game. 
*/
Actor ChoppableTree
{
  //$Sprite NTREA0
  +SHOOTABLE
  +NOBLOOD
  +NOTARGET
  +FRIENDLY
  +CANTSEEK
  +STANDSTILL
  //+ACTLIKEBRIDGE //Added in r14 so that enemies don't get stuck on top of trees
  +SOLID
   Monster
  -COUNTKILL
  -PUSHABLE
  -WINDTHRUST
  -CANBLAST

  +ISMONSTER //So master/child works with fire
  Mass 0x7FFFFFFF
  //Health 0x7FFFFFFF
  Health 200
  PainChance 255
  
  //PainChance "Axe", 255 //100% chance of going into Pain state
  //PainChance "Fire", 255 //100% chance of going into Pain state
  //DamageFactor "Axe", 1.0
  //DamageFactor "Fire", 1.0
  //DamageFactor "Fire" 0.1
  
  States
  {
	  Spawn:
	 	TNT1 A 1
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		TNT1 A 0 A_Look
	  SpawnStill:
		NTRE A -1
		Stop
	 //These pain states have to be in this order! And there must be matching Death states!
	  /*
	  Pain:
		TRNP A 35 A_Pain
		goto Spawn
	    //goto Pain.Fire
	  */
	  Pain.Axe:
		TNT1 A 0 //Dummy frame so A_Jump works
		TNT1 A 0 A_GiveToTarget("TreeChopXP", 1)
		TNT1 A 0 A_SpawnItemEx("LogItem", 0, 0, 64.0, random(-4.0,4.0), random(-4.0,4.0), 8.0)
	  Pain.Water:
	    TNT1 A 0 A_KillChildren //Put out any fires
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 1 //Dummy frame if we get this far
		//TNT1 A 18 //Half a second wait to prevent server desync
		//TNT1 A -1
		goto Spawn //Weird hack to make the trees not go invisible in multiplayer, Zandro bug?
	   Pain.Fire:
		TNT1 A 0
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		goto SpawnStill
	  //These death states have to be in this order!
	  /*Death:
		TNT1 A 0
	    Stop
	  */
	  Death.Axe:
		TNT1 A 0
		TNT1 A 0 A_SpawnItemEx("LogItem", 0.0, 0.0, 32.0, 0, 0, 8.0)
		TNT1 A 0 A_SpawnItemEx("TreeSeeds", 0.0, 0.0, 64.0, 3.0, 0.0, 8.0, random(0, 255), 0, 128)
		TNT1 A 1 A_SpawnItemEx("TreeSeeds", 0.0, 0.0, 64.0, 2.0, 0.0, 8.0, random(0, 255), 0, 128)
		TNT1 A 1 A_SpawnItemEx("TreeSeeds", 0.0, 0.0, 64.0, 1.0, 0.0, 8.0, random(0, 255), 0, 128)
		TNT1 A 1 A_SpawnItemEx("TreeSeeds", 0.0, 0.0, 64.0, 1.0, 0.0, 8.0, random(0, 255), 0, 128)
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_RemoveChildren(true)
		TNT1 A 0 A_PlaySound("treebreak")
		//TNT1 A 35 ACS_ExecuteAlways(947, 0, 0) //Call a script that uses APROP_NameTag to spawn another tree after a delay
	    Stop
	  Death.Fire:
		TNT1 A 0
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_RemoveChildren(true)
		TNT1 A 0 A_PlaySound("treebreak")
		//TNT1 A 0 ACS_ExecuteAlways(947, 0, 0) //Call a script that uses APROP_NameTag to spawn another tree after a delay
		Stop
  }
}

/* 
Old tree class that becomes invisible/non-solid once it "dies", and "respawns"
a few minutes later. Deprecated in r14.

Actor ChoppableTree
{
  +SHOOTABLE
  +NOBLOOD
  +NOTARGET
  +FRIENDLY
  +CANTSEEK
  +STANDSTILL
  +SOLID
   Monster
  -PUSHABLE
  -WINDTHRUST
  -CANBLAST

  +ISMONSTER //So master/child works with fire
  Mass 0x7FFFFFFF
  //Health 0x7FFFFFFF
  Health 20000
  PainChance 255
  
  //PainChance "Axe", 255 //100% chance of going into Pain state
  //PainChance "Fire", 255 //100% chance of going into Pain state
  //DamageFactor "Axe", 1.0
  //DamageFactor "Fire", 1.0
  //DamageFactor "Fire" 0.1
  
  States
  {
	  Spawn:
	 	TNT1 A 1
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
	  SpawnStill:
		NTRE A -1
		Stop
	  HealAndSpawn: //Part of regrowing. This state makes inheritance easier.
		TNT1 A 1
		//TNT1 A 1 A_TakeInventory("OnFire", 0x7FFFFFFF)
		TNT1 A 1 ACS_ExecuteAlways(937) //Heal us. HealThing doesn't have a max in Zandronum 1.2 :(
		TNT1 A 0 A_UnHideThing //Have to call these here for some reason. They don't work right in the Spawn state in subclasses... ?
		TNT1 A 0 A_SetShootable
		TNT1 A 0 A_SetSolid
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		// Repeating these to workaround desync issues in multiplayer
	    TNT1 A 4 A_Jump(256, "Spawn")
	    TNT1 A 4 A_Jump(256, "Spawn")
	    TNT1 A 4 A_Jump(256, "Spawn")
	    TNT1 A 4 A_Jump(256, "Spawn")
	    TNT1 A 4 A_Jump(256, "Spawn")
	    TNT1 A 4 A_Jump(256, "Spawn")
		goto Spawn
	  Crumble:
	    TNT1 A 0 A_PlaySound("treebreak")
	  Grow:
		TNT1 A 0 //Dummy frame so A_Jump works
		//TNT1 A 5 A_NoBlocking
		TNT1 A 0 A_HideThing
		TNT1 A 0 A_UnSetShootable
		TNT1 A 0 A_UnsetSolid
		TNT1 A 0 A_KillChildren
		//25% chance it'll regrow every 4 minutes
		//TNT1 A 8400 - r12
		//25% chance it'll regrow every 10 minutes
		TNT1 A 21000 //- r13
		TNT1 A 1 A_Jump(64, "HealAndSpawn") //Dynamic dispatch
		Loop
	 //These pain states have to be in this order! And there must be matching Death states!
	 //
	  //Pain:
	//	TRNP A 35 A_Pain
	//	goto Spawn
	 //   //goto Pain.Fire
	  Pain.Axe:
		TNT1 A 0 //Dummy frame so A_Jump works
		TNT1 A 0 A_KillChildren //Put out any fires
		TNT1 A 0 A_SpawnItemEx("LogItem", 0, 0, 64.0, random(-4.0,4.0), random(-4.0,4.0), 8.0)
		TNT1 A 0 A_JumpIf(health < 20000-200, "Crumble") //A_JumpIfHealthLower(20000-200, "Grow") <-- does not work
		TNT1 A 0 A_JumpIf(health < 20000-200, "Crumble") //A_JumpIfHealthLower(20000-200, "Grow") <-- does not work
		TNT1 A 0 A_JumpIf(health < 20000-200, "Crumble") //A_JumpIfHealthLower(20000-200, "Grow") <-- does not work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "Spawn") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 1 //Dummy frame if we get this far
		//TNT1 A 18 //Half a second wait to prevent server desync
		//TNT1 A -1
		goto Spawn //Weird hack to make the trees not go invisible in multiplayer, Zandro bug?
	   Pain.Fire:
		TNT1 A 0
		TNT1 A 0 A_JumpIf(health < 20000-300, "Crumble")
		TNT1 A 0 A_Jump(256, "SpawnStill") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "SpawnStill") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "SpawnStill") //A_Jump is evaluated at run-time so this makes inheritance work
		TNT1 A 0 A_Jump(256, "SpawnStill") //A_Jump is evaluated at run-time so this makes inheritance work		
		goto SpawnStill

	  //These death states have to be in this order!
	  //Death:
	  // TNT1 A 0
	  // Stop
	  Death.Axe:
		TNT1 A 0
		TNT1 A 0 A_SpawnItemEx("LogItem")
		TNT1 A 0 A_KillChildren
		TNT1 A 0 //TODO: Call a script that uses APROP_NameTag to spawn another tree after a delay
	    Stop
	  Death.Fire:
		TNT1 A 0
		TNT1 A 0 A_KillChildren
		Stop
  }
}

*/

Actor SlowChoppableTree : ChoppableTree
{
  States
  {
	  GrowCheck:
	  	TNT1 A 1 //Dummy frame so A_Jump works
		//There's a 1/256 chance that this tree will respawn
		//every 10 second (35 ticks).
		TNT1 A 0 A_UnsetSolid
		TNT1 A 0 A_UnsetShootable
		//6% chance it'll regrow every 4 minutes
		//TNT1 A 8400 // r12
		//6% chance it'll regrow every 10 minutes
		TNT1 A 21000 //- r13
		TNT1 A 1 A_Jump(16, "HealAndSpawn") //Dynamic dispatch
		Loop
		/*
	    TNT1 A 350 A_Jump(1, "Spawn")
		TNT1 A 35 A_Jump(256, "GrowCheck")
		goto GrowCheck*/
  }
}


ACTOR SmallTreeHarvestable : ChoppableTree 10710
{
  //$Sprite TRENA0

  Radius 16
  Height 96
    
  States
  {
	  Spawn:
	  	TNT1 A 0 		
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		TREN A 0 A_SetSolid
		TREN A 0 A_SetShootable
	  SpawnStill:
		TREN A -1
		Stop
  }
}

ACTOR Tree6 : SmallTreeHarvestable 10711
{
    //$Sprite TREAA0

	States
	{
	  Spawn:
		TREA A 0 A_SetSolid
		TREA A 0 A_SetShootable
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
	  SpawnStill:
		TREA A -1
		Stop
	}
}



ACTOR PlantedTree //Sapling
{
  //$Sprite TRNPC0

  Radius 16
  Height 16
  
  +SHOOTABLE
  +NOBLOOD
  -SOLID
  
  
  Mass 0x7FFFFFFF
  Health 300
  PainChance 255 //100% chance of going into Pain state
  DamageFactor "Axe", 7.0
  DamageFactor "Fire", 1.0
  
  States
  {
	  Spawn:
		TNT1 A 1 
		//Check if we planted on soil or grass
		TNT1 A 0 A_JumpIf(!ACS_ExecuteWithResult(941), "Wilt")
		//Make monsters attack us:
		TNT1 A 0 ACS_ExecuteAlways(503, 0)
	//Ripening will take 10 mins by default. If you water your plant 10 times, you can cut it down to 3 minutes.
	WaitUntilRipe:
		TNT1 A 0 A_JumpIfInventory("Ripeness", 0, "Ripe") //Max ripeness is 100
		TRNP C 2100 // Progress every 1 minute
		TRNP C 1 A_GiveInventory("Ripeness", 10) //Ripen the plant a bit
		goto WaitUntilRipe
	WaitUntilRipeWatered:
		TNT1 A 0 A_JumpIfInventory("Ripeness", 0, "Ripe") //Max ripeness is 100
		TRNP D 2100 // Progress every 1 minute
		TRNP D 1 A_GiveInventory("Ripeness", 34) //Ripen the plant a bit
	Pain.Water:
		TNT1 A 0 
		goto WaitUntilRipeWatered
	  Ripe:
		TNT1 A 0 A_SpawnItemEx("RandomTreeSpawner", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
		Stop
	  Death.Axe:
	  Death:
	  Wilt:
	    TRNP E 350
		Stop
  }
}

actor RandomTreeSpawner : RandomSpawner
{
    //$Sprite DFFBY0

	+FRIENDLY //Turrets would shoot these trees otherwise
	DropItem "DaggerFallThing51" //"Daggerfall Tree - Full"
	DropItem "DaggerFallThing53" //"Daggerfall Tree - Evergreen"
	DropItem "DaggerFallThing86" //"Daggerfall Tree - Evergreen 4"
	DropItem "DaggerFallThing135" //"Daggerfall Tree 3"
	DropItem "DaggerFallThing137" //"Daggerfall Tree 4"
	DropItem "DaggerFallThing155" //"Daggerfall Tree - Plush 3"
	DropItem "DaggerFallThing162" //"Daggerfall Tree - Plush 4"
	DropItem "DaggerFallThing169" //"Daggerfall Tree - Evergreen"
	DropItem "DaggerFallThing173" //"Daggerfall Tree - Evergreen 2"
	DropItem "DaggerFallThing180" //"Daggerfall Tree - Evergreen Skinny"
	DropItem "DaggerFallThing183" //"Daggerfall Tree - Skinny Bunchy"
}

actor RandomSafariTreeSpawner : RandomSpawner
{
    //$Sprite DFFBF0

	+FRIENDLY //Turrets would shoot trees otherwise
	DropItem "DaggerFallThing32" //"Daggerfall Tree - Safari"
	DropItem "DaggerFallThing33" //"Daggerfall Tree - Japanese Garden"
	DropItem "DaggerFallThing142" //"Daggerfall Tree - Plush"
	DropItem "DaggerFallThing148" //"Daggerfall Tree - Plush 2"
	DropItem "DaggerFallThing149" //"Daggerfall Tree - Japanese"
	DropItem "DaggerFallThing174" //"Daggerfall Tree - Plush 6"
	DropItem "DaggerFallThing178" //"Daggerfall Tree - Jungle 1"
	DropItem "DaggerFallThing185" //"Daggerfall Tree - Japanese Garden"
	DropItem "DaggerFallThing171" //"Daggerfall Tree - Jungle Skinny"
}

/* Others:
DaggerFallThing112 - "Daggerfall Tree - Small Shaped"
DaggerFallThing140 - "Daggerfall Tree - Dead 4"
DaggerFallThing150 -  "Daggerfall Tree - Desert"
DaggerFallThing157 - "Daggerfall Tree - Palm tree"
DaggerFallThing164 - "Daggerfall Tree - Palm Short"
DaggerFallThing172 - "Daggerfall Tree - Ferntree"

DaggerFallThing163 - "Daggerfall Tree - Dead Swamp"
DaggerFallThing153 - "Daggerfall Tree - Dead 6"
DaggerFallThing152 - "Daggerfall Tree - Dead 5"
DaggerFallThing52 - "Daggerfall Tree - Dead"

*/



Actor NTreeDead1 : ChoppableTree 
{
    //$Sprite NTREA0
Radius 13
Height 160
+SOLID

  States
  {
	  Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRE A 0 A_SetSolid
		NTRE A 0 A_SetShootable
		NTRE A -1
		Stop
	}
}

Actor NTreeDead2 : ChoppableTree 
{
  //$Sprite NTRFA0
Radius 13
Height 160
+SOLID

  States
  {
	  Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRF A 0 A_SetSolid
		NTRF A 0 A_SetShootable
	  SpawnStill:
		NTRF B -1
		Stop
	}
}

Actor NTreeSwamp1 : ChoppableTree 24002
{
  //$Sprite NTRGA0
Radius 13
Height 140
+SOLID
  States
  {
	  Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRG A 0 A_SetSolid
		NTRG A 0 A_SetShootable
	  SpawnStill:
		NTRG A -1
		Stop
	}
}

Actor NTreeSwamp2 : ChoppableTree 24003
{
  //$Sprite NTRHA0
Radius 10
Height 120
+SOLID
  States
  {
	  Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRH A 0 A_SetSolid
		NTRH A 0 A_SetShootable
	  SpawnStill:
		NTRH A -1
		Stop
	}
}

Actor ZedTreeDead1 : ChoppableTree replaces NTreeDead1 24000
{
  //$Sprite NTREA0
Radius 13
Height 160
+SOLID

  States
  {
	Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRE A 0 A_SetSolid
		NTRE A 0 A_SetShootable
		Goto Spawn10
	  Spawn10:
		NTRE A 0 A_JumpIf(health<181,"Spawn9")
		NTRE A 1
		Loop
	Spawn9:
		NTRE B 0 A_JumpIf(health<161,"Spawn8")
		NTRE B 1
		Loop
	Spawn8:
		NTRE C 0 A_JumpIf(health<141,"Spawn7")
		NTRE C 1
		Loop
	Spawn7:
		NTRE D 0 A_JumpIf(health<121,"Spawn6")
		NTRE D 1
		Loop
	Spawn6:
		NTRE E 0 A_JumpIf(health<101,"Spawn5")
		NTRE E 1
		Loop
	Spawn5:
		NTRE F 0 A_JumpIf(health<81,"Spawn4")
		NTRE F 1
		Loop
	Spawn4:
		NTRE G 0 A_JumpIf(health<61,"Spawn3")
		NTRE G 1
		Loop
	Spawn3:
		NTRE H 0 A_JumpIf(health<41,"Spawn2")
		NTRE H 1
		Loop
	Spawn2:
		NTRE I 0 A_JumpIf(health<21,"Spawn1")
		NTRE I 1
		Loop
	Spawn1:
		NTRE J 1
		Loop
	Death.Axe:
		TNT1 A 0
		TNT1 A 0 A_PlaySound("treebreak")
		NTRE K 30
		NTRE KKKKKKKKKK 1 A_FadeOut(0.1)
		Goto SpawnItems
		
	}
}

Actor ZedTreeDead2 : ChoppableTree replaces NTreeDead2 24001
{
  //$Sprite NTR2A0
Radius 13
Height 160
+SOLID

  States
  {
	Spawn:
		TNT1 A 0 A_KillChildren
		TNT1 A 0 A_SpawnItemEx("DoomZFlamePawn", 0, 0, 0, 0,0,0, 0, SXF_SETMASTER, 0)
		NTRE A 0 A_SetSolid
		NTR2 A 0 A_SetShootable
		Goto Spawn10
	  Spawn10:
		NTR2 A 0 A_JumpIf(health<181,"Spawn9")
		NTR2 A 1
		Loop
	Spawn9:
		NTR2 B 0 A_JumpIf(health<161,"Spawn8")
		NTR2 B 1
		Loop
	Spawn8:
		NTR2 C 0 A_JumpIf(health<141,"Spawn7")
		NTR2 C 1
		Loop
	Spawn7:
		NTR2 D 0 A_JumpIf(health<121,"Spawn6")
		NTR2 D 1
		Loop
	Spawn6:
		NTR2 E 0 A_JumpIf(health<101,"Spawn5")
		NTR2 E 1
		Loop
	Spawn5:
		NTR2 F 0 A_JumpIf(health<81,"Spawn4")
		NTR2 F 1
		Loop
	Spawn4:
		NTR2 G 0 A_JumpIf(health<61,"Spawn3")
		NTR2 G 1
		Loop
	Spawn3:
		NTR2 H 0 A_JumpIf(health<41,"Spawn2")
		NTR2 H 1
		Loop
	Spawn2:
		NTR2 I 0 A_JumpIf(health<21,"Spawn1")
		NTR2 I 1
		Loop
	Spawn1:
		NTR2 J 1
		Loop
	Death.Axe:
		TNT1 A 0
		TNT1 A 0 A_PlaySound("treebreak")
		NTR2 K 30
		NTR2 KKKKKKKKKK 1 A_FadeOut(0.1)
		Goto SpawnItems
		
	}
}
