Skip to content

Commit

Permalink
Actors will now keep track of their parents and children. (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccoystephenson authored Apr 3, 2022
1 parent 04fe05b commit b879a41
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void execute(Actor actor, Actor other) {
if (!actor.isFriend(other)) {
return;
}
Actor offspring = ActorFactory.getInstance().createActor();
Actor offspring = ActorFactory.getInstance().createActorFromParents(actor, other);
World world = actor.getWorld();
boolean success = PersistentData.getInstance().placeIntoEnvironment(world, offspring); // TODO: place in same square as parents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean execute(CommandSender sender, String[] args) {
String name = doubleQuoteArgs.get(1);

if (type.equalsIgnoreCase("actor")) {
ActorFactory.getInstance().createActor(name);
ActorFactory.getInstance().createActorWithRandomName(name);
sender.sendMessage("Actor created.");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean execute(CommandSender sender) {
return false;
}
for (String name : names) {
ActorFactory.getInstance().createActor(name);
ActorFactory.getInstance().createActorWithRandomName(name);
Actor actor;
try {
actor = PersistentData.getInstance().getActor(name);
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/dansapps/interakt/factories/ActorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static ActorFactory getInstance() {
return instance;
}

public Actor createActor(String name) {
public Actor createActorWithRandomName(String name) {
if (isNameTaken(name)) {
return null;
}
Expand All @@ -37,15 +37,24 @@ public Actor createActor(String name) {
return actor;
}

public Actor createActor() {
public Actor createActorFromParents(Actor parent1, Actor parent2) {
Actor child = createActorWithRandomName();
child.addParent(parent1.getUUID());
child.addParent(parent2.getUUID());
parent1.addChild(child.getUUID());
parent2.addChild(child.getUUID());
return child;
}

public Actor createActorWithRandomName() {
String name;
do {
name = generateRandomString(5);
} while (isNameTaken(name));
return createActor(name);
return createActorWithRandomName(name);
}

public void createActor(Map<String, String> data) {
public void createActorWithRandomName(Map<String, String> data) {
Actor actor = new Actor(data);
PersistentData.getInstance().addActor(actor);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dansapps/interakt/misc/CONFIG.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public class CONFIG {
public static final int MAX_CHANCE_TO_MOVE = 25;
public static final int MAX_CHANCE_TO_BEFRIEND = 50;
public static final int MAX_CHANCE_TO_ATTACK = 50;
public static final int MAX_CHANCE_TO_REPRODUCE = 50;
public static final int MAX_CHANCE_TO_REPRODUCE = 10;
public static final int SECONDS_BETWEEN_AUTO_SAVES = 300;
public static final double MAX_HEALTH = 100;
public static final double MAX_DAMAGE = 50;
public static final GRID_DISPLAY_TYPE DISPLAY_TYPE = GRID_DISPLAY_TYPE.NUMBER_OF_ENTITIES;
public static final boolean SHOW_LINEAGE_INFO = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dansapps.interakt.objects;

import preponderous.environmentlib.abs.objects.Entity;

import java.util.HashSet;
import java.util.UUID;

/**
* @author Daniel McCoy Stephenson
*/
public abstract class AbstractFamilialEntity extends Entity {
protected HashSet<UUID> parentIDs = new HashSet<>();
protected HashSet<UUID> childIDs = new HashSet<>();

public AbstractFamilialEntity(String name) {
super(name);
}

public HashSet<UUID> getParentUUIDs() {
return parentIDs;
}

public boolean addParent(UUID uuid) {
return parentIDs.add(uuid);
}

public boolean removeParent(UUID uuid) {
return parentIDs.remove(uuid);
}

public HashSet<UUID> getChildUUIDs() {
return childIDs;
}

public boolean addChild(UUID uuid) {
return childIDs.add(uuid);
}

public boolean removeChild(UUID uuid) {
return childIDs.remove(uuid);
}

public String getParentsUUIDsSeparatedByCommas() {
String toReturn = "";
int count = 0;
for (UUID uuid : parentIDs) {
toReturn = toReturn + uuid.toString();
count++;
if (count != parentIDs.size()) {
toReturn = toReturn + ", ";
}
}
return toReturn;
}

public String getChildrenUUIDsSeparatedByCommas() {
String toReturn = "";
int count = 0;
for (UUID uuid : childIDs) {
toReturn = toReturn + uuid.toString();
count++;
if (count != childIDs.size()) {
toReturn = toReturn + ", ";
}
}
return toReturn;
}
}
53 changes: 50 additions & 3 deletions src/main/java/dansapps/interakt/objects/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Daniel McCoy Stephenson
* @since January 7th, 2022
*/
public class Actor extends Entity implements Savable {
public class Actor extends AbstractFamilialEntity implements Savable {
private final LinkedList<ActionRecord> actionRecords = new LinkedList<>();
private int chanceToMove;
private int chanceToBefriend;
Expand Down Expand Up @@ -221,9 +221,43 @@ public boolean isDead() {
return getHealth() <= 0;
}

private String getParentNamesSeparatedByCommas() {
String toReturn = "";
int count = 0;
for (UUID uuid : parentIDs) {
Actor actor = PersistentData.getInstance().getActor(uuid);
if (actor == null) {
continue;
}
toReturn = toReturn + actor.getName();
count++;
if (count != parentIDs.size()) {
toReturn = toReturn + ", ";
}
}
return toReturn;
}

private String getChildrenNamesSeparatedByCommas() {
String toReturn = "";
int count = 0;
for (UUID uuid : childIDs) {
Actor actor = PersistentData.getInstance().getActor(uuid);
if (actor == null) {
continue;
}
toReturn = toReturn + actor.getName();
count++;
if (count != childIDs.size()) {
toReturn = toReturn + ", ";
}
}
return toReturn;
}

@Override
public String toString() {
return "=== Details of " + getName() + " ===" + "\n" +
String toReturn = "=== Details of " + getName() + " ===" + "\n" +
"Health: " + getHealth() + "/" + getMaxHealth() + "\n" +
getWorldInfo() + "\n" +
getSquareInfo() + "\n" +
Expand All @@ -234,7 +268,16 @@ public String toString() {
"Chance to reproduce: " + getChanceToReproduce() + "\n" +
"Num times moved: " + getNumTimesMoved() + "\n" +
"Num squares explored: " + exploredSquares.size() + "\n" +
"Num friends: " + friends.size();
"Num friends: " + friends.size() + "\n";
if (CONFIG.SHOW_LINEAGE_INFO) {
if (parentIDs.size() > 0) {
toReturn += "Parents: " + getParentNamesSeparatedByCommas();
}
if (childIDs.size() > 0) {
toReturn += "Children: " + getChildrenNamesSeparatedByCommas();
}
}
return toReturn;
}

@Override
Expand All @@ -254,6 +297,8 @@ public Map<String, String> save() {
saveMap.put("chanceToBefriend", gson.toJson(chanceToBefriend));
saveMap.put("chanceToAttack", gson.toJson(chanceToAttack));
saveMap.put("chanceToReproduce", gson.toJson(chanceToReproduce));
saveMap.put("parentIDs", gson.toJson(parentIDs));
saveMap.put("childIDs", gson.toJson(childIDs));

return saveMap;
}
Expand All @@ -277,6 +322,8 @@ public void load(Map<String, String> data) {
chanceToBefriend = Integer.parseInt(gson.fromJson(data.get("chanceToBefriend"), String.class));
chanceToAttack = Integer.parseInt(gson.fromJson(data.get("chanceToAttack"), String.class));
chanceToReproduce = Integer.parseInt(gson.fromJson(data.get("chanceToReproduce"), String.class));
parentIDs = gson.fromJson(data.get("parentIDs"), hashsetTypeUUID);
childIDs = gson.fromJson(data.get("childIDs"), hashsetTypeUUID);
}
catch(Exception e) {
Logger.getInstance().logError("Something went wrong loading an actor.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void loadActors() {
PersistentData.getInstance().getActors().clear();
ArrayList<HashMap<String, String>> data = jsonWriterReader.loadDataFromFilename(FILE_PATH + ACTORS_FILE_NAME);
for (Map<String, String> actorData : data){
ActorFactory.getInstance().createActor(actorData);
ActorFactory.getInstance().createActorWithRandomName(actorData);
}
}

Expand Down

0 comments on commit b879a41

Please sign in to comment.