- Le jeu copie des bandes de lignes de PANEL1.PCX (verbes en texte) et PANEL2.PCX (icônes) sous le décor (lignes 140-199) ; le panneau d'options en superpose plusieurs, des deux planches. hd.c rapproche chaque ligne d'écran de sa ligne de planche (correspondance précédente, suite de la ligne du dessus, sinon recherche avec premier tri sur 8 pixels) et fournit des bandes (planche, ligne source) ; ce qui est dessiné par-dessus reste au jeu. - gfx_gl.c : deuxième couche HD, une bande par quad (u_vrange dans FS_HD), dessinée entre le décor et les sprites ; fonctionne aussi sans décor HD. - Menu F12 : « Barre d'actions HD » (clé display.hd_panel), sous « Pack HD ». Pas des lignes du menu 34 -> 32 px pour la place. - hd_export.py : PANEL1, PANEL2 exportés par défaut avec les décors. - docs/SETTINGS.md, docs/MODS.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
345 lines
13 KiB
C
345 lines
13 KiB
C
/* Pack HD : voir hd.h. */
|
||
#include <ctype.h>
|
||
#include <math.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include "hd.h"
|
||
#include "mods.h"
|
||
#include "rt.h"
|
||
|
||
#define W0 320
|
||
#define H0 200
|
||
#define NSTRIPS 4
|
||
#define NSAMPLES 512
|
||
|
||
typedef struct {
|
||
int id, parts;
|
||
char stem[16]; /* nom sans la lettre finale (LOC03 pour LOC03A, LOC03B…) */
|
||
char last; /* lettre du dernier morceau, 0 si nom sans lettre */
|
||
char png[HD_MAX_PARTS][600];
|
||
uint8_t *pix; /* parts × 320 × 200, ligne par ligne */
|
||
uint8_t pal[768]; /* palette 8 bits du premier morceau */
|
||
uint8_t used[256]; /* indices présents dans le décor */
|
||
int last_off, last_score;
|
||
} Strip;
|
||
|
||
static Strip strips[NSTRIPS]; /* strips[0] = le plus récent */
|
||
static int enabled = 1, next_id = 1;
|
||
static int sx[NSAMPLES], sy[NSAMPLES];
|
||
static const char *png_ptr[HD_MAX_PARTS];
|
||
|
||
/* planches de la barre d'actions (voir hd.h) */
|
||
#define PANEL_ROWS 188 /* lignes utiles des planches PANEL (le reste est vide) */
|
||
#define ROW_PROBE 8 /* premier tri : 8 pixels par ligne */
|
||
#define ROW_SAMPLES 40 /* puis 40 */
|
||
#define ROW_MATCH 30 /* 75 % : les objets d'inventaire et le texte couvrent une partie de la ligne */
|
||
static const char *const panel_names[HD_PANELS] = {"PANEL1", "PANEL2"};
|
||
typedef struct {
|
||
int loaded;
|
||
char png[600];
|
||
uint8_t pix[W0 * H0];
|
||
uint8_t pal[768], used[256];
|
||
uint8_t flat[H0]; /* ligne d'une seule couleur : ne permet pas de reconnaître la ligne */
|
||
} Panel;
|
||
static Panel panels[HD_PANELS];
|
||
static int panel_enabled = 1, panel_id = 1;
|
||
static signed char row_sheet[H0]; /* dernière correspondance de chaque ligne d'écran ; -1 : aucune */
|
||
static uint8_t row_src[H0];
|
||
static const char *panel_ptr[HD_PANELS];
|
||
|
||
void hd_set_enabled(int on) { enabled = on; }
|
||
void hd_set_panel_enabled(int on) { panel_enabled = on; }
|
||
int hd_enabled(void) { return enabled; }
|
||
|
||
void hd_reset(void) {
|
||
int i;
|
||
for (i = 0; i < NSTRIPS; i++) { free(strips[i].pix); memset(&strips[i], 0, sizeof strips[i]); }
|
||
for (i = 0; i < HD_PANELS; i++) panels[i].loaded = 0;
|
||
}
|
||
|
||
/* planche de la barre d'actions : 1 si le nom en est une (chargée ou non) */
|
||
static int panel_opened(const char *name, const char *host_path, const char *png) {
|
||
int k, y, x;
|
||
Panel *p;
|
||
for (k = 0; k < HD_PANELS && strcmp(name, panel_names[k]); k++) {}
|
||
if (k == HD_PANELS) return 0;
|
||
p = &panels[k];
|
||
if (p->loaded && !strcmp(p->png, png)) return 1; /* rouverte à chaque changement de pièce */
|
||
p->loaded = 0;
|
||
if (!hd_read_pcx(host_path, p->pix, p->pal)) return 1;
|
||
memset(p->used, 0, sizeof p->used);
|
||
for (y = 0; y < H0; y++) {
|
||
const uint8_t *row = p->pix + y * W0;
|
||
p->flat[y] = 1;
|
||
for (x = 0; x < W0; x++) {
|
||
p->used[row[x]] = 1;
|
||
if (row[x] != row[0]) p->flat[y] = 0;
|
||
}
|
||
}
|
||
snprintf(p->png, sizeof p->png, "%s", png);
|
||
p->loaded = 1;
|
||
panel_id++;
|
||
memset(row_sheet, -1, sizeof row_sheet);
|
||
if (rt_cfg.verbose) fprintf(stderr, "[hd] %s : barre d'actions (%s)\n", name, png);
|
||
return 1;
|
||
}
|
||
|
||
/* PCX 320×200 8 bits → indices + palette ; 0 si format inattendu */
|
||
int hd_read_pcx(const char *path, uint8_t *pix, uint8_t *pal) {
|
||
FILE *f = fopen(path, "rb");
|
||
uint8_t *d;
|
||
long len;
|
||
int ok = 0;
|
||
if (!f) return 0;
|
||
fseek(f, 0, SEEK_END);
|
||
len = ftell(f);
|
||
fseek(f, 0, SEEK_SET);
|
||
d = (len > 128 + 769 && len < (1 << 22)) ? (uint8_t *)malloc((size_t)len) : NULL;
|
||
if (d && fread(d, 1, (size_t)len, f) == (size_t)len && d[0] == 10 && d[3] == 8 && d[65] == 1 && d[len - 769] == 12) {
|
||
int w = (d[8] | d[9] << 8) - (d[4] | d[5] << 8) + 1, h = (d[10] | d[11] << 8) - (d[6] | d[7] << 8) + 1;
|
||
int bpl = d[66] | d[67] << 8, y = 0, x = 0;
|
||
long i = 128, end = len - 769;
|
||
if (w == W0 && h == H0 && bpl >= W0) {
|
||
while (y < H0 && i < end) {
|
||
int b = d[i++], run = 1;
|
||
if (b >= 0xc0) { run = b & 0x3f; b = i < end ? d[i++] : 0; }
|
||
while (run-- > 0 && y < H0) {
|
||
if (x < W0) pix[y * W0 + x] = (uint8_t)b;
|
||
if (++x == bpl) { x = 0; y++; }
|
||
}
|
||
}
|
||
memcpy(pal, d + len - 768, 768);
|
||
ok = y == H0;
|
||
}
|
||
}
|
||
free(d);
|
||
fclose(f);
|
||
return ok;
|
||
}
|
||
|
||
/* « LOC03A.PCX » → racine « LOC03 », lettre 'A' ; nom sans lettre finale → lettre 0 */
|
||
static void split_name(const char *base, char *stem, size_t n, char *letter) {
|
||
size_t len = 0;
|
||
while (base[len] && base[len] != '.') len++;
|
||
if (len >= n) len = n - 1;
|
||
memcpy(stem, base, len);
|
||
stem[len] = 0;
|
||
*letter = 0;
|
||
if (len >= 2 && stem[len - 1] >= 'A' && stem[len - 1] <= 'D' && isdigit((unsigned char)stem[len - 2])) {
|
||
*letter = stem[len - 1];
|
||
stem[len - 1] = 0;
|
||
}
|
||
}
|
||
|
||
void hd_file_opened(const char *rel, const char *host_path) {
|
||
const char *base = strrchr(rel, '\\');
|
||
char stem[16], letter, name[20], png[600];
|
||
uint8_t *part;
|
||
Strip *s = &strips[0];
|
||
size_t len;
|
||
base = base ? base + 1 : rel;
|
||
len = strlen(base);
|
||
if (len < 5 || strcmp(base + len - 4, ".PCX")) return; /* noté même désactivé : réactivable en jeu */
|
||
snprintf(name, sizeof name, "%.*s", (int)(len - 4), base);
|
||
if (!mods_find_hd(name, png, sizeof png)) return;
|
||
if (panel_opened(name, host_path, png)) return;
|
||
split_name(base, stem, sizeof stem, &letter);
|
||
/* morceau suivant d'une pièce en plusieurs parties (LOCxxB juste après LOCxxA…) */
|
||
if (!(letter && s->pix && s->last && letter == s->last + 1 && s->parts < HD_MAX_PARTS && !strcmp(s->stem, stem))) {
|
||
free(strips[NSTRIPS - 1].pix);
|
||
memmove(&strips[1], &strips[0], (NSTRIPS - 1) * sizeof strips[0]);
|
||
memset(s, 0, sizeof *s);
|
||
snprintf(s->stem, sizeof s->stem, "%s", stem);
|
||
}
|
||
part = (uint8_t *)malloc(W0 * H0);
|
||
if (!part) return;
|
||
{
|
||
static uint8_t other_pal[768]; /* les morceaux suivants partagent la palette du premier */
|
||
if (!hd_read_pcx(host_path, part, s->parts ? other_pal : s->pal)) { free(part); return; }
|
||
}
|
||
{
|
||
int n = s->parts + 1, y;
|
||
uint8_t *pix = (uint8_t *)malloc((size_t)n * W0 * H0);
|
||
if (!pix) { free(part); return; }
|
||
for (y = 0; y < H0; y++) {
|
||
if (s->parts) memcpy(pix + (size_t)y * n * W0, s->pix + (size_t)y * s->parts * W0, (size_t)s->parts * W0);
|
||
memcpy(pix + (size_t)y * n * W0 + (size_t)s->parts * W0, part + y * W0, W0);
|
||
}
|
||
for (y = 0; y < W0 * H0; y++) s->used[part[y]] = 1;
|
||
free(s->pix);
|
||
free(part);
|
||
s->pix = pix;
|
||
snprintf(s->png[s->parts], sizeof s->png[0], "%s", png);
|
||
s->parts = n;
|
||
s->last = letter;
|
||
s->id = next_id++;
|
||
s->last_score = 0;
|
||
if (rt_cfg.verbose) fprintf(stderr, "[hd] %s : morceau %d (%s)\n", stem, n, png);
|
||
}
|
||
}
|
||
|
||
static void init_samples(void) {
|
||
uint32_t r = 12345;
|
||
int i;
|
||
for (i = 0; i < NSAMPLES; i++) {
|
||
r = r * 1103515245u + 12345u; sx[i] = (int)((r >> 8) % W0);
|
||
r = r * 1103515245u + 12345u; sy[i] = (int)((r >> 8) % H0);
|
||
}
|
||
}
|
||
|
||
static int score_at(const Strip *s, const uint8_t *fb, int off) {
|
||
int i, n = 0, w = s->parts * W0;
|
||
for (i = 0; i < NSAMPLES; i++) n += fb[sy[i] * W0 + sx[i]] == s->pix[sy[i] * w + sx[i] + off];
|
||
return n;
|
||
}
|
||
|
||
/* fondu : rapport entre palette courante et palette du décor, s'il s'agit bien d'un simple facteur */
|
||
static int palette_tint(const uint8_t *used, const uint8_t *pal, const uint8_t *dac, float tint[3]) {
|
||
double cur[3] = {0}, org[3] = {0}, err = 0, tot = 0;
|
||
int i, c;
|
||
for (i = 0; i < 256; i++)
|
||
if (used[i])
|
||
for (c = 0; c < 3; c++) {
|
||
int v = dac[i * 3 + c] & 63;
|
||
cur[c] += (v << 2 | v >> 4);
|
||
org[c] += pal[i * 3 + c];
|
||
}
|
||
for (c = 0; c < 3; c++) tint[c] = (float)((cur[c] + 1.0) / (org[c] + 1.0));
|
||
for (i = 0; i < 256; i++)
|
||
if (used[i])
|
||
for (c = 0; c < 3; c++) {
|
||
int v = dac[i * 3 + c] & 63;
|
||
err += fabs((v << 2 | v >> 4) - tint[c] * pal[i * 3 + c]);
|
||
tot += pal[i * 3 + c];
|
||
}
|
||
return tot <= 0 || err / tot < 0.12;
|
||
}
|
||
|
||
static int compose_decor(const uint8_t *fb, const uint8_t *dac, uint8_t *mask, HdFrame *out) {
|
||
int k, best_k = -1, best_off = 0, best = 0, x, y, i;
|
||
if (!enabled || !strips[0].pix) return 0;
|
||
if (!sx[0] && !sy[0]) init_samples();
|
||
for (k = 0; k < NSTRIPS; k++) {
|
||
Strip *s = &strips[k];
|
||
int off, maxoff, sc;
|
||
if (!s->pix) continue;
|
||
maxoff = (s->parts - 1) * W0;
|
||
/* d'abord le décalage précédent : en général rien n'a bougé */
|
||
sc = score_at(s, fb, s->last_off);
|
||
if (s->last_score && sc * 10 >= s->last_score * 9) off = s->last_off;
|
||
else {
|
||
int o;
|
||
off = s->last_off;
|
||
for (o = 0; o <= maxoff; o++) {
|
||
int v = o == s->last_off ? sc : score_at(s, fb, o);
|
||
if (v > sc) { sc = v; off = o; }
|
||
}
|
||
}
|
||
if (sc > best) { best = sc; best_k = k; best_off = off; }
|
||
s->last_off = off;
|
||
s->last_score = sc;
|
||
}
|
||
if (best_k < 0 || best * 100 < NSAMPLES * 35) return 0; /* le décor n'est pas à l'écran */
|
||
{
|
||
Strip *s = &strips[best_k];
|
||
int w = s->parts * W0;
|
||
if (!palette_tint(s->used, s->pal, dac, out->tint)) return 0;
|
||
for (y = 0; y < H0; y++) {
|
||
const uint8_t *bg = s->pix + (size_t)y * w + best_off, *row = fb + y * W0;
|
||
uint8_t *m = mask + y * W0;
|
||
for (x = 0; x < W0; x++) m[x] = row[x] == bg[x] ? 0 : 255;
|
||
}
|
||
out->active = 1;
|
||
out->set_id = s->id;
|
||
out->parts = s->parts;
|
||
for (i = 0; i < s->parts; i++) png_ptr[i] = s->png[i];
|
||
for (i = 0; i < s->parts; i++) out->png[i] = png_ptr[i];
|
||
out->offset = best_off;
|
||
out->bg = s->pix + best_off;
|
||
out->bg_stride = w;
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
/* ------------------------------------------------------------------ barre d'actions */
|
||
static int row_score(const uint8_t *a, const uint8_t *b, int need_probe) {
|
||
int i, n = 0;
|
||
for (i = 0; i < ROW_PROBE; i++) n += a[20 + i * 40] == b[20 + i * 40];
|
||
if (n < need_probe) return 0;
|
||
for (i = n = 0; i < ROW_SAMPLES; i++) n += a[4 + i * 8] == b[4 + i * 8];
|
||
return n;
|
||
}
|
||
|
||
/* ligne d'écran y → (planche, ligne) : correspondance précédente, sinon suite de la ligne du dessus, sinon
|
||
* recherche dans les deux planches (8 pixels d'abord, pour écarter vite les mauvaises lignes) */
|
||
static void match_row(const uint8_t *row, int y) {
|
||
int k = row_sheet[y], py = row_src[y], kk, pp, best = 0;
|
||
if (k >= 0 && panels[k].loaded && row_score(row, panels[k].pix + py * W0, 0) >= ROW_MATCH) return;
|
||
k = y > HD_PANEL_Y0 ? row_sheet[y - 1] : -1;
|
||
py = k >= 0 ? row_src[y - 1] + 1 : 0;
|
||
if (k >= 0 && py < PANEL_ROWS && !panels[k].flat[py] && row_score(row, panels[k].pix + py * W0, 0) >= ROW_MATCH) {
|
||
row_sheet[y] = (signed char)k;
|
||
row_src[y] = (uint8_t)py;
|
||
return;
|
||
}
|
||
row_sheet[y] = -1;
|
||
for (kk = 0; kk < HD_PANELS; kk++) {
|
||
if (!panels[kk].loaded) continue;
|
||
for (pp = 0; pp < PANEL_ROWS; pp++) {
|
||
int v;
|
||
if (panels[kk].flat[pp]) continue;
|
||
v = row_score(row, panels[kk].pix + pp * W0, ROW_PROBE * 5 / 8);
|
||
if (v >= ROW_MATCH && v > best) { best = v; row_sheet[y] = (signed char)kk; row_src[y] = (uint8_t)pp; }
|
||
}
|
||
}
|
||
}
|
||
|
||
static int compose_panel(const uint8_t *fb, const uint8_t *dac, HdFrame *out) {
|
||
int y, k, n = 0, used_sheet = -1;
|
||
if (!panel_enabled) return 0;
|
||
for (k = 0; k < HD_PANELS && !panels[k].loaded; k++) {}
|
||
if (k == HD_PANELS) return 0;
|
||
for (y = HD_PANEL_Y0; y < H0; y++) {
|
||
match_row(fb + y * W0, y);
|
||
k = row_sheet[y];
|
||
if (k < 0) continue;
|
||
if (n && out->runs[n - 1].y1 == y && out->runs[n - 1].sheet == k
|
||
&& out->runs[n - 1].src_y + (y - out->runs[n - 1].y0) == row_src[y])
|
||
out->runs[n - 1].y1 = y + 1;
|
||
else if (n < HD_MAX_RUNS) {
|
||
out->runs[n].y0 = y;
|
||
out->runs[n].y1 = y + 1;
|
||
out->runs[n].sheet = k;
|
||
out->runs[n].src_y = row_src[y];
|
||
n++;
|
||
}
|
||
if (used_sheet < 0) used_sheet = k;
|
||
}
|
||
if (!n || !palette_tint(panels[used_sheet].used, panels[used_sheet].pal, dac, out->panel_tint)) return 0;
|
||
out->nruns = n;
|
||
out->panel_id = panel_id;
|
||
for (k = 0; k < HD_PANELS; k++) {
|
||
panel_ptr[k] = panels[k].loaded ? panels[k].png : NULL;
|
||
out->panel_png[k] = panel_ptr[k];
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
int hd_compose(const uint8_t *fb, const uint8_t *dac, uint8_t *mask, HdFrame *out) {
|
||
int decor, i, y, x;
|
||
memset(out, 0, sizeof *out);
|
||
decor = compose_decor(fb, dac, mask, out);
|
||
if (!compose_panel(fb, dac, out) && !decor) return 0;
|
||
if (!decor) memset(mask, 255, W0 * H0);
|
||
for (i = 0; i < out->nruns; i++) {
|
||
const HdRun *r = &out->runs[i];
|
||
for (y = r->y0; y < r->y1; y++) {
|
||
const uint8_t *src = panels[r->sheet].pix + (r->src_y + y - r->y0) * W0, *row = fb + y * W0;
|
||
uint8_t *m = mask + y * W0;
|
||
for (x = 0; x < W0; x++) m[x] = row[x] == src[x] ? 0 : 255;
|
||
}
|
||
}
|
||
return 1;
|
||
}
|