Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
Updates and bug fixes
Browse files Browse the repository at this point in the history
* Added life to bots.
* Fixed a Bug where Bots would freeze even though they have resources,
if there is no Resource Spawner to be seen.
* Fixed ConcurrentModificationExceptions occurring with the Evolution
class. (Specifically in the loop() method)
* Added some functionality to Spawnpoints and made them create
Bots/Structures at random (when they have enough Resources, of course).
  • Loading branch information
Zeale committed Mar 11, 2017
1 parent 924e000 commit c75c204
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
47 changes: 44 additions & 3 deletions src/zeale/evolution/Evolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -72,7 +73,7 @@ private Evolution() {
* Go figure...
*/
private void loop() {
long delta = System.nanoTime(), past = 1000000000 / 60;
long delta = System.nanoTime(), past = delta - 1000000000 / 60;
while (true) {
delta = System.nanoTime();
if (delta - past < 1000000000 / 60)
Expand All @@ -83,9 +84,21 @@ private void loop() {
for (final Structure s : structures)
if (s.isAlive())
s.work(delta - past);
if (!modificationStructsList.isEmpty())// Prevents
// ConcurrentModificationExceptions
{
structures.addAll(modificationStructsList);
modificationStructsList.clear();
}
for (final Bot b : bots)
if (b.isAlive())
b.work(delta - past);
if (!modificationBotsList.isEmpty())// Prevents
// ConcurrentModificationExceptions
{
bots.addAll(modificationBotsList);
modificationBotsList.clear();
}

}
past = delta;
Expand Down Expand Up @@ -115,12 +128,40 @@ private void render(final Graphics g) {
* Adds a {@link Bot} to the game.
*
* @param bot
* @return
* The {@link Bot} that will be added to the game.
* @return As specified in {@link LinkedList#add(Object)}.
*/
public boolean addBot(final Bot bot) {
return bots.add(bot);
return modificationBotsList.add(bot);
}

/**
* Adds a {@link Structure} to the game.
*
* @param struct
* The {@link Structure} that will be added to the game.
* @return As specified in {@link LinkedList#add(Object)}.
*/
public boolean addStruct(final Structure struct) {
return modificationStructsList.add(struct);
}

/**
* This List is used to prevent {@link ConcurrentModificationException}s
* from occurring. While iterating over {@link #structures},
* {@link #addStruct(Structure)} adds its objects here to prevent
* {@link ConcurrentModificationException}s caused by {@link #structures}.
*/
private LinkedList<Structure> modificationStructsList = new LinkedList<>();

/**
* This List is used to prevent {@link ConcurrentModificationException}s
* from occurring. While iterating over {@link #bots}, {@link #addBot(Bot)}
* adds its objects here to prevent {@link ConcurrentModificationException}s
* caused by {@link #bots}.
*/
private LinkedList<Bot> modificationBotsList = new LinkedList<>();

/**
* <p>
* <strong>A getter for the {@link Bot}s in game.</strong>
Expand Down
60 changes: 59 additions & 1 deletion src/zeale/evolution/bots/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,52 @@
*/
public class Bot extends Object {

/**
* The life that this {@link Bot} has remaining before it dies (in seconds).
*/
private double life = 100.0;

/**
* Adds life to this {@link Bot}. See {@link #life} for more details.
*
* @param life
* The amount of life to add to this {@link Bot}.
* @return The total amount of life that this {@link Bot} has left.
*/
public double addLife(double life) {
return this.life += life;
}

/**
* A getter for {@link Bot#life}.
*
* @return The life remaining for this {@link Bot}.
*/
public double getLife() {
return life;
}

/**
* Sets the amount of life that this {@link Bot} has left.
*
* @param life
* The amount of life that will be set to this {@link Bot}.
*/
public void setLife(double life) {
this.life = life;
}

/**
* Decrements the life of this {@link Bot} by the specified amount.
*
* @param life
* The amount of life to take from this {@link Bot}.
* @return The amount of life that this {@link Bot} has left over.
*/
public double decrementLife(double life) {
return this.life -= life;
}

/**
* The maximum amount of {@link Resource}s that this {@link Bot} can carry.
* (AKA its inventory size.)
Expand Down Expand Up @@ -186,6 +232,15 @@ public LinkedList<Resource> takeResources() {
@Override
public void work(final long delta) {
// Handle wait time...

if (life > 0) {
life -= Evolution.nanosecToSec(delta);
if (life <= 0) {
life = 0;
kill();
}
}

if (waitTime > 0) {
waitTime -= delta / 1000000;
if (waitTime < 0)
Expand All @@ -204,7 +259,10 @@ public void work(final long delta) {

// This happens if there are no Resource Spawners left.
if (target == null)
return;
if (resources.size() > 0)
target = Evolution.<Spawnpoint>getClosestStructure(this, Spawnpoint.class);
else
return;

// This is the distance between this bot and its target.
final double distance = Evolution.getDistance(this, target);
Expand Down
37 changes: 36 additions & 1 deletion src/zeale/evolution/structures/Spawnpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import zeale.evolution.Evolution;
import zeale.evolution.Object;
import zeale.evolution.bots.Bot;
import zeale.evolution.resources.Resource;
import zeale.evolution.structures.resourcespawners.ResourceSpawner;

public final class Spawnpoint extends Structure {

/**
* A private static random for wherever/whenever it's needed in this class.
*/
private static Random rand = new Random();

/**
* A {@link LinkedList} of the {@link Resource}s that this
* {@link Spawnpoint} currently holds.
Expand All @@ -35,7 +44,20 @@ public Spawnpoint(final double posx, final double posy) {

@Override
public void activate(final Bot bot) {
resources.addAll(bot.takeResources());
List<Resource> list = bot.takeResources();
for (Resource r : list)
bot.addLife(r.getValue() * 6);
resources.addAll(list);
}

public LinkedList<Resource> removeResources(int count) {
LinkedList<Resource> list = new LinkedList<>();
for (int i = 0; i < count; i++)
if (!resources.isEmpty())
list.add(resources.removeFirst());
else
return list;
return list;
}

@Override
Expand All @@ -53,6 +75,19 @@ public void render(final Graphics g) {

@Override
public void work(final long delta) {
if (resources.size() >= 15)
if (rand.nextInt(20) == 0) {
if (rand.nextBoolean())
Evolution.getCurrentInstance()
.addStruct(new ResourceSpawner(rand.nextDouble() * Evolution.calculateSize(1920, true),
rand.nextDouble() * Evolution.calculateSize(1080, false), (short) 50));
else
Evolution.getCurrentInstance()
.addBot(new Bot(rand.nextDouble() * Evolution.calculateSize(1920, true),
rand.nextDouble() * Evolution.calculateSize(1080, false), (short) 2));
removeResources(15);

}

}

Expand Down

0 comments on commit c75c204

Please sign in to comment.