boolean isChecksumValid() { // - read Checksum from File int fileChecksum = readBEIntFrom(0); mapData.fileChecksum = fileChecksum; // - make "count" a Multiple of four int count = mapContent.length & 0xFFFFFFFC; int currentChecksum = 0; // - Map Content starts at Byte 8 for (int i = 8; i < count; i += 4) { // - read DWord int currentInt = (mapContent[i] & 0xFF) | ((mapContent[i + 1] & 0xFF) << 8) | ((mapContent[i + 2] & 0xFF) << 16) | ((mapContent[i + 3] & 0xFF) << 24); // - using: Logic Right-Shift-Operator: >>> currentChecksum = ((currentChecksum >>> 31) | ((currentChecksum << 1) ^ currentInt)); } // - return TRUE if the checksum is OK! return (currentChecksum == fileChecksum); }
private void readPlayerInfo() throws MapLoadException { MapResourceInfo filePart = findAndDecryptFilePartSafe(EOriginalMapFilePartType.PLAYER_INFO); // - file position int pos = filePart.offset; for (int i = 0; i < mapData.getPlayerCount(); i++) { int nation = readBEIntFrom(pos); pos += 4; int startX = readBEIntFrom(pos); pos += 4; int startY = readBEIntFrom(pos); pos += 4; String playerName = readCStrFrom(pos, 33); pos += 33; mapData.setPlayer(i, startX, startY, nation, playerName); } }
void loadMapResources() throws MapLoadException { int fileVersion = readBEIntFrom(4); partTypeTemp = readBEIntFrom(filePos); int partLen = readBEIntFrom(filePos + 4);
private void readMapInfo() throws MapLoadException { MapResourceInfo filePartOptional = findAndDecryptFilePartSafe(EOriginalMapFilePartType.MAP_INFO); // - file position int pos = filePartOptional.offset; // ---------------------------------- // - read mapType (single / multiplayer map?) int mapType = readBEIntFrom(pos); pos += 4; if (mapType == 1) { isSinglePlayerMap = true; } else if (mapType == 0) { isSinglePlayerMap = false; } else { throw new MapLoadException("wrong value for 'isSinglePlayerMap' " + Integer.toString(mapType) + " in mapfile!"); } // ---------------------------------- // - read Player count int playerCount = readBEIntFrom(pos); pos += 4; mapData.setPlayerCount(playerCount); // ---------------------------------- // - read start resources int startResourcesValue = readBEIntFrom(pos); this.startResources = EMapStartResources.fromMapValue(startResourcesValue); }
fileChecksum = readBEIntFrom(0); widthHeight = readBEIntFrom(pos);
void readSettlers() throws MapLoadException { Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.SETTLERS); if (filePartOptional.isPresent()) { MapResourceInfo filePart = filePartOptional.get(); // - file position int pos = filePart.offset; // - Number of buildings int settlerCount = readBEIntFrom(pos); pos += 4; // - safety check if ((settlerCount * 6 > filePart.size) || (settlerCount < 0)) { throw new MapLoadException("wrong number of settlers in map File: " + settlerCount); } // - read all Stacks for (int i = 0; i < settlerCount; i++) { int party = readByteFrom(pos++); int settlerType = readByteFrom(pos++); int posX = readBEWordFrom(pos); pos += 2; int posY = readBEWordFrom(pos); pos += 2; // ------------- // - update data mapData.setSettler(posX, posY, settlerType, party); } } }
void readStacks() throws MapLoadException { Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.STACKS); if (filePartOptional.isPresent()) { MapResourceInfo filePart = filePartOptional.get(); // - file position int pos = filePart.offset; // - Number of buildings int stackCount = readBEIntFrom(pos); pos += 4; // - safety check if ((stackCount * 8 > filePart.size) || (stackCount < 0)) { throw new MapLoadException("wrong number of stacks in map File: " + stackCount); } // - read all Stacks for (int i = 0; i < stackCount; i++) { int posX = readBEWordFrom(pos); pos += 2; int posY = readBEWordFrom(pos); pos += 2; int stackType = readByteFrom(pos++); int count = readByteFrom(pos++); pos += 2; // not used - maybe: padding to size of 8 (2 INTs) // ------------- // - update data mapData.setStack(posX, posY, stackType, count); } } }
/** * Reads in the Map Data / Landscape and MapObjects like trees */ void readMapData() throws MapLoadException { MapResourceInfo filePart = findAndDecryptFilePartSafe(EOriginalMapFilePartType.AREA); // - file position int pos = filePart.offset; // - height and width are the same int widthHeight = readBEIntFrom(pos); pos += 4; // - init size of MapData mapData.setWidthHeight(widthHeight); // - points to read int dataCount = widthHeight * widthHeight; for (int i = 0; i < dataCount; i++) { mapData.setLandscapeHeight(i, readByteFrom(pos++)); mapData.setLandscape(i, readByteFrom(pos++)); mapData.setMapObject(i, readByteFrom(pos++)); readByteFrom(pos++); // - which Player is the owner of this position mapData.setAccessible(i, mapContent[pos++]); mapData.setResources(i, readHighNibbleFrom(pos), readLowNibbleFrom(pos)); pos++; } }
int buildingsCount = readBEIntFrom(pos); pos += 4;