-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Radioactive Game Object onto Map #236
Draft
khalludi
wants to merge
11
commits into
simon987:master
Choose a base branch
from
khalludi:radioactive_gameobj
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e97770f
Skeleton setup + Tuple Impl
khalludi f526f36
Add getTiles() to calculate tiles between two coords
khalludi 72ea92f
Add Euclidean Distance calculation method from coords
khalludi a3c4c33
Add getAlphaCounts() to Radioactive interface
khalludi 3c16bd3
Implement RadiationDetector's handleInterrupt
khalludi 2aeca6c
Small cleanup
khalludi 0c3a3f0
Add Beta and Gamma particles; added suggestions
khalludi 3b12e2a
Set unique HWID
khalludi 678b56c
Add RadioactiveWorldUtils and refactor spacing in WorldUtils
khalludi a922560
Added Radioactive Obstacle to map with WorldCreationListener
khalludi 2ee98a8
Added getCount() method to World
khalludi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...adioactive Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadiationDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package net.simon987.pluginradioactivecloud; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.bson.Document; | ||
import net.simon987.server.assembly.HardwareModule; | ||
import net.simon987.server.assembly.Status; | ||
import net.simon987.server.game.objects.ControllableUnit; | ||
import net.simon987.server.game.objects.GameObject; | ||
import net.simon987.server.game.objects.Radioactive; | ||
|
||
public class RadiationDetector extends HardwareModule { | ||
|
||
/** | ||
* Should be unique and same as HWID | ||
*/ | ||
public static final int DEFAULT_ADDRESS = 0x000E; | ||
|
||
/** | ||
* Hardware ID (Should be unique) | ||
*/ | ||
public static final char HWID = 0x000E; | ||
|
||
/** | ||
* Radiation Constants | ||
*/ | ||
private static final int ALPHA_BLOCKED_VALUE = 5; | ||
private static final int BETA_BLOCKED_VALUE = 2; | ||
private static final int GAMMA_BLOCKED_VALUE = 1; | ||
|
||
/** | ||
* Helper class for getTiles | ||
*/ | ||
private class Tuple { | ||
public final int x; | ||
public final int y; | ||
|
||
public Tuple(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
|
||
/** | ||
* Finds the tiles between the two tiles located at the given coordinates. The | ||
* tiles located at the coordinates are not included in the list. | ||
* | ||
* @param x0 x-coordinate of first point | ||
* @param y0 y-coordinate of first point | ||
* @param x1 x-coordinate of second point | ||
* @param y1 y-coordinate of second point | ||
* @return List of tile coordinates. An empty list indicates tiles are next to | ||
* each other. | ||
*/ | ||
public ArrayList<Tuple> getTiles(int x0, int y0, int x1, int y1) { | ||
|
||
ArrayList<Tuple> ret = new ArrayList<>(); | ||
double slope; | ||
if (x1 > x0) { | ||
slope = (y1 - y0) / (double) (x1 - x0); | ||
} else { | ||
slope = (y0 - y1) / (double) (x0 - x1); | ||
|
||
// Swap values so that x0 < x1. This preps the following code where y is | ||
// determined by adding a step value (1) to x0 till it reaches x1. | ||
int tmp = x1; | ||
x1 = x0; | ||
x0 = tmp; | ||
|
||
tmp = y1; | ||
y1 = y0; | ||
y0 = tmp; | ||
} | ||
|
||
// If slope is zero or undefined, return tiles directly along the | ||
// appropriate cardinal direction. | ||
if (x0 == x1) { | ||
int smaller = Math.min(y0, y1); | ||
int larger = Math.max(y0, y1); | ||
System.out.printf("%d %d", smaller, larger); | ||
for (int i = smaller + 1; i < larger; i++) { | ||
ret.add(new Tuple(x0, i)); | ||
} | ||
} else if (y0 == y1) { | ||
int smaller = Math.min(x0, x1); | ||
int larger = Math.max(x0, x1); | ||
for (int i = smaller + 1; i < larger; i++) { | ||
ret.add(new Tuple(i, y0)); | ||
} | ||
} else { | ||
// Find all coordinates with 0.1 step | ||
int lastX = x0; | ||
int lastY = y0; | ||
for (int i = x0 * 10; i < x1 * 10; i += 1) { | ||
if (i / 10 != lastX || (int) (slope * i / 10) != lastY) { | ||
// Update last values | ||
lastX = i / 10; | ||
lastY = (int) (slope * i / 10); | ||
|
||
// Add new values to array | ||
ret.add(new Tuple(lastX, lastY)); | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/** | ||
* Finds the Euclidean Distance between two coordinates. | ||
* | ||
* @param x0 x-coordinate of first point | ||
* @param y0 y-coordinate of first point | ||
* @param x1 x-coordinate of second point | ||
* @param y1 y-coordinate of second point | ||
* @return distance between two points | ||
*/ | ||
public double getDistanceOfCoords(int x0, int y0, int x1, int y1) { | ||
return Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)); | ||
} | ||
|
||
public RadiationDetector(ControllableUnit unit) { | ||
super(null, unit); | ||
} | ||
|
||
public RadiationDetector(Document document, ControllableUnit cubot) { | ||
super(document, cubot); | ||
} | ||
|
||
@Override | ||
public void handleInterrupt(Status status) { | ||
|
||
// Find all game entities in world | ||
ArrayList<GameObject> entities = new ArrayList<>(unit.getWorld().getGameObjects()); | ||
|
||
// Check for alpha particles by finding Radioactive entities | ||
int alphaParticles = 0; | ||
int betaParticles = 0; | ||
int gammaParticles = 0; | ||
for (GameObject entity : entities) { | ||
if (entity instanceof Radioactive) { | ||
// Calculate distance between object and cubot | ||
double pathLength = getDistanceOfCoords(unit.getX(), unit.getY(), entity.getX(), entity.getY()); | ||
alphaParticles += ((Radioactive) entity).getAlphaCounts(pathLength); | ||
betaParticles += ((Radioactive) entity).getBetaCounts(pathLength); | ||
gammaParticles += ((Radioactive) entity).getGammaCounts(pathLength); | ||
|
||
// Get all tiles in between cubot and Radioactive entity | ||
ArrayList<Tuple> tiles = getTiles(unit.getX(), unit.getY(), entity.getX(), entity.getY()); | ||
for (Tuple tup : tiles) { | ||
// If intermediary tile is blocked, reduce alphaParticles by 5 | ||
if (unit.getWorld().isTileBlocked(tup.x, tup.y)) { | ||
alphaParticles -= ALPHA_BLOCKED_VALUE; | ||
betaParticles -= BETA_BLOCKED_VALUE; | ||
gammaParticles -= GAMMA_BLOCKED_VALUE; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Save Alpha Radioactive Particles to register A | ||
getCpu().getRegisterSet().getRegister("A").setValue(alphaParticles); | ||
|
||
// Save Beta Radioactive Particles to register B | ||
getCpu().getRegisterSet().getRegister("B").setValue(betaParticles); | ||
|
||
// Save Gamma Radioactive Particles to register C | ||
getCpu().getRegisterSet().getRegister("C").setValue(gammaParticles); | ||
} | ||
|
||
@Override | ||
public char getId() { | ||
return HWID; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...active Cloud/src/main/java/net/simon987/pluginradioactivecloud/RadioactiveWorldUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package net.simon987.pluginradioactivecloud; | ||
|
||
import net.simon987.server.game.world.TileMap; | ||
import net.simon987.server.game.world.TilePlain; | ||
import net.simon987.server.game.world.World; | ||
import org.bson.types.ObjectId; | ||
|
||
import java.awt.*; | ||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
public class RadioactiveWorldUtils { | ||
|
||
/** | ||
* Generate a list of biomass blobs for a world | ||
*/ | ||
public static ArrayList<RadioactiveObstacle> generateRadioactiveObstacles(World world, int minCount, int maxCount) { | ||
|
||
Random random = new Random(); | ||
int radioactiveObjCount = random.nextInt(maxCount - minCount) + minCount; | ||
ArrayList<RadioactiveObstacle> radioactiveObstacles = new ArrayList<>(radioactiveObjCount); | ||
|
||
// Count number of plain tiles. If there is less plain tiles than desired amount | ||
// of radioactive objects, set the desired amount of radioactive objects to the | ||
// plain tile count | ||
TileMap m = world.getTileMap(); | ||
int plainCount = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exact code block is used twice, we should extract it into its own function (e.g. WorldUtils.countTiles(int tileId)) |
||
for (int y = 0; y < world.getWorldSize(); y++) { | ||
for (int x = 0; x < world.getWorldSize(); x++) { | ||
|
||
if (m.getTileIdAt(x, y) == TilePlain.ID) { | ||
plainCount++; | ||
} | ||
} | ||
} | ||
|
||
if (radioactiveObjCount > plainCount) { | ||
radioactiveObjCount = plainCount; | ||
} | ||
|
||
outerLoop: for (int i = 0; i < radioactiveObjCount; i++) { | ||
|
||
Point p = m.getRandomTile(TilePlain.ID); | ||
if (p != null) { | ||
|
||
// Don't block worlds | ||
int counter = 0; | ||
while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 | ||
|| world.getGameObjectsAt(p.x, p.y).size() != 0) { | ||
p = m.getRandomTile(TilePlain.ID); | ||
counter++; | ||
|
||
if (counter > 25) { | ||
continue outerLoop; | ||
} | ||
} | ||
|
||
for (RadioactiveObstacle radioactiveObstacle : radioactiveObstacles) { | ||
if (radioactiveObstacle.getX() == p.x && radioactiveObstacle.getY() == p.y) { | ||
// There is already a blob here | ||
continue outerLoop; | ||
} | ||
} | ||
|
||
RadioactiveObstacle radioactiveObstacle = new RadioactiveObstacle(); | ||
radioactiveObstacle.setObjectId(new ObjectId()); | ||
radioactiveObstacle.setX(p.x); | ||
radioactiveObstacle.setY(p.y); | ||
radioactiveObstacle.setWorld(world); | ||
|
||
radioactiveObstacles.add(radioactiveObstacle); | ||
} | ||
} | ||
|
||
return radioactiveObstacles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RadioactiveObstacle