aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/DeviceFrameBuffer.h
Commit message (Expand)AuthorAgeFilesLines
* Mark overridden virtual functionsPierre Ossman2024-06-241-1/+1
* Clean up initialization of DIBSectionBufferPierre Ossman2020-01-031-5/+0
* Change cursor API to use RGBA dataPierre Ossman2017-02-221-2/+0
* Remove full support for colour mapsPierre Ossman2014-07-071-1/+0
* Migrating to new directory structure adopted from the RealVNC's source tree. ...Constantin Kaplinsky2006-05-251-0/+106
class='linenumbers'>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*

Copyright (c) Xerox Corporation 1998-2002.  All rights reserved.

Use and copying of this software and preparation of derivative works based
upon this software are permitted.  Any distribution of this software or
derivative works must comply with all applicable United States export control
laws.

This software is made available AS IS, and Xerox Corporation makes no warranty
about the software, its performance or its conformity to any specification.

|<---            this code is formatted to fit into 80 columns             --->|
|<---            this code is formatted to fit into 80 columns             --->|
|<---            this code is formatted to fit into 80 columns             --->|

Ship.java
Part of the Spacewar system.

*/

package spacewar;

class Ship extends SpaceObject {

    pointcut helmCommandsCut(Ship ship):
        target(ship) && ( call(void rotate(int))     ||
			  call(void thrust(boolean)) ||
			  call(void fire()) );


    /**
     * Energy and Damage are key values in the state of a ship.  Energy is
     * basically about fuel, and damage is about how bad a shape we are in.
     *
     * The energy related values are:
     * <ul>
     *   <li>MAX_ENERGY</li>
     *   <li>BULLET_ENERGY</li>
     *   <li>ACCELERATION_ENERGY_FACTOR</li>
     *   <li>energy</li>
     * </ul>
     * The damage related values are:
     * <ul>
     *   <li>MAX_DAMAGE</li>
     *   <li>BULLET_DAMAGE</li>
     *   <li>COLLISION_DAMAGE_FACTOR</li>
     *   <li>damage</li>
     * </ul>
     * Finally, REPAIR_RATE is the rate at which energy is consumed to fix
     * damage.
     *
     */
    private static final int    MAX_ENERGY = 100;
    private static final int    BULLET_ENERGY= 2;
    private static final double ACCELERATION_COST_FACTOR = 0.05;

    //XXX was private
    static final int    MAX_DAMAGE = 100;
    private static final int    BULLET_DAMAGE = 15;
    private static final double COLLISION_DAMAGE_FACTOR = 0.1;

    private static final double REPAIR_RATE = 0.08;


    private static final int    EXPLOSION_LENGTH = 10;

    static final int    BULLET_SPEED = 10;

    static final int    CLOCKWISE = 1;
    static final int    STOP = 0;
    static final int    COUNTERCLOCKWISE = (-1);

    static final double DEFAULT_ANGULAR_VELOCITY = 0.2;
    static final double DEFAULT_ACCELERATION = .4;

    static private final int SIZE = 30;     //Can't be changed for now!!!

    private double    energy;               // range: 0 to MAX_ENERGY
    private double    damage;               // range: 0 to MAX_DAMAGE
    private double    orientation;          // in degrees
    private double    angularVel;           // in ???
    private double    xAcc, yAcc, rAcc;     //
    private int       countdown;            // remaining explosion time

    private Pilot     pilot;

    Ship(Game theGame, double xPos, double yPos, double orientation) {
        super(theGame, xPos, yPos, 0, 0);
        xAcc = 0;
        yAcc = 0;
        this.orientation = orientation;
        angularVel = 0;

        energy = MAX_ENERGY;
        damage = 0;
        countdown = EXPLOSION_LENGTH;
    }


    int getSize()            { return SIZE; }

    double getEnergy()       { return energy; }
    double getDamage()       { return damage; }
    double getOrientation()  { return orientation; }
    double getRAcc()         { return rAcc; }

    Pilot getPilot()         { return pilot; }
    void  setPilot (Pilot p) { pilot = p; }

    float getEnergyLevel() {
        return (float)energy / (float)MAX_ENERGY;
    }
    float getDamageLevel() {
        return (float)damage / (float)MAX_DAMAGE;
    }

    /** returns false if energy is out, otherwise decrements energy by amount
     * and returns true
     */
    boolean expendEnergy(double amount) {
        if (amount <= energy) {
            energy -= amount;
            return true;
        }
        else
            return false;
    }

    /** increments damage by amount and handles the destruction of a ship if
     * damage reaches MAX_DAMAGE.
     */
    void inflictDamage(double amount) {
        if (amount < 0)     // shouldn't happen
            return;
        damage = Math.min(MAX_DAMAGE, damage + amount);
        if (damage == MAX_DAMAGE)
            setIsAlive(false);
    }

    /** repairs some damage
     */
    void repairDamage(double amount) {
        if (amount < 0)     // shouldn't happen
            return;
        if (damage == 0)
            return;
        damage = Math.max(0, damage - amount);
    }

    public void clockTick() {
        if (! isAlive())  {
            //
            // If we aren't alive, but we are still in the registry, it means
            // we are exploding.  countdown counts the length of the explosion.
            //
            if (--countdown == 0)
                die();
        }
        else {
            if (angularVel != 0) {
                orientation += angularVel;
                xAcc = rAcc * Math.cos(orientation);
                yAcc = rAcc * Math.sin(orientation);
            }
            setXVel(getXVel() + xAcc);
            setYVel(getYVel() + yAcc);

            //expend energy
            if (!expendEnergy(rAcc * ACCELERATION_COST_FACTOR))
                rAcc = xAcc = yAcc = 0;

            // fix damage
            if (energy > 10 && damage > REPAIR_RATE) {
                expendEnergy(REPAIR_RATE);
                repairDamage(REPAIR_RATE);
            }
        }
        super.clockTick();
    }

    /**
     * First check to make sure we have enough energy to accelerate.  If
     * we do, then go ahead and do so.  Acceleration is in the direction
     * we are already facing (i.e. orientation).
     */
    void setAcceleration(double acc) {
        if (acc * ACCELERATION_COST_FACTOR <= energy) {
            rAcc = acc;
            xAcc = rAcc * Math.cos(orientation);
            yAcc = rAcc * Math.sin(orientation);
        }
    }

    /**
     * First check to make sure we have enough energy to rotate.  If
     * we do, then go ahead and do so.
     */
    void setAngularVelocity(double omega) {
        // changing direction of rotation takes energy
        if (!expendEnergy(Math.abs(omega - angularVel) / 2))
            return;
        //sets amount of degree rotation per clock tick, in radians;
        //clockwise is positive
        angularVel = omega;
    }

    /** affect rotation thrusters.  Direction can be one of {@link
     * #CLOCKWISE}, {@link #COUNTERCLOCKWISE}, or zero for turning off
     * the thrusters.
     */
    void rotate(int direction) {
        setAngularVelocity(
          direction == CLOCKWISE        ? DEFAULT_ANGULAR_VELOCITY :
          direction == COUNTERCLOCKWISE ? -DEFAULT_ANGULAR_VELOCITY :
          0);
    }

    /** turn on acceleration */
    void thrust(boolean onOff) {
        setAcceleration(onOff ? DEFAULT_ACCELERATION : 0);
    }

    /** create a bullet and fire it */
    void fire() {
        // firing a shot takes energy
        if (!expendEnergy(BULLET_ENERGY))
            return;

        //create a bullet object so it doesn't hit the ship that's firing it
        double xV = getXVel() + BULLET_SPEED * (Math.cos(orientation));
        double yV = getYVel() + BULLET_SPEED * (Math.sin(orientation));

        // create the actual bullet
        new Bullet(
         getGame(),
         (getXPos() + ((getSize()/2 + 2) * (Math.cos(orientation))) + xV),
         (getYPos() + ((getSize()/2 + 2) * (Math.sin(orientation))) + yV),
         xV,
         yV);
    }


    void handleCollision(SpaceObject obj) {
        if (obj instanceof Ship) {
            // should never be called. ship - ship collisions are handled in
            // Ship.bounce(Ship shipA, Ship shipB)
        }
        else if (obj instanceof Bullet) {
            inflictDamage(BULLET_DAMAGE);
        }
        else if (obj instanceof EnergyPacket) {
            double packetEnergy = ((EnergyPacket)obj).getEnergy();
            energy = Math.max(0, Math.min(energy + packetEnergy, MAX_ENERGY));
        }
        else {
            System.err.println("collision with UFO!");
        }
    }

    static void bounce(Ship shipA, Ship shipB) {
        double  dx, dy, denominator,
            xAccA, yAccA, xAccB, yAccB, damage,
            xComp, yComp, dvx, dvy;

        dx = Math.abs(shipA.getXPos() - shipB.getXPos());
        dy = Math.abs(shipA.getYPos() - shipB.getYPos());
        denominator = Math.sqrt(dx * dx + dy * dy);
        xComp = dx / denominator;
        yComp = dy / denominator;
        xAccA = shipB.getXVel() * xComp + shipA.getXVel() * (1 - xComp) -
            shipA.getXVel();
        yAccA = shipB.getYVel() * yComp + shipA.getYVel() * (1 - yComp) -
            shipA.getYVel();
        xAccB = shipA.getXVel() * xComp + shipB.getXVel() * (1 - xComp) -
            shipB.getXVel();
        yAccB = shipA.getYVel() * yComp + shipB.getYVel() * (1 - yComp) -
            shipB.getYVel();
        shipA.accelerate(xAccA, yAccA);
        shipB.accelerate(xAccB, yAccB);
        dvx = shipA.getXVel() - shipB.getXVel();
        dvy = shipA.getYVel() - shipA.getYVel();
        damage = COLLISION_DAMAGE_FACTOR * (dvx * dvx + dvy * dvy);
        shipA.inflictDamage(damage);
        shipB.inflictDamage(damage);

        // !!!
        // !!! poopers!  this does a local time warp.  this has to be a
        // !!! violation of the clockTick protocol
        // !!!
        while (Game.isCollision(shipA, shipB)) {
            shipA.clockTick();
            shipB.clockTick();
        }
    }
}