project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetRoomState.java
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    39 
    39 
    40 /**
    40 /**
    41  * This class manages the room state in a network game.
    41  * This class manages the room state in a network game.
    42  */
    42  */
    43 class NetRoomState extends BasicRoomState {
    43 class NetRoomState extends BasicRoomState {
    44 	final Map<String, TeamInGame> requestedTeams = new TreeMap<String, TeamInGame>();
    44     final Map<String, TeamInGame> requestedTeams = new TreeMap<String, TeamInGame>();
    45 	private Netplay netplay;
    45     private Netplay netplay;
    46 	
       
    47 	public NetRoomState(Netplay netplay) {
       
    48 		this.netplay = netplay;
       
    49 		initRoomState(false);
       
    50 	}
       
    51 
    46 
    52 	public void changeWeaponset(Weaponset weaponset) {
    47     public NetRoomState(Netplay netplay) {
    53 		if(getChiefStatus() && !equal(weaponset, getWeaponset())) {
    48         this.netplay = netplay;
    54 			sendToNet(MSG_SEND_WEAPONSET, weaponset);
    49         initRoomState(false);
    55 			setWeaponset(weaponset);
    50     }
    56 		}
       
    57 	}
       
    58 	
       
    59 	public void changeMapRecipe(MapRecipe mapRecipe) {
       
    60 		if(getChiefStatus() && !equal(mapRecipe, getMapRecipe())) {
       
    61 			sendToNet(MSG_SEND_MAP, mapRecipe);
       
    62 			setMapRecipe(mapRecipe);
       
    63 		}
       
    64 	}
       
    65 	
       
    66 	public void changeMapNameAndGenerator(String mapName) {
       
    67 		if(getChiefStatus() && !equal(mapName, getMapRecipe().name)) {
       
    68 			int newGenerator = MapRecipe.generatorForMapname(mapName);
       
    69 			if(newGenerator != getMapRecipe().mapgen) {
       
    70 				sendToNet(MSG_SEND_MAP_GENERATOR, newGenerator, null);
       
    71 			}
       
    72 			sendToNet(MSG_SEND_MAP_NAME, mapName);
       
    73 			setMapRecipe(getMapRecipe().withName(mapName).withMapgen(newGenerator));
       
    74 		}
       
    75 	}
       
    76 	
       
    77 	public void changeMapTemplate(int template) {
       
    78 		if(getChiefStatus() && template != getMapRecipe().templateFilter) {
       
    79 			sendToNet(MSG_SEND_MAP_TEMPLATE, template, null);
       
    80 			setMapRecipe(getMapRecipe().withTemplateFilter(template));
       
    81 		}
       
    82 	}
       
    83 	
       
    84 	public void changeMazeSize(int mazeSize) {
       
    85 		if(getChiefStatus() && mazeSize != getMapRecipe().mazeSize) {
       
    86 			sendToNet(MSG_SEND_MAZE_SIZE, mazeSize, 0);
       
    87 			setMapRecipe(getMapRecipe().withMazeSize(mazeSize));
       
    88 		}
       
    89 	}
       
    90 	
       
    91 	public void changeMapSeed(String seed) {
       
    92 		if(getChiefStatus() && !equal(seed, getMapRecipe().seed)) {
       
    93 			sendToNet(MSG_SEND_MAP_SEED, seed);
       
    94 			setMapRecipe(getMapRecipe().withSeed(seed));
       
    95 		}
       
    96 	}
       
    97 	
       
    98 	public void changeMapTheme(String theme) {
       
    99 		if(getChiefStatus() && !equal(theme, getMapRecipe().theme)) {
       
   100 			sendToNet(MSG_SEND_MAP_THEME, theme);
       
   101 			setMapRecipe(getMapRecipe().withTheme(theme));
       
   102 		}
       
   103 	}
       
   104 	
       
   105 	public void changeMapDrawdata(byte[] drawdata) {
       
   106 		if(getChiefStatus() && !Arrays.equals(drawdata, getMapRecipe().getDrawData())) {
       
   107 			sendToNet(MSG_SEND_MAP_DRAWDATA, drawdata);
       
   108 			setMapRecipe(getMapRecipe().withDrawData(drawdata));
       
   109 		}
       
   110 	}
       
   111 	
       
   112 	public void changeGameStyle(String gameStyle) {
       
   113 		if(getChiefStatus() && !equal(gameStyle, getGameStyle())) {
       
   114 			sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
       
   115 			setGameStyle(gameStyle);
       
   116 		}
       
   117 	}
       
   118 	
       
   119 	public void changeScheme(Scheme scheme) {
       
   120 		if(getChiefStatus() && !equal(scheme, getScheme())) {
       
   121 			sendToNet(MSG_SEND_SCHEME, scheme);
       
   122 			setScheme(scheme);
       
   123 		}
       
   124 	}
       
   125 	
       
   126 	void initRoomState(boolean chief) {
       
   127 		setTeams(Collections.<String, TeamInGame>emptyMap());
       
   128 		requestedTeams.clear();
       
   129 		
       
   130 		setChief(chief);
       
   131 		setGameStyle(GameConfig.DEFAULT_STYLE);
       
   132 		setMapRecipe(MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME));
       
   133 		setScheme(netplay.defaultScheme);
       
   134 		setWeaponset(netplay.defaultWeaponset);
       
   135 		sendFullConfig();
       
   136 	}
       
   137 	
       
   138 	void sendFullConfig() {
       
   139 		if(getChiefStatus()) {
       
   140 			sendToNet(MSG_SEND_GAMESTYLE, getGameStyle());
       
   141 			sendToNet(MSG_SEND_SCHEME, getScheme());
       
   142 			sendToNet(MSG_SEND_WEAPONSET, getWeaponset());
       
   143 			sendToNet(MSG_SEND_MAP, getMapRecipe());
       
   144 		}
       
   145 	}
       
   146 	
       
   147 	private boolean sendToNet(ToNetMsgType what, Object obj) {
       
   148 		return netplay.sendToNet(what, 0, obj);
       
   149 	}
       
   150 	
       
   151 	private boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
       
   152 		return netplay.sendToNet(what, arg1, obj);
       
   153 	}
       
   154 
    51 
   155 	public void requestAddTeam(Team team, int colorIndex) {
    52     public void changeWeaponset(Weaponset weaponset) {
   156 		TeamIngameAttributes tia = new TeamIngameAttributes(netplay.getPlayerName(), colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
    53         if(getChiefStatus() && !equal(weaponset, getWeaponset())) {
   157 		TeamInGame newTeamInGame = new TeamInGame(team, tia);
    54             sendToNet(MSG_SEND_WEAPONSET, weaponset);
   158 		requestedTeams.put(team.name, newTeamInGame);
    55             setWeaponset(weaponset);
   159 		sendToNet(MSG_SEND_ADD_TEAM, newTeamInGame);
    56         }
   160 	}
    57     }
   161 
    58 
   162 	public void requestRemoveTeam(String teamname) {
    59     public void changeMapRecipe(MapRecipe mapRecipe) {
   163 		sendToNet(MSG_SEND_REMOVE_TEAM, teamname);
    60         if(getChiefStatus() && !equal(mapRecipe, getMapRecipe())) {
   164 	}
    61             sendToNet(MSG_SEND_MAP, mapRecipe);
       
    62             setMapRecipe(mapRecipe);
       
    63         }
       
    64     }
   165 
    65 
   166 	public void changeTeamColorIndex(String teamname, int colorIndex) {
    66     public void changeMapNameAndGenerator(String mapName) {
   167 		if(getChiefStatus()) {
    67         if(getChiefStatus() && !equal(mapName, getMapRecipe().name)) {
   168 			TeamInGame team = getTeams().get(teamname);
    68             int newGenerator = MapRecipe.generatorForMapname(mapName);
   169 			if(team.ingameAttribs.colorIndex != colorIndex) {
    69             if(newGenerator != getMapRecipe().mapgen) {
   170 				sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamname);
    70                 sendToNet(MSG_SEND_MAP_GENERATOR, newGenerator, null);
   171 				putTeam(team.withAttribs(team.ingameAttribs.withColorIndex(colorIndex)));
    71             }
   172 			}
    72             sendToNet(MSG_SEND_MAP_NAME, mapName);
   173 		}
    73             setMapRecipe(getMapRecipe().withName(mapName).withMapgen(newGenerator));
   174 	}
    74         }
       
    75     }
   175 
    76 
   176 	public void changeTeamHogCount(String teamname, int hogcount) {
    77     public void changeMapTemplate(int template) {
   177 		if(getChiefStatus()) {
    78         if(getChiefStatus() && template != getMapRecipe().templateFilter) {
   178 			TeamInGame team = getTeams().get(teamname);
    79             sendToNet(MSG_SEND_MAP_TEMPLATE, template, null);
   179 			if(team.ingameAttribs.hogCount != hogcount) {
    80             setMapRecipe(getMapRecipe().withTemplateFilter(template));
   180 				sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogcount, teamname);
    81         }
   181 				putTeam(team.withAttribs(team.ingameAttribs.withHogCount(hogcount)));
    82     }
   182 			}
    83 
   183 		}
    84     public void changeMazeSize(int mazeSize) {
   184 	}
    85         if(getChiefStatus() && mazeSize != getMapRecipe().mazeSize) {
       
    86             sendToNet(MSG_SEND_MAZE_SIZE, mazeSize, 0);
       
    87             setMapRecipe(getMapRecipe().withMazeSize(mazeSize));
       
    88         }
       
    89     }
       
    90 
       
    91     public void changeMapSeed(String seed) {
       
    92         if(getChiefStatus() && !equal(seed, getMapRecipe().seed)) {
       
    93             sendToNet(MSG_SEND_MAP_SEED, seed);
       
    94             setMapRecipe(getMapRecipe().withSeed(seed));
       
    95         }
       
    96     }
       
    97 
       
    98     public void changeMapTheme(String theme) {
       
    99         if(getChiefStatus() && !equal(theme, getMapRecipe().theme)) {
       
   100             sendToNet(MSG_SEND_MAP_THEME, theme);
       
   101             setMapRecipe(getMapRecipe().withTheme(theme));
       
   102         }
       
   103     }
       
   104 
       
   105     public void changeMapDrawdata(byte[] drawdata) {
       
   106         if(getChiefStatus() && !Arrays.equals(drawdata, getMapRecipe().getDrawData())) {
       
   107             sendToNet(MSG_SEND_MAP_DRAWDATA, drawdata);
       
   108             setMapRecipe(getMapRecipe().withDrawData(drawdata));
       
   109         }
       
   110     }
       
   111 
       
   112     public void changeGameStyle(String gameStyle) {
       
   113         if(getChiefStatus() && !equal(gameStyle, getGameStyle())) {
       
   114             sendToNet(MSG_SEND_GAMESTYLE, gameStyle);
       
   115             setGameStyle(gameStyle);
       
   116         }
       
   117     }
       
   118 
       
   119     public void changeScheme(Scheme scheme) {
       
   120         if(getChiefStatus() && !equal(scheme, getScheme())) {
       
   121             sendToNet(MSG_SEND_SCHEME, scheme);
       
   122             setScheme(scheme);
       
   123         }
       
   124     }
       
   125 
       
   126     void initRoomState(boolean chief) {
       
   127         setTeams(Collections.<String, TeamInGame>emptyMap());
       
   128         requestedTeams.clear();
       
   129 
       
   130         setChief(chief);
       
   131         setGameStyle(GameConfig.DEFAULT_STYLE);
       
   132         setMapRecipe(MapRecipe.makeRandomMap(0, "seed", GameConfig.DEFAULT_THEME));
       
   133         setScheme(netplay.defaultScheme);
       
   134         setWeaponset(netplay.defaultWeaponset);
       
   135         sendFullConfig();
       
   136     }
       
   137 
       
   138     void sendFullConfig() {
       
   139         if(getChiefStatus()) {
       
   140             sendToNet(MSG_SEND_GAMESTYLE, getGameStyle());
       
   141             sendToNet(MSG_SEND_SCHEME, getScheme());
       
   142             sendToNet(MSG_SEND_WEAPONSET, getWeaponset());
       
   143             sendToNet(MSG_SEND_MAP, getMapRecipe());
       
   144         }
       
   145     }
       
   146 
       
   147     private boolean sendToNet(ToNetMsgType what, Object obj) {
       
   148         return netplay.sendToNet(what, 0, obj);
       
   149     }
       
   150 
       
   151     private boolean sendToNet(ToNetMsgType what, int arg1, Object obj) {
       
   152         return netplay.sendToNet(what, arg1, obj);
       
   153     }
       
   154 
       
   155     public void requestAddTeam(Team team, int colorIndex) {
       
   156         TeamIngameAttributes tia = new TeamIngameAttributes(netplay.getPlayerName(), colorIndex, TeamIngameAttributes.DEFAULT_HOG_COUNT, false);
       
   157         TeamInGame newTeamInGame = new TeamInGame(team, tia);
       
   158         requestedTeams.put(team.name, newTeamInGame);
       
   159         sendToNet(MSG_SEND_ADD_TEAM, newTeamInGame);
       
   160     }
       
   161 
       
   162     public void requestRemoveTeam(String teamname) {
       
   163         sendToNet(MSG_SEND_REMOVE_TEAM, teamname);
       
   164     }
       
   165 
       
   166     public void changeTeamColorIndex(String teamname, int colorIndex) {
       
   167         if(getChiefStatus()) {
       
   168             TeamInGame team = getTeams().get(teamname);
       
   169             if(team.ingameAttribs.colorIndex != colorIndex) {
       
   170                 sendToNet(MSG_SEND_TEAM_COLOR_INDEX, colorIndex, teamname);
       
   171                 putTeam(team.withAttribs(team.ingameAttribs.withColorIndex(colorIndex)));
       
   172             }
       
   173         }
       
   174     }
       
   175 
       
   176     public void changeTeamHogCount(String teamname, int hogcount) {
       
   177         if(getChiefStatus()) {
       
   178             TeamInGame team = getTeams().get(teamname);
       
   179             if(team.ingameAttribs.hogCount != hogcount) {
       
   180                 sendToNet(MSG_SEND_TEAM_HOG_COUNT, hogcount, teamname);
       
   181                 putTeam(team.withAttribs(team.ingameAttribs.withHogCount(hogcount)));
       
   182             }
       
   183         }
       
   184     }
   185 }
   185 }