bump
|
How can I make it so it doesn't overwrite the original array (NORMAL_ROOMS1)? Right now modifying the rooms in POSSIBILITIES modifies the one in NORMAL_ROOMS1 even though they're final.
Code:public class DungeonConstants { public static final NormalRoom[] NORMAL_ROOMS1 = { // FROZEN_FLOORS new NormalRoom(8, 240, SOUTH_DOOR), new NormalRoom(10, 240, SOUTH_DOOR), new NormalRoom(12, 240, SOUTH_DOOR) , new NormalRoom(8, 242, SOUTH_DOOR, WEST_DOOR), new NormalRoom(10, 242, SOUTH_DOOR, WEST_DOOR), new NormalRoom(12, 242, SOUTH_DOOR, WEST_DOOR) , new NormalRoom(8, 244, NORTH_DOOR, SOUTH_DOOR), new NormalRoom(10, 244, NORTH_DOOR, SOUTH_DOOR), new NormalRoom(12, 244, NORTH_DOOR, SOUTH_DOOR) , new NormalRoom(8, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR), new NormalRoom(10, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR), new NormalRoom(12, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR) , new NormalRoom(8, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR, EAST_DOOR), new NormalRoom(10, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR, EAST_DOOR), new NormalRoom(12, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR, EAST_DOOR)}; public static final int EAST_DOOR = 0, WEST_DOOR = 1, NORTH_DOOR = 2, SOUTH_DOOR = 3; public static NormalRoom generateNormalRoom() { List<NormalRoom> POSSIBILITIES = new ArrayList<NormalRoom>(); int i_1; int i_2; for (i_1 = 0 ; i_1 < 4 ; i_1++) { POSSIBILITIES.clear(); for (i_2 = 0 ; i_2 < START_ROOMS1.length ; i_2++) { POSSIBILITIES.add(START_ROOMS1[i_2]); POSSIBILITIES.get(i_2).modifyRot(i_1); } } } }
something like this?Code:public static List<NormalRoom> randomise(NormalRoom[] rooms) { /* Adds all pre-existing rooms to the array. */ List<NormalRoom> randomised = Arrays.asList(rooms); /* Copy a room, but with a different rotations. */ int modCount = 5, modified = 0; // XXX: Change me? while (modified != modCount) { int id = new SecureRandom().nextInt(rooms.length); // XXX: Change me? NormalRoom room = randomised.get(id); if (room == null) { /* Shouldn't happen. */ continue; } NormalRoom random = new NormalRoom(...); // Add new parameters. randomised.add(random; modified++; } return randomised; }
Didn't mean to double post.. just noticed.. zzz
You're going to need to create a new copy of the NormalRoom if you intend on the values inside of the array to stay the same.
For a quick solution, make NormalRoom implement Cloneable and inside of your 'generateNormalRoom' method change:
to:Code:POSSIBILITIES.add(START_ROOMS1[i_2]);
or instead of using clone you can make a static method inside of NormalRoom such as:Code:POSSIBILITIES.add(START_ROOMS1[i_2].clone());
Note: This is just a quick way to make your design work.Code:public static NormalRoom copyOf(NormalRoom copy) { return new NormalRoom(copy.param1, copy.param2, copy.param3.... etc); }
Also, the final keyword indicates that the array's set value cannot be modified, but not the values inside of the array.
Used this instead but it still modified it.. wtf... I used a command to confirmed it wasn't the ints SOUTH_DOOR, WEST_DOOR etc... and it's not.
Btw the cmd I used to debug is this:Code:public static NormalRoom generateNormalRoom() { List<NormalRoom> POSSIBILITIES = new ArrayList<NormalRoom>(); NormalRoom[] copy; int i_1; int i_2; for (i_1 = 0 ; i_1 < 4 ; i_1++) { POSSIBILITIES.clear(); copy = NORMAL_ROOMS1.clone(); for (i_2 = 0 ; i_2 < copy.length ; i_2++) { POSSIBILITIES.add(copy[i_2]); POSSIBILITIES.get(i_2).modifyRot(i_1); } } }
Not optimised at all but it gets the job done. The numbers for ROT: weren't the same before/after doing the void method but the int constants were the same.Code:if (cmd[0].equals("dgconst")) { NormalRoom roomy = DungeonConstants.NORMAL_ROOMS1[81]; for (int i = 0 ; i < roomy.getDirections().length ; i++) player.getPackets().sendGameMessage("ROT: "+roomy.getDirections()[i]); if (DungeonConstants.WEST_DOOR != 0 || DungeonConstants.NORTH_DOOR != 1 || DungeonConstants.EAST_DOOR != 2 || DungeonConstants.SOUTH_DOOR != 3) System.out.println("DOOR ROTATIONS VALUES HAVE BEEN MODIFIED."); else System.out.println("Looks good."); return true; }
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |