This commit is contained in:
Denis Molony 2016-09-19 19:09:41 +10:00
parent 9cfb15f01a
commit 8b6db14ead
2 changed files with 53 additions and 51 deletions

View File

@ -1,24 +1,27 @@
package com.bytezone.diskbrowser.wizardry; package com.bytezone.diskbrowser.wizardry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import com.bytezone.common.Utility; import com.bytezone.common.Utility;
import com.bytezone.diskbrowser.applefile.AbstractFile; import com.bytezone.diskbrowser.applefile.AbstractFile;
import com.bytezone.diskbrowser.utilities.HexFormatter; import com.bytezone.diskbrowser.utilities.HexFormatter;
public class Wiz5Monsters extends AbstractFile public class Wiz5Monsters extends AbstractFile implements Iterable<Wiz5Monsters.Monster>
{ {
List<Monster> monsters = new ArrayList<Monster> (); private static final int BLOCK_SIZE = 512;
private final List<Monster> monsters = new ArrayList<Monster> ();
public Wiz5Monsters (String name, byte[] buffer) public Wiz5Monsters (String name, byte[] buffer)
{ {
super (name, buffer); super (name, buffer);
int p = 0; int p = 0;
int nextBlock = buffer[0] & 0xFF; int nextBlock = buffer[p] & 0xFF;
int nextOffset = Utility.getWord (buffer, 256); int nextOffset = Utility.getWord (buffer, 256);
Monster monster = new Monster (1, nextBlock * 512 + nextOffset);
Monster monster = new Monster (p + 1);
monsters.add (monster); monsters.add (monster);
boolean createMonster = false; boolean createMonster = false;
@ -27,36 +30,38 @@ public class Wiz5Monsters extends AbstractFile
int firstBlock = nextBlock; int firstBlock = nextBlock;
int firstOffset = nextOffset; int firstOffset = nextOffset;
int ndx = nextBlock * 512 + nextOffset; int ndx = nextBlock * BLOCK_SIZE + nextOffset;
if (buffer[ndx] == (byte) 0) if (buffer[ndx] != (byte) 0)
{
nextBlock = buffer[++p] & 0xFF;
nextOffset = Utility.getWord (buffer, p * 2 + 256);
createMonster = true;
}
else
{ {
nextBlock = buffer[ndx] & 0xFF; nextBlock = buffer[ndx] & 0xFF;
nextOffset = Utility.getWord (buffer, ndx + 1); nextOffset = Utility.getWord (buffer, ndx + 1);
} }
else
{
nextBlock = buffer[++p] & 0xFF;
nextOffset = Utility.getWord (buffer, p * 2 + 256);
createMonster = true;
}
int length = nextOffset > 0 ? nextOffset : 512 - firstOffset; int length = nextOffset > 0 ? nextOffset : BLOCK_SIZE - firstOffset;
monster.dataBuffers.add (new DataBuffer (firstBlock, firstOffset, length));
Buffer monsterBuffer = new Buffer (firstBlock, firstOffset, length);
monster.buffers.add (monsterBuffer);
// System.out.println (monsterBuffer);
if (createMonster && nextBlock > 0) if (createMonster && nextBlock > 0)
{ {
createMonster = false; monster = new Monster (p + 1);
monster = new Monster (p + 1, nextBlock * 512 + nextOffset);
monsters.add (monster); monsters.add (monster);
createMonster = false;
} }
} }
} }
@Override
public Iterator<Monster> iterator ()
{
return monsters.iterator ();
}
@Override @Override
public String getText () public String getText ()
{ {
@ -64,13 +69,13 @@ public class Wiz5Monsters extends AbstractFile
for (Monster monster : monsters) for (Monster monster : monsters)
{ {
text.append ( DataBuffer dataBuffer = monster.dataBuffers.get (0);
String.format ("%02X : %02X %04X : %s%n", monster.id, monster.offset / 512, text.append (String.format ("%02X : %02X %04X : %s%n", monster.id, dataBuffer.block,
monster.offset % 512, monster.buffers.get (0).toHexString ())); dataBuffer.offset, dataBuffer.toHexString ()));
for (int i = 1; i < monster.buffers.size (); i++) for (int i = 1; i < monster.dataBuffers.size (); i++)
{ {
Buffer monsterBuffer = monster.buffers.get (i); dataBuffer = monster.dataBuffers.get (i);
text.append (String.format (" : %s%n", monsterBuffer.toHexString ())); text.append (String.format (" : %s%n", dataBuffer.toHexString ()));
} }
} }
@ -82,36 +87,33 @@ public class Wiz5Monsters extends AbstractFile
class Monster class Monster
{ {
int id; private final int id;
int offset; private final List<DataBuffer> dataBuffers = new ArrayList<DataBuffer> ();
int length;
Wiz4Image image;
byte[] data;
List<Buffer> buffers = new ArrayList<Buffer> (); private Wiz4Image image;
private byte[] data;
public Monster (int id, int offset) public Monster (int id)
{ {
this.id = id; this.id = id;
this.offset = offset;
} }
public Wiz4Image getImage () public Wiz4Image getImage ()
{ {
if (image == null) if (image == null)
{ {
length = 0; int length = 0;
for (Buffer monsterBuffer : buffers) for (DataBuffer dataBuffer : dataBuffers)
length += monsterBuffer.length - 3; length += dataBuffer.length - 3;
data = new byte[length]; data = new byte[length];
int ptr = 0; int ptr = 0;
for (Buffer monsterBuffer : buffers) for (DataBuffer dataBuffer : dataBuffers)
{ {
int offset = monsterBuffer.block * 512 + monsterBuffer.offset + 3; int offset = dataBuffer.block * BLOCK_SIZE + dataBuffer.offset + 3;
System.arraycopy (buffer, offset, data, ptr, monsterBuffer.length - 3); System.arraycopy (buffer, offset, data, ptr, dataBuffer.length - 3);
ptr += monsterBuffer.length - 3; ptr += dataBuffer.length - 3;
} }
image = new Wiz4Image ("Image " + id, data, 8, 8); image = new Wiz4Image ("Image " + id, data, 8, 8);
} }
@ -123,9 +125,9 @@ public class Wiz5Monsters extends AbstractFile
{ {
StringBuilder text = new StringBuilder (); StringBuilder text = new StringBuilder ();
for (Buffer monsterBuffer : buffers) for (DataBuffer dataBuffer : dataBuffers)
{ {
text.append (monsterBuffer); text.append (dataBuffer);
text.append ("\n"); text.append ("\n");
} }
@ -133,13 +135,13 @@ public class Wiz5Monsters extends AbstractFile
} }
} }
class Buffer class DataBuffer
{ {
int block; private final int block;
int offset; private final int offset;
int length; private final int length;
public Buffer (int block, int offset, int length) public DataBuffer (int block, int offset, int length)
{ {
this.block = block; this.block = block;
this.offset = offset; this.offset = offset;
@ -148,13 +150,13 @@ public class Wiz5Monsters extends AbstractFile
public String toHexString () public String toHexString ()
{ {
return HexFormatter.getHexString (buffer, block * 512 + offset, length); return HexFormatter.getHexString (buffer, block * BLOCK_SIZE + offset, length);
} }
@Override @Override
public String toString () public String toString ()
{ {
return (HexFormatter.format (buffer, block * 512 + offset, length)); return (HexFormatter.format (buffer, block * BLOCK_SIZE + offset, length));
} }
} }
} }

View File

@ -157,7 +157,7 @@ public class Wizardry4BootDisk extends PascalDisk
Wiz5Monsters w5monsters = Wiz5Monsters w5monsters =
new Wiz5Monsters ("monsters", fileEntry.getDataSource ().buffer); new Wiz5Monsters ("monsters", fileEntry.getDataSource ().buffer);
fileEntry.setFile (w5monsters); fileEntry.setFile (w5monsters);
for (Wiz5Monsters.Monster monster : w5monsters.monsters) for (Wiz5Monsters.Monster monster : w5monsters)
{ {
List<DiskAddress> monsterBlocks = new ArrayList<DiskAddress> (); List<DiskAddress> monsterBlocks = new ArrayList<DiskAddress> ();
// monsterBlocks.add (pictureBlocks.get (block)); // monsterBlocks.add (pictureBlocks.get (block));