Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Created January 12, 2017 20:26
Show Gist options
  • Save Jacajack/ac8635311f9294c19c43e20036754e96 to your computer and use it in GitHub Desktop.
Save Jacajack/ac8635311f9294c19c43e20036754e96 to your computer and use it in GitHub Desktop.
Simple, old combat simulator
function Buffs( )
{
this.Bleeding = 0;
this.Death = false;
this.Blinded = false;
this.Suffocating = 0;
this.Fallen = false;
this.Pain = 0;
}
//WIP
// V Add or remove those two
// /*
function Organ( Owner, Name, Action, Health )
{
this.Health = Health; //How many damage is neede to hurt this organ
this.SubOrgans = []; //Other organs that can be injured by hitting this one
//eg. SubOrgans = [[ Heart, 0.5], [ Lungs, 1]];
// Name, Chance, Name, Chance
this.Owner = Owner; //Owner of organ
this.Armor = new Armor( ); //Armor
this.Cut = function( )
{
var Choice = Math.random( );
for ( var i = 0; i < this.SubOrgans.length; i++ )
{
if ( Choice > this.SubOrgans[i][1] && ( Choice < this.SubOrgans[i + 1][1] && this.SubOrgans.length == 1 ) ) //Choose one organ from list
{
console.log( this.Name + " hit!" );
this.SubOrgans[i][0].Cut( ); //Cut it
}
}
};
}
Organ.prototype = new Buffs( );
var Brain = new Organ( Gnome, "brain", "mangling", 10 );
var Hd = new Organ( Gnome, "head", "hitting", 0 );
Hd.SubOrgans = [[Brain, 1]];
Hd.Cut( );
/**/
function Weapon( Name, Action, Power )
{
this.Name = Name;
this.Action = Action;
this.Power = Power;
switch( Name )
{
case "sword":
this.Action = "cut";
this.Power = 13;
break;
case "axe":
this.Action = "cut";
this.Power = 7;
break;
}
}
function Armor( )
{
this.Name = ""; //Example "wooden armor"
this.Protection = 0; //How many damage will it reduce
this.Health = 100; //If goes to 0 armor is broken
this.Durability = 4; //How many times armor can be hit with maximum power
this.Wear = function( Name, Protection, Durability )
{
this.Name = Name;
this.Protection = Protection;
this.Durability = Durability;
};
this.Repair = function( Durability )
{
this.Durability = Durability;
};
this.Unwear = function( )
{
this.Name = "";
this.Protection = 0;
this.Durability = 0;
};
}
function Head( Owner )
{
this.Skin = 5;
this.Skull = 50;
this.Brain = 3;
this.BrainChance = 0.80;
this.Eyes = 3;
this.EyesChance = 0.05;
this.Armor = new Armor( );
this.Owner = Owner;
this.Hit = function( Weapon, Power ) //Called when head is hit
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
//ARMOR - In development
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Head hit - " + Damage + " damage dealen" );
switch ( Weapon.Action )
{
case "cut":
if ( Math.random( ) < this.EyesChance ) //If true eyes are lost
{
this.Eyes = 0;
Damage -= this.Eyes;
HitLog += ", breaking eyes";
this.Blinded = true;
this.Pain += 30;
}
if ( Damage >= this.Skin ) //Skin cut
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Damage >= this.Skull ) //Skull crushed
{
HitLog += ", crushing skull";
Damage -= this.Skull;
this.Pain += 20;
if ( Math.random( ) < this.BrainChance && Damage > this.Brain ) //Brain destroyed
{
this.Brain -= Damage;
Damage -= this.Brain;
HitLog += ", mangling brain, causing death";
this.Death = true;
this.Pain += 10;
}
}
}
break;
}
return HitLog;
};
}
Head.prototype = new Buffs( );
function UpperBody( Owner )
{
this.Skin = 5;
this.Ribs = 45;
this.Lungs = 3;
this.LungsChance = 0.40;
this.Heart = 3;
this.HeartChance = 0.10;
this.Armor = new Armor( );
this.Owner = Owner;
this.Hit = function( Weapon, Power ) //Called when Upper Body is hit
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Upper body hit - " + Damage + " damage dealen" );
switch ( Weapon.Action )
{
case "cut":
if ( Damage >= this.Skin ) //Skin cut
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Damage >= this.Ribs ) //Ribs cut
{
HitLog += ", breaking ribs";
Damage -= this.Ribs;
this.Pain += 40;
if ( Math.random( ) < this.LungsChance && Damage > this.Lungs ) //Lungs cut
{
this.Lungs -= Damage;
Damage -= this.Lungs;
HitLog += ", piercing lungs";
this.Suffocating += 40;
this.Bleeding += 20;
this.Pain += 50;
}
if ( Math.random( ) < this.HeartChance && Damage > this.Heart ) //Heart crushed
{
this.Heart -= Damage;
Damage -= this.Heart;
HitLog += ", piercing heart, causing death";
this.Death = true;
this.Bleeding += 100;
this.Pain += 100;
}
}
}
break;
}
return HitLog;
};
}
UpperBody.prototype = new Buffs( );
function Arm( Owner )
{
this.Skin = 5;
this.Bone = 100;
this.Muscles = 25;
this.Missing = false;
this.HandMissing = false;
this.Armor = new Armor( );
this.Owner = Owner;
this.Item = {};
this.Hit = function( Weapon, Power )
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Arm hit - " + Damage + " damage dealen" );
if ( this.Missing === true ) return " {THIS IS MISSING} ";
switch ( Weapon.Action )
{
case "cut":
if ( Math.random( ) < 0.30 && this.HandMissing === false ) //Hand
{
if ( Damage >= this.Skin )
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Damage >= this.Muscles )
{
HitLog += ", cutting muscles";
Damage -= this.Muscles;
this.Bleeding += 5;
this.Pain += 15;
if ( Damage >= this.Bone ) //Hand cut off
{
HitLog += ", crushing bone, cutting hand off, causing bleeding";
Damage -= this.Bone;
this.Bleeding += 25;
this.HandMissing = true;
this.Pain += 50;
}
}
}
}
else //Arm
{
if ( Damage >= this.Skin )
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Damage >= this.Muscles )
{
HitLog += ", cutting muscles";
Damage -= this.Muscles;
this.Bleeding += 5;
this.Pain += 20;
if ( Damage >= this.Bone ) //Arm cut off
{
HitLog += ", crushing bone, cutting arm off, causing bleeding";
Damage -= this.Bone;
this.Bleeding += 30;
this.Missing = true;
this.Pain += 60;
}
}
}
}
break;
}
return HitLog;
};
}
Arm.prototype = new Buffs( );
function Neck( Owner )
{
this.Skin = 5;
this.Spine = 75;
this.Throat = 20;
this.ThroatChance = 0.9;
this.Armor = new Armor( );
this.Owner = Owner;
this.Hit = function( Weapon, Power )
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Neck hit - " + Damage + " damage dealen" );
switch ( Weapon.Action )
{
case "cut":
if ( Damage >= this.Skin ) //Skin cut
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Math.random( ) < this.ThroatChance && Damage > this.Throat ) //Throat crushed
{
this.Throat -= Damage;
Damage -= this.Throat;
HitLog += ", slitting throat, causing suffocation";
this.Suffocating += 20;
this.Bleeding += 20;
this.Pain += 25;
}
if ( Damage >= this.Spine ) //Head cut off
{
HitLog += ", snapping spine, decapitating " + this.Owner.Name;
Damage -= this.Spine;
this.Death = true;
this.Pain += 100;
}
}
break;
}
return HitLog;
};
}
Neck.prototype = new Buffs( );
function Leg( Owner )
{
this.Skin = 5;
this.Bone = 110;
this.Muscles = 25;
this.Missing = false;
this.FeetMissing = false;
this.Armor = new Armor( );
this.Owner = Owner;
this.Hit = function( Weapon, Power )
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Leg hit - " + Damage + " damage dealen" );
if ( this.Missing === true ) return 1;
switch ( Weapon.Action )
{
case "cut":
if ( Math.random( ) < 0.20 && this.FeetMissing === false ) //Feet
{
if ( Damage >= this.Skin )
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
if ( Damage >= this.Muscles )
{
HitLog += ", cutting muscles";
Damage -= this.Muscles;
this.Bleeding += 5;
if ( Damage >= this.Bone ) //Feet cut off
{
HitLog += ", crushing bone, cutting off foot";
Damage -= this.Bone;
this.Bleeding += 25;
this.FeetMissing = true;
this.Fallen = true;
}
}
}
}
else //Leg
{
if ( Damage >= this.Skin )
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
if ( Damage >= this.Muscles )
{
HitLog += ", cutting muscles";
Damage -= this.Muscles;
this.Bleeding += 5;
if ( Damage >= this.Bone ) //Leg cut off
{
HitLog += ", crushing bone, cutting off leg";
Damage -= this.Bone;
this.Bleeding += 30;
this.Missing = true;
this.Fallen = true;
}
}
}
}
break;
}
return HitLog;
};
}
Leg.prototype = new Buffs( );
function LowerBody( Owner )
{
this.Skin = 5;
this.Muscles = 25;
this.Guts = 20;
this.GutsChance = 0.75;
this.Bleeding = 0;
this.Death = false;
this.Blinded = false;
this.Suffocating = 0;
this.Armor = new Armor( );
this.Owner = Owner;
this.Hit = function( Weapon, Power ) //Called when Upper Body is hit
{
var HitLog = "";
var Damage = Weapon.Power * Power;
if ( typeof DEBUG_ENABLED != "undefined" ) if ( DEBUG_ENABLED === true ) console.log( "\nDAMAGE = " + Damage );
if ( this.Armor.Name !== "" && this.Armor.Health > 0 )
{
Damage -= this.Armor.Protection;
this.Armor.Health -= ( Damage / this.Armor.Protection ) * 100 / this.Armor.Durability;
if ( this.Armor.Health > 0 )
{
HitLog += ", hitting " + this.Armor.Name;
}
else
{
this.Armor.Health = 0;
HitLog += ", crushing " + this.Armor.Name; //If eventually broke armor
}
}
//console.log( "Lower body hit - " + Damage + " damage dealen" );
switch ( Weapon.Action )
{
case "cut":
if ( Damage >= this.Skin ) //Skin cut
{
HitLog += ", cutting skin";
Damage -= this.Skin;
this.Bleeding += 2;
this.Pain += 3;
if ( Damage >= this.Muscles )
{
HitLog += ", cutting muscles";
Damage -= this.Muscles;
this.Pain += 20;
if ( Math.random( ) < this.GutsChance && Damage > this.Guts ) //Guts crushed
{
this.Heart -= Damage;
Damage -= this.Guts;
HitLog += ", cutting guts, causing internal bleeding";
this.Bleeding += 35;
this.Pain += 45;
}
}
}
break;
}
return HitLog;
};
}
LowerBody.prototype = new Buffs( );
function Fighter( Name, Health, Strength )
{
this.Head = new Head( this );
this.Neck = new Neck( this );
this.LeftArm = new Arm( this );
this.RightArm = new Arm( this );
this.LeftLeg = new Leg( this );
this.RightLeg = new Leg( this );
this.UpperBody = new UpperBody( this );
this.LowerBody = new LowerBody( this );
this.Health = Health; //Useless!!!
this.Name = Name; //Name
this.Strength = Strength; //The bigger it is the higher damage is given
this.Evasion = 0.4; //The smaller it is the lower is chance to get hit...
this.Precision = 0.8; //The bigger it is the higher chance to hit enemy is...
this.BloodLevel = 300; //How much blood does your body contain?
this.Air = 200; //How big your lungs are?
this.Fallen = false; //Are you on ground?
this.Blinded = false; //Do you have your eyes?
this.Dead = false; //Are you alive?
this.Pain = 0; //How much pain do you feel?
this.Weapon = new Weapon( "sword", "", 0 ); //Move to right hand in future
this.Attack = function ( Target )
{
if ( this.Dead ) return 1;
var AttackLog = "";
if ( this.PainParalysed )
{
AttackLog += this.Name + " can't move!"; //" is screaming and is paralysed by pain!";
}
else
{
AttackLog += this.Name + " attacks with his " + this.Weapon.Name;
AttackLog += Target.Hit( this );
}
return AttackLog;
};
this.Hit = function ( Attacker )
{
var HitLog = "";
if ( this.Dead ) return 1;
if ( ( ( Attacker.Precision - ( Attacker.Blinded * Attacker.Precision ) ) * this.Evasion < Math.random( ) ) && this.PainParalysed === false && this.Blinded === false && this.Fallen === false )
{
HitLog += ", but " + this.Name + " dodges the attack.";
return HitLog;
}
//Choose body part to hit
var BodyPart = Math.random( );
//console.log( BodyPart );
if ( BodyPart <= 0.30 )
{
HitLog += " hitting " + this.Name +"'s upper body";
HitLog += this.UpperBody.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.55 )
{
HitLog += " hitting " + this.Name +"'s lower body";
HitLog += this.LowerBody.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.7 )
{
HitLog += " hitting " + this.Name +"'s left arm";
HitLog += this.LeftArm.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.85 )
{
HitLog += " hitting " + this.Name +"'s right arm";
HitLog += this.RightArm.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.90 )
{
HitLog += " hitting " + this.Name +"'s right leg";
HitLog += this.RightLeg.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.95 )
{
HitLog += " hitting " + this.Name +"'s left leg";
HitLog += this.LeftLeg.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else if ( BodyPart <= 0.98 )
{
HitLog += " hitting " + this.Name +"'s head";
HitLog += this.Head.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
else
{
HitLog += " hitting " + this.Name +"'s neck";
HitLog += this.Neck.Hit( Attacker.Weapon, Attacker.Strength * Math.random( ) );
}
//Replace last , with and
HitLog = HitLog.replace(/,(?![\s\S]*,)/, ' and') + ".";
//Update buffs
//Death from damage
if ( this.LeftArm.Death || this.RightArm.Death || this.Head.Death || this.LeftLeg.Death || this.RightLeg.Death || this.UpperBody.Death || this.LowerBody.Death || this.Neck.Death )
{
this.Dead = true;
return HitLog;
}
//Blood loses
this.BloodLevel -= ( this.LeftArm.Bleeding + this.RightArm.Bleeding + this.Head.Bleeding + this.LeftLeg.Bleeding + this.RightLeg.Bleeding + this.UpperBody.Bleeding + this.LowerBody.Bleeding + this.Neck.Bleeding );
//Suffocating
this.Air -= ( this.Head.Suffocating + this.UpperBody.Suffocating + this.Neck.Suffocating );
if ( this.Air <= 0 )
{
this.Dead = true;
HitLog += "\n" + this.Name + " has suffocated.";
}
//Bleed out
if ( this.BloodLevel <= 0 )
{
this.Dead = true;
HitLog += "\n" + this.Name + " has bleed out.";
}
//Pain
this.Pain += this.LeftArm.Pain + this.RightArm.Pain + this.Head.Pain + this.Neck.Pain + this.LeftLeg.Pain + this.RightLeg.Pain + this.UpperBody.Pain + this.LowerBody.Pain;
this.Pain -= Math.ceil( this.Pain / 10 );
this.PainParalysed = this.Pain >= 400;
//Fallen
if ( this.LeftLeg.Fallen || this.RightLeg.Fallen && this.Fallen === false && this.Dead === false )
{
HitLog += "\n" + this.Name + " falls to the ground!";
this.Fallen = true;
}
if ( typeof DEBUG_ENABLED != "undefined" )
{
if ( DEBUG_ENABLED === true )
{
HitLog += "\n" + this.Name + "'s air = " + this.Air; //For debugging
HitLog += "\n" + this.Name + "'s blood = " + this.BloodLevel;
HitLog += "\n" + this.Name + "'s pain = " + this.Pain;
}
}
return HitLog;
};
}
console.log( "If you want to have debugging output, please define DEBUG_ENABLED variable and set it to true;\n" );
var Goblin = new Fighter( "Goblin", 100, 30 );
var Gnome = new Fighter( "Gnome", 100, 10 );
//Goblin armor
Goblin.Head.Armor.Wear( "leather helmet", 30, 10 );
Goblin.Neck.Armor.Wear( "chainmail", 100, 25 );
Goblin.UpperBody.Armor.Wear( "leather chestplate", 30, 10 );
Goblin.LowerBody.Armor.Wear( "leather armor", 30, 10 );
Goblin.LeftArm.Armor.Wear( "leather gauntlet", 30 , 10 );
Goblin.RightArm.Armor.Wear( "leather gauntlet", 30, 10 );
Goblin.LeftLeg.Armor.Wear( "leather greave", 30, 10 );
Goblin.RightLeg.Armor.Wear( "leather greave", 30, 10 );
Goblin.Evasion = 0.32;
//Gnome armor
Gnome.Head.Armor.Wear( "steel helmet", 200, 30 );
Gnome.Neck.Armor.Wear( "chainmail", 100, 25 );
Gnome.UpperBody.Armor.Wear( "steel chestplate", 200, 30 );
Gnome.LowerBody.Armor.Wear( "steel armor", 200, 30 );
Gnome.LeftArm.Armor.Wear( "steel gauntlet", 200 , 30 );
Gnome.RightArm.Armor.Wear( "steel gauntlet", 200, 30 );
Gnome.LeftLeg.Armor.Wear( "steel greave", 200, 30 );
Gnome.RightLeg.Armor.Wear( "steel greave", 200, 30 );
Gnome.Evasion = 0.4; //NEEDS TO BE AUTOMATED IN FUTURE
while ( Gnome.Dead === false && Goblin.Dead === false )
{
console.log( Gnome.Attack( Goblin ) + "\n" );
console.log( Goblin.Attack( Gnome ) + "\n" );
}
//Wooo!!!!! 7.05.15 19:27 - 600 lines :D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment