Kotlin'D Counter class, apparently it's been Java this whole time and I didn't even notice

This commit is contained in:
Yair Morgenstern 2019-04-25 10:28:46 +03:00
parent aea2e3efcf
commit 931f485a8e
10 changed files with 41 additions and 39 deletions

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

View File

@ -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()

View File

@ -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)
}

View File

@ -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
}

View File

@ -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;
}
}

View 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
}
}