mirror of
https://github.com/yairm210/Unciv.git
synced 2025-03-07 07:53:00 +07:00
Kotlin'D Counter class, apparently it's been Java this whole time and I didn't even notice
This commit is contained in:
parent
aea2e3efcf
commit
931f485a8e
Binary file not shown.
Before Width: | Height: | Size: 6.6 KiB |
Binary file not shown.
Before Width: | Height: | Size: 3.6 KiB |
Binary file not shown.
Before Width: | Height: | Size: 10 KiB |
Binary file not shown.
Before Width: | Height: | Size: 20 KiB |
Binary file not shown.
Before Width: | Height: | Size: 32 KiB |
@ -32,7 +32,7 @@ class NextTurnAutomation{
|
||||
}
|
||||
|
||||
private fun buyBuildingOrUnit(civInfo: CivilizationInfo) {
|
||||
//allow AI spending money to purchase building & unit. Buying staff has slightly lower priority than buying tech.
|
||||
//allow AI spending money to purchase building & unit
|
||||
for (city in civInfo.cities.sortedByDescending{ it.population.population }) {
|
||||
val construction = city.cityConstructions.getCurrentConstruction()
|
||||
if (construction.canBePurchased()
|
||||
|
@ -128,7 +128,7 @@ class CityInfo {
|
||||
}
|
||||
|
||||
for (building in cityConstructions.getBuiltBuildings().filter { it.requiredResource != null }) {
|
||||
val resource = GameBasics.TileResources[building.requiredResource]
|
||||
val resource = GameBasics.TileResources[building.requiredResource]!!
|
||||
cityResources.add(resource, -1)
|
||||
}
|
||||
|
||||
|
@ -230,7 +230,7 @@ class CivilizationInfo {
|
||||
val civResources = Counter<TileResource>()
|
||||
for (city in cities) civResources.add(city.getCityResources())
|
||||
for (dip in diplomacy.values) civResources.add(dip.resourcesFromTrade())
|
||||
for(resource in getCivUnits().mapNotNull { it.baseUnit.requiredResource }.map { GameBasics.TileResources[it] })
|
||||
for(resource in getCivUnits().mapNotNull { it.baseUnit.requiredResource }.map { GameBasics.TileResources[it]!! })
|
||||
civResources.add(resource,-1)
|
||||
return civResources
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
package com.unciv.models;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class Counter<K> extends LinkedHashMap<K,Integer> {
|
||||
|
||||
public Integer get(Object key){ // don't return null if empty
|
||||
if(containsKey(key)) return super.get(key);
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public void add(K key, int value){
|
||||
if(!containsKey(key)) put(key,value);
|
||||
else put(key,get(key)+value);
|
||||
if(get(key)==0) remove(key); // No objects of this sort left, no need to count
|
||||
}
|
||||
|
||||
public void add(Counter<K> other){
|
||||
for (K key : other.keySet()) {
|
||||
add(key,other.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Counter<K> other){
|
||||
for (K key : other.keySet()) {
|
||||
add(key,-other.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Counter<K> clone() {
|
||||
Counter<K> newCounter = new Counter<K>();
|
||||
newCounter.add(this);
|
||||
return newCounter;
|
||||
}
|
||||
}
|
38
core/src/com/unciv/models/Counter.kt
Normal file
38
core/src/com/unciv/models/Counter.kt
Normal file
@ -0,0 +1,38 @@
|
||||
package com.unciv.models
|
||||
|
||||
import java.util.*
|
||||
|
||||
class Counter<K> : LinkedHashMap<K, Int>() {
|
||||
|
||||
override operator fun get(key: K): Int? { // don't return null if empty
|
||||
if (containsKey(key))
|
||||
return super.get(key)
|
||||
else return 0
|
||||
}
|
||||
|
||||
fun add(key: K, value: Int) {
|
||||
if (!containsKey(key))
|
||||
put(key, value)
|
||||
else
|
||||
put(key, get(key)!! + value)
|
||||
if (get(key) == 0) remove(key) // No objects of this sort left, no need to count
|
||||
}
|
||||
|
||||
fun add(other: Counter<K>) {
|
||||
for (key in other.keys) {
|
||||
add(key, other[key]!!)
|
||||
}
|
||||
}
|
||||
|
||||
fun remove(other: Counter<K>) {
|
||||
for (key in other.keys) {
|
||||
add(key, -other[key]!!)
|
||||
}
|
||||
}
|
||||
|
||||
override fun clone(): Counter<K> {
|
||||
val newCounter = Counter<K>()
|
||||
newCounter.add(this)
|
||||
return newCounter
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user