Ticket #154: additional parameters for CarController.poll and added CarController.getLastSignal.patch
File additional parameters for CarController.poll and added CarController.getLastSignal.patch, 17.0 KB (added by , 16 years ago) |
---|
-
src/de/jacavi/appl/racelogic/RaceEngine.java
36 36 37 37 private int raceTimerInterval = 0; 38 38 39 boolean isTimerRunning = false;39 private boolean isTimerRunning = false; 40 40 41 41 private Track track; 42 42 43 43 private List<Player> players; 44 44 45 private RaceEngine() {}46 47 45 public void setPlayers(List<Player> players) { 48 46 this.players = players; 49 47 } … … 144 142 // get players hal connector 145 143 SlotCarSystemConnector slotCarSystemConnector = player.getSlotCarSystemConnector(); 146 144 // get players controller signal 147 ControllerSignal controllerSignal = carController.poll( );145 ControllerSignal controllerSignal = carController.poll(player, players, track); 148 146 // get the tda 149 147 TrackDataApproximator tda = player.getTda(); 150 148 -
src/de/jacavi/appl/controller/device/impl/MouseDevice.java
1 1 package de.jacavi.appl.controller.device.impl; 2 2 3 import java.util.List; 4 3 5 import org.eclipse.swt.SWT; 4 6 import org.eclipse.swt.widgets.Display; 5 7 import org.eclipse.swt.widgets.Event; … … 7 9 8 10 import de.jacavi.appl.controller.ControllerSignal; 9 11 import de.jacavi.appl.controller.device.DeviceController; 12 import de.jacavi.appl.racelogic.Player; 13 import de.jacavi.appl.track.Track; 10 14 11 15 12 16 … … 58 62 } 59 63 60 64 @Override 61 public ControllerSignal poll( ) {65 public ControllerSignal poll(Player you, List<Player> others, Track track) { 62 66 return currentControllerSignal; 63 67 } 64 68 -
src/de/jacavi/appl/controller/device/impl/GameControllerDevice.java
1 1 package de.jacavi.appl.controller.device.impl; 2 2 3 import java.util.List; 4 3 5 import com.centralnexus.input.Joystick; 4 6 5 7 import de.jacavi.appl.controller.ControllerSignal; 6 8 import de.jacavi.appl.controller.device.DeviceController; 7 9 import de.jacavi.appl.controller.device.impl.GameControllerDeviceManager.GameControllerDescriptor; 10 import de.jacavi.appl.racelogic.Player; 11 import de.jacavi.appl.track.Track; 8 12 9 13 10 14 … … 21 25 } 22 26 23 27 @Override 24 public ControllerSignal poll( ) {28 public ControllerSignal poll(Player you, List<Player> others, Track track) { 25 29 device.poll(); 26 30 27 31 int speed = normaliseSpeedSignal(device.getY()); -
src/de/jacavi/appl/controller/device/impl/WiimoteDevice.java
1 1 package de.jacavi.appl.controller.device.impl; 2 2 3 import java.util.List; 4 3 5 import org.apache.log4j.Logger; 4 6 5 7 import wiiusej.Wiimote; … … 18 20 import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent; 19 21 import de.jacavi.appl.controller.ControllerSignal; 20 22 import de.jacavi.appl.controller.device.DeviceController; 23 import de.jacavi.appl.racelogic.Player; 24 import de.jacavi.appl.track.Track; 21 25 import de.jacavi.rcp.util.Check; 22 26 23 27 … … 59 63 } 60 64 61 65 @Override 62 public ControllerSignal poll( ) {66 public ControllerSignal poll(Player you, List<Player> others, Track track) { 63 67 return currentControllerSignal; 64 68 } 65 69 -
src/de/jacavi/appl/controller/device/impl/KeyboardDevice.java
1 1 package de.jacavi.appl.controller.device.impl; 2 2 3 import java.util.List; 4 3 5 import org.eclipse.swt.SWT; 4 6 import org.eclipse.swt.widgets.Display; 5 7 import org.eclipse.swt.widgets.Event; … … 7 9 8 10 import de.jacavi.appl.controller.ControllerSignal; 9 11 import de.jacavi.appl.controller.device.DeviceController; 12 import de.jacavi.appl.racelogic.Player; 13 import de.jacavi.appl.track.Track; 10 14 11 15 12 16 … … 50 54 } 51 55 52 56 @Override 53 public ControllerSignal poll( ) {57 public ControllerSignal poll(Player you, List<Player> others, Track track) { 54 58 ControllerSignal retVal = currentControllerSignal; 55 59 currentControllerSignal = new ControllerSignal(currentControllerSignal.getThrust(), currentControllerSignal 56 60 .isTrigger()); -
src/de/jacavi/appl/controller/device/impl/MouseKeyboardDeviceAdapter.java
1 1 package de.jacavi.appl.controller.device.impl; 2 2 3 import java.util.List; 4 3 5 import de.jacavi.appl.controller.ControllerSignal; 4 6 import de.jacavi.appl.controller.device.DeviceController; 7 import de.jacavi.appl.racelogic.Player; 8 import de.jacavi.appl.track.Track; 5 9 6 10 7 11 … … 35 39 } 36 40 37 41 @Override 38 public ControllerSignal poll( ) {39 ControllerSignal mouse = mouseDevice.poll( );40 ControllerSignal keyboard = keyboardDevice.poll( );42 public ControllerSignal poll(Player you, List<Player> others, Track track) { 43 ControllerSignal mouse = mouseDevice.poll(you, others, track); 44 ControllerSignal keyboard = keyboardDevice.poll(you, others, track); 41 45 return new ControllerSignal(mouse.getThrust(), mouse.isTrigger(), keyboard.isSwitchFrontLight(), keyboard 42 46 .isSwitchBackLight(), mouse.isReset()); 43 47 } -
src/de/jacavi/appl/controller/CarController.java
1 1 package de.jacavi.appl.controller; 2 2 3 import java.util.List; 3 4 import java.util.UUID; 4 5 6 import de.jacavi.appl.racelogic.Player; 7 import de.jacavi.appl.track.Track; 8 5 9 6 10 7 11 public abstract class CarController implements Comparable<CarController> { … … 14 18 this.name = name; 15 19 } 16 20 17 abstract public ControllerSignal poll( );21 abstract public ControllerSignal poll(Player you, List<Player> others, Track track); 18 22 19 23 public void cleanup() { 20 24 deactivate(); … … 54 58 public void deactivate() {} 55 59 56 60 /** 57 * Reset resets the state of a Controller without deactivating its listeners. Currently it is called in the RaceEngine 58 * if ControllerSignal.isReset() to make sure the Controller has the same state as a freshly activated one. 61 * Reset resets the state of a Controller without deactivating its listeners. Currently it is called in the 62 * RaceEngine if ControllerSignal.isReset() to make sure the Controller has the same state as a freshly activated 63 * one. 59 64 * <p> 60 65 * We need the reset because some Controllers save their thrust in state. 61 66 */ 62 67 public void reset() {} 63 68 69 /** 70 * Returns the last signal that was poll()ed. 71 * <p> 72 * The difference to poll() is that this should not actually communicate in any way with the hardware. It's 73 * primarily used by the TrackWidget to draw the thrust gauges. 74 * <p> 75 * The default implementation just returns the result of poll() with null arguments. When in doubt, overwrite it! 76 * <p> 77 * IMPORTANT NOTE: This might actually be called *before* the first poll()! 78 */ 79 public ControllerSignal getLastSignal() { 80 return poll(null, null, null); 81 } 82 64 83 } -
src/de/jacavi/appl/controller/agent/DrivingAgent.java
1 1 package de.jacavi.appl.controller.agent; 2 2 3 import java.util.List; 4 3 5 import de.jacavi.appl.controller.ControllerSignal; 6 import de.jacavi.appl.racelogic.Player; 7 import de.jacavi.appl.track.CarPosition; 8 import de.jacavi.appl.track.Track; 4 9 5 10 6 11 7 12 public interface DrivingAgent { 8 ControllerSignal poll( /*CarPosition you, CarPosition[] others, Track track*/);13 ControllerSignal poll(CarPosition you, List<Player> others, Track track); 9 14 } -
src/de/jacavi/appl/controller/agent/ScriptController.java
4 4 import java.io.PrintWriter; 5 5 import java.io.StringWriter; 6 6 import java.io.Writer; 7 import java.util.List; 7 8 8 9 import de.jacavi.appl.controller.ControllerSignal; 10 import de.jacavi.appl.racelogic.Player; 11 import de.jacavi.appl.track.Track; 9 12 import de.jacavi.rcp.util.ExceptionHandler; 10 13 11 14 12 15 13 16 abstract public class ScriptController extends DrivingAgentController { 17 14 18 @SuppressWarnings("serial") 15 19 public class ScriptExecutionException extends Exception { 16 20 public ScriptExecutionException(String message) { … … 22 26 23 27 private DrivingAgent scriptObject; 24 28 29 private ControllerSignal lastSignal = new ControllerSignal(); 30 25 31 public ScriptController(String name, File agentFile) { 26 32 super(name); 27 33 this.agentFile = agentFile; … … 31 37 return agentFile; 32 38 } 33 39 40 // @SuppressWarnings("unchecked") 34 41 @Override 35 public ControllerSignal poll( ) {42 public ControllerSignal poll(Player you, List<Player> others, Track track) { 36 43 if(scriptObject == null) 37 44 return null; 38 else 39 return scriptObject.poll(/*null, null, null*/); 45 else { 46 // determine the arguments to pass to the script 47 /*List<Player> players = (List<Player>) ContextLoader.getBean("playersBean"); 48 Player you = null; 49 List<Player> others = new ArrayList<Player>(); 50 for(Player p: players) { 51 if(p.getController() == this) { 52 assert you == null: "Error: this controller was apparently assigned to multiple players"; 53 you = p; 54 } else 55 others.add(p); 56 }*/ 57 lastSignal = scriptObject.poll(you.getPosition(), others, track); 58 return lastSignal; 59 } 60 } 61 62 @Override 63 public ControllerSignal getLastSignal() { 64 return lastSignal; 40 65 } 41 66 42 67 @Override … … 61 86 62 87 public ControllerSignal dryRun() throws ScriptExecutionException { 63 88 try { 64 return parseScript().poll( );89 return parseScript().poll(null, null, null); 65 90 } catch(Exception exc) { 66 91 final Writer writer = new StringWriter(); 67 92 exc.printStackTrace(new PrintWriter(writer)); -
src/de/jacavi/appl/controller/agent/impl/XmlRpcController.java
1 1 package de.jacavi.appl.controller.agent.impl; 2 2 3 import java.util.List; 4 3 5 import org.apache.xmlrpc.XmlRpcException; 4 6 import org.apache.xmlrpc.XmlRpcRequest; 5 7 import org.apache.xmlrpc.server.PropertyHandlerMapping; … … 10 12 11 13 import de.jacavi.appl.controller.ControllerSignal; 12 14 import de.jacavi.appl.controller.agent.ExternalController; 15 import de.jacavi.appl.racelogic.Player; 16 import de.jacavi.appl.track.Track; 13 17 import de.jacavi.rcp.util.ExceptionHandler; 14 18 15 19 … … 149 153 } 150 154 151 155 @Override 152 public ControllerSignal poll( ) {156 public ControllerSignal poll(Player you, List<Player> others, Track track) { 153 157 return controllerSignal; 154 158 } 155 159 -
src/de/jacavi/rcp/dlg/InputDeviceSettingsDialog.java
93 93 @Override 94 94 public void run() { 95 95 if(!progressBar.isDisposed() && carControllerManager.isIdValid(deviceID)) { 96 ControllerSignal signal = carControllerManager.getDevice(deviceID).poll( );96 ControllerSignal signal = carControllerManager.getDevice(deviceID).poll(null, null, null); 97 97 progressBar.setSelection(signal.getThrust()); 98 98 progressBar.setForeground(signal.isTrigger() ? Display.getDefault().getSystemColor( 99 99 SWT.COLOR_DARK_MAGENTA) : null); -
src/de/jacavi/rcp/widgets/TrackWidget.java
1046 1046 boolean isOnTrack = p.getPosition().isOnTrack; 1047 1047 CarController dc = p.getController(); 1048 1048 String name = p.getName(); 1049 ControllerSignal signal = dc. poll();1049 ControllerSignal signal = dc.getLastSignal(); 1050 1050 if(signal != null) { 1051 1051 int thrust = isOnTrack ? signal.getThrust() : 100; 1052 1052 boolean isTriggered = isOnTrack ? signal.isTrigger() : false; -
agents/SampleAgent.groovy
1 // SampleAgent: 2 // A very simple Groovy driving agent that alternates 3 // between a faster and a slower mode every 100 gameticks. 4 5 6 import java.util.List 1 7 import de.jacavi.appl.controller.agent.DrivingAgent 2 8 import de.jacavi.appl.controller.ControllerSignal 9 import de.jacavi.appl.track.CarPosition 10 import de.jacavi.appl.track.Track 3 11 4 12 public class SampleAgent implements DrivingAgent { 5 13 private int counter = 0 6 14 7 public ControllerSignal poll( ) {15 public ControllerSignal poll(CarPosition you, List others, Track track) { 8 16 counter++ 9 17 if(((counter / 100) as int) % 2 == 1) { 10 18 return new ControllerSignal(35, true) -
agents/TrackAwareAgent.py
1 # TrackAwareAgent: 2 # A Jython driving agent that detects the tile he's 3 # currently on and goes 60% thrust on straight tiles 4 # and 30% thrust on curvy tiles. 5 6 class MyAgent(DrivingAgent): 7 def __init__(self): 8 self.counter = 0 9 10 def poll(self, you, others, track): 11 if you and track: # during a race 12 13 # determine the section I'm on 14 section = you.trackSectionIndex 15 16 # which tile is that? 17 tile = track.getSections().get(section).getTile() 18 19 # if the entry-to-exit angle is 0, full speed 20 if tile.getEntryToExitAngle().angle == 0: 21 return ControllerSignal(60, False) 22 23 # if it's a turn tile, slow down 24 else: 25 return ControllerSignal(30, False) 26 27 else: # for the dry run 28 return ControllerSignal(10, True) -
agents/SampleAgent.py
1 #import de.jacavi.appl.controller.agent.DrivingAgent 2 #import de.jacavi.appl.controller.ControllerSignal 1 # SampleAgent: 2 # A very simple Jython driving agent that alternates 3 # between a faster and a slower mode every 100 gameticks. 3 4 4 5 class MyAgent(DrivingAgent): 5 6 def __init__(self): 6 7 self.counter = 0 7 8 8 def poll(self ):9 def poll(self, you, others, track): 9 10 self.counter += 1 10 11 if self.counter / 100 % 2: 11 12 return ControllerSignal(35, True)