Refactor: restructure world entities and evolution logic; remove unused files and improve batch processing
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
extends Node
|
||||
class_name Owniverse
|
||||
|
||||
var evolution: OwniverseEvolution = OwniverseEvolution.new()
|
||||
|
||||
signal unlock_item(item_id: String)
|
||||
|
||||
var current_year: float = 0
|
||||
var current_speed: int = 1
|
||||
var current_speed_in_years:float = 1
|
||||
|
||||
var entities: Dictionary[String, OwniverseEntity] = {}
|
||||
var entity_groups: Dictionary[String, Array] = {}
|
||||
var entity_event_groups: Dictionary[String, Array] = {}
|
||||
|
||||
var items_to_update: Dictionary[String, float] = {}
|
||||
|
||||
var auto_production: Array = []
|
||||
var locked_entities: Array = []
|
||||
|
||||
var cumulative_amount: Dictionary[String, float] = {}
|
||||
var current_amount: Dictionary[String, float] = {}
|
||||
|
||||
var batch_count: int = 40
|
||||
var entity_batches = {}
|
||||
|
||||
|
||||
func cycle(year_delta: float) -> void:
|
||||
# all owniverse activity should be called here
|
||||
_auto_production(year_delta)
|
||||
#_entity_evolution(year_delta)
|
||||
|
||||
_check_locked_items()
|
||||
|
||||
#var v = _get_distribution(current_amount)
|
||||
evolution.update_distribution(current_amount)
|
||||
|
||||
#for k in current_amount:
|
||||
#if current_amount[k] != 0:
|
||||
#print(k, "|", current_amount[k])
|
||||
|
||||
|
||||
#region auto production and evolution
|
||||
|
||||
func _auto_production(year_delta: float) -> void:
|
||||
var _cycle_production = {"S": 0.0, "H": 0.0}
|
||||
for entity: OwniverseEntity in auto_production:
|
||||
if current_amount[entity.id] == 0:
|
||||
continue
|
||||
_cycle_production[entity.product_per_year_type] += \
|
||||
current_amount[entity.id] * entity.product_per_year_amount * year_delta
|
||||
|
||||
for item in _cycle_production:
|
||||
_count_amount(item, _cycle_production[item])
|
||||
|
||||
|
||||
func _entity_evolution(year_delta: float) -> void:
|
||||
print("evolution: ", year_delta)
|
||||
pass
|
||||
|
||||
func _get_distribution(source: Dictionary) -> Dictionary:
|
||||
var distribution: Dictionary = {}
|
||||
var total: float = 0
|
||||
for id in source:
|
||||
if source[id] != 0:
|
||||
total += source[id]
|
||||
distribution[id] = source[id]
|
||||
for id in distribution:
|
||||
distribution[id] /= total
|
||||
return distribution
|
||||
|
||||
#endregion
|
||||
|
||||
#region Production
|
||||
|
||||
func produce(item_id: String, amount: float) -> void:
|
||||
var entity: OwniverseEntity = entities[item_id]
|
||||
|
||||
var entity_transaction = {}
|
||||
|
||||
for elem in entity.cost:
|
||||
if current_amount[elem] < amount * entity.cost[elem]:
|
||||
return
|
||||
if entity.cost[elem] == 0: continue
|
||||
|
||||
entity_transaction[elem] = -amount * entity.cost[elem]
|
||||
|
||||
for elem in entity_transaction:
|
||||
_process_enitity(elem, entity_transaction[elem])
|
||||
_process_enitity(item_id, amount)
|
||||
|
||||
|
||||
func automated_production(_delta: float) -> void:
|
||||
for entity_id in auto_production:
|
||||
var _entity = entities[entity_id]
|
||||
var _amount_to_produce = int(_entity.amount * _entity.product_per_year_amount * _delta)
|
||||
|
||||
|
||||
func get_available_to_produce(item_id: String) -> float:
|
||||
var entity: OwniverseEntity = entities[item_id]
|
||||
if entity.cost == {}: return 0
|
||||
var amount: float = 1E50
|
||||
for elem in entity.cost:
|
||||
amount = min(floorf(current_amount[elem] / entity.cost[elem]), amount)
|
||||
return amount
|
||||
|
||||
# add to all lists
|
||||
func _process_enitity(entity_id: String, amount: float) -> void:
|
||||
if amount == 0: return
|
||||
_count_amount(entity_id, amount)
|
||||
|
||||
|
||||
func _count_amount(entity_id: String, amount: float) -> void:
|
||||
if amount == 0: return
|
||||
|
||||
current_amount[entity_id] += amount
|
||||
var entity: OwniverseEntity = entities[entity_id]
|
||||
if entity.group_as != "":
|
||||
current_amount[entity.group_as] += amount
|
||||
|
||||
if amount < 0: return
|
||||
|
||||
cumulative_amount[entity_id] += amount
|
||||
if entity.group_as != "":
|
||||
cumulative_amount[entity.group_as] += amount
|
||||
|
||||
items_to_update[entity_id] = current_amount[entity_id]
|
||||
|
||||
func _check_locked_items() -> void:
|
||||
for entity:OwniverseEntity in locked_entities:
|
||||
for elem in entity.unlock:
|
||||
if cumulative_amount[elem] < entity.unlock[elem]:
|
||||
continue
|
||||
#print(entity.id, " ", elem, ":", entity.unlock[elem])
|
||||
unlock_item.emit(entity.id)
|
||||
locked_entities.erase(entity)
|
||||
return # SIC!
|
||||
|
||||
#endregion
|
||||
|
||||
#region Time
|
||||
|
||||
func add_time_delta(delta) -> float:
|
||||
var year_delta: float = delta * current_speed_in_years
|
||||
current_year += year_delta
|
||||
return current_year
|
||||
|
||||
|
||||
func change_speed(delta: int) -> int:
|
||||
current_speed = clampi(current_speed + delta, 1, Global.SPEED_LIMIT)
|
||||
current_speed_in_years = pow(10, current_speed - 1)
|
||||
return current_speed
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Init save load routines
|
||||
|
||||
|
||||
func load_data() -> bool:
|
||||
#var _is_succesfull = false
|
||||
|
||||
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
|
||||
if game_data == {}:
|
||||
return false
|
||||
|
||||
if !game_data.has_all(["cumulative_amount", "current_amount", "entity_batches"]):
|
||||
return false
|
||||
|
||||
entity_batches = game_data["entity_batches"]
|
||||
cumulative_amount = game_data["cumulative_amount"]
|
||||
current_amount = game_data["current_amount"]
|
||||
|
||||
for item in current_amount:
|
||||
items_to_update[item] = current_amount[item]
|
||||
|
||||
return true
|
||||
|
||||
|
||||
func save_data() -> void:
|
||||
var game_data: Dictionary = {}
|
||||
game_data["cumulative_amount"] = cumulative_amount
|
||||
game_data["current_amount"] = current_amount
|
||||
game_data["entity_batches"] = entity_batches
|
||||
SaveManager.save_data("owniverse", "qwerty", game_data)
|
||||
|
||||
|
||||
func init_enitities() -> void:
|
||||
for entity_id in Global.OwniverseEntities:
|
||||
var entity = OwniverseEntity.new(Global.OwniverseEntities[entity_id])
|
||||
entities[entity_id] = entity
|
||||
|
||||
current_amount[entity_id] = 0
|
||||
cumulative_amount[entity_id] = 0
|
||||
if entity.group_as != "":
|
||||
current_amount[entity.group_as] = 0
|
||||
cumulative_amount[entity.group_as] = 0
|
||||
|
||||
if entity.unlock != {}:
|
||||
locked_entities.append(entity)
|
||||
|
||||
if entity.product_per_year_type != "":
|
||||
auto_production.append(entity)
|
||||
|
||||
# groups
|
||||
if entity.group_as != "":
|
||||
if !entity_groups.has(entity.group_as):
|
||||
entity_groups[entity.group_as] = []
|
||||
entity_groups[entity.group_as].append(entity_id)
|
||||
|
||||
if entity.event_group != "":
|
||||
if !entity_event_groups.has(entity.event_group):
|
||||
entity_event_groups[entity.event_group] = []
|
||||
entity_event_groups[entity.event_group].append(entity_id)
|
||||
|
||||
#endregion
|
||||
@@ -0,0 +1 @@
|
||||
uid://chlw6b5conenp
|
||||
@@ -0,0 +1,32 @@
|
||||
extends Node
|
||||
|
||||
class_name OwniverseBatch
|
||||
|
||||
var from_year: float
|
||||
var to_year: float
|
||||
|
||||
var amount: float = 0
|
||||
|
||||
|
||||
func process(year: float) -> float:
|
||||
var _amount: float = 0
|
||||
if year < to_year: return 0
|
||||
if year >= to_year:
|
||||
_amount = amount
|
||||
amount = 0
|
||||
return _amount
|
||||
|
||||
_amount = clampf(amount * (year - from_year) / (to_year - from_year), 0, amount)
|
||||
amount -= _amount
|
||||
from_year = year + 1
|
||||
|
||||
return _amount
|
||||
|
||||
|
||||
func add(delta: float) -> void:
|
||||
amount += delta
|
||||
|
||||
|
||||
func _init(year: float, year_delta: float) -> void:
|
||||
from_year = year - year_delta
|
||||
to_year = year + year_delta
|
||||
@@ -0,0 +1 @@
|
||||
uid://cs0mllvnraida
|
||||
@@ -0,0 +1,102 @@
|
||||
extends Node
|
||||
class_name OwniverseEntity
|
||||
|
||||
var id: String
|
||||
var is_visible: bool = false
|
||||
var unlock: Dictionary = {}
|
||||
var cost: Dictionary = {}
|
||||
var bind: Dictionary = {}
|
||||
var mass: float
|
||||
var space: int
|
||||
var lifespan: float
|
||||
var lifespan_cycle: float
|
||||
var auto: float
|
||||
var gamma: float
|
||||
var blast: int
|
||||
var life_product: Dictionary = {}
|
||||
var product_per_year_type: String = ""
|
||||
var product_per_year_amount: float = 0.0
|
||||
var voids: int
|
||||
var output_or: Dictionary = {}
|
||||
var output_and: Dictionary = {}
|
||||
var destroy_level: int
|
||||
var degradation: Dictionary = {}
|
||||
var planets: Dictionary = {}
|
||||
var count_as: String
|
||||
var group_as: String
|
||||
var event_group: String
|
||||
var space_bonus: float
|
||||
var civ_bonus: float
|
||||
var claim: String
|
||||
|
||||
var amount: float = 0.0
|
||||
|
||||
|
||||
func _init(values: Dictionary) -> void:
|
||||
id = values.id
|
||||
is_visible = values.is_visible == "1"
|
||||
unlock = _parse_and_string(values.unlock)
|
||||
cost = _parse_and_string(values.cost)
|
||||
bind = _parse_and_string(values.bind)
|
||||
mass = float(values.mass)
|
||||
space = int(values.space)
|
||||
lifespan = float(values.lifespan)
|
||||
lifespan_cycle = lifespan / Global.batch_count # need for proper math
|
||||
print(id, " → ", lifespan, " | ", lifespan_cycle)
|
||||
auto = float(values.auto)
|
||||
gamma = float(values.gamma)
|
||||
blast = int(values.blast)
|
||||
life_product = _parse_and_string(values.life_product)
|
||||
for key in life_product.keys():
|
||||
product_per_year_type = key
|
||||
product_per_year_amount = life_product[key] / lifespan if lifespan != 0 else 0
|
||||
|
||||
voids = int(values.voids)
|
||||
output_or = _parse_or_string(values.output)
|
||||
output_and = _parse_and_string(values.output)
|
||||
destroy_level = int(values.destroy_level)
|
||||
degradation = _parse_and_string(values.degradation)
|
||||
planets = _parse_planet_string(values.planets)
|
||||
count_as = values.count_as
|
||||
group_as = values.group_as
|
||||
event_group = values.event_group
|
||||
space_bonus = float(values.space_bonus)
|
||||
civ_bonus = float(values.civ_bonus)
|
||||
claim = values.claim
|
||||
|
||||
|
||||
func _parse_or_string(source_line: String) -> Dictionary:
|
||||
var result = {}
|
||||
if source_line.contains("^"):
|
||||
var first_split = source_line.split("^")
|
||||
var second_split = first_split[1].split("%")
|
||||
var probability = float(second_split[1])
|
||||
result["chance"] = probability
|
||||
result["positive"] = second_split[0]
|
||||
result["negative"] = first_split[0]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
func _parse_and_string(source_line: String) -> Dictionary:
|
||||
var result = {}
|
||||
var first_split = source_line.split("&")
|
||||
|
||||
for elem in first_split:
|
||||
var second_split = elem.split(":")
|
||||
if len(second_split) == 2:
|
||||
result[second_split[0]] = float(second_split[1])
|
||||
|
||||
return result
|
||||
|
||||
func _parse_planet_string(source_line: String) -> Dictionary:
|
||||
var result = {}
|
||||
if source_line.contains("&"):
|
||||
var first_split = source_line.split("&")
|
||||
for elem in first_split:
|
||||
var second_split = elem.split(":")
|
||||
result[second_split[0]] = {}
|
||||
result[second_split[0]]["amount"] = int(second_split[1])
|
||||
result[second_split[0]]["chance"] = int(second_split[2])
|
||||
|
||||
return result
|
||||
@@ -0,0 +1 @@
|
||||
uid://tvj1grut2p4x
|
||||
@@ -0,0 +1,36 @@
|
||||
extends Node
|
||||
|
||||
class_name OwniverseEvolution
|
||||
|
||||
var batch_count: int = 40
|
||||
var batches: Dictionary[String, Array] = {}
|
||||
|
||||
var distribution: Dictionary[String, float] = {}
|
||||
|
||||
|
||||
func evolute():
|
||||
pass
|
||||
|
||||
func add_to_batch(enitity: OwniverseEntity, amount: float, year: float) -> void:
|
||||
var to_year: float = year + enitity.lifespan
|
||||
var _entity_batches: Array[OwniverseBatch] = batches[enitity.id]
|
||||
if _entity_batches.size() == 0 or _entity_batches[-1].to_year <= to_year:
|
||||
_entity_batches.append(OwniverseBatch.new(to_year, enitity.lifespan_cycle))
|
||||
|
||||
_entity_batches[-1].add(amount)
|
||||
|
||||
|
||||
func update_distribution(_distribute_by: Dictionary[String, float]) -> void:
|
||||
distribution = {}
|
||||
var _total: float = 0
|
||||
for id in _distribute_by:
|
||||
if _distribute_by[id] != 0:
|
||||
var _value: float = _distribute_by[id]
|
||||
_total += _value
|
||||
distribution[id] = _value
|
||||
|
||||
if _total == 0: return
|
||||
|
||||
for id in distribution:
|
||||
distribution[id] /= _total
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://mgnxt6kbf80n
|
||||
Reference in New Issue
Block a user