adjusted huffman decode routine

This commit is contained in:
Denis Molony 2022-06-04 13:06:20 +10:00
parent f5664a9ce9
commit 7d4d8c75e6
7 changed files with 100 additions and 46 deletions

View File

@ -266,12 +266,13 @@ public class CharacterV4 extends Character
if (!party.slogan.isEmpty () || party.characters.size () > 1) if (!party.slogan.isEmpty () || party.characters.size () > 1)
{ {
text.append ("\n"); for (int i = possessionsCount; i < 9; i++)
text.append ("\n");
text.append (party); text.append (party);
} }
text.append ("\n\n"); // text.append ("\n\n");
text.append (HexFormatter.format (buffer, 1, buffer[0] & 0xFF)); // text.append (HexFormatter.format (buffer, 1, buffer[0] & 0xFF));
return text.toString (); return text.toString ();
} }

View File

@ -32,7 +32,7 @@ class Huffman extends AbstractFile
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
byte[] decodeMessage (byte[] buffer, int offset, int length) byte[] decodeMessageOld (byte[] buffer, int offset, int length)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
this.message = buffer; this.message = buffer;
@ -54,6 +54,27 @@ class Huffman extends AbstractFile
return returnBuffer; return returnBuffer;
} }
// ---------------------------------------------------------------------------------//
byte[] decodeMessage (byte[] buffer, int offset)
// ---------------------------------------------------------------------------------//
{
this.message = buffer;
depth = 0;
msgPtr = offset;
currentByte = 0;
int size = (getChar () & 0xFF) + 1;
byte[] returnBuffer = new byte[size];
returnBuffer[0] = (byte) size;
int ptr = 1;
while (ptr < size)
returnBuffer[ptr++] = getChar ();
return returnBuffer;
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
String decodeMessage (byte[] message) String decodeMessage (byte[] message)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -93,7 +114,7 @@ class Huffman extends AbstractFile
currentByte = message[msgPtr++]; // ...get a new byte currentByte = message[msgPtr++]; // ...get a new byte
int currentBit = currentByte & 0x01; // extract the next bit to process int currentBit = currentByte & 0x01; // extract the next bit to process
currentByte >>= 1; // and remove it from the current byte currentByte >>>= 1; // and remove it from the current byte
// use currentBit to determine whether to use the left or right node // use currentBit to determine whether to use the left or right node
byte nodeValue = buffer[treePtr + offset[currentBit]]; byte nodeValue = buffer[treePtr + offset[currentBit]];
@ -118,6 +139,7 @@ class Huffman extends AbstractFile
walk (0, "", text); walk (0, "", text);
bufferContents = text.toString (); bufferContents = text.toString ();
} }
return bufferContents; return bufferContents;
} }

View File

@ -21,9 +21,9 @@ public class Item extends AbstractFile
int spellPwr; int spellPwr;
int classUseFlags; int classUseFlags;
int healPts; int healPts;
int wepvstyFlags; int flags1;
int wepvsty2Flags; int flags2;
int wepvsty3Flags; int flags3;
int wephitmd; int wephitmd;
boolean crithitm; boolean crithitm;
@ -32,6 +32,7 @@ public class Item extends AbstractFile
Item changeToItem; Item changeToItem;
Spell spell; Spell spell;
String spellName;
public enum Alignment public enum Alignment
{ {
@ -80,13 +81,15 @@ public class Item extends AbstractFile
text.append (String.format ("Boltac ........... %d%n", boltac)); text.append (String.format ("Boltac ........... %d%n", boltac));
String spellName = spell == null ? "" : spell.getName (); String spellName = spell == null ? "" : spell.getName ();
if (this.spellName != null)
spellName = this.spellName;
text.append (String.format ("Spell ............ %s%n", spellName)); text.append (String.format ("Spell ............ %s%n", spellName));
text.append (String.format ("Heal ............. %d%n", healPts)); text.append (String.format ("Heal ............. %d%n", healPts));
text.append (String.format ("Class use ........ %d%n", classUseFlags)); text.append (String.format ("Class use ........ %d%n", classUseFlags));
text.append (String.format ("Flags ............ %d%n", wepvstyFlags)); text.append (String.format ("Flags ............ %d%n", flags1));
text.append (String.format ("Flags2 ........... %d%n", wepvsty2Flags)); text.append (String.format ("Flags2 ........... %d%n", flags2));
text.append (String.format ("Flags3 ........... %d%n", wepvsty3Flags)); text.append (String.format ("Flags3 ........... %d%n", flags3));
return text.toString (); return text.toString ();
} }

View File

@ -21,14 +21,6 @@ class ItemV1 extends Item // implements Comparable<ItemV1>
itemId = counter++; itemId = counter++;
genericName = HexFormatter.getPascalString (buffer, 16); genericName = HexFormatter.getPascalString (buffer, 16);
// type = buffer[32];
// cost = getWizLong (buffer, 44);
// cost = Utility.getShort (buffer, 44) + Utility.getShort (buffer, 46) * 10000
// + Utility.getShort (buffer, 48) * 100000000L;
// armourClass = buffer[62];
// wephpdam = new Dice (buffer, 66);
// xtraSwing = buffer[72]; // 14 flags
type = ObjectType.values ()[buffer[32]]; type = ObjectType.values ()[buffer[32]];
alignment = Alignment.values ()[buffer[34]]; alignment = Alignment.values ()[buffer[34]];
cursed = Utility.signedShort (buffer, 36) == -1; cursed = Utility.signedShort (buffer, 36) == -1;
@ -41,14 +33,14 @@ class ItemV1 extends Item // implements Comparable<ItemV1>
classUseFlags = Utility.getShort (buffer, 54); // 8 flags classUseFlags = Utility.getShort (buffer, 54); // 8 flags
healPts = Utility.signedShort (buffer, 56); healPts = Utility.signedShort (buffer, 56);
wepvsty2Flags = Utility.getShort (buffer, 58); // 16 flags flags2 = Utility.getShort (buffer, 58); // 16 flags
wepvsty3Flags = Utility.getShort (buffer, 60); // 16 flags flags3 = Utility.getShort (buffer, 60); // 16 flags
armourClass = Utility.signedShort (buffer, 62); armourClass = Utility.signedShort (buffer, 62);
wephitmd = Utility.signedShort (buffer, 64); wephitmd = Utility.signedShort (buffer, 64);
wephpdam = new Dice (buffer, 66); // Dice wephpdam = new Dice (buffer, 66); // Dice
xtraSwing = Utility.getShort (buffer, 72); xtraSwing = Utility.getShort (buffer, 72);
crithitm = Utility.getShort (buffer, 74) == 1; // boolean crithitm = Utility.getShort (buffer, 74) == 1; // boolean
wepvstyFlags = Utility.getShort (buffer, 76); // 14 flags flags1 = Utility.getShort (buffer, 76); // 14 flags
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//

View File

@ -1,6 +1,8 @@
package com.bytezone.diskbrowser.wizardry; package com.bytezone.diskbrowser.wizardry;
import com.bytezone.diskbrowser.utilities.HexFormatter; import java.util.List;
import com.bytezone.diskbrowser.utilities.Utility;
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
public class ItemV4 extends Item public class ItemV4 extends Item
@ -17,8 +19,38 @@ public class ItemV4 extends Item
name = names[1]; name = names[1];
genericName = names[0]; genericName = names[0];
type = ObjectType.values ()[buffer[1]];
alignment = Alignment.values ()[buffer[3]];
cursed = Utility.signedShort (buffer, 5) == -1;
special = Utility.signedShort (buffer, 7);
changeTo = Utility.getShort (buffer, 9); // decay #
changeChance = Utility.getShort (buffer, 11);
price = getWizLong (buffer, 13); price = getWizLong (buffer, 13);
boltac = Utility.signedShort (buffer, 19);
spellPwr = Utility.getShort (buffer, 21);
classUseFlags = Utility.getShort (buffer, 23); // 8 flags
healPts = Utility.signedShort (buffer, 25);
flags2 = Utility.getShort (buffer, 27); // 16 flags
flags3 = Utility.getShort (buffer, 29); // 16 flags
armourClass = Utility.signedShort (buffer, 31);
wephitmd = Utility.signedShort (buffer, 33);
wephpdam = new Dice (buffer, 35); wephpdam = new Dice (buffer, 35);
xtraSwing = Utility.getShort (buffer, 41);
crithitm = Utility.getShort (buffer, 43) == 1; // boolean
flags1 = Utility.getShort (buffer, 45); // 14 flags
}
// ---------------------------------------------------------------------------------//
void link (List<ItemV4> items, List<String> spellNames)
// ---------------------------------------------------------------------------------//
{
if (changeChance > 0)
changeToItem = items.get (changeTo);
if (spellPwr > 0)
spellName = spellNames.get (spellPwr);
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -28,8 +60,8 @@ public class ItemV4 extends Item
{ {
StringBuilder text = new StringBuilder (super.getText ()); StringBuilder text = new StringBuilder (super.getText ());
text.append ("\n\n"); // text.append ("\n\n");
text.append (HexFormatter.format (buffer)); // text.append (HexFormatter.format (buffer));
return text.toString (); return text.toString ();
} }

View File

@ -1,6 +1,5 @@
package com.bytezone.diskbrowser.wizardry; package com.bytezone.diskbrowser.wizardry;
import com.bytezone.diskbrowser.utilities.HexFormatter;
import com.bytezone.diskbrowser.utilities.Utility; import com.bytezone.diskbrowser.utilities.Utility;
// -----------------------------------------------------------------------------------// // -----------------------------------------------------------------------------------//
@ -42,18 +41,4 @@ public class MonsterV4 extends Monster
resistance = Utility.getShort (buffer, 73); // bit flags resistance = Utility.getShort (buffer, 73); // bit flags
abilities = Utility.getShort (buffer, 75); // bit flags abilities = Utility.getShort (buffer, 75); // bit flags
} }
// ---------------------------------------------------------------------------------//
@Override
public String getText ()
// ---------------------------------------------------------------------------------//
{
StringBuilder text = new StringBuilder (super.getText ());
text.append ("\n\n");
// text.append (HexFormatter.format (buffer, 1, buffer[0] & 0xFF));
text.append (HexFormatter.format (buffer));
return text.toString ();
}
} }

View File

@ -31,6 +31,7 @@ public class Wizardry4BootDisk extends PascalDisk
private List<CharacterParty> parties = new ArrayList<> (); private List<CharacterParty> parties = new ArrayList<> ();
private List<ItemV4> items = new ArrayList<> (); private List<ItemV4> items = new ArrayList<> ();
private List<MonsterV4> monsters = new ArrayList<> (); private List<MonsterV4> monsters = new ArrayList<> ();
public List<String> spellNames = new ArrayList<> ();
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
public Wizardry4BootDisk (AppleDisk[] dataDisks) public Wizardry4BootDisk (AppleDisk[] dataDisks)
@ -98,6 +99,7 @@ public class Wizardry4BootDisk extends PascalDisk
fileEntry.setFile (null); fileEntry.setFile (null);
scenarioNode.setAllowsChildren (true); scenarioNode.setAllowsChildren (true);
scenarioHeader = new Header (scenarioNode, this); scenarioHeader = new Header (scenarioNode, this);
readSpells ();
linkCharacters4 (scenarioNode, fileEntry); linkCharacters4 (scenarioNode, fileEntry);
linkParties (); linkParties ();
linkMazeLevels4 (scenarioNode, fileEntry); linkMazeLevels4 (scenarioNode, fileEntry);
@ -161,7 +163,7 @@ public class Wizardry4BootDisk extends PascalDisk
for (int i = 0; i < 500; i++) for (int i = 0; i < 500; i++)
{ {
byte[] out = huffman.decodeMessage (buffer, ptr, sd.totalBlocks); byte[] out = huffman.decodeMessage (buffer, ptr);
String name = HexFormatter.getPascalString (out, 1); String name = HexFormatter.getPascalString (out, 1);
@ -201,6 +203,19 @@ public class Wizardry4BootDisk extends PascalDisk
} }
} }
// ---------------------------------------------------------------------------------//
private void readSpells ()
// ---------------------------------------------------------------------------------//
{
for (int i = 0; i < 51; i++)
{
String spellName = messageBlock.getMessageLine (i + 5000);
if (spellName.startsWith ("*"))
spellName = spellName.substring (1);
spellNames.add (spellName);
}
}
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private void link (CharacterV4 character, CharacterParty party) private void link (CharacterV4 character, CharacterParty party)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -232,7 +247,7 @@ public class Wizardry4BootDisk extends PascalDisk
for (int i = 0; i < sd.total; i++) for (int i = 0; i < sd.total; i++)
{ {
byte[] out = huffman.decodeMessage (buffer, ptr, sd.totalBlocks); byte[] out = huffman.decodeMessage (buffer, ptr);
int len = out[0] & 0xFF; int len = out[0] & 0xFF;
if (len > out.length) if (len > out.length)
System.out.printf ("Decoded array too short: (#%3d) %3d > %3d%n", i, len, out.length); System.out.printf ("Decoded array too short: (#%3d) %3d > %3d%n", i, len, out.length);
@ -242,6 +257,7 @@ public class Wizardry4BootDisk extends PascalDisk
MonsterV4 monster = new MonsterV4 (monsterNames, out, i); MonsterV4 monster = new MonsterV4 (monsterNames, out, i);
monsters.add (monster); monsters.add (monster);
// System.out.println (monster.getName ());
List<DiskAddress> monsterBlocks = new ArrayList<> (); List<DiskAddress> monsterBlocks = new ArrayList<> ();
DiskAddress da = blocks.get (ptr / 512); DiskAddress da = blocks.get (ptr / 512);
@ -276,13 +292,13 @@ public class Wizardry4BootDisk extends PascalDisk
for (int i = 0; i < sd.total; i++) for (int i = 0; i < sd.total; i++)
{ {
byte[] out = huffman.decodeMessage (buffer, ptr, sd.totalBlocks); byte[] out = huffman.decodeMessage (buffer, ptr);
for (int j = 0; j < itemNames.length; j++) for (int j = 0; j < itemNames.length; j++)
{ {
itemNames[j] = messageBlock.getMessageLine (i * 2 + 14000 + j); itemNames[j] = messageBlock.getMessageLine (i * 2 + 14000 + j);
if (itemNames[j] == null) if (itemNames[j] == null)
itemNames[j] = "Not found"; itemNames[j] = "Broken Item";
} }
ItemV4 item = new ItemV4 (itemNames, out, i); ItemV4 item = new ItemV4 (itemNames, out, i);
@ -305,8 +321,11 @@ public class Wizardry4BootDisk extends PascalDisk
for (CharacterV4 character : characters) for (CharacterV4 character : characters)
character.addPossessions (items); character.addPossessions (items);
// for (ItemV4 item : items) // for (int i = 0; i < items.size (); i++)
// item.link (items, spells); // System.out.printf ("%3d %s%n", i, items.get (i));
for (ItemV4 item : items)
item.link (items, spellNames);
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//