Skip to content

Commit

Permalink
Merge pull request #64 from floralvikings/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
floralvikings committed Oct 19, 2013
2 parents a34adab + e313ab9 commit 43f35b9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class ExecutableForceStateMessage extends WorldClientExecutableMessage
private double absoluteAngle;
/** The time of the start of the server update during which the state was forced. */
private long timeOfForce;
/** The number of steps the player had taken when they were forced. */
private int stepsAtForce;

/**
* Construct an ExecutableMessage with the given Message.
Expand All @@ -36,8 +38,22 @@ public ExecutableForceStateMessage(WorldClient client, Message message)
public void runSynced()
{
ClientPlayer player = getClient().getPlayer();
int stepsTaken = (int) ((System.nanoTime() - timeOfForce) / (getClient().getPeriod() * 1000000));
player.forcePosition(vector2D, relativeAngle, absoluteAngle, stepsTaken);
double timeSinceForce = System.nanoTime() - timeOfForce;
double periodInNanos = getClient().getPeriod() * 1000000;
double stepsTaken = timeSinceForce / periodInNanos;

int intSteps = (int) stepsTaken;
/* The amount of "leftover" steps taken. */
double leftovers = stepsTaken - intSteps;

if (leftovers > 0.5)
{
intSteps++;
}

intSteps += stepsAtForce;

player.forcePosition(vector2D, relativeAngle, absoluteAngle, intSteps);
}

@Override
Expand All @@ -49,5 +65,6 @@ public void runASync()
relativeAngle = (double) getMessage().getArgument("direction");
absoluteAngle = (double) getMessage().getArgument("angle");
timeOfForce = (long) getMessage().getArgument("timeOfForce");
stepsAtForce = (int) getMessage().getArgument("stepsAtForce");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private Message generateForcedStateMessage()
forcedStateMessage.setArgument("angle", actor.getMoveAngle());
forcedStateMessage.setArgument("xCoordinate", actor.getVector2D().getXCoordinate());
forcedStateMessage.setArgument("zCoordinate", actor.getVector2D().getZCoordinate());
forcedStateMessage.setArgument("stepsAtForce", actor.getStepsTaken());
forcedStateMessage.setArgument("timeOfForce", server.getCycleStartTime());
return forcedStateMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jenjinstudios.message.Message;
import com.jenjinstudios.sql.WorldSQLHandler;
import com.jenjinstudios.world.Actor;
import com.jenjinstudios.world.InvalidLocationException;

/**
* Handles requests to login to the world.
Expand All @@ -16,6 +17,8 @@ public class ExecutableWorldLoginRequest extends WorldExecutableMessage
private final WorldSQLHandler sqlHandler;
/** The player added to the world. */
private Actor player;
/** The LoginResponse to send to the client. */
private Message loginResponse;

/**
* Construct a new ExecutableMessage. Must be implemented by subclasses.
Expand All @@ -33,7 +36,21 @@ public ExecutableWorldLoginRequest(WorldClientHandler handler, Message message)
public void runSynced()
{
if (player != null)
getClientHandler().getServer().getWorld().addObject(player);
{
try
{
getClientHandler().getServer().getWorld().addObject(player);
} catch (InvalidLocationException ex)
{
loginResponse.setArgument("success", false);
loginResponse.setArgument("id", -1);
loginResponse.setArgument("loginTime", getClientHandler().getLoggedInTime());
loginResponse.setArgument("xCoord", 0d);
loginResponse.setArgument("zCoord", 0d);
}
}
getClientHandler().queueMessage(loginResponse);

}

@Override
Expand All @@ -49,7 +66,7 @@ public void runASync()
boolean success = player != null;
getClientHandler().setLoginStatus(success);

Message loginResponse = new Message("WorldLoginResponse");
loginResponse = new Message("WorldLoginResponse");
loginResponse.setArgument("success", success);

if (success)
Expand All @@ -67,6 +84,6 @@ public void runASync()
loginResponse.setArgument("zCoord", 0d);
}

getClientHandler().queueMessage(loginResponse);

}
}
8 changes: 4 additions & 4 deletions jgsf/src/main/java/com/jenjinstudios/world/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ public void step()
private void tryStateChange()
{
if (nextState == null) return;
int overStepped = stepsTaken - nextState.stepsUntilChange;
if (overStepped >= MAX_CORRECT)
int overstepped = stepsTaken - nextState.stepsUntilChange;
if (overstepped >= MAX_CORRECT)
{
stopMaxCorrect();
} else if (overStepped >= 0)
} else if (overstepped >= 0)
{
doStateChange(overStepped);
doStateChange(overstepped);
}
}

Expand Down
12 changes: 6 additions & 6 deletions jgsf/src/main/java/com/jenjinstudios/world/Location.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jenjinstudios.world;

import java.util.ArrayList;
import java.util.TreeMap;
import java.util.HashSet;

/**
* Represents a location in the world's location grid.
Expand All @@ -17,7 +17,7 @@ public class Location
/** The z coordinate of the location in it's zone's grid. */
public final int Z_COORDINATE;
/** The objects residing in this location. */
private final TreeMap<Integer, WorldObject> objects;
private final HashSet<WorldObject> objects;
/** The property of this location. */
private Property property;

Expand All @@ -32,7 +32,7 @@ public Location(int x, int z)
X_COORDINATE = x;
Z_COORDINATE = z;
property = Property.OPEN;
objects = new TreeMap<>();
objects = new HashSet<>();
}

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public void setProperty(Property property)
*/
public ArrayList<WorldObject> getObjects()
{
return new ArrayList<>(objects.values());
return new ArrayList<>(objects);
}

/**
Expand All @@ -72,7 +72,7 @@ public ArrayList<WorldObject> getObjects()
*/
public void addObject(WorldObject object)
{
objects.put(object.getId(), object);
objects.add(object);
}

/**
Expand All @@ -82,7 +82,7 @@ public void addObject(WorldObject object)
*/
public void removeObject(WorldObject object)
{
objects.remove(object.getId());
objects.remove(object);
}

@Override
Expand Down
16 changes: 6 additions & 10 deletions jgsf/src/main/java/com/jenjinstudios/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.jenjinstudios.math.Vector2D;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Contains all the Zones, Locations and GameObjects.
Expand All @@ -13,10 +11,8 @@
*/
public class World
{
/** The Logger used for this class. */
private static final Logger LOGGER = Logger.getLogger(World.class.getName());
/** The size of the world's location grid. */
public final int SIZE = 10;
public final int SIZE = 50;
/** The grid of locations in the game world. */
private final Location[][] locationGrid;
/** The GameObjects contained in the world. */
Expand All @@ -38,15 +34,18 @@ public World()
* Add an object to the world.
*
* @param object The object to add.
*
* @throws InvalidLocationException If an object is attempted to be added with an invalid location.
*/
public void addObject(WorldObject object)
public void addObject(WorldObject object) throws InvalidLocationException
{
if (object == null)
throw new IllegalArgumentException("addObject(WorldObject obj) argument 0 not allowed to be null!");
object.setWorld(this);
object.setId(worldObjects.size());
object.setVector2D(object.getVector2D());
synchronized (worldObjects)
{
object.setId(worldObjects.size());
worldObjects.add(object);
}
objectCount++;
Expand Down Expand Up @@ -81,12 +80,9 @@ public Location getLocationForCoordinates(double x, double z) throws InvalidLoca
{
try
{
if (x < 0 || z < 0)
throw new InvalidLocationException(new Vector2D(x, z));
return locationGrid[(int) x / Location.SIZE][(int) z / Location.SIZE];
} catch (ArrayIndexOutOfBoundsException ex)
{
LOGGER.log(Level.SEVERE, "Attempted to access location with invalid coodinates: ({0}, {1})", new Object[]{x, z});
throw new InvalidLocationException(new Vector2D(x, z));
}
}
Expand Down
7 changes: 7 additions & 0 deletions jgsf/src/main/java/com/jenjinstudios/world/WorldObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Vector2D getVector2D()
* Set this object's current position.
*
* @param vector2D The new position.
*
* @throws InvalidLocationException If the supplied coordinates point to an invalid location.
*/
public void setVector2D(Vector2D vector2D) throws InvalidLocationException
Expand All @@ -100,6 +101,7 @@ public void setVector2D(Vector2D vector2D) throws InvalidLocationException
*
* @param x The new x coordinate.
* @param z The new z coordinate.
*
* @throws InvalidLocationException If the supplied coordinates indicate an invalid location.
*/
public void setVector2D(double x, double z) throws InvalidLocationException
Expand Down Expand Up @@ -184,4 +186,9 @@ public void update()
{

}

public String toString()
{
return name + ": " + id + " @ " + vector2D;
}
}
1 change: 1 addition & 0 deletions jgsf/src/main/resources/Messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<argument type="double" name="angle" />
<argument type="double" name="xCoordinate" />
<argument type="double" name="zCoordinate" />
<argument type="int" name="stepsAtForce" />
<argument type="long" name="timeOfForce" />
</message>

Expand Down

0 comments on commit 43f35b9

Please sign in to comment.