fallout2-ce/src/cycle.cc

330 lines
6.6 KiB
C++
Raw Normal View History

2022-05-19 01:51:26 -07:00
#include "cycle.h"
#include "color.h"
2022-10-04 23:23:27 -07:00
#include "input.h"
2022-05-19 01:51:26 -07:00
#include "palette.h"
#include "settings.h"
2022-10-05 00:35:05 -07:00
#include "svga.h"
2022-05-19 01:51:26 -07:00
2022-09-23 05:43:44 -07:00
namespace fallout {
2023-01-05 00:29:00 -08:00
static constexpr unsigned int kSlowCyclePeriod = 1000 / 5;
static constexpr unsigned int kMediumCyclePeriod = 1000 / 7;
static constexpr unsigned int kFastCyclePeriod = 1000 / 10;
static constexpr unsigned int kVeryFastCyclePeriod = 1000 / 30;
2022-06-18 06:09:35 -07:00
2022-05-19 01:51:26 -07:00
// 0x51843C
2022-06-18 06:09:35 -07:00
static int gColorCycleSpeedFactor = 1;
2022-05-19 01:51:26 -07:00
// TODO: Convert colors to RGB.
// clang-format off
// Green.
//
// 0x518440
2023-01-05 00:29:00 -08:00
static unsigned char slime[12] = {
2022-05-19 01:51:26 -07:00
0, 108, 0,
11, 115, 7,
27, 123, 15,
43, 131, 27,
};
// Light gray?
//
// 0x51844C
2023-01-05 00:29:00 -08:00
static unsigned char shoreline[18] = {
2022-05-19 01:51:26 -07:00
83, 63, 43,
75, 59, 43,
67, 55, 39,
63, 51, 39,
55, 47, 35,
51, 43, 35,
};
// Orange.
//
// 0x51845E
2023-01-05 00:29:00 -08:00
static unsigned char fire_slow[15] = {
2022-05-19 01:51:26 -07:00
255, 0, 0,
215, 0, 0,
147, 43, 11,
255, 119, 0,
255, 59, 0,
};
// Red.
//
// 0x51846D
2023-01-05 00:29:00 -08:00
static unsigned char fire_fast[15] = {
2022-05-19 01:51:26 -07:00
71, 0, 0,
123, 0, 0,
179, 0, 0,
123, 0, 0,
71, 0, 0,
};
// Light blue.
//
// 0x51847C
2023-01-05 00:29:00 -08:00
static unsigned char monitors[15] = {
2022-05-19 01:51:26 -07:00
107, 107, 111,
99, 103, 127,
87, 107, 143,
0, 147, 163,
107, 187, 255,
};
// clang-format on
// 0x51848C
2022-06-18 06:09:35 -07:00
static bool gColorCycleInitialized = false;
2022-05-19 01:51:26 -07:00
// 0x518490
2022-06-18 06:09:35 -07:00
static bool gColorCycleEnabled = false;
2022-05-19 01:51:26 -07:00
// 0x56D7D0
2023-01-05 00:29:00 -08:00
static unsigned int last_cycle_fast;
2022-05-19 01:51:26 -07:00
// 0x56D7D4
2023-01-05 00:29:00 -08:00
static unsigned int last_cycle_slow;
2022-05-19 01:51:26 -07:00
// 0x56D7D8
2023-01-05 00:29:00 -08:00
static unsigned int last_cycle_medium;
2022-05-19 01:51:26 -07:00
// 0x56D7DC
2023-01-05 00:29:00 -08:00
static unsigned int last_cycle_very_fast;
2022-05-19 01:51:26 -07:00
// 0x42E780
void colorCycleInit()
{
if (gColorCycleInitialized) {
return;
}
if (!settings.system.color_cycling) {
2022-05-19 01:51:26 -07:00
return;
}
for (int index = 0; index < 12; index++) {
2023-01-05 00:29:00 -08:00
slime[index] >>= 2;
2022-05-19 01:51:26 -07:00
}
for (int index = 0; index < 18; index++) {
2023-01-05 00:29:00 -08:00
shoreline[index] >>= 2;
2022-05-19 01:51:26 -07:00
}
for (int index = 0; index < 15; index++) {
2023-01-05 00:29:00 -08:00
fire_slow[index] >>= 2;
2022-05-19 01:51:26 -07:00
}
for (int index = 0; index < 15; index++) {
2023-01-05 00:29:00 -08:00
fire_fast[index] >>= 2;
2022-05-19 01:51:26 -07:00
}
for (int index = 0; index < 15; index++) {
2023-01-05 00:29:00 -08:00
monitors[index] >>= 2;
2022-05-19 01:51:26 -07:00
}
tickersAdd(colorCycleTicker);
gColorCycleInitialized = true;
gColorCycleEnabled = true;
cycleSetSpeedFactor(settings.system.cycle_speed_factor);
2022-05-19 01:51:26 -07:00
}
// 0x42E8CC
void colorCycleReset()
{
if (gColorCycleInitialized) {
2023-01-05 00:29:00 -08:00
last_cycle_slow = 0;
last_cycle_medium = 0;
last_cycle_fast = 0;
last_cycle_very_fast = 0;
2022-05-19 01:51:26 -07:00
tickersAdd(colorCycleTicker);
gColorCycleEnabled = true;
}
}
// 0x42E90C
void colorCycleFree()
{
if (gColorCycleInitialized) {
tickersRemove(colorCycleTicker);
gColorCycleInitialized = false;
gColorCycleEnabled = false;
}
}
// 0x42E930
void colorCycleDisable()
{
gColorCycleEnabled = false;
}
// 0x42E93C
void colorCycleEnable()
{
gColorCycleEnabled = true;
}
// 0x42E948
bool colorCycleEnabled()
{
return gColorCycleEnabled;
}
// 0x42E950
void cycleSetSpeedFactor(int value)
{
gColorCycleSpeedFactor = value;
settings.system.cycle_speed_factor = value;
2022-05-19 01:51:26 -07:00
}
// 0x42E97C
void colorCycleTicker()
{
2023-01-05 00:29:00 -08:00
// 0x518494
static int slime_start = 0;
// 0x518498
static int shoreline_start = 0;
// 0x51849C
static int fire_slow_start = 0;
// 0x5184A0
static int fire_fast_start = 0;
// 0x5184A4
static int monitors_start = 0;
// 0x5184A8
static unsigned char bobber_red = 0;
// 0x5184A9
static signed char bobber_diff = -4;
2022-05-19 01:51:26 -07:00
if (!gColorCycleEnabled) {
return;
}
bool changed = false;
unsigned char* palette = _getSystemPalette();
2022-10-05 00:11:47 -07:00
unsigned int time = getTicks();
2022-05-19 01:51:26 -07:00
2023-01-05 00:29:00 -08:00
if (getTicksBetween(time, last_cycle_slow) >= kSlowCyclePeriod * gColorCycleSpeedFactor) {
2022-05-19 01:51:26 -07:00
changed = true;
2023-01-05 00:29:00 -08:00
last_cycle_slow = time;
2022-05-19 01:51:26 -07:00
int paletteIndex = 229 * 3;
2023-01-05 00:29:00 -08:00
for (int index = slime_start; index < 12; index++) {
palette[paletteIndex++] = slime[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
for (int index = 0; index < slime_start; index++) {
palette[paletteIndex++] = slime[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
slime_start -= 3;
if (slime_start < 0) {
slime_start = 9;
2022-05-19 01:51:26 -07:00
}
paletteIndex = 248 * 3;
2023-01-05 00:29:00 -08:00
for (int index = shoreline_start; index < 18; index++) {
palette[paletteIndex++] = shoreline[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
for (int index = 0; index < shoreline_start; index++) {
palette[paletteIndex++] = shoreline[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
shoreline_start -= 3;
if (shoreline_start < 0) {
shoreline_start = 15;
2022-05-19 01:51:26 -07:00
}
paletteIndex = 238 * 3;
2023-01-05 00:29:00 -08:00
for (int index = fire_slow_start; index < 15; index++) {
palette[paletteIndex++] = fire_slow[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
for (int index = 0; index < fire_slow_start; index++) {
palette[paletteIndex++] = fire_slow[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
fire_slow_start -= 3;
if (fire_slow_start < 0) {
fire_slow_start = 12;
2022-05-19 01:51:26 -07:00
}
}
2023-01-05 00:29:00 -08:00
if (getTicksBetween(time, last_cycle_medium) >= kMediumCyclePeriod * gColorCycleSpeedFactor) {
2022-05-19 01:51:26 -07:00
changed = true;
2023-01-05 00:29:00 -08:00
last_cycle_medium = time;
2022-05-19 01:51:26 -07:00
int paletteIndex = 243 * 3;
2023-01-05 00:29:00 -08:00
for (int index = fire_fast_start; index < 15; index++) {
palette[paletteIndex++] = fire_fast[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
for (int index = 0; index < fire_fast_start; index++) {
palette[paletteIndex++] = fire_fast[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
fire_fast_start -= 3;
if (fire_fast_start < 0) {
fire_fast_start = 12;
2022-05-19 01:51:26 -07:00
}
}
2023-01-05 00:29:00 -08:00
if (getTicksBetween(time, last_cycle_fast) >= kFastCyclePeriod * gColorCycleSpeedFactor) {
2022-05-19 01:51:26 -07:00
changed = true;
2023-01-05 00:29:00 -08:00
last_cycle_fast = time;
2022-05-19 01:51:26 -07:00
int paletteIndex = 233 * 3;
2023-01-05 00:29:00 -08:00
for (int index = monitors_start; index < 15; index++) {
palette[paletteIndex++] = monitors[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
for (int index = 0; index < monitors_start; index++) {
palette[paletteIndex++] = monitors[index];
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
monitors_start -= 3;
2022-05-19 01:51:26 -07:00
2023-01-05 00:29:00 -08:00
if (monitors_start < 0) {
monitors_start = 12;
2022-05-19 01:51:26 -07:00
}
}
2023-01-05 00:29:00 -08:00
if (getTicksBetween(time, last_cycle_very_fast) >= kVeryFastCyclePeriod * gColorCycleSpeedFactor) {
2022-05-19 01:51:26 -07:00
changed = true;
2023-01-05 00:29:00 -08:00
last_cycle_very_fast = time;
2022-05-19 01:51:26 -07:00
2023-01-05 00:29:00 -08:00
if (bobber_red == 0 || bobber_red == 60) {
bobber_diff = -bobber_diff;
2022-05-19 01:51:26 -07:00
}
2023-01-05 00:29:00 -08:00
bobber_red += bobber_diff;
2022-05-19 01:51:26 -07:00
int paletteIndex = 254 * 3;
2023-01-05 00:29:00 -08:00
palette[paletteIndex++] = bobber_red;
2022-05-19 01:51:26 -07:00
palette[paletteIndex++] = 0;
palette[paletteIndex++] = 0;
}
if (changed) {
paletteSetEntriesInRange(palette + 229 * 3, 229, 255);
}
}
2022-09-23 05:43:44 -07:00
} // namespace fallout