Skip to content

Commit

Permalink
Merge pull request #6 from thekovic/hash-fix
Browse files Browse the repository at this point in the history
Fix hash calculation for texture names that are too long
  • Loading branch information
Immorpher authored Feb 19, 2024
2 parents b64ca8f + 4696595 commit e0d9078
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions graphics/textures/d64hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@
QForm = {};

QForm.onEncode = function () {

var texture = document.getElementById("texname").value; // get the texture name
var hash = 1315423911; // set initial hash value
var i = 0;

texture = texture.toUpperCase(); // convert to upper case
// get the texture name and convert to upper case
var texture = document.getElementById("texname").value.toUpperCase();
// clamp the number of characters we check to 8 because lumpinfo_t
// can only store 8 bytes (texture names in WAD can only be 8 characters long)
var repetitions = texture.length < 8 ? texture.length : 8;
// set initial hash value
var hash = 1315423911;

for (let i = 0; i < texture.length; i++)
for (let i = 0; i < repetitions; i++)
{
hash ^= ((hash << 5) + texture.charCodeAt(i) + (hash >> 2));
}

hash = (hash >>>0) % 65536; // make it show as an unsigned int

// Special case for blank texture
if (texture == "BLANK")
{
hash = -6735;
}


// make it show as 16-bit unsigned integer
hash = (hash >>> 0) % 65536;

document.getElementById("hashnum").value = hash;

document.getElementById("hashnum").value = hash;
};


0 comments on commit e0d9078

Please sign in to comment.