diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-03-12 11:07:22 -0500 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-03-12 11:07:22 -0500 |
commit | 836f56a40fa4a2b8e68c23b2c299e50fefe0c15d (patch) | |
tree | 56ca750d4f2c93b5a72d52fd7c1d4b575b3c061a | |
parent | 8f5a73f1daba39c8b0175036cf688f8ce1399a8f (diff) |
-renamed constants to fit new datatypes
-reimplemented corpses
-implemented hunger and starving (if creature is starving they will eat corpses and will take damage)
-removed sides data from dna (think it is irrelavent)
-removed data from dna that is not used for plants
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | inc/constants.hpp | 39 | ||||
-rw-r--r-- | inc/dna.hpp | 3 | ||||
-rw-r--r-- | inc/organism.hpp | 2 | ||||
-rw-r--r-- | src/dna.cpp | 39 | ||||
-rw-r--r-- | src/list.cpp | 4 | ||||
-rw-r--r-- | src/organism.cpp | 40 | ||||
-rw-r--r-- | src/spritebatch.cpp | 5 |
8 files changed, 80 insertions, 60 deletions
@@ -5,10 +5,10 @@ apt install libsdl2-dev libglm-dev libglew-dev ideas - make code compilable on various distrobutions (maybe wangblows if not too big of changes) -- camera zoom -- collision -- carnivores / omnivores -- click to add entities +- camera zoom / movement +- maybe collision +- omnivores +- click to add new organisms - randomly generate new types of creatures - gui speed up / down - DNA mutations with normal distrobution diff --git a/inc/constants.hpp b/inc/constants.hpp index 49b7678..76a40c1 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -4,36 +4,43 @@ // General const int HERBAVORES = 100; const int CARNIVORES = 50;; -const int PLANTS = 5000; const int MINIMUM_PLANTS = 4000; +const int PLANTS = 5000; const int WINDOW_X = 1000; const int WINDOW_Y = 1000; +const float BOUNDS = 30; // Types -const int PLANT_TYPE = 1; -const int HERBAVORE_TYPE = 2; -const int CARNIVORE_TYPE = 3; - -// Creatures -const int CREATURE_MAX_HEALTH = 1000; -const int CREATURE_BEST_SENSE = 1.5; -const int CREATURE_BITE = 10; -const int CREATURE_EXP_PREG_TIME = 100; +const int HERBAVORE_TYPE = 1; +const int CARNIVORE_TYPE = 2; +const int PLANT_TYPE = 3; +const int CORPSE_TYPE = 4; + +// Creature stats +const int CREATURE_MAX_HEALTH = 100; +const int CREATURE_BEST_SENSE = 2; +const int CREATURE_BITE = 5; +const int CREATURE_EXP_PREG_TIME = 1000; const int CREATURE_EXP_AGE = 10000; -const float CREATURE_SPEED = .1; +const int CREATURE_HUNGRY_AMOUNT = 50; +const int CREATURE_STARVE_AMOUNT = 90; +const float CREATURE_SPEED = .05; const float CREATURE_REACH = .1; const float CREATURE_MUTATION_PERCENT = .25; const float CREATURE_MUTATION_CHANCE = .05; -// Resource -const int RESOURCE_MAX_HEALTH = 200; -const int RESOURCE_GROW_AMOUNT = 1; +// Plant Stats +const int PLANT_MAX_HEALTH = 200; +const int PLANT_GROW_AMOUNT = 1; + +// Corpse Stats +const int CORPSE_MAX_HEALTH = 400; +const int CORPSE_DECAY_AMOUNT = -1; // Opengl const int NUM_SHADERS = 3; const int NUM_UNIFORMS = 3; -const float CREATURE_SIDES = 4; -const float RESOURCE_SIDES = 10; +const float SIDES = 5; // Quadtree const int MAX_OBJECTS = 5; diff --git a/inc/dna.hpp b/inc/dna.hpp index 01f373b..11886e9 100644 --- a/inc/dna.hpp +++ b/inc/dna.hpp @@ -22,6 +22,8 @@ class DNA int expectedPregnancyTime; int expectedAge; int growAmount; + int hungryAmount; + int starveAmount; float reach; float speed; @@ -29,7 +31,6 @@ class DNA float mutationChance; struct Visuals{ - float sides; float red; float green; float blue; diff --git a/inc/organism.hpp b/inc/organism.hpp index 2a7a67c..954d25f 100644 --- a/inc/organism.hpp +++ b/inc/organism.hpp @@ -46,7 +46,9 @@ class Organism int health; int pregnancyTime; int age; + int hunger; + bool starving; bool gender; bool pregnate; bool hungry; diff --git a/src/dna.cpp b/src/dna.cpp index 4e22d7a..dd1d606 100644 --- a/src/dna.cpp +++ b/src/dna.cpp @@ -10,52 +10,50 @@ DNA::DNA(std::string s) bite = CREATURE_BITE; expectedPregnancyTime = CREATURE_EXP_PREG_TIME; expectedAge = CREATURE_EXP_AGE; - growAmount = 0; + hungryAmount = CREATURE_HUNGRY_AMOUNT; + starveAmount = CREATURE_STARVE_AMOUNT; reach = CREATURE_REACH; speed = CREATURE_SPEED; mutationPercent = CREATURE_MUTATION_PERCENT; mutationChance = CREATURE_MUTATION_CHANCE; - appearance.sides = CREATURE_SIDES; appearance.red = 0; - appearance.green = 0.5; + appearance.green = 1; appearance.blue = 1; } else if(s == "carnivore"){ type = CARNIVORE_TYPE; eatType = HERBAVORE_TYPE; - maxHealth = CREATURE_MAX_HEALTH * 2; + maxHealth = CREATURE_MAX_HEALTH; bestSense = CREATURE_BEST_SENSE * 2; bite = CREATURE_BITE * 2; expectedPregnancyTime = CREATURE_EXP_PREG_TIME; expectedAge = CREATURE_EXP_AGE * 2; - growAmount = 0; + hungryAmount = CREATURE_HUNGRY_AMOUNT; + starveAmount = CREATURE_STARVE_AMOUNT; reach = CREATURE_REACH * 2; speed = CREATURE_SPEED * 2; mutationPercent = CREATURE_MUTATION_PERCENT; mutationChance = CREATURE_MUTATION_CHANCE; - appearance.sides = CREATURE_SIDES; appearance.red = 1; appearance.green = 0; - appearance.blue = 0; + appearance.blue = 1; } else if(s == "plant"){ type = PLANT_TYPE; - eatType = 0; - maxHealth = RESOURCE_MAX_HEALTH; - bestSense = 0; - bite = 0; - expectedPregnancyTime = 0; - expectedAge = 0; - growAmount = RESOURCE_GROW_AMOUNT; - reach = 0; - speed = 0; - mutationPercent = 0; - mutationChance = 0; - appearance.sides = RESOURCE_SIDES; + maxHealth = PLANT_MAX_HEALTH; + growAmount = PLANT_GROW_AMOUNT; appearance.red = 0; appearance.green = 1; appearance.blue = 0; } + else if(s == "corpse"){ + type = CORPSE_TYPE; + maxHealth = CORPSE_MAX_HEALTH; + growAmount = CORPSE_DECAY_AMOUNT; + appearance.red = 0; + appearance.green = 0; + appearance.blue = 1; + } } DNA DNA::combine(DNA D) @@ -69,11 +67,12 @@ DNA DNA::combine(DNA D) N.expectedPregnancyTime = (expectedPregnancyTime + D.expectedPregnancyTime)/2; N.expectedAge = (expectedAge + D.expectedAge)/2; N.growAmount = (growAmount + D.growAmount)/2; + N.hungryAmount = (hungryAmount + D.hungryAmount)/2; + N.starveAmount = (starveAmount + D.starveAmount)/2; N.reach = (reach + D.reach)/2; N.speed = (speed + D.speed)/2; N.mutationPercent = (mutationPercent + D.mutationPercent)/2; N.mutationChance = (mutationChance + D.mutationChance)/2; - N.appearance.sides = CREATURE_SIDES; N.appearance.red = appearance.red; N.appearance.green = appearance.green; N.appearance.blue = appearance.blue; diff --git a/src/list.cpp b/src/list.cpp index 9d32b21..0719ff0 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -22,7 +22,7 @@ List::List() resources.push_back(X); } - Rectangle R1 = Rectangle(0,0,60,60); + Rectangle R1 = Rectangle(0,0,BOUNDS*2,BOUNDS*2); tree = Quadtree(0, R1); } @@ -30,7 +30,7 @@ void List::Remove() { for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++) if(it->getHealth()<=0){ - DNA d = DNA("plant"); + DNA d = DNA("corpse"); Organism X(it->getRectangle(), d); resources.push_back(X); creatures.erase(it--); diff --git a/src/organism.cpp b/src/organism.cpp index 93e2041..c3ec992 100644 --- a/src/organism.cpp +++ b/src/organism.cpp @@ -6,14 +6,16 @@ Organism::Organism(Rectangle r, DNA d) myDNA = d; if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(30); - rect.y = getRandom(30); + rect.x = getRandom(BOUNDS); + rect.y = getRandom(BOUNDS); } + hunger = 50; health = myDNA.maxHealth/2; gender = rand() % 2; age = 0; pregnancyTime = 0; + able = false; pregnancyReady = false; pregnate = false; hasTarget = false; @@ -22,8 +24,6 @@ Organism::Organism(Rectangle r, DNA d) void Organism::Behavior() { - health-=1; - this->Priority(); if(!hasTarget) @@ -42,17 +42,30 @@ void Organism::Behavior() age++; if(age > myDNA.expectedAge) health = 0; + + hunger++; + if(starving) + health-=1; + if(able) + health+=1; } void Organism::Priority() { - if(health < myDNA.maxHealth / 2){ - hungry = true; - able = false; + if(hunger > myDNA.hungryAmount){ + starving=false; + hungry = true; + able = false; + } + else if(hunger > myDNA.starveAmount){ + hungry = true; + starving= true; + able = false; } else{ - hungry = false; - able = true; + hungry = false; + starving= false; + able = true; } } @@ -61,7 +74,7 @@ void Organism::setTarget() std::random_shuffle(nearMe.begin(),nearMe.end()); for(std::vector<Organism*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ - if( (*it)->getType() == myDNA.eatType && hungry){ + if( ((*it)->getType() == myDNA.eatType && hungry) || ((*it)->getType() == CORPSE_TYPE && starving) ){ target = *it; hasTarget = true; wander = false; @@ -77,7 +90,7 @@ void Organism::setTarget() if(!hasTarget&&!wander){ wander = true; - Rectangle tmp(getRandom(30),getRandom(30),0,0); + Rectangle tmp(getRandom(BOUNDS),getRandom(BOUNDS),0,0); wTarget = tmp; } } @@ -95,9 +108,9 @@ void Organism::Action() { if(hasTarget){ if(Distance(rect,target->getRectangle()) < myDNA.reach){ - if(target->getType() == myDNA.eatType){ + if( (target->getType() == myDNA.eatType) || (target->getType() == CORPSE_TYPE) ){ target->takeBite(myDNA.bite); - health+=myDNA.bite; + hunger-=myDNA.bite; if(target->getHealth()<=0) hasTarget = false; } @@ -107,7 +120,6 @@ void Organism::Action() } hasTarget = false; } - } else moveTowards(target->getRectangle()); diff --git a/src/spritebatch.cpp b/src/spritebatch.cpp index c756494..5b45e43 100644 --- a/src/spritebatch.cpp +++ b/src/spritebatch.cpp @@ -10,7 +10,6 @@ void SpriteBatch::init() void SpriteBatch::begin()
{
_renderBatches.clear();
-
// Makes _glpyhs.size() == 0, however it does not free internal memory.
// So when we later call emplace_back it doesn't need to internally call new.
_gfx.clear();
@@ -67,7 +66,7 @@ void SpriteBatch::createRenderBatches() vertices[cv++] = _gfxPtr[0]->second.red;
vertices[cv++] = _gfxPtr[0]->second.green;
vertices[cv++] = _gfxPtr[0]->second.blue;
- vertices[cv++] = _gfxPtr[0]->second.sides;
+ vertices[cv++] = SIDES;
offset += 6;
@@ -87,7 +86,7 @@ void SpriteBatch::createRenderBatches() vertices[cv++] = _gfxPtr[cg]->second.red;
vertices[cv++] = _gfxPtr[cg]->second.green;
vertices[cv++] = _gfxPtr[cg]->second.blue;
- vertices[cv++] = _gfxPtr[cg]->second.sides;
+ vertices[cv++] = SIDES;
offset += 6;
}
|