| 1 |
/* |
| 2 |
* PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 |
* Development, and is not to be reproduced, transmitted, or disclosed |
| 4 |
* in any way without written permission. |
| 5 |
* |
| 6 |
* Produced by: Jeff Lait |
| 7 |
* |
| 8 |
* POWDER Development |
| 9 |
* |
| 10 |
* NAME: speed.cpp ( POWDER Library, C++ ) |
| 11 |
* |
| 12 |
* COMMENTS: |
| 13 |
* Implementation of the speed system. |
| 14 |
* |
| 15 |
* Speed is INTENTIONALLY round based. The goal is to allow |
| 16 |
* predictable movement, rather than the tendency to floating |
| 17 |
* point speed systems that have confused (IMO) later roguelikes. |
| 18 |
* |
| 19 |
* As such, there are three speed modifiers: Fast (F), Quick (Q), |
| 20 |
* and Slow (S). There are also 4 types of phases: Fast (F), |
| 21 |
* Quick (Q), Slow (S), and Normal (N). |
| 22 |
* |
| 23 |
* doHeartbeat style functions execute only on N & S phases. |
| 24 |
* |
| 25 |
* doAI movement operates on the phases according to the following |
| 26 |
* chart: |
| 27 |
* |
| 28 |
* F N S Q N Speed |
| 29 |
* N x x x 100% |
| 30 |
* F x x x x 133% |
| 31 |
* Q x x x x 133% |
| 32 |
* FQ x x x x x 167% |
| 33 |
* S x x 67% |
| 34 |
* FS x x x 100% |
| 35 |
* QS x x x 100% |
| 36 |
* FQS x x x x 133% |
| 37 |
*/ |
| 38 |
|
| 39 |
#include "speed.h" |
| 40 |
#include "sramstream.h" |
| 41 |
#include "assert.h" |
| 42 |
|
| 43 |
int glbGameMoves = 0; |
| 44 |
int glbHeartbeatCount = 0; |
| 45 |
|
| 46 |
// We cache this so speed_getphase is fast. |
| 47 |
PHASE_NAMES glbCurPhase; |
| 48 |
|
| 49 |
#define PHASE_SEQ_LEN 5 |
| 50 |
|
| 51 |
// This is the prebuilt table of phases. We use it to determine |
| 52 |
// our current phase given our time tick. |
| 53 |
const PHASE_NAMES glbPhaseOrder[PHASE_SEQ_LEN] = |
| 54 |
{ |
| 55 |
PHASE_FAST, |
| 56 |
PHASE_NORMAL, |
| 57 |
PHASE_SLOW, |
| 58 |
PHASE_QUICK, |
| 59 |
PHASE_NORMAL |
| 60 |
}; |
| 61 |
|
| 62 |
void |
| 63 |
speed_init() |
| 64 |
{ |
| 65 |
glbGameMoves = 0; |
| 66 |
glbHeartbeatCount = 0; |
| 67 |
// Initial tick to initialize glbCurPhase. |
| 68 |
speed_ticktime(); |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
speed_reset() |
| 73 |
{ |
| 74 |
glbGameMoves = 0; |
| 75 |
glbHeartbeatCount = 0; |
| 76 |
speed_ticktime(); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
speed_ticktime() |
| 81 |
{ |
| 82 |
glbGameMoves++; |
| 83 |
glbCurPhase = glbPhaseOrder[glbGameMoves % PHASE_SEQ_LEN]; |
| 84 |
if (speed_isheartbeatphase()) |
| 85 |
glbHeartbeatCount++; |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
speed_gettime() |
| 90 |
{ |
| 91 |
return glbGameMoves; |
| 92 |
} |
| 93 |
|
| 94 |
PHASE_NAMES |
| 95 |
speed_getphase() |
| 96 |
{ |
| 97 |
return glbCurPhase; |
| 98 |
} |
| 99 |
|
| 100 |
bool |
| 101 |
speed_isheartbeatphase() |
| 102 |
{ |
| 103 |
// Determine if we are in a proper phase. |
| 104 |
switch (speed_getphase()) |
| 105 |
{ |
| 106 |
case PHASE_FAST: |
| 107 |
case PHASE_QUICK: |
| 108 |
// Do not hearbeat on these phases. |
| 109 |
return false; |
| 110 |
|
| 111 |
case PHASE_NORMAL: |
| 112 |
case PHASE_SLOW: |
| 113 |
// we want a heartbeat. |
| 114 |
return true; |
| 115 |
|
| 116 |
case NUM_PHASES: |
| 117 |
UT_ASSERT(!"Invalid phase!"); |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
// Should not go here. |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
bool |
| 126 |
speed_matchheartbeat(int modulus) |
| 127 |
{ |
| 128 |
if (modulus < 2) |
| 129 |
return true; |
| 130 |
|
| 131 |
// Avoid division where possible. |
| 132 |
switch (modulus) |
| 133 |
{ |
| 134 |
case 2: |
| 135 |
if (glbHeartbeatCount & 1) |
| 136 |
return false; |
| 137 |
return true; |
| 138 |
case 4: |
| 139 |
if (glbHeartbeatCount & 3) |
| 140 |
return false; |
| 141 |
return true; |
| 142 |
case 8: |
| 143 |
if (glbHeartbeatCount & 7) |
| 144 |
return false; |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
if (glbHeartbeatCount % modulus) |
| 149 |
return false; |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
speed_save(SRAMSTREAM &os) |
| 155 |
{ |
| 156 |
os.write(glbGameMoves, 32); |
| 157 |
os.write(glbHeartbeatCount, 32); |
| 158 |
} |
| 159 |
|
| 160 |
void |
| 161 |
speed_load(SRAMSTREAM &is) |
| 162 |
{ |
| 163 |
is.read(glbGameMoves, 32); |
| 164 |
is.read(glbHeartbeatCount, 32); |
| 165 |
} |