Changed API to start using Poolable stats

Changed API to start using Poolable stats
Moved modified attribute to Stat
This commit is contained in:
Collin Smith 2019-04-21 03:41:23 -07:00
parent b8449d6f42
commit 87c372bad5
2 changed files with 82 additions and 58 deletions

View File

@ -58,7 +58,7 @@ public class PropertyList implements Iterable<Stat> {
}
public void put(int stat, int value) {
props.put(stat, Stat.create(stat, value));
props.put(stat, Stat.obtain(stat, value));
}
Stat get() {
@ -67,7 +67,7 @@ public class PropertyList implements Iterable<Stat> {
}
public int read(int stat, BitStream bitStream) {
Stat instance = Stat.read(stat, bitStream);
Stat instance = Stat.obtain(stat, bitStream);
props.put(instance.hash, instance);
return instance.val;
}
@ -233,17 +233,17 @@ public class PropertyList implements Iterable<Stat> {
switch (prop.func[j]) {
case 1: // vit, str, hp, etc.
value = MathUtils.random(min[i], max[i]);
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 2: // item_armor_percent
value = MathUtils.random(min[i], max[i]);
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 3: // res-all, all-stats, etc -- reference previous index for values
assert value != Integer.MIN_VALUE;
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 5: // dmg-min
@ -261,24 +261,24 @@ public class PropertyList implements Iterable<Stat> {
return value;
case 8: // fcr, fwr, fbr, fhr, etc
value = MathUtils.random(min[i], max[i]);
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 10: // skilltab
value = MathUtils.random(min[i], max[i]);
inst = Stat.create(desc.ID, params[i], value);
inst = Stat.obtain(desc.ID, params[i], value);
props.put(inst.hash, inst);
return value;
case 11: // att-skill, hit-skill, gethit-skill, kill-skill, death-skill, levelup-skill
value = min[i]; // skill
param = Stat.encodeParam(desc.Encode, max[i], params[i]); // %, level
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 12: // skill-rand (Ormus' Robes)
value = params[i]; // skill level
param = MathUtils.random(min[i], max[i]); // random skill
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 13: // dur%
@ -288,22 +288,22 @@ public class PropertyList implements Iterable<Stat> {
case 14: // sock
// TODO: set item SOCKETED flag?
value = MathUtils.random(min[i], max[i]);
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 15: // dmg-* (min)
value = min[i];
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 16: // dmg-* (max)
value = max[i];
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 17: // dmg-* (length) and */lvl
value = params[i];
inst = Stat.create(desc.ID, value);
inst = Stat.obtain(desc.ID, value);
props.put(inst.hash, inst);
return value;
case 18: // */time // TODO: Add support
@ -312,7 +312,7 @@ public class PropertyList implements Iterable<Stat> {
case 19: // charged (skill)
value = Stat.encodeValue(3, min[i], min[i]); // charges
param = Stat.encodeParam(3, max[i], params[i]); // level, skill
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 20: // indestruct
@ -323,13 +323,13 @@ public class PropertyList implements Iterable<Stat> {
case 21: // ama, pal, nec, etc. (item_addclassskills) and fireskill
value = MathUtils.random(min[i], max[i]);
param = prop.val[j];
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 22: // skill, aura, oskill
value = MathUtils.random(min[i], max[i]);
param = params[i];
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 23: // ethereal
@ -338,13 +338,13 @@ public class PropertyList implements Iterable<Stat> {
case 24: // reanimate, att-mon%, dmg-mon%, state
value = MathUtils.random(min[i], max[i]);
param = params[i];
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 36: // randclassskill
value = prop.val[j]; // skill levels
param = MathUtils.random(min[i], max[i]); // random class
inst = Stat.create(desc.ID, param, value);
inst = Stat.obtain(desc.ID, param, value);
props.put(inst.hash, inst);
return value;
case 4:

View File

@ -3,6 +3,8 @@ package com.riiablo.item;
import com.google.common.primitives.UnsignedInts;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pools;
import com.riiablo.CharData;
import com.riiablo.CharacterClass;
import com.riiablo.Riiablo;
@ -15,7 +17,7 @@ import com.riiablo.codec.util.BitStream;
import java.util.Arrays;
@SuppressWarnings("unused")
public class Stat implements Comparable<Stat> {
public class Stat implements Comparable<Stat>, Pool.Poolable {
private static final String TAG = "Stat";
public static final int strength = 0;
@ -455,56 +457,73 @@ public class Stat implements Comparable<Stat> {
}
}
static Stat read(int stat, BitStream bitStream) {
return new Stat(stat, bitStream);
private static final Pool<Stat> POOL = Pools.get(Stat.class, 256);
static Stat obtain(int stat, BitStream bitStream) {
return POOL.obtain()._obtain(stat, bitStream);
}
static Stat create(int stat, int value) {
return create(stat, 0, value);
static Stat obtain(int stat, int value) {
return obtain(stat, 0, value);
}
static Stat create(int stat, int param, int value) {
return new Stat(stat, param, value);
static Stat obtain(int stat, int param, int value) {
return POOL.obtain()._obtain(stat, param, value);
}
public final int id;
public final int param; // can probably safely be truncated to short
final int hash;
public final ItemStatCost.Entry entry;
static Stat obtain(Stat src) {
return POOL.obtain()._obtain(src);
}
public int id;
public int param;
public ItemStatCost.Entry entry;
boolean modified;
int hash;
int val;
Stat(int stat, BitStream bitStream) {
this.id = stat;
entry = Riiablo.files.ItemStatCost.get(stat);
param = bitStream.readUnsigned31OrLess(entry.Save_Param_Bits);
val = bitStream.readUnsigned31OrLess(entry.Save_Bits) - entry.Save_Add;
hash = Stat.hash(stat, param);
Stat() {}
@Override
public void reset() {}
Stat _obtain(int stat, BitStream bitStream) {
this.id = stat;
entry = Riiablo.files.ItemStatCost.get(stat);
param = bitStream.readUnsigned31OrLess(entry.Save_Param_Bits);
val = bitStream.readUnsigned31OrLess(entry.Save_Bits) - entry.Save_Add;
hash = hash(stat, param);
modified = false;
return this;
}
Stat(int stat, int param, int value) {
this.id = stat;
Stat _obtain(int stat, int param, int value) {
this.id = stat;
this.param = param;
this.val = value;
entry = Riiablo.files.ItemStatCost.get(stat);
hash = Stat.hash(stat, param);
this.val = value;
entry = Riiablo.files.ItemStatCost.get(stat);
hash = Stat.hash(stat, param);
modified = false;
return this;
}
Stat(Stat src) {
this.id = src.id;
this.param = src.param;
this.hash = src.hash;
this.entry = src.entry;
this.val = src.val;
Stat _obtain(Stat src) {
id = src.id;
param = src.param;
hash = src.hash;
entry = src.entry;
val = src.val;
modified = src.modified;
return this;
}
Stat copy() {
return new Stat(this);
return obtain(this);
}
@Override
public int compareTo(Stat o) {
return o.entry.descpriority - this.entry.descpriority;
public int compareTo(Stat other) {
return other.entry.descpriority - entry.descpriority;
}
@Override
@ -521,19 +540,21 @@ public class Stat implements Comparable<Stat> {
* 4 : 0 | 2,10,10
*/
public Stat add(Stat other) {
int value1, value2, value3;
switch (entry.Encode) {
case 3:
value1 = Math.min(value1() + other.value1(), (1 << 8) - 1);
value2 = Math.min(value2() + other.value2(), (1 << 8) - 1);
case 3: {
int value1 = Math.min(value1() + other.value1(), (1 << 8) - 1);
int value2 = Math.min(value2() + other.value2(), (1 << 8) - 1);
val = (value2 << 8) | value1;
break;
case 4:
}
case 4: {
// TODO: see issue #24
value2 = Math.min(value2() + other.value2(), (1 << 10) - 1);
value3 = Math.min(value3() + other.value3(), (1 << 10) - 1);
val = (value3 << 12) | (value2 << 2) | (val & 0x3);
int value1 = value1();
int value2 = Math.min(value2() + other.value2(), (1 << 10) - 1);
int value3 = Math.min(value3() + other.value3(), (1 << 10) - 1);
val = (value3 << 12) | (value2 << 2) | value1;
break;
}
case 0:
case 1:
case 2:
@ -900,7 +921,10 @@ public class Stat implements Comparable<Stat> {
Stat[] stats;
Aggregate(int stat, String str, String str2, Stat... stats) {
super(stat, 0, 0);
id = stat;
param = 0;
val = 0;
entry = Riiablo.files.ItemStatCost.get(stat);
this.stats = stats;
this.str = str;
this.str2 = str2;