Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from imnetcat/release-1.0.0
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
imnetcat authored Mar 18, 2021
2 parents e2aa5a1 + 1490054 commit 40546b5
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 275 deletions.
7 changes: 3 additions & 4 deletions src/logic/cell.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "cell.h"
using namespace Ecosystem::Logic;

void cell::Init(unsigned int lpower, double lcoef, size_t x, size_t y)
void cell::Init(unsigned int lpower, size_t x, size_t y)
{
light_power = lpower;
light_coef = lcoef;
SetPosition(x, y);
}

Expand Down Expand Up @@ -44,12 +43,12 @@ OrganicIterator cell::GetOrganic() const
return object;
}

bool cell::IsContainsOrganic() const
bool cell::ContainsOrganic() const
{
return has_object;
}

unsigned int cell::LightPower() const
{
return y() ? y() * light_coef * light_power : light_power;
return (light_power / std::pow(1.3, y()));
}
4 changes: 2 additions & 2 deletions src/logic/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Ecosystem
unsigned int light_power;
double light_coef;
public:
void Init(unsigned int light_power, double light_coef, size_t x, size_t y);
void Init(unsigned int light_power, size_t x, size_t y);
void SetEntity(EntitiesIterator);
void DelEntity();
EntitiesIterator GetEntity() const;
Expand All @@ -27,7 +27,7 @@ namespace Ecosystem
void AddOrganic(size_t value);
void DelOrganic();
OrganicIterator GetOrganic() const;
bool IsContainsOrganic() const;
bool ContainsOrganic() const;
};
}
}
106 changes: 4 additions & 102 deletions src/logic/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,24 @@ Entity::Entity()
position(0, 0),
age(0),
max_age(0),
hp(0),
energy(0),
defence(0),
attack(0),
max_hp(0),
max_energy(0)
{}

Entity::Entity(
size_t x,
size_t y,
unsigned short max_hp,
unsigned short energy,
unsigned short max_energy,
unsigned short max_age,
double defence,
double attack,
Genome g)
Genome g,
unsigned short max_age
)
:
position(x, y),
age(0),
max_age(max_age),
max_hp(max_hp),
hp(max_hp),
energy(energy),
max_energy(max_energy),
defence(defence),
attack(attack),
genom(g)
{}

Expand All @@ -45,58 +35,9 @@ unsigned short Entity::MaxAge() const
{
return max_age;
}
bool Entity::Defencing(double attack)
{
if (attack < defence)
{
return false;
}

attack -= defence;

unsigned short dif = attack * hp;
if (hp < dif)
{
hp = 0;
return false;
}
else
{
hp -= dif;
defence -= 0.01; // defence skill up
return true;
}
}
double Entity::Attack() const
{
return attack;
}
void Entity::AttackUp()
{
attack += 0.01;
}
double Entity::Defence() const
{
return defence;
}

void Entity::IncreaceEnergy(unsigned short value)
{
unsigned short dif = max_hp - hp;
if (dif != 0)
{
if (value > dif)
{
IncreaceHp(dif);
value -= dif;
}
else
{
IncreaceHp(dif - value);
value = 0;
}
}

if (energy + value < max_energy)
energy += value;
else
Expand All @@ -113,28 +54,6 @@ void Entity::DecreaceEnergy(unsigned short value)

value -= energy;
energy = 0;

DecreaceHp(value);
}

void Entity::IncreaceHp(unsigned short value)
{
if (hp + value > max_hp)
hp = 100;
else
hp += value;
}

void Entity::DecreaceHp(unsigned short value)
{
if (hp > value)
{
hp -= value;
}
else
{
hp = 0;
}
}

unsigned short Entity::Energy() const
Expand All @@ -146,14 +65,9 @@ unsigned short Entity::MaxEnergy() const
return max_energy;
}

unsigned short Entity::Hp() const
{
return hp;
}

bool Entity::IsDead() const
{
return !hp || age > max_age;
return !energy || age > max_age;
}

size_t Entity::ReproductionCost() const
Expand All @@ -179,14 +93,6 @@ void Entity::SetGenome(Genome value)
{
genom = value;
}
void Entity::Defence(double value)
{
defence = value;
}
void Entity::Attack(double value)
{
attack = value;
}
void Entity::Age(unsigned short value)
{
age = value;
Expand All @@ -199,7 +105,3 @@ void Entity::Energy(unsigned short value)
{
energy = value;
}
void Entity::Hp(unsigned short value)
{
hp = value;
}
21 changes: 2 additions & 19 deletions src/logic/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ namespace Ecosystem
protected:
unsigned short age;
unsigned short max_age;
unsigned short hp;
unsigned short max_hp;
unsigned short energy;
unsigned short max_energy;
double defence;
double attack;
Genome genom;
unsigned char carnivorousing = 0;
unsigned char fotosintesis = 0;
Expand All @@ -26,13 +22,10 @@ namespace Ecosystem
Entity(
size_t x,
size_t y,
unsigned short max_hp,
unsigned short energy,
unsigned short max_energy,
unsigned short max_age,
double defence,
double attack,
Genome g
Genome g,
unsigned short max_age = 50
);


Expand All @@ -45,27 +38,17 @@ namespace Ecosystem

void Tic();

bool Defencing(double);
double Defence() const;
double Attack() const;
void AttackUp();
unsigned short Age() const;
unsigned short MaxAge() const;
void IncreaceEnergy(unsigned short value);
void DecreaceEnergy(unsigned short value);
void IncreaceHp(unsigned short value);
void DecreaceHp(unsigned short value);
unsigned short Energy() const;
unsigned short MaxEnergy() const;
unsigned short Hp() const;

void SetGenome(Genome value);
void Defence(double);
void Attack(double);
void Age(unsigned short);
void MaxAge(unsigned short);
void Energy(unsigned short);
void Hp(unsigned short);
};

using EntitiesIterator = pool<Entity>::iterator;
Expand Down
24 changes: 12 additions & 12 deletions src/logic/gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Ecosystem
Move,
Photosyntesis,
EatOrganic,
Carnivorous,
Attack,
Birth,
Separate,

Expand All @@ -35,21 +35,21 @@ namespace Ecosystem
const std::map<Operation, unsigned int> CREATION_COST = {
{Operation::Stay, 1},
{Operation::Move, 15},
{Operation::Photosyntesis, 10},
{Operation::EatOrganic, 15},
{Operation::Carnivorous, 25},
{Operation::Birth, 30},
{Operation::Separate, 30}
{Operation::Photosyntesis, 20},
{Operation::EatOrganic, 40},
{Operation::Attack, 60},
{Operation::Birth, 100},
{Operation::Separate, 100}
};

const std::map<Operation, unsigned int> MAINTENANACE_COST = {
{Operation::Stay, 1},
{Operation::Move, 5},
{Operation::Photosyntesis, 1},
{Operation::EatOrganic, 2},
{Operation::Carnivorous, 5},
{Operation::Birth, 6},
{Operation::Separate, 6}
{Operation::Move, 2},
{Operation::Photosyntesis, 2},
{Operation::EatOrganic, 4},
{Operation::Attack, 8},
{Operation::Birth, 10},
{Operation::Separate, 10}
};
}
}
22 changes: 11 additions & 11 deletions src/logic/genome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Genome Genome::Replicate(Coefficient coef)
auto new_genom = genome;
auto new_args = args;

unsigned __int8 rand = random.Generate(100);
unsigned __int8 rand = random.Generate(1000);
// Is mutation be
if (rand < mutationChance)
{
Expand Down Expand Up @@ -177,10 +177,10 @@ Genome Genome::Replicate(Coefficient coef)
switch (coef)
{
case Coefficient::enlarge:
if (new_mutationChance > 0) new_mutationChance--;
if (new_mutationChance > 1) new_mutationChance--;
break;
case Coefficient::reduce:
if (new_mutationChance < 100) new_mutationChance++;
if (new_mutationChance < 1000) new_mutationChance++;
break;
case Coefficient::unchanged:
default:
Expand Down Expand Up @@ -239,7 +239,7 @@ void Genome::Construct()
species.g %= 255;
species.b %= 255;

double food_triggers = 0;
double food_source_triggers = 0;
for (unsigned __int8 i = 0; i < genome_size; i++)
{
Gen gen = Read();
Expand All @@ -250,23 +250,23 @@ void Genome::Construct()
// Acummulate ration
switch (gen.trigger)
{
case Operation::Carnivorous:
food_triggers++;
case Operation::Attack:
food_source_triggers++;
ration.r++;
break;
case Operation::Photosyntesis:
food_triggers++;
food_source_triggers++;
ration.g++;
break;
case Operation::EatOrganic:
food_triggers++;
food_source_triggers++;
ration.b++;
break;
}
}

// Hash ration
ration.r = (ration.r / food_triggers) * 255;
ration.g = (ration.g / food_triggers) * 255;
ration.b = (ration.b / food_triggers) * 255;
ration.r = (ration.r / food_source_triggers) * 255;
ration.g = (ration.g / food_source_triggers) * 255;
ration.b = (ration.b / food_source_triggers) * 255;
}
2 changes: 1 addition & 1 deletion src/logic/genome.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ namespace Ecosystem
Genome Replicate(Coefficient coef);
};
}
}
}
Loading

0 comments on commit 40546b5

Please sign in to comment.