project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/BasicRoomState.java
author unc0rr
Mon, 28 Dec 2015 09:06:29 +0300
changeset 11469 a9957113404a
parent 10017 de822cd3df3a
permissions -rw-r--r--
Allow only one query per session
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import static org.hedgewars.hedgeroid.util.ObjectUtils.equal;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Collections;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.LinkedList;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.List;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import java.util.Map;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import java.util.TreeMap;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import org.hedgewars.hedgeroid.RoomStateManager;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import org.hedgewars.hedgeroid.Datastructures.Scheme;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import org.hedgewars.hedgeroid.Datastructures.Weaponset;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
 * Common base implementation for a roomstate that will call listeners on every
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
 * change. The derived classes have to coordinate how state is changed to
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * complete the implementation of the RoomStateManager interface.
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
 *
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
 * See {@link RoomStateManager} for a description of what this is for.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
public abstract class BasicRoomState implements RoomStateManager {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    private final List<RoomStateManager.Listener> observers = new LinkedList<RoomStateManager.Listener>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    private boolean chief;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    private String gameStyle;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
    private Scheme scheme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    private MapRecipe map;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
    private Weaponset weaponset;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    private Map<String, TeamInGame> teams = Collections.emptyMap();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
    public final MapRecipe getMapRecipe() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        return map;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
    public final boolean getChiefStatus() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
        return chief;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    public final Scheme getScheme() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        return scheme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    public final String getGameStyle() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
        return gameStyle;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
    public final Weaponset getWeaponset() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        return weaponset;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    public final Map<String, TeamInGame> getTeams() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
        return teams;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    public final void setWeaponset(Weaponset weaponset) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
        if(!equal(weaponset, this.weaponset)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
            this.weaponset = weaponset;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
                observer.onWeaponsetChanged(weaponset);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
    public final void setMapRecipe(MapRecipe map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        if(!equal(map, this.map)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
            this.map = map;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
                observer.onMapChanged(map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
    public final void setGameStyle(String gameStyle) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        if(!equal(gameStyle, this.gameStyle)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
            this.gameStyle = gameStyle;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
                observer.onGameStyleChanged(gameStyle);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
    public final void setScheme(Scheme scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
        if(!equal(scheme, this.scheme)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
            this.scheme = scheme;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
                observer.onSchemeChanged(scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
    public final void setChief(boolean chief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        if(chief != this.chief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
            this.chief = chief;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
                observer.onChiefStatusChanged(chief);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
    public final void putTeam(TeamInGame team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
        TeamInGame oldEntry = teams.get(team.team.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
        if(!equal(team, oldEntry)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
            Map<String, TeamInGame> changedMap = new TreeMap<String, TeamInGame>(teams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
            changedMap.put(team.team.name, team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
            teams = Collections.unmodifiableMap(changedMap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
                observer.onTeamsChanged(teams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   134
    public final void removeTeam(String teamname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
        if(teams.containsKey(teamname)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
            Map<String, TeamInGame> changedMap = new TreeMap<String, TeamInGame>(teams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   137
            changedMap.remove(teamname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   138
            teams = Collections.unmodifiableMap(changedMap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   139
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   140
                observer.onTeamsChanged(teams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   141
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   142
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   143
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   144
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   145
    public final void setTeams(Map<String, TeamInGame> newTeams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   146
        if(!newTeams.equals(teams)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   147
            teams = Collections.unmodifiableMap(new TreeMap<String, TeamInGame>(newTeams));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   148
            for(RoomStateManager.Listener observer : observers) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   149
                observer.onTeamsChanged(teams);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   150
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   151
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   152
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   153
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   154
    public final void addListener(Listener observer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   155
        observers.add(observer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   156
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   157
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   158
    public final void removeListener(Listener observer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   159
        observers.remove(observer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   160
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
}