Datasets:
File size: 7,359 Bytes
765e9a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | class_name RLNumericObservationEncoder
extends RefCounted
const SCHEMA_VERSION := 1
const ACTION_ID_MODE := "state_local_cards"
const BOARD_CHANNEL_COUNT := 12
const BOARD_SQUARE_COUNT := 64
const HAND_FEATURE_COUNT := 3
const EFFECT_FEATURE_COUNT := 2
const EFFECT_SLOT_NAMES: Array[String] = [
"extended_range",
"temporary_monarch",
]
const SCALAR_NAMES: Array[String] = [
"current_turn",
"game_over",
"winner",
"white_health",
"black_health",
"friendly_health",
"enemy_health",
"friendly_material",
"enemy_material",
"friendly_king_threatened",
"enemy_king_threatened",
"has_moved_piece_this_turn",
"cards_used_this_turn",
"max_cards_per_turn",
"card_actions_enabled",
"pending_card_credit_count",
]
const HAND_FEATURE_NAMES: Array[String] = [
"card_index",
"rarity",
"is_playable",
]
const EFFECT_FEATURE_NAMES: Array[String] = [
"active",
"turns_remaining",
]
func observation_size(hand_size: int = 5) -> int:
return (
BOARD_CHANNEL_COUNT * BOARD_SQUARE_COUNT
+ SCALAR_NAMES.size()
+ hand_size * HAND_FEATURE_COUNT
+ EFFECT_SLOT_NAMES.size() * EFFECT_FEATURE_COUNT
)
func schema(hand_size: int = 5) -> Dictionary:
var board_offset: int = 0
var scalar_offset: int = board_offset + BOARD_CHANNEL_COUNT * BOARD_SQUARE_COUNT
var hand_offset: int = scalar_offset + SCALAR_NAMES.size()
var effect_offset: int = hand_offset + hand_size * HAND_FEATURE_COUNT
return {
"schema_version": SCHEMA_VERSION,
"observation_size": observation_size(hand_size),
"action_id_mode": ACTION_ID_MODE,
"layout": {
"board_channels": {
"offset": board_offset,
"size": BOARD_CHANNEL_COUNT * BOARD_SQUARE_COUNT,
"channels": BOARD_CHANNEL_COUNT,
"squares": BOARD_SQUARE_COUNT,
"order": "friendly pawn,knight,bishop,rook,queen,king then enemy pawn,knight,bishop,rook,queen,king",
},
"scalars": {
"offset": scalar_offset,
"size": SCALAR_NAMES.size(),
"names": SCALAR_NAMES,
},
"hand": {
"offset": hand_offset,
"size": hand_size * HAND_FEATURE_COUNT,
"hand_size": hand_size,
"features_per_slot": HAND_FEATURE_COUNT,
"feature_names": HAND_FEATURE_NAMES,
},
"effects": {
"offset": effect_offset,
"size": EFFECT_SLOT_NAMES.size() * EFFECT_FEATURE_COUNT,
"slots": EFFECT_SLOT_NAMES,
"features_per_slot": EFFECT_FEATURE_COUNT,
"feature_names": EFFECT_FEATURE_NAMES,
},
},
}
func encode(observation: Dictionary, hand_size: int = 5) -> Array[float]:
var vector: Array[float] = []
vector.resize(observation_size(hand_size))
vector.fill(0.0)
var offset: int = 0
offset = _write_board_channels(vector, offset, observation.get("board_channels", []))
offset = _write_scalars(vector, offset, observation)
offset = _write_hand(vector, offset, observation.get("hand", []), hand_size)
_write_effects(vector, offset, observation.get("active_effects", []))
return vector
func _write_board_channels(vector: Array[float], offset: int, raw_channels: Variant) -> int:
var channels: Array = []
if raw_channels is Array:
channels = raw_channels
for channel_index in range(BOARD_CHANNEL_COUNT):
var channel_data: Variant = null
if channel_index < channels.size():
channel_data = channels[channel_index]
for square_index in range(BOARD_SQUARE_COUNT):
vector[offset] = _channel_value(channel_data, square_index)
offset += 1
return offset
func _write_scalars(vector: Array[float], offset: int, observation: Dictionary) -> int:
var state: Dictionary = {}
if observation.get("state", {}) is Dictionary:
state = observation.get("state", {})
vector[offset] = _as_float(state.get("current_turn", 0))
offset += 1
vector[offset] = _as_float(state.get("game_over", false))
offset += 1
vector[offset] = _as_float(state.get("winner", -1), -1.0)
offset += 1
vector[offset] = _as_float(state.get("white_health", 0))
offset += 1
vector[offset] = _as_float(state.get("black_health", 0))
offset += 1
vector[offset] = _as_float(state.get("friendly_health", 0))
offset += 1
vector[offset] = _as_float(state.get("enemy_health", 0))
offset += 1
vector[offset] = _as_float(state.get("friendly_material", 0))
offset += 1
vector[offset] = _as_float(state.get("enemy_material", 0))
offset += 1
vector[offset] = _as_float(state.get("friendly_king_threatened", false))
offset += 1
vector[offset] = _as_float(state.get("enemy_king_threatened", false))
offset += 1
vector[offset] = _as_float(state.get("has_moved_piece_this_turn", false))
offset += 1
vector[offset] = _as_float(observation.get("cards_used_this_turn", 0))
offset += 1
vector[offset] = _as_float(observation.get("max_cards_per_turn", 0))
offset += 1
vector[offset] = _as_float(observation.get("card_actions_enabled", false))
offset += 1
vector[offset] = _as_float(observation.get("pending_card_credit_count", 0))
offset += 1
return offset
func _write_hand(
vector: Array[float],
offset: int,
raw_hand: Variant,
hand_size: int
) -> int:
var hand: Array = []
if raw_hand is Array:
hand = raw_hand
for hand_index in range(hand_size):
var entry: Dictionary = {}
if hand_index < hand.size() and hand[hand_index] is Dictionary:
entry = hand[hand_index]
vector[offset] = _as_float(entry.get("card_index", -1), -1.0)
offset += 1
vector[offset] = _as_float(entry.get("rarity", -1), -1.0)
offset += 1
vector[offset] = _as_float(entry.get("is_playable", false))
offset += 1
return offset
func _write_effects(vector: Array[float], offset: int, raw_effects: Variant) -> int:
var effect_offsets: Dictionary = {}
for slot_index in range(EFFECT_SLOT_NAMES.size()):
effect_offsets[slot_index] = offset + slot_index * EFFECT_FEATURE_COUNT
var effects: Array = []
if raw_effects is Array:
effects = raw_effects
for raw_effect in effects:
if not (raw_effect is Dictionary):
continue
var effect: Dictionary = raw_effect
var slot_index: int = _effect_slot_index(str(effect.get("effect", "")))
if slot_index < 0:
continue
var slot_offset: int = int(effect_offsets[slot_index])
vector[slot_offset] = 1.0
vector[slot_offset + 1] = maxf(
vector[slot_offset + 1],
_as_float(effect.get("turns_remaining", 0))
)
return offset + EFFECT_SLOT_NAMES.size() * EFFECT_FEATURE_COUNT
func _channel_value(channel_data: Variant, square_index: int) -> float:
if channel_data is PackedInt32Array:
var int_channel: PackedInt32Array = channel_data
if square_index < int_channel.size():
return float(int_channel[square_index])
if channel_data is PackedFloat32Array:
var float_channel: PackedFloat32Array = channel_data
if square_index < float_channel.size():
return float(float_channel[square_index])
if channel_data is Array:
var array_channel: Array = channel_data
if square_index < array_channel.size():
return _as_float(array_channel[square_index])
return 0.0
func _effect_slot_index(effect_name: String) -> int:
var normalized: String = effect_name.to_lower()
if normalized.find("extended") >= 0 or normalized.find("range") >= 0:
return 0
if normalized.find("temporary") >= 0 or normalized.find("monarch") >= 0:
return 1
return -1
func _as_float(value: Variant, default_value: float = 0.0) -> float:
if value == null:
return default_value
if value is bool:
return 1.0 if bool(value) else 0.0
if value is int or value is float:
return float(value)
return default_value
|