Small steps towards mods

This commit is contained in:
Yair Morgenstern 2019-12-27 00:15:42 +02:00
parent 87830bf8b2
commit 80be3c276e
4 changed files with 77 additions and 27 deletions

View File

@ -1,13 +1,16 @@
[ [
{ {
name:"Warrior", name:"Maori Warrior",
unitType:"Melee", unitType:"Melee",
uniqueTo:"Polynesia",
replaces:"Warrior",
movement:2, movement:2,
strength:12, strength:80,
cost: 40, cost: 40,
hurryCostModifier:20, hurryCostModifier:20,
obsoleteTech:"Metal Casting", obsoleteTech:"Metal Casting",
promotions:["Haka War Dance"],
upgradesTo:"Swordsman", upgradesTo:"Swordsman",
attackSound:"nonmetalhit" attackSound:"nonmetalhit"
} },
] ]

View File

@ -13,7 +13,8 @@ import com.unciv.models.stats.INamed
import kotlin.collections.set import kotlin.collections.set
class Ruleset { class Ruleset {
val mods = ArrayList<String>() var name=""
val mods = LinkedHashSet<String>()
val buildings = LinkedHashMap<String, Building>() val buildings = LinkedHashMap<String, Building>()
val terrains = LinkedHashMap<String, Terrain>() val terrains = LinkedHashMap<String, Terrain>()
val tileResources = LinkedHashMap<String, TileResource>() val tileResources = LinkedHashMap<String, TileResource>()
@ -25,7 +26,20 @@ class Ruleset {
val policyBranches = LinkedHashMap<String, PolicyBranch>() val policyBranches = LinkedHashMap<String, PolicyBranch>()
val difficulties = LinkedHashMap<String, Difficulty>() val difficulties = LinkedHashMap<String, Difficulty>()
fun clone(): Ruleset{
val newRuleset = Ruleset(false)
newRuleset.add(this)
return newRuleset
}
constructor(load:Boolean=true){
if(load) load()
}
fun <T> getFromJson(tClass: Class<T>, filePath:String): T { fun <T> getFromJson(tClass: Class<T>, filePath:String): T {
val file = Gdx.files.internal(filePath)
if(!file.exists()) return tClass Array<>().first()
val jsonText = Gdx.files.internal(filePath).readString(Charsets.UTF_8.name()) val jsonText = Gdx.files.internal(filePath).readString(Charsets.UTF_8.name())
return Json().apply { ignoreUnknownFields = true }.fromJson(tClass, jsonText) return Json().apply { ignoreUnknownFields = true }.fromJson(tClass, jsonText)
} }
@ -37,24 +51,32 @@ class Ruleset {
return hashMap return hashMap
} }
fun clone(): Ruleset{ fun add(ruleset: Ruleset){
val newRuleset = Ruleset(false) buildings.putAll(ruleset.buildings)
newRuleset.buildings.putAll(buildings) difficulties.putAll(ruleset.difficulties)
newRuleset.difficulties.putAll(difficulties) nations .putAll(ruleset.nations)
newRuleset.nations .putAll(nations) policyBranches.putAll(ruleset.policyBranches)
newRuleset.policyBranches.putAll(policyBranches) technologies.putAll(ruleset.technologies)
newRuleset.technologies.putAll(technologies) buildings.putAll(ruleset.buildings)
newRuleset.buildings.putAll(buildings) terrains.putAll(ruleset.terrains)
newRuleset.terrains.putAll(terrains) tileImprovements.putAll(ruleset.tileImprovements)
newRuleset.tileImprovements.putAll(tileImprovements) tileResources.putAll(ruleset.tileResources)
newRuleset.tileResources.putAll(tileResources) unitPromotions.putAll(ruleset.unitPromotions)
newRuleset.unitPromotions.putAll(unitPromotions) units.putAll(ruleset.units)
newRuleset.units.putAll(units)
return newRuleset
} }
constructor(load:Boolean=true){ fun clearExceptModNames(){
if(load) load() buildings.clear()
difficulties.clear()
nations.clear()
policyBranches.clear()
technologies.clear()
buildings.clear()
terrains.clear()
tileImprovements.clear()
tileResources.clear()
unitPromotions.clear()
units.clear()
} }
fun load(folderPath: String="jsons") { fun load(folderPath: String="jsons") {

View File

@ -25,7 +25,7 @@ class NewGameScreen: PickerScreen(){
val newGameParameters= UncivGame.Current.gameInfo.gameParameters val newGameParameters= UncivGame.Current.gameInfo.gameParameters
val mapParameters = UncivGame.Current.gameInfo.tileMap.mapParameters val mapParameters = UncivGame.Current.gameInfo.tileMap.mapParameters
val ruleSet = UncivGame.Current.ruleset val ruleSet = UncivGame.Current.ruleset.clone()
init { init {
setDefaultCloseAction() setDefaultCloseAction()

View File

@ -16,10 +16,11 @@ import com.unciv.models.translations.tr
import com.unciv.ui.utils.CameraStageBaseScreen import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.toLabel import com.unciv.ui.utils.toLabel
class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val onMultiplayerToggled:()->Unit) class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlayerPickerTable:()->Unit)
: Table(CameraStageBaseScreen.skin) { : Table(CameraStageBaseScreen.skin) {
val newGameParameters = newGameScreen.newGameParameters val newGameParameters = newGameScreen.newGameParameters
val mapParameters = newGameScreen.mapParameters val mapParameters = newGameScreen.mapParameters
val baseRuleset = newGameScreen.ruleSet.clone()
val ruleset = newGameScreen.ruleSet val ruleset = newGameScreen.ruleSet
init { init {
@ -133,7 +134,7 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val onMultipla
isOnlineMultiplayerCheckbox.addListener(object : ChangeListener() { isOnlineMultiplayerCheckbox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) { override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.isOnlineMultiplayer = isOnlineMultiplayerCheckbox.isChecked newGameParameters.isOnlineMultiplayer = isOnlineMultiplayerCheckbox.isChecked
onMultiplayerToggled() updatePlayerPickerTable()
} }
}) })
add(isOnlineMultiplayerCheckbox).colspan(2).row() add(isOnlineMultiplayerCheckbox).colspan(2).row()
@ -225,20 +226,44 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val onMultipla
val modCheckboxTable = Table().apply { defaults().pad(10f) } val modCheckboxTable = Table().apply { defaults().pad(10f) }
val mods = Gdx.files.local("mods") val modFolders = Gdx.files.local("mods")
val loadableMods = ArrayList<Ruleset>()
for (modFolder in mods.list()) { for (modFolder in modFolders.list()) {
if (modFolder.list().any { it.name() == "jsons" }) { if (modFolder.list().any { it.name() == "jsons" }) {
val ruleSet = Ruleset(false) val ruleSet = Ruleset(false)
try { try {
val modRuleset = ruleSet.load(modFolder.path() + "/jsons") ruleSet.load(modFolder.path() + "/jsons")
ruleSet.name = modFolder.nameWithoutExtension()
loadableMods.add(ruleset)
} catch (ex: Exception) { } catch (ex: Exception) {
print(ex.message)
} }
} }
} }
fun reloadMods(){
ruleset.clearExceptModNames()
ruleset.add(baseRuleset)
for(modName in ruleset.mods){
val correspondingMod = loadableMods.first { it.name==modName }
ruleset.add(correspondingMod)
}
}
for(mod in loadableMods){
val checkBox = CheckBox(mod.name,CameraStageBaseScreen.skin)
checkBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
if(checkBox.isChecked) ruleset.mods.add(mod.name)
else ruleset.mods.remove(mod.name)
reloadMods()
updatePlayerPickerTable()
}
})
}
add(modCheckboxTable).colspan(2).row() add(modCheckboxTable).colspan(2).row()
} }