Subversion Repositories display

Rev

Rev 311 | 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 uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.imgfmt.app.map.MapReader;

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

/**
 * 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 = 30;

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

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

        /**
         * Try to read a map with the name as given by the map id.
         * @param mapIndex The index of the map in mdr1
         * @param mapName The map name as an integer to be used as part of a filename.
         */

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

                int mapName = idToName(mapId);

                Log.log("# reading %d", mapName);
                String filename = String.format("%08d.img", mapName);
                MapReader mapReader = null;
                try {
                        mapReader = new MapReader(filename);
                        MapDetails detail = fetchDetails(mapReader, mapName, 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())
                        return null;
                return mapDetails.get(number - 1);
        }

        private MapDetails fetchDetails(MapReader mapReader, int mapId, int mapIndex) {
                MapDetails md = new MapDetails(mapId, 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);
        }
}