Skip to main content
Move explanation to top
Source Link

One solution is to simulate bit fields using EnumSets. An EnumSet uses bit-wise arithmetic for operations that involve the whole set, which means it is very fast. If you are using a language that does not have these features, you could implement something like this using regular bit fields.

If you are using a different language, you could implement something like this using plain old bit fields.

One solution is to simulate bit fields using EnumSets. An EnumSet uses bit-wise arithmetic for operations that involve the whole set, which means it is very fast.

If you are using a different language, you could implement something like this using plain old bit fields.

One solution is to simulate bit fields using EnumSets. An EnumSet uses bit-wise arithmetic for operations that involve the whole set, which means it is very fast. If you are using a language that does not have these features, you could implement something like this using regular bit fields.

Remove building example code
Source Link
public enum TechType {
    NONE, VEGETATION, STRAWBERRIES, BIODOME, LIVESTOCK, BIOMASS;
  }
public class VegetationTech implements Tech {
    public TechType type = TechType.VEGETATION;
 
    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.NONE);

    @Override
    public Set getPrerequisites() {
        return prerequisites;
    } 

    @Override
    public TechType getType() {
        return type;
    }
}

...

public class BiomassTech implements Tech {
    public TechType type = TechType.BIOMASS;
 
    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.VEGETATION, TechType.LIVESTOCK);

    public Set getPrerequisites() {
        return prerequisites;
    }

    public TechType getType() {
        return type;
    }
}

public class StrawberriesTech implements Tech {
    public TechType type = TechType.STRAWBERRIES;

    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.VEGETATIONLIVESTOCK); 

    @Override
    public Set getPrerequisites() {
        return prerequisites;
    } 

    @Override
    public TechType getType() {
        return type;
    }
}

Here you can add as many parameters as you want, such as a thumbnail icon and description string or whatever.

For a building needing a certain tech:

public class StrawberrySilo implements Building {
    private static Set<Tech> prerequisites = EnumSet.of(Tech.STRAWBERRIES);
    ...
}

Then you could schedule research jobs and construction jobs through the Player class:

public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }

    public boolean constructBuilding(Building building) {
        if (techsKnown.containsAll(building.prerequisites())) {
            addConstructionJob(building, building.turnsRequired());
            return true;
        }
        return false;
    }

    ...
}
public enum TechType {
    NONE, VEGETATION, STRAWBERRIES, BIODOME, LIVESTOCK, BIOMASS;
 }
public class VegetationTech implements Tech {
    public TechType type = TechType.VEGETATION;
 
    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.NONE);

    public Set getPrerequisites() {
        return prerequisites;
    }

    public TechType getType() {
        return type;
    }
}

...

public class BiomassTech implements Tech {
    public TechType type = TechType.BIOMASS;
 
    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.VEGETATION, TechType.LIVESTOCK);

    public Set getPrerequisites() {
        return prerequisites;
    }

    public TechType getType() {
        return type;
    }
}

public class StrawberriesTech implements Tech {
    public TechType type = TechType.STRAWBERRIES;

    public static Set<TechType> prerequisites
        = EnumSet.of(TechType.VEGETATION);

    public Set getPrerequisites() {
        return prerequisites;
    }

    public TechType getType() {
        return type;
    }
}

Here you can add as many parameters as you want, such as a thumbnail icon and description string or whatever.

For a building needing a certain tech:

public class StrawberrySilo implements Building {
    private static Set<Tech> prerequisites = EnumSet.of(Tech.STRAWBERRIES);
    ...
}

Then you could schedule research jobs and construction jobs through the Player class:

public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }

    public boolean constructBuilding(Building building) {
        if (techsKnown.containsAll(building.prerequisites())) {
            addConstructionJob(building, building.turnsRequired());
            return true;
        }
        return false;
    }

    ...
}
public enum TechType { NONE, VEGETATION, STRAWBERRIES, BIODOME, LIVESTOCK, BIOMASS; }
public class VegetationTech implements Tech {
    public TechType type = TechType.VEGETATION;
    public static Set<TechType> prerequisites = EnumSet.of(TechType.NONE);

    @Override
    public Set getPrerequisites() {
        return prerequisites;
    } 

    @Override
    public TechType getType() {
        return type;
    }
}

public class BiomassTech implements Tech {
    public TechType type = TechType.BIOMASS;
    public static Set<TechType> prerequisites = EnumSet.of(TechType.VEGETATION,
                                                           TechType.LIVESTOCK); 

    @Override
    public Set getPrerequisites() {
        return prerequisites;
    } 

    @Override
    public TechType getType() {
        return type;
    }
}

Here you can add as many parameters as you want, such as a thumbnail icon and description string or whatever.

Then you could schedule research jobs through the Player class:

public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }
}
Fix variable name
Source Link
public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }

    public boolean constructBuilding(Building building) {
        if (currentTechstechsKnown.containsAll(building.prerequisites())) {
            addConstructionJob(building, building.turnsRequired());
            return true;
        }
        return false;
    }

    ...
}
public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }

    public boolean constructBuilding(Building building) {
        if (currentTechs.containsAll(building.prerequisites())) {
            addConstructionJob(building, building.turnsRequired());
            return true;
        }
        return false;
    }

    ...
}
public class Player {
    private Set<Tech> techsKnown;

    public Player() {
        techsKnown = EnumSet.of(Tech.NONE);
    }

    public boolean researchTech(Tech tech) {
        if (techsKnown.containsAll(tech.prerequisites())) {
            addResearchJob(tech, tech.turnsRequired());
            return true;
        }
        return false;
    }

    ...

    public void researchJobDone(Tech tech) {
        techsKnown.add(tech);
    }

    public boolean constructBuilding(Building building) {
        if (techsKnown.containsAll(building.prerequisites())) {
            addConstructionJob(building, building.turnsRequired());
            return true;
        }
        return false;
    }

    ...
}
Added 2nd implementation
Source Link
Loading
added 229 characters in body
Source Link
Loading
Source Link
Loading