/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.tinylib.logic.transform.rom24support;


import net.sourceforge.pain.*;
import net.sourceforge.pain.data.*;
import net.sourceforge.pain.data.role.*;
import net.sourceforge.pain.tinylib.*;
import net.sourceforge.pain.tinylib.data.*;
import net.sourceforge.pain.tinylib.data.prototype.*;
import net.sourceforge.pain.tinylib.data.type.*;
import net.sourceforge.pain.tinylib.logic.fn.*;
import net.sourceforge.pain.util.*;

import java.io.*;
import java.util.*;


public final class Rom24ToPainConverter {

    private static final int[] defaultMoveDice = new int[]{1, 100, 0};

    private Rom24ToPainConverter() {
    };

    public static void transform(File areaListFile, Rom24ConversionLogListener l) throws Exception {
        String path = areaListFile.getParent();
        log(l, "Opening area list file for reading:" + areaListFile.getAbsolutePath());
        BufferedReader reader = new BufferedReader(new FileReader(areaListFile));
        log(l, "Reading area list file");
        ArrayList files = new ArrayList();
        try {
            while (true) {
                String areaFileName = reader.readLine();
                if (areaFileName == null) {
                    break;
                }
                areaFileName = areaFileName.trim();
                if (areaFileName.equals("$")) { // end of list
                    break;
                }
                if (!areaFileName.startsWith("#") && !areaFileName.startsWith("!")) {
                    files.add(path + File.separator + areaFileName);
                }
            }
        } finally {
            reader.close();
        }
        Log.info("Area list file read");
        log(l, "Area list file read (found areas:" + files.size() + "), loading areas..");
        Log.info("Loading areas..");
        ArrayList areas = new ArrayList();
        for (int i = 0; i < files.size(); i++) {
            String fileName = (String) files.get(i);
            Log.info("Loading area :" + fileName);
            log(l, "Loading area:" + fileName + " , num:" + i);
            Rom24AreaModel area = Rom24AreaLoader.loadArea(fileName);
            if (area.areaName == null) {
                throw new RuntimeException("ParseError: Area has no name:" + fileName);
            }
            areas.add(area);
            Log.info("Area:" + fileName + " loaded OK");
        }

        Log.info("ROM areas parsed!");
        log(l, "area loading done, importing  data...");
        Log.info("Creating areas and rooms");

        HashMap prototypes = new HashMap(); // by VNUM;
        HashMap rooms = new HashMap(); // rooms by VNUM;
        HashMap resetGroupByArea = new HashMap(); // ResetGroup by rom24support area - used for reset binding

        World world = Mudlib.getWorld();
        Space birthSpaceCandidate = null;
        RoomsRegistry roomsRegistry = world.getRoomsRegistry();
        PrototypesRegistry protoRegistry = world.getPrototypesRegistry();
        for (int j = 0; j < areas.size(); j++) {
            Rom24AreaModel romArea = (Rom24AreaModel) areas.get(j);
            SpaceGroup area = (SpaceGroup) ObjectFactory.createRaw(SpaceGroup.class);
            ResetGroup rg = (ResetGroup) area.addRole(ResetGroup.class);
            rg.setResetMessage(romArea.resetMessage);
            rg.setResetPeriod(Pulse.PULSE_PER_MIN * 3);
            rg.setNextResetTime(0);
            rg.setGroupInfo(romArea.areaName);
            rg.setGroupId(romArea.areaName.trim());
            resetGroupByArea.put(romArea, rg);

            // creating rooms;
            for (Iterator it = romArea.rooms.values().iterator(); it.hasNext();) {
                ROMRoom romRoom = (ROMRoom) it.next();
                Log.info("creating room:" + romRoom.vnum);
                Room room = (Room) ObjectFactory.createRaw(Room.class);
                room.setRoomUniqueId("rom24:" + romRoom.vnum);
                roomsRegistry.registerRoom(room);
                Space space = room.asSpace();
                space.setName(romRoom.name);
                space.setDesc(romRoom.desc);
                space.setCapacity(1000);
                //!!!rInfo.setArea(romArea);
                rooms.put(romRoom.vnum, room);
                area.addSpace(space);
                // exits will be bound later
            }

            //creating mobile prototypes
            Log.info("Creating creatures prototypes");
            Race defaultRace = (Race) world.getRaces().iterator().next(); // temporaty unprocessed
            for (Iterator it = romArea.mobiles.values().iterator(); it.hasNext();) {
                ROMMobile romMobile = (ROMMobile) it.next();
                Log.info("creating creature prototype:" + romMobile.vnum);
                PrototypeInfo p = (PrototypeInfo) ObjectFactory.createRaw(PrototypeInfo.class);
                p.setVnum("rom24creature:" + romMobile.vnum);
                p.setName(romMobile.shortDesc);
                PhysicalPrototype ph = (PhysicalPrototype) p.addRole(PhysicalPrototype.class);
                ph.setAppearanceDesc(romMobile.lookDesc);
                ph.setWeight(1);
                ph.setSize(1);

                InteractivePrototype ip = ph.asInteractivePrototype();
                ip.setTargetList(romMobile.nameList);
                ip.setInteractiveName(romMobile.shortDesc);
                ip.setDesc(romMobile.longDesc);

                CreaturePrototype cp = (CreaturePrototype) p.addRole(CreaturePrototype.class);
                cp.setHPDice(new Dice(romMobile.hitDice));
                cp.setSex(romMobile.gender.equals("male") ? CreaturePrototype.SEX_MALE : romMobile.gender.equals("female") ? CreaturePrototype.SEX_FEMALE : romMobile.gender.equals("either") ? CreaturePrototype.SEX_EITHER : CreaturePrototype.SEX_UNDEFINED);
                cp.setRace(defaultRace);
                cp.setMovePointsDice(new Dice(defaultMoveDice));
                prototypes.put("creature:" + romMobile.vnum, p);


                final SpacePrototype inventoryPrototype = (SpacePrototype) ObjectFactory.createRaw(SpacePrototype.class);
                inventoryPrototype.setSpaceName("Inventory");
                inventoryPrototype.setSpaceDesc("");
                inventoryPrototype.setCapacity(Integer.MAX_VALUE);
                cp.setInventoryPrototype(inventoryPrototype);

                protoRegistry.registerPrototype(p);
            }

            //creating object prototypes
            Log.info("Creating thing prototypes");
            for (Iterator it = romArea.objects.values().iterator(); it.hasNext();) {
                ROMObject romObject = (ROMObject) it.next();
                Log.info("creating thing prototype:" + romObject.vnum);
                PrototypeInfo p = (PrototypeInfo) ObjectFactory.createRaw(PrototypeInfo.class);
                p.setName(romObject.shortDesc);
                p.setVnum("rom24thing:" + romObject.vnum);
                PhysicalPrototype ph = (PhysicalPrototype) p.addRole(PhysicalPrototype.class);
                ph.setAppearanceDesc(romObject.longDesc);
                ph.setWeight(1);
                ph.setSize(1);

                InteractivePrototype ip = ph.asInteractivePrototype();
                ip.setTargetList(romObject.nameList);
                ip.setInteractiveName(romObject.shortDesc);
                ip.setDesc(romObject.longDesc);


                prototypes.put("thing:" + romObject.vnum, p);

                protoRegistry.registerPrototype(p);
            }
        }
        // binding room exits
        // and resets
        log(l, "linking rooms and creating resets...");
        Log.info("Creating exits and resets");
        for (int j = 0; j < areas.size(); j++) {
            Rom24AreaModel romArea = (Rom24AreaModel) areas.get(j);
            ResetGroup rg = (ResetGroup) resetGroupByArea.get(romArea);
            for (Iterator it = romArea.rooms.values().iterator(); it.hasNext();) {
                ROMRoom romRoom = (ROMRoom) it.next();
                Room room1 = (Room) rooms.get(romRoom.vnum);
                Space space1 = room1.asSpace();
                for (int i = 0; i < romRoom.exits.length; i++) {
                    if (romRoom.exits[i] != null) {
                        if (birthSpaceCandidate == null) {
                            if (space1.getName().toLowerCase().indexOf("temple") > 0) {
                                birthSpaceCandidate = space1;
                            }
                        }
                        Room room2 = (Room) rooms.get(romRoom.exits[i]);
                        if (room2 == null) {
                            continue;
                        }
                        Exit exit1to2 = (Exit) ObjectFactory.createRaw(Exit.class);
                        exit1to2.setTargetRoom(room2);
                        exit1to2.setExitDesc(romRoom.exitsDescs[i]);
                        exit1to2.setMoveConst(1);
                        room1.setExit(i, exit1to2);
                    }
                }

                if (romRoom.resets.size() > 0) {
                    for (int r = 0; r < romRoom.resets.size(); r++) {
                        Object romReset = romRoom.resets.get(r);
                        PrototypeInfo p;
                        if (romReset instanceof ROMMobileReset) {
                            ROMMobileReset mreset = (ROMMobileReset) romReset;
                            Log.info("mob reset :" + mreset.mob.vnum);
                            p = (PrototypeInfo) prototypes.get("creature:" + mreset.mob.vnum);
                        } else {// this is ROMObjectReset
                            ROMObjectReset oreset = (ROMObjectReset) romReset;
                            Log.info("obj reset :" + oreset.obj.vnum);
                            p = (PrototypeInfo) prototypes.get("thing:" + oreset.obj.vnum);
                        }
                        if (p == null) {
                            Log.warn("Prototype is not found for reset:" + romReset.toString() + "space vnum:" + romRoom.vnum + " space name:" + romRoom.name);
                        } else {
                            SpaceReset reset = (SpaceReset) ObjectFactory.createRaw(SpaceReset.class);
                            reset.setResettedPrototype(p);
                            reset.setLocation(space1);
                            RelocateFn.addToSpace(space1, reset.asLocated());
                            rg.addReset(reset.asReset());
                        }
                    }
                }
            }// rooms
        } // areas
        if (birthSpaceCandidate == null && world.getDefaultBirthSpace() == null) {
            // any space
            world.setDefaultBirthSpace((Space) rooms.values().iterator().next());
        }
        Log.info("schema converted OK!");
        log(l, "convertion done!");
        log(l, "{w Note: {c rooms vnums adjusted with rom24:<vnum> prefix");
        log(l, "{c use 'goto' or builders 'link' command to check (example: goto rom24:3001){x");
    }

    private static void log(Rom24ConversionLogListener l, String message) {
        if (l != null) {
            l.onConversionMessage(message);
        }
    }

    // adding one dir way from the initial space1 to rom world
//    Room room = (Room) rooms.values().iterator().next();
//    IndexedSpace initial = namedSpacesRegistry.getSpace("initial");
//    Room link = (Room) initial.getRole(Room.class);
//    if (link == null) {
//        link = (Room) initial.addRole(Room.class);
//    }
//    Exit e = (Exit) ObjectFactory.create(Exit.class);
//    link.setExit(Room.DIR_DOWN, e);
//    e.setTargetRoom(room);


}