The credits for every asset used on this NPC, can be found inside credits.txt.

SHOWCASE VIDEO AND DOCUMENTATION OF THE MARINES: https://www.youtube.com/watch?v=PlZSs5XyEdU
UPDATE VIDEO: https://youtu.be/unfIzwMtQJ4

If you have any issues with the marines or some other NPC made by me, you can ask me for help on Discord at inkoalawetrust#9783, or Twitter at @inkoalawetrust. Or put them on the issue tracker: https://github.com/inkoalawetrust/Smart-Marines/issues

The marines have 120 health, somewhat higher than the players'. You can turn on the Friendly flag for them in the editor to use them as smart allies.
In fact, I've sort of just made them with the intent of being used as allies, like the Allied Marines resource from Realm667. But they work just as well as enemies.

Their name in the editor is "AI Marine" and "AI Marine (Turret)", and they can be found under the Marines folder.
Their class names are "SmartMarine", "TurretMarine"
The turret's editor name is "Machine Gun Emplacement", and it can be found in the Weapons folder, its' class name is "SmartMarineMGTurret"
The editor name of their No Crouch Zone map spots (More info below) is "No Crouch Zone (AI Marines)", and it can be found in the ZDoom folder, its' class name is SM_NoCrouchZone.

================|WHAT THEY CAN DO|================

- Attack stuff by:
  - Bashing it with their rifle. But they normally avoid it if they can help it. (Melee)
	- Can also use berserk packs, if one is somehow in their inventory, which triple their melee damage, and make them more likely to engage in melee.
  - Shooting at it, with a random chance of changing position or lying down while doing so. (Ranged)
  - Throw a grenade at it, more likely to throw it at crowds of enemies. (Area of effect)
- They avoid firing their rifles, grenades, or turrets if their line of fire to the target is blocked by obstacles or allies.
- Run away from the grenades they throw, if those grenades are about to explode.
- Intelligently dodge enemy or extremely harmful projectiles heading their way. More information below.
- Dynamically take cover behind sufficiently tall level geometry or shootable actors when attacking enemies. More information below.
- Can be set up into squads in which they stick together and follow a designated leader. And can change leaders if the previous one died.
- They have multiple death animations that can play randomly.
- Reload their rifles for every 20 rounds they fire in total.
  - They first try a few times to run away from their target until out of sight, to not reload while vulnerable.
- Chase around their target for some time before losing track of it, if they can't hear or see it for too long.
- When not set to friendly, they attack friendly NPCs on sight, unlike normal enemy NPCs/monsters, that don't attack friendly NPCs unless provoked by them first.
- Alert other marines that are on their side of their current target, if the other marines have no target they are dealing with already. They can also hear the alerts of enemy marines.
- Retreat and keep a distance away from very powerful or large enemies like Cyberdemons. Or enemy players and marines on turrets.
- Marines friendly to the player can be ordered to stand still or wander around by pressing use on them. This only works if they aren't currently fighting.
- They work with ZDoom's built-in patrol system, so you can have them walk around set paths. To do stuff like guard areas.*
- Has placeable machine gun turrets that both players and the marines can use, you can press the use key on a friendly marine to get him off of one.
- Includes a separate marine that permanently stays on the turret until killed, who can also be colored like below. Pressing use on them will get them off, if they are friendly, and User_DisobeyCommands is off.
- Can be colored with a variety of colors:
  - Red
  - Gray
  - White
  - Black
  - Blue
  - Yellow
  - Orange
  - Pink
  - Dark Green
  - Dark Red
  - Random (Randomly pick a color.)
  - Default (Use the default shade of green. Similar to just leaving User_Color empty.)
- Most of the marines' behaviour can be configured on a per actor basis using user variables.
- Can have a randomized personality, that gives the marine a random armor color, different attention spans and chances to throw a grenade etc. Adding a custom value to a user variable prevents it from being randomized.
- They also have a rifle that they sometimes drop, which you can pick up and use yourself.
  - The rifle fires at full auto, and needs reloading after firing 20 rounds, just like when the marines use them. You can reload before the magazine fully runs out too.
  - It also features animated digital sights complete with an animation for looking in and out of them, using the sights increases your accuracy as well.
  - The rifle can be entirely disabled from dropping from dead marines, by enabling User_NoRifleDrop on the marine.
- Has easter eggs.

================|PROJECTILE DODGING|================
Marines are also able to dodge projectiles that are heading in their direction, the text below will explain how the dodging behavior works exactly.

WHAT THEY DODGE:
Friendly marines will avoid all projectiles fired by unfriendly monsters, but will not avoid projectiles fired by other friendly monsters or players,
unless the projectile in question would do way too much damage to them, or it is part of the Always Avoid list. So for example, if you fire a rocket
at a friendly marine, he will always dodge even though it was fired by an ally, because the projectile is both part of the Always Avoid list, and
because it would do enough damage to kill him most of the time. But if you fire a plasma rifle at him, he won't dodge the projectiles until his health
is so low that getting hit by one or two more plasma balls would kill him.

Hostile marines on the other hand, will avoid all incoming projectiles, Including ones fired by other enemy monsters,
as unfriendly monsters do not attempt to avoid friendly fire at all by default.

Both friendly and unfriendly marines will dodge projectiles heading at them, that are within their field of view. If a projectile is coming at them from
an angle out of their FOV, such as behind them. They will also be able to notice it and dodge it, but the range at which they react to projectiles coming
behind them is much shorter. Similar to how monsters can still see players that get too close to them. Even if the player sneaks up behind the monster.

In addition to all this, the function that the marines use to determine if they should dodge a projectile (SM_FindNearbyProjectile()) has two easy to edit
lists:

AlwaysAvoid: Any projectile class name added to this list will ALWAYS be avoided by friendly marines, even if it doesn't do enough damage to kill them in 1-2 hits.
The vanilla rocket projectile and the BFG Ball is included in this list by default.

IgnoredProjectiles: Any projectiles in this list will always be ignored by both friendly and hostile marines, the marines' grenades (SM_Grenade) are in this list by default,
as the marines have special handling for grenades in particular (Running away from them entirely.)

WHEN THEY DODGE:
If any of the above conditions are true, and the marine decides it needs to dodge the incoming projectile. It will not just blindly jump in any direction.
First the marine will check if he can jump to his left and right, if a tall ledge, wall, or other blocking actor is on his right,
then he will only jump to the left to dodge, since his right side is blocked, and vice versa.
If he is able to jump both left and right, then he will randomly pick a direction to jump to. But if both his left and right are blocked
(e.g he is on a small bridge, or tight corridor), then he won't try dodging the incoming projectile at all. Since he has nowhere to jump away from it.

STRAFE FIRING:

In addition, when marines dodge a projectile, they have a fairly high chance to decide to shoot while in mid air. Normally, they will shoot at the projectiles'
shooter, but if the projectile doesn't exist anymore, has no shooter attached to it, the shooter has the NOTARGET or NEVERTARGET actor flags and/or cheats,
Or is an ally of the marine that shot him by accident. Then the marine will instead fall back to shooting at his target. If the marine has no target either.
Or has an empty magazine currently. Then he will simply not shoot.

When shooting at a target while strafing, the marines can fire faster, at the expense of accuracy, due to them hip firing while in mid-air.
Of course, marines will also stop firing if anything gets in the way of the enemy shooter/target. Or if they simply run out of ammo. In which case, their
gun will make a clicking sound just like the one you hear when you pick up their rifles and try shooting it with no ammo.

HOW IT WORKS (TECHNICAL):
Because projectiles are not part of the blockmap normally, a BlockThingsIterator cannot be used to detect nearby projectiles. So an event handler is used instead. (SM_ProjectileHandler)
The event handler has a dynamic array inside of it. When an actor with the bMissile flag is spawned during WorldThingSpawned, a pointer to that actor will be added to the array.
Likewise, when a projectile explodes or is erased entirely, the array will update on WorldThingDied and WorldThingDestroyed respectively, to remove the pointer to that projectile.
This dynamic array inside of the SM_ProjectileHandler event handler is then accessed and iterated through by SM_FindNearbyProjectile() to search for projectiles.

This method is a lot faster than using a Thinker Iterator instead, which would have to iterate through every single actor on the map every time SM_FindNearbyProjectile() is called.
But it does have its' limits, for example, if your mod or map has actors that use the bMissile flag for whatever reason. But are not projectiles. The marines will still treat them as such.
And try to dodge them, in cases like those, you should add the class names of those actors inside of the IgnoredProjectiles list.

NOTE:
While the dodging behavior works pretty well with projectiles like the ones in vanilla Doom, they will not be able to dodge faster projectiles as well.
Unless the total range at which they check for projectiles is extended in the code.

================|COVER BEHAVIOR|================

When attacking, marines are able to take cover behind any sufficiently tall barricade.

WHEN THEY TAKE COVER:
Marines take cover any time that they are attacking an enemy, and detect that any tall enough (Around 24 map units.) level geometry is between them and their target.
They will also crouch behind sufficiently tall actors, but only if those actors are not NPCs or players, are as wide or wider than the marines' own width,
can block both projectiles AND hitscans (So Doom's vanilla obstacles are excluded.), and the actor doesn't have very low health.
This ensures that they will only take cover behind obstacles that can protect them fully, and are not fragile.
Do note that they will not take cover when patrolling or heading to a turret.

WHEN THEY GET OUT:
Marines that have found cover will only get out of cover in the following situations:
- Their target has gone over or around their cover, or is within melee range.
- A grenade is nearby and about to explode.
- The area of the map they are taking cover at has changed, e.g the wall they were hiding behind lowered, or the actor they were behind moved or was destroyed.
- Their target is gone, and they haven't found any other target for several seconds.

NO CROUCH ZONE ACTOR:
If you do not want the marines to crouch in certain areas, such as behind particular staircases, you can use the SM_NoCrouchZone actor.
Which will prevent marines entering its' range from crouching at all. The actor can be found under the "ZDoom" folder.
It also uses custom arguments (Not user variables) that allow you to specify its' range and for how long marines will not crouch once out of said range.
The custom arguments can be accessed by selecting the no crouch zone actor(s) and going to the Action/Tag/Misc. tab.
In addition to the custom arguments, you can also disable and enable the no crouch zone actors with Thing_Activate and Thing_Deactivate.

MISCELLANEOUS:
- Marines also avoid projectiles when behind cover. By ducking back in again. Friendly marines in cover will also avoid other friendly projectiles. Unless they are on the ignore list.
- While crouched, marines will sometimes look up. This is a purely cosmetic visual effect.
- When crouched, they use a different pain state, and will also play a crouched death animation if killed while in cover.
- Marines behind cover are a bit more accurate with their shooting, and when they are crouched down they will take 20% less damage, and get pushed back by damage less easily.

================|MARINE COMMANDS|================
Friendly marines can be commanded to do different things.
You can press use on them to cycle them through the following orders:

Wander: The marine will randomly wander around without following the player, and sometimes stay in place to look around.
Follow: The marine will follow the player that pressed use on him (If the player is friendly). Marines following a player move faster than when wandering, and will not stop to look around.
Stand Still: The marine will stay in place, like when first spawned. After they finish searching for enemies, they go back to standing still instead of wandering.
Exit Turret: Only applicable for marines on turrets, makes the marine exit the turret.

Pressing use on a hostile marine will cause him to alert other nearby hostile marines.

================|SQUAD BEHAVIOR|================
Marines can be optionally put into squads, in which they will follow the squads' leader around.
Marines in squads can also alert each other of enemies, this works similarly to how marines outside of squads alert each other, except there is no distance limit.

HOW TO SET UP:
To form a squad on your map, you need to give 2 or more marines a User_SquadName, this is the name of the squad the marines will be part of.
Then you need to give the squad a leader for them to rally around, to do that, just pick or make a marine that shares the same squad name,
and turn on the User_Leader variable, which will assign him as the squads' leader.

NOTE: You cannot have a squad with more than one leader, you also cannot make two opposing squads, that both share the same name.

LEADERSHIP CHANGES:
Marine squads can change their leadership around if their leader is killed. If the squads' leader is killed or entirely removed from the game,
then the squad will assign its' healthiest marine as their new leader.

In addition, marines, including the former leader, are not actually removed from their squad when killed, as there is still a chance for them
to be resurrected. However marines WILL be entirely removed from their squad if they are gibbed (Cannot be resurrected), or are entirely erased
from existance. If all the members are gibbed or erased, the squad is dissipated entirely.

NOTE: If an entire squad has been killed, and then one of the dead marines in it is resurrected. The surviving marine will set himself as the leader.

SQUAD MOVEMENT & BEHAVIOR:
When in a squad, marines will follow their leader around, and try to stay within range of him.
Generally, marine squads stay the closest together when fighting, but squad members whose current target is very powerful, will keep more distance
from their leader. To avoid all getting too close together and then being killed by whichever powerful enemy. When wandering around looking for enemies,
squads are at their most lax, and squad members are allowed to be as far as 1024 map units away from their leader without needing to head back to him.

Friendly marines that are part of a squad, but are not the squads' leader, will not follow the players' commands.
Instead, to command the members of a squad you have to give commands to the leader, which the rest of the squad
will then generally follow, such as making a squad stand still by ordering their leader to do so.

Note: Friendly marines in squads have little icons that appear over their heads for distinction, the icon with 4 human figures indicates that the marine
is a normal member. The star icon indicates that the marine is the squads' leader. Unfriendly marines do not have these icons over their heads.

================|USER VARIABLES|================
User variables allow an actor to be configured by the mapper through the editor on a per-actor basis, without requiring to modify the source code of the actor in any way.
If you don't know how to use and/or change the user variables of an actor, then click on this Imgur album to see how: https://imgur.com/a/IF9Ezo2
The marines have plenty of user variables, for you to fine tune their behavior or even straight up disable features as you see fit.

User_SearchTime: The amount of time the marine will try to look for his target before giving up. (And getting off the turret if on one.)
- Default is 300. -1 makes the marine never give up persuit while the target is alive. And immediately go back to wandering once the target is dead.
User_GrenadeThreshold: How likely the marine is to throw a grenade, the lower the number, the more likely he is to throw one.
- Default is 240. -1 makes them always throw grenades, unless they would be caught in the blast. And 256 makes them never throw grenades.
User_DisobeyCommands: Friendly marines will not wander, follow, stay in place, or leave turrets when the player presses use on them.
User_NoReload: Marines do not need to ever reload their gun.
User_NoCover: Marines do not take cover when attacking.
User_ClipPlayers: Marines and players on the same side phase through each other.
User_NoRifleDrop: Marines will never drop their rifles when killed.
User_RetreatAttempts: The amount of times the marine tries to run out of sight of his target, when he needs to reload.
- Default is 10. -1 makes the marine reload in the open, without trying to get behind cover at all.
User_AlertRange: The distance around which a marine will alert other marines on his side of a target.
- Default is 384 map units. -1 disables alerting entirely for this marine, but still allows them to hear the alerts of other marines.
User_Leader: If the marine also has a squad name specified, this tells the game that this marine should be the leader of that squad. Otherwise, it does nothing.
User_SquadName: The name of the squad the marine is in, if the squad has a leader, the marine will follow him around.
User_DodgeRange: The maximum range in which marines react to incoming projectiles.
- Default is 384 map units. -1 disables dodging entirely for this marine.
User_EnemyAlertHearingRange: The distance around which a marine can hear the alerts of other, enemy marines.
- Default is 1024 map units. -1 disables hearing enemy alerts entirely, but the marine can still be alerted by other marines on his side.
User_TurretThreshold: How likely the marine is to use a turret near him. Marines are much more likely to use turrets, if they have friendly players and/or NPCs around to cover them.
- Default is 100, -1 means that the marine will never use a turret. Lower values increase the use chance.
User_FearDistance: The distance that the marine runs away to, if his target is scary enough.
- Default is 2048 map units, a value of 0 or less makes them fearless. (Within reason, so they still avoid grenades for example.)
User_Color: Can be used to give the marines different armor colors.
- Default is no translation, the available colors are Default, Gray, Red, White, Black, Blue, Yellow, Orange, Pink, Dark Green, Dark Red, and Random, which randomly picks any of the afformentioned colors.
User_DefaultOrder: Specifies if the marine should follow the player or wander around by default.
- By default, the marine looks for any friendly players in its' line of sight, if there are none, he will wonder by default, if there is one, he will follow them by default.
- The other two options are "follow" and "wander", which are fairly self explanatory.
User_RandomPersonality: When on, it randomizes several user variables that could be considered aspects of the marines' personality, such as:
- User_SearchTime, User_GrenadeThreshold, User_TurretThreshold, User_RetreatAttempts, User_FearDistance, and User_Color.
- If any of those variables have any values besides their defaults, then they won't be randomized, so you can still for example have marines with random grenade and turret thresholds, but the same melee chance.
- The variable can be set to true again in-game using SetUserVariable() in ACS, allowing you to randomize a marines' personality mid-game, if you want to do that for some reason.

STATIONARY TURRET MARINE VARIABLES:
- User_RunFromGrenades: When turned on, the stationary marine will leave his turret to run from any grenades about to explode, like the marines normally do.
- User_DisoberyCommands: Like the same variable on the normal NPC. This will prevent the friendly turret marines from getting off the turret when you press use on them.
- User_NoRifleDrop: Works exactly like it does on the main marine NPC.
- User_Color: Also works exactly like it does on the main marine NPC.