Files
patman/compress.cpp
2026-01-13 08:01:00 +01:00

153 lines
4.5 KiB
C++

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
char *base64_encode(const unsigned char *data, int len)
{
static const char table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int raw_len = 4 * ((len + 2) / 3); // base64 chars without newlines
int lines = raw_len / 80; // number of full 80-char lines
int out_len = raw_len + lines; // add newline per full line
char *out = (char*)malloc(out_len + 1); // +1 for '\0'
if (!out) return NULL;
int i = 0, j = 0, line_count = 0;
while (i < len) {
unsigned int a = i < len ? data[i++] : 0;
unsigned int b = i < len ? data[i++] : 0;
unsigned int c = i < len ? data[i++] : 0;
unsigned int triple = (a << 16) | (b << 8) | c;
out[j++] = table[(triple >> 18) & 0x3F];
out[j++] = table[(triple >> 12) & 0x3F];
out[j++] = (i > len + 1) ? '=' : table[(triple >> 6) & 0x3F];
out[j++] = (i > len) ? '=' : table[(triple) & 0x3F];
line_count += 4;
if (line_count == 80) {
out[j++] = '\n';
line_count = 0;
}
}
out[j] = '\0';
return out;
}
uint8_t *compress(uint8_t *map, int *size)
{
bool on_zeros = false;
int zero_count = 0;
int &total_size = *size;
uint8_t *compressed = (uint8_t*)malloc(1440);
uint8_t *p = compressed;
for (int i=0;i<1440;++i) {
if (map[i]==0) {
if (on_zeros) {
zero_count++;
if (zero_count==255) {
on_zeros=false;
*(p++) = 255;
total_size++;
}
} else {
on_zeros=true;
zero_count=0;
*(p++) = 0;
total_size++;
}
} else {
if (on_zeros) {
on_zeros=false;
*(p++) = zero_count;
total_size++;
}
*(p++) = map[i];
total_size++;
}
}
return compressed;
}
struct actor_t
{
uint8_t x, y, z, t;
};
int main(int argc, char *argv[])
{
FILE *f;
f = fopen("data/Cuina.map", "rb");
for (int i=0; i<256; ++i) {
bool ignore = false;
char buffer[33]; int size;
fread(buffer, 33, 1, f); size = buffer[0]; for (int j=0; j<size; ++j) buffer[j] = buffer[j+1]; buffer[size]=0;
if (strcmp(buffer, "sense_nom")==0) ignore = true;
int zona = fgetc(f);
if (!ignore) {
printf("name: %s\n", buffer);
printf("id: %i\n", i);
printf("zone: %i\n", zona);
}
uint8_t mapa[1440]; fread(mapa, 12*12*10, 1, f);
/*if (!ignore) {
int zeros=0; for (int z=0;z<10;++z) for (int y=0;y<12;++y) for (int x=0;x<12;++x) if (mapa[x][y][z]!=0) zeros++;
printf(" Size: %i\n", zeros+180);
}*/
if (!ignore) {
printf("map: {\n");
int total_size = 0;
uint8_t *compressed = compress(mapa, &total_size);
char *b64 = base64_encode(compressed, total_size);
printf("%s\n}\n", b64);
free(b64);
free(compressed);
}
uint8_t flags[12][12][10]; fread(flags, 12*12*10, 1, f);
if (!ignore) {
printf("flags: {\n");
int total_size = 0;
uint8_t *compressed = compress(&flags[0][0][0], &total_size);
char *b64 = base64_encode(compressed, total_size);
printf("%s\n}\n", b64);
free(b64);
free(compressed);
}
/*if (!ignore) {
int zeros=0; for (int z=0;z<10;++z) for (int y=0;y<12;++y) for (int x=0;x<12;++x) if (flags[x][y][z]!=0) zeros++;
printf("Zeros: %i\n", zeros);
}*/
fread(buffer, 33, 1, f); size = buffer[0]; for (int j=0; j<size; ++j) buffer[j] = buffer[j+1]; buffer[size]=0;
if (!ignore) printf("bitmap: %s\n", buffer);
uint8_t portes[6];
fread(portes, 6, 1, f);
if (!ignore) {
printf("doors: %i %i %i %i %i %i\n", portes[0],portes[1],portes[2],portes[3],portes[4],portes[5]);
}
actor_t actors[15];
fread(actors, sizeof(actor_t)*15, 1, f);
if (!ignore) {
printf("actors: {\n");
for (int i=0; i<15; ++i) {
if (actors[i].t!=0) printf(" %i %i %i %i\n", actors[i].x, actors[i].y, actors[i].z, actors[i].t);
}
printf("}\n\n");
}
}
fclose(f);
return 0;
}