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 Henrik Heimbuerger, 16 years ago)

fix via additional poll() parameters

  • src/de/jacavi/appl/racelogic/RaceEngine.java

     
    3636
    3737    private int raceTimerInterval = 0;
    3838
    39     boolean isTimerRunning = false;
     39    private boolean isTimerRunning = false;
    4040
    4141    private Track track;
    4242
    4343    private List<Player> players;
    4444
    45     private RaceEngine() {}
    46 
    4745    public void setPlayers(List<Player> players) {
    4846        this.players = players;
    4947    }
     
    144142                // get players hal connector
    145143                SlotCarSystemConnector slotCarSystemConnector = player.getSlotCarSystemConnector();
    146144                // get players controller signal
    147                 ControllerSignal controllerSignal = carController.poll();
     145                ControllerSignal controllerSignal = carController.poll(player, players, track);
    148146                // get the tda
    149147                TrackDataApproximator tda = player.getTda();
    150148
  • src/de/jacavi/appl/controller/device/impl/MouseDevice.java

     
    11package de.jacavi.appl.controller.device.impl;
    22
     3import java.util.List;
     4
    35import org.eclipse.swt.SWT;
    46import org.eclipse.swt.widgets.Display;
    57import org.eclipse.swt.widgets.Event;
     
    79
    810import de.jacavi.appl.controller.ControllerSignal;
    911import de.jacavi.appl.controller.device.DeviceController;
     12import de.jacavi.appl.racelogic.Player;
     13import de.jacavi.appl.track.Track;
    1014
    1115
    1216
     
    5862    }
    5963
    6064    @Override
    61     public ControllerSignal poll() {
     65    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    6266        return currentControllerSignal;
    6367    }
    6468
  • src/de/jacavi/appl/controller/device/impl/GameControllerDevice.java

     
    11package de.jacavi.appl.controller.device.impl;
    22
     3import java.util.List;
     4
    35import com.centralnexus.input.Joystick;
    46
    57import de.jacavi.appl.controller.ControllerSignal;
    68import de.jacavi.appl.controller.device.DeviceController;
    79import de.jacavi.appl.controller.device.impl.GameControllerDeviceManager.GameControllerDescriptor;
     10import de.jacavi.appl.racelogic.Player;
     11import de.jacavi.appl.track.Track;
    812
    913
    1014
     
    2125    }
    2226
    2327    @Override
    24     public ControllerSignal poll() {
     28    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    2529        device.poll();
    2630
    2731        int speed = normaliseSpeedSignal(device.getY());
  • src/de/jacavi/appl/controller/device/impl/WiimoteDevice.java

     
    11package de.jacavi.appl.controller.device.impl;
    22
     3import java.util.List;
     4
    35import org.apache.log4j.Logger;
    46
    57import wiiusej.Wiimote;
     
    1820import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
    1921import de.jacavi.appl.controller.ControllerSignal;
    2022import de.jacavi.appl.controller.device.DeviceController;
     23import de.jacavi.appl.racelogic.Player;
     24import de.jacavi.appl.track.Track;
    2125import de.jacavi.rcp.util.Check;
    2226
    2327
     
    5963    }
    6064
    6165    @Override
    62     public ControllerSignal poll() {
     66    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    6367        return currentControllerSignal;
    6468    }
    6569
  • src/de/jacavi/appl/controller/device/impl/KeyboardDevice.java

     
    11package de.jacavi.appl.controller.device.impl;
    22
     3import java.util.List;
     4
    35import org.eclipse.swt.SWT;
    46import org.eclipse.swt.widgets.Display;
    57import org.eclipse.swt.widgets.Event;
     
    79
    810import de.jacavi.appl.controller.ControllerSignal;
    911import de.jacavi.appl.controller.device.DeviceController;
     12import de.jacavi.appl.racelogic.Player;
     13import de.jacavi.appl.track.Track;
    1014
    1115
    1216
     
    5054    }
    5155
    5256    @Override
    53     public ControllerSignal poll() {
     57    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    5458        ControllerSignal retVal = currentControllerSignal;
    5559        currentControllerSignal = new ControllerSignal(currentControllerSignal.getThrust(), currentControllerSignal
    5660                .isTrigger());
  • src/de/jacavi/appl/controller/device/impl/MouseKeyboardDeviceAdapter.java

     
    11package de.jacavi.appl.controller.device.impl;
    22
     3import java.util.List;
     4
    35import de.jacavi.appl.controller.ControllerSignal;
    46import de.jacavi.appl.controller.device.DeviceController;
     7import de.jacavi.appl.racelogic.Player;
     8import de.jacavi.appl.track.Track;
    59
    610
    711
     
    3539    }
    3640
    3741    @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);
    4145        return new ControllerSignal(mouse.getThrust(), mouse.isTrigger(), keyboard.isSwitchFrontLight(), keyboard
    4246                .isSwitchBackLight(), mouse.isReset());
    4347    }
  • src/de/jacavi/appl/controller/CarController.java

     
    11package de.jacavi.appl.controller;
    22
     3import java.util.List;
    34import java.util.UUID;
    45
     6import de.jacavi.appl.racelogic.Player;
     7import de.jacavi.appl.track.Track;
     8
    59
    610
    711public abstract class CarController implements Comparable<CarController> {
     
    1418        this.name = name;
    1519    }
    1620
    17     abstract public ControllerSignal poll();
     21    abstract public ControllerSignal poll(Player you, List<Player> others, Track track);
    1822
    1923    public void cleanup() {
    2024        deactivate();
     
    5458    public void deactivate() {}
    5559
    5660    /**
    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.
    5964     * <p>
    6065     * We need the reset because some Controllers save their thrust in state.
    6166     */
    6267    public void reset() {}
    6368
     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
    6483}
  • src/de/jacavi/appl/controller/agent/DrivingAgent.java

     
    11package de.jacavi.appl.controller.agent;
    22
     3import java.util.List;
     4
    35import de.jacavi.appl.controller.ControllerSignal;
     6import de.jacavi.appl.racelogic.Player;
     7import de.jacavi.appl.track.CarPosition;
     8import de.jacavi.appl.track.Track;
    49
    510
    611
    712public interface DrivingAgent {
    8     ControllerSignal poll(/*CarPosition you, CarPosition[] others, Track track*/);
     13    ControllerSignal poll(CarPosition you, List<Player> others, Track track);
    914}
  • src/de/jacavi/appl/controller/agent/ScriptController.java

     
    44import java.io.PrintWriter;
    55import java.io.StringWriter;
    66import java.io.Writer;
     7import java.util.List;
    78
    89import de.jacavi.appl.controller.ControllerSignal;
     10import de.jacavi.appl.racelogic.Player;
     11import de.jacavi.appl.track.Track;
    912import de.jacavi.rcp.util.ExceptionHandler;
    1013
    1114
    1215
    1316abstract public class ScriptController extends DrivingAgentController {
     17
    1418    @SuppressWarnings("serial")
    1519    public class ScriptExecutionException extends Exception {
    1620        public ScriptExecutionException(String message) {
     
    2226
    2327    private DrivingAgent scriptObject;
    2428
     29    private ControllerSignal lastSignal = new ControllerSignal();
     30
    2531    public ScriptController(String name, File agentFile) {
    2632        super(name);
    2733        this.agentFile = agentFile;
     
    3137        return agentFile;
    3238    }
    3339
     40    // @SuppressWarnings("unchecked")
    3441    @Override
    35     public ControllerSignal poll() {
     42    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    3643        if(scriptObject == null)
    3744            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;
    4065    }
    4166
    4267    @Override
     
    6186
    6287    public ControllerSignal dryRun() throws ScriptExecutionException {
    6388        try {
    64             return parseScript().poll();
     89            return parseScript().poll(null, null, null);
    6590        } catch(Exception exc) {
    6691            final Writer writer = new StringWriter();
    6792            exc.printStackTrace(new PrintWriter(writer));
  • src/de/jacavi/appl/controller/agent/impl/XmlRpcController.java

     
    11package de.jacavi.appl.controller.agent.impl;
    22
     3import java.util.List;
     4
    35import org.apache.xmlrpc.XmlRpcException;
    46import org.apache.xmlrpc.XmlRpcRequest;
    57import org.apache.xmlrpc.server.PropertyHandlerMapping;
     
    1012
    1113import de.jacavi.appl.controller.ControllerSignal;
    1214import de.jacavi.appl.controller.agent.ExternalController;
     15import de.jacavi.appl.racelogic.Player;
     16import de.jacavi.appl.track.Track;
    1317import de.jacavi.rcp.util.ExceptionHandler;
    1418
    1519
     
    149153    }
    150154
    151155    @Override
    152     public ControllerSignal poll() {
     156    public ControllerSignal poll(Player you, List<Player> others, Track track) {
    153157        return controllerSignal;
    154158    }
    155159
  • src/de/jacavi/rcp/dlg/InputDeviceSettingsDialog.java

     
    9393                @Override
    9494                public void run() {
    9595                    if(!progressBar.isDisposed() && carControllerManager.isIdValid(deviceID)) {
    96                         ControllerSignal signal = carControllerManager.getDevice(deviceID).poll();
     96                        ControllerSignal signal = carControllerManager.getDevice(deviceID).poll(null, null, null);
    9797                        progressBar.setSelection(signal.getThrust());
    9898                        progressBar.setForeground(signal.isTrigger() ? Display.getDefault().getSystemColor(
    9999                                SWT.COLOR_DARK_MAGENTA) : null);
  • src/de/jacavi/rcp/widgets/TrackWidget.java

     
    10461046            boolean isOnTrack = p.getPosition().isOnTrack;
    10471047            CarController dc = p.getController();
    10481048            String name = p.getName();
    1049             ControllerSignal signal = dc.poll();
     1049            ControllerSignal signal = dc.getLastSignal();
    10501050            if(signal != null) {
    10511051                int thrust = isOnTrack ? signal.getThrust() : 100;
    10521052                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
     6import java.util.List
    17import de.jacavi.appl.controller.agent.DrivingAgent
    28import de.jacavi.appl.controller.ControllerSignal
     9import de.jacavi.appl.track.CarPosition
     10import de.jacavi.appl.track.Track
    311
    412public class SampleAgent implements DrivingAgent {
    513        private int counter = 0
    614
    7     public ControllerSignal poll() {
     15    public ControllerSignal poll(CarPosition you, List others, Track track) {
    816        counter++
    917        if(((counter / 100) as int) % 2 == 1) {
    1018            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
     6class 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.
    34
    45class MyAgent(DrivingAgent):
    56    def __init__(self):
    67        self.counter = 0
    78
    8     def poll(self):
     9    def poll(self, you, others, track):
    910        self.counter += 1
    1011        if self.counter / 100 % 2:
    1112            return ControllerSignal(35, True)