fallout2-ce/src/skilldex.cc

416 lines
12 KiB
C++
Raw Normal View History

2022-05-19 01:51:26 -07:00
#include "skilldex.h"
2022-09-15 02:38:23 -07:00
#include <stdio.h>
#include <string.h>
2022-06-18 22:18:05 -07:00
#include "art.h"
2022-05-19 01:51:26 -07:00
#include "color.h"
#include "cycle.h"
#include "debug.h"
#include "draw.h"
#include "game.h"
#include "game_mouse.h"
#include "game_sound.h"
2022-06-18 22:18:05 -07:00
#include "geometry.h"
2022-10-04 23:23:27 -07:00
#include "input.h"
#include "interface.h"
2022-10-03 06:42:34 -07:00
#include "kb.h"
2022-05-19 01:51:26 -07:00
#include "map.h"
#include "memory.h"
2022-06-18 22:18:05 -07:00
#include "message.h"
2022-05-19 01:51:26 -07:00
#include "object.h"
2022-05-28 02:34:49 -07:00
#include "platform_compat.h"
2022-05-19 01:51:26 -07:00
#include "skill.h"
2022-10-05 00:35:05 -07:00
#include "svga.h"
2022-05-19 01:51:26 -07:00
#include "text_font.h"
#include "window_manager.h"
2022-09-23 05:43:44 -07:00
namespace fallout {
2022-05-20 17:10:45 -07:00
#define SKILLDEX_WINDOW_RIGHT_MARGIN 4
#define SKILLDEX_WINDOW_BOTTOM_MARGIN 6
2022-06-18 22:18:05 -07:00
#define SKILLDEX_SKILL_BUTTON_BUFFER_COUNT (SKILLDEX_SKILL_COUNT * 2)
typedef enum SkilldexFrm {
SKILLDEX_FRM_BACKGROUND,
SKILLDEX_FRM_BUTTON_ON,
SKILLDEX_FRM_BUTTON_OFF,
SKILLDEX_FRM_LITTLE_RED_BUTTON_UP,
SKILLDEX_FRM_LITTLE_RED_BUTTON_DOWN,
SKILLDEX_FRM_BIG_NUMBERS,
SKILLDEX_FRM_COUNT,
} SkilldexFrm;
typedef enum SkilldexSkill {
SKILLDEX_SKILL_SNEAK,
SKILLDEX_SKILL_LOCKPICK,
SKILLDEX_SKILL_STEAL,
SKILLDEX_SKILL_TRAPS,
SKILLDEX_SKILL_FIRST_AID,
SKILLDEX_SKILL_DOCTOR,
SKILLDEX_SKILL_SCIENCE,
SKILLDEX_SKILL_REPAIR,
SKILLDEX_SKILL_COUNT,
} SkilldexSkill;
static int skilldexWindowInit();
static void skilldexWindowFree();
2022-05-19 01:51:26 -07:00
// 0x51D43C
2022-06-18 22:18:05 -07:00
static bool gSkilldexWindowIsoWasEnabled = false;
2022-05-19 01:51:26 -07:00
// 0x51D440
2022-06-18 22:18:05 -07:00
static const int gSkilldexFrmIds[SKILLDEX_FRM_COUNT] = {
2022-05-19 01:51:26 -07:00
121,
119,
120,
8,
9,
170,
};
// Maps Skilldex options into skills.
//
// 0x51D458
2022-06-18 22:18:05 -07:00
static const int gSkilldexSkills[SKILLDEX_SKILL_COUNT] = {
2022-05-19 01:51:26 -07:00
SKILL_SNEAK,
SKILL_LOCKPICK,
SKILL_STEAL,
SKILL_TRAPS,
SKILL_FIRST_AID,
SKILL_DOCTOR,
SKILL_SCIENCE,
SKILL_REPAIR,
};
// 0x6680B8
2022-06-18 22:18:05 -07:00
static unsigned char* gSkilldexButtonsData[SKILLDEX_SKILL_BUTTON_BUFFER_COUNT];
2022-05-19 01:51:26 -07:00
// skilldex.msg
// 0x6680F8
2022-06-18 22:18:05 -07:00
static MessageList gSkilldexMessageList;
2022-05-19 01:51:26 -07:00
// 0x668100
2022-06-18 22:18:05 -07:00
static MessageListItem gSkilldexMessageListItem;
2022-05-19 01:51:26 -07:00
// 0x668140
2022-06-18 22:18:05 -07:00
static int gSkilldexWindow;
2022-05-19 01:51:26 -07:00
// 0x668144
2022-06-18 22:18:05 -07:00
static unsigned char* gSkilldexWindowBuffer;
2022-05-19 01:51:26 -07:00
// 0x668148
2022-06-18 22:18:05 -07:00
static int gSkilldexWindowOldFont;
2022-05-19 01:51:26 -07:00
static FrmImage _skilldexFrmImages[SKILLDEX_FRM_COUNT];
2022-05-19 01:51:26 -07:00
// skilldex_select
// 0x4ABFD0
int skilldexOpen()
{
if (skilldexWindowInit() == -1) {
debugPrint("\n ** Error loading skilldex dialog data! **\n");
return -1;
}
int rc = -1;
while (rc == -1) {
2022-10-07 14:54:27 -07:00
sharedFpsLimiter.mark();
2022-10-05 00:11:47 -07:00
int keyCode = inputGetInput();
2022-05-19 01:51:26 -07:00
2022-08-07 11:49:53 -07:00
// SFALL: Close with 'S'.
if (keyCode == KEY_ESCAPE || keyCode == KEY_UPPERCASE_S || keyCode == KEY_LOWERCASE_S || keyCode == 500 || _game_user_wants_to_quit != 0) {
2022-05-19 01:51:26 -07:00
rc = 0;
} else if (keyCode == KEY_RETURN) {
soundPlayFile("ib1p1xx1");
rc = 0;
} else if (keyCode >= 501 && keyCode <= 509) {
rc = keyCode - 500;
}
2022-10-07 14:54:27 -07:00
renderPresent();
sharedFpsLimiter.throttle();
2022-05-19 01:51:26 -07:00
}
if (rc != 0) {
2022-10-05 00:11:47 -07:00
inputBlockForTocks(1000 / 9);
2022-05-19 01:51:26 -07:00
}
skilldexWindowFree();
return rc;
}
// 0x4AC054
2022-06-18 22:18:05 -07:00
static int skilldexWindowInit()
2022-05-19 01:51:26 -07:00
{
gSkilldexWindowOldFont = fontGetCurrent();
gSkilldexWindowIsoWasEnabled = false;
gameMouseObjectsHide();
gameMouseSetCursor(MOUSE_CURSOR_ARROW);
if (!messageListInit(&gSkilldexMessageList)) {
return -1;
}
2022-05-28 02:34:49 -07:00
char path[COMPAT_MAX_PATH];
2022-05-19 01:51:26 -07:00
sprintf(path, "%s%s", asc_5186C8, "skilldex.msg");
if (!messageListLoad(&gSkilldexMessageList, path)) {
return -1;
}
int frmIndex;
for (frmIndex = 0; frmIndex < SKILLDEX_FRM_COUNT; frmIndex++) {
int fid = buildFid(OBJ_TYPE_INTERFACE, gSkilldexFrmIds[frmIndex], 0, 0, 0);
if (!_skilldexFrmImages[frmIndex].lock(fid)) {
2022-05-19 01:51:26 -07:00
break;
}
}
if (frmIndex < SKILLDEX_FRM_COUNT) {
while (--frmIndex >= 0) {
_skilldexFrmImages[frmIndex].unlock();
2022-05-19 01:51:26 -07:00
}
messageListFree(&gSkilldexMessageList);
return -1;
}
bool cycle = false;
int buttonDataIndex;
for (buttonDataIndex = 0; buttonDataIndex < SKILLDEX_SKILL_BUTTON_BUFFER_COUNT; buttonDataIndex++) {
gSkilldexButtonsData[buttonDataIndex] = (unsigned char*)internal_malloc(_skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getHeight() * _skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getWidth() + 512);
2022-05-19 01:51:26 -07:00
if (gSkilldexButtonsData[buttonDataIndex] == NULL) {
break;
}
// NOTE: Original code uses bitwise XOR.
cycle = !cycle;
unsigned char* data;
int size;
if (cycle) {
size = _skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth() * _skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getHeight();
data = _skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getData();
2022-05-19 01:51:26 -07:00
} else {
size = _skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getWidth() * _skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getHeight();
data = _skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getData();
2022-05-19 01:51:26 -07:00
}
memcpy(gSkilldexButtonsData[buttonDataIndex], data, size);
}
if (buttonDataIndex < SKILLDEX_SKILL_BUTTON_BUFFER_COUNT) {
while (--buttonDataIndex >= 0) {
internal_free(gSkilldexButtonsData[buttonDataIndex]);
}
for (int index = 0; index < SKILLDEX_FRM_COUNT; index++) {
_skilldexFrmImages[index].unlock();
2022-05-19 01:51:26 -07:00
}
messageListFree(&gSkilldexMessageList);
return -1;
}
// Maintain original position relative to centered interface bar.
int skilldexWindowX = (screenGetWidth() - 640) / 2 + 640 - _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() - SKILLDEX_WINDOW_RIGHT_MARGIN;
int skilldexWindowY = screenGetHeight() - INTERFACE_BAR_HEIGHT - 1 - _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getHeight() - SKILLDEX_WINDOW_BOTTOM_MARGIN;
2022-05-20 17:10:45 -07:00
gSkilldexWindow = windowCreate(skilldexWindowX,
skilldexWindowY,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getHeight(),
2022-05-19 01:51:26 -07:00
256,
WINDOW_FLAG_0x10 | WINDOW_FLAG_0x02);
if (gSkilldexWindow == -1) {
for (int index = 0; index < SKILLDEX_SKILL_BUTTON_BUFFER_COUNT; index++) {
internal_free(gSkilldexButtonsData[index]);
}
for (int index = 0; index < SKILLDEX_FRM_COUNT; index++) {
_skilldexFrmImages[index].unlock();
2022-05-19 01:51:26 -07:00
}
messageListFree(&gSkilldexMessageList);
return -1;
}
gSkilldexWindowIsoWasEnabled = isoDisable();
colorCycleDisable();
gameMouseSetCursor(MOUSE_CURSOR_ARROW);
gSkilldexWindowBuffer = windowGetBuffer(gSkilldexWindow);
memcpy(gSkilldexWindowBuffer,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getData(),
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() * _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getHeight());
2022-05-19 01:51:26 -07:00
fontSetCurrent(103);
// Render "SKILLDEX" title.
char* title = getmsg(&gSkilldexMessageList, &gSkilldexMessageListItem, 100);
fontDrawText(gSkilldexWindowBuffer + 14 * _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() + 55,
2022-05-19 01:51:26 -07:00
title,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth(),
2022-05-19 01:51:26 -07:00
_colorTable[18979]);
// Render skill values.
int valueY = 48;
for (int index = 0; index < SKILLDEX_SKILL_COUNT; index++) {
int value = skillGetValue(gDude, gSkilldexSkills[index]);
2022-08-06 08:21:47 -07:00
// SFALL: Fix for negative values.
//
// NOTE: Sfall's fix is different. It simply renders 0 even if
// calculated skill level is negative (which can be the case when
// playing on Hard difficulty + low Agility). For unknown reason -5 is
// the error code from `skillGetValue`, which is not handled here
// because -5 is also a legitimate skill value.
//
// TODO: Provide other error code in `skillGetValue`.
unsigned char* numbersFrmData = _skilldexFrmImages[SKILLDEX_FRM_BIG_NUMBERS].getData();
2022-08-06 08:21:47 -07:00
if (value < 0) {
// First half of the bignum.frm is white, second half is red.
numbersFrmData += _skilldexFrmImages[SKILLDEX_FRM_BIG_NUMBERS].getWidth() / 2;
2022-08-06 08:21:47 -07:00
value = -value;
2022-05-19 01:51:26 -07:00
}
int hundreds = value / 100;
2022-08-06 08:21:47 -07:00
blitBufferToBuffer(numbersFrmData + 14 * hundreds,
2022-05-19 01:51:26 -07:00
14,
24,
336,
gSkilldexWindowBuffer + _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() * valueY + 110,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth());
2022-05-19 01:51:26 -07:00
int tens = (value % 100) / 10;
2022-08-06 08:21:47 -07:00
blitBufferToBuffer(numbersFrmData + 14 * tens,
2022-05-19 01:51:26 -07:00
14,
24,
336,
gSkilldexWindowBuffer + _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() * valueY + 124,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth());
2022-05-19 01:51:26 -07:00
int ones = (value % 100) % 10;
2022-08-06 08:21:47 -07:00
blitBufferToBuffer(numbersFrmData + 14 * ones,
2022-05-19 01:51:26 -07:00
14,
24,
336,
gSkilldexWindowBuffer + _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() * valueY + 138,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth());
2022-05-19 01:51:26 -07:00
valueY += 36;
}
// Render skill buttons.
int lineHeight = fontGetLineHeight();
int buttonY = 45;
int nameY = ((_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getHeight() - lineHeight) / 2) + 1;
2022-05-19 01:51:26 -07:00
for (int index = 0; index < SKILLDEX_SKILL_COUNT; index++) {
char name[MESSAGE_LIST_ITEM_FIELD_MAX_SIZE];
strcpy(name, getmsg(&gSkilldexMessageList, &gSkilldexMessageListItem, 102 + index));
int nameX = ((_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth() - fontGetStringWidth(name)) / 2) + 1;
2022-05-19 01:51:26 -07:00
if (nameX < 0) {
nameX = 0;
}
fontDrawText(gSkilldexButtonsData[index * 2] + _skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getWidth() * nameY + nameX,
2022-05-19 01:51:26 -07:00
name,
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_ON].getWidth(),
2022-05-19 01:51:26 -07:00
_colorTable[18979]);
fontDrawText(gSkilldexButtonsData[index * 2 + 1] + _skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth() * nameY + nameX,
2022-05-19 01:51:26 -07:00
name,
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth(),
2022-05-19 01:51:26 -07:00
_colorTable[14723]);
int btn = buttonCreate(gSkilldexWindow,
15,
buttonY,
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BUTTON_OFF].getHeight(),
2022-05-19 01:51:26 -07:00
-1,
-1,
-1,
501 + index,
gSkilldexButtonsData[index * 2],
gSkilldexButtonsData[index * 2 + 1],
NULL,
BUTTON_FLAG_TRANSPARENT);
if (btn != -1) {
buttonSetCallbacks(btn, _gsound_lrg_butt_press, _gsound_lrg_butt_release);
}
buttonY += 36;
}
// Render "CANCEL" button.
char* cancel = getmsg(&gSkilldexMessageList, &gSkilldexMessageListItem, 101);
fontDrawText(gSkilldexWindowBuffer + _skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth() * 337 + 72,
2022-05-19 01:51:26 -07:00
cancel,
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_BACKGROUND].getWidth(),
2022-05-19 01:51:26 -07:00
_colorTable[18979]);
int cancelBtn = buttonCreate(gSkilldexWindow,
48,
338,
_skilldexFrmImages[SKILLDEX_FRM_LITTLE_RED_BUTTON_UP].getWidth(),
_skilldexFrmImages[SKILLDEX_FRM_LITTLE_RED_BUTTON_UP].getHeight(),
2022-05-19 01:51:26 -07:00
-1,
-1,
-1,
500,
_skilldexFrmImages[SKILLDEX_FRM_LITTLE_RED_BUTTON_UP].getData(),
_skilldexFrmImages[SKILLDEX_FRM_LITTLE_RED_BUTTON_DOWN].getData(),
2022-05-19 01:51:26 -07:00
NULL,
BUTTON_FLAG_TRANSPARENT);
if (cancelBtn != -1) {
buttonSetCallbacks(cancelBtn, _gsound_red_butt_press, _gsound_red_butt_release);
}
windowRefresh(gSkilldexWindow);
return 0;
}
// 0x4AC67C
2022-06-18 22:18:05 -07:00
static void skilldexWindowFree()
2022-05-19 01:51:26 -07:00
{
windowDestroy(gSkilldexWindow);
for (int index = 0; index < SKILLDEX_SKILL_BUTTON_BUFFER_COUNT; index++) {
internal_free(gSkilldexButtonsData[index]);
}
for (int index = 0; index < SKILLDEX_FRM_COUNT; index++) {
_skilldexFrmImages[index].unlock();
2022-05-19 01:51:26 -07:00
}
messageListFree(&gSkilldexMessageList);
fontSetCurrent(gSkilldexWindowOldFont);
if (gSkilldexWindowIsoWasEnabled) {
isoEnable();
}
colorCycleEnable();
gameMouseSetCursor(MOUSE_CURSOR_ARROW);
}
2022-09-23 05:43:44 -07:00
} // namespace fallout