ajbowen249 / dungeon-delver-engine

A Tandy Model 100 and ZX Spectrum implementation of OGL 5.1 in Assembly
4 stars 1 forks source link

Initial text compressor #4

Closed ajbowen249 closed 5 months ago

ajbowen249 commented 5 months ago

Adds basic text compression. Algorithm works by brute-force finding the best sequence of characters to commonize and pulling it out to a table until it can't find any left that would save any space. "Best" in this case is defined by the formula:

(l c) - (2 c) - l - 1

Where l is the length of the string, and c is the number of time it's used. This is the number of bytes saved by extracting one instance of that string to a table and replacing all instances of it with a two-byte reference ID. -1 more for the additional null terminator.

This also adds print_compressed_string and updates BLOCK_PRINT to use it.

The algorithm likely has room for improvement, but saves approximately 335 bytes of text (62ish of which are taken back up by print_compressed_string) in the "flagship" project (no the test campaign) for this library.