Subversion Repositories display

Rev

Rev 377 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2011.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package test.display.check;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.imgfmt.app.map.MapReader;

/**
 * Holds information read from the actual maps themselves (ie not from the mdr file).
 *
 * @author Steve Ratcliffe
 */

public class MapDetailList {
        // We only hold a certain number of maps in memory
        private static final int MAX_MAPS = 50;

        private final List<MapDetails> mapDetails = new ArrayList<>();

        private Map<Integer, Integer> numberMap = new HashMap<>();

        /**
         * Try to read a map with the name as given by the map id.
         * @param mapIndex The index of the map in mdr1
         * @param mapId The map name as an integer to be used as part of a filename.
         * @param subHeaderOffset Offset of the sub-header for this map.
         */

        public void readMap(int mapIndex, int mapId, int subHeaderOffset) {
                if (mapIndex > MAX_MAPS)
                        return;

                int mapName = idToName(mapId);

                System.out.printf("# reading %d\n", mapName);
                String filename = String.format("%08d.img", mapName);
                MapReader mapReader = null;
                try {
                        mapReader = new MapReader(filename);
                        MapDetails detail = fetchDetails(mapReader, mapIndex);
                        detail.setSubHeaderOffset(subHeaderOffset);
                        mapDetails.add(detail);
                } catch (FileNotFoundException e) {
                        System.out.println("Cannot read map file " + filename);
                        mapDetails.add(null);
                } finally {
                        Utils.closeFile(mapReader);
                }
        }

        private int idToName(int mapId) {
                Integer mapName;
                if (numberMap != null)
                        mapName = numberMap.get(mapId);
                else
                        mapName = mapId;
                if (mapName == null)
                        return mapId;
                return mapName;
        }

        public MapDetails getMap(int number) {
                if (number > mapDetails.size() || number < 1)
                        return null;
                return mapDetails.get(number - 1);
        }

        private MapDetails fetchDetails(MapReader mapReader, int mapIndex) {
                MapDetails md = new MapDetails(mapIndex);

                md.readMap(mapReader);
                md.check();

                return md;
        }

        public boolean mapSaved(int mapNumber) {
                return mapNumber <= mapDetails.size();
        }

        public void setNumberMap(Map<Integer, Integer> numberMap) {
                this.numberMap = numberMap;
        }

        public List<Street> getStreetsWithName(int mapNumber, String name) {
                return getMap(mapNumber).getStreetsWithName(name);
        }
}