Files
owniverse/world/owniverse/owniverse_evolution.gd
T

150 lines
4.7 KiB
GDScript

extends Node
class_name OwniverseEvolution
const FLOAT_THRESHOLD: float = 1.0E14
# TODO to save !!!
var state: Dictionary = {
'year': 0.0,
'energy': 1E14,
}
var entities: Dictionary[String, OwniverseEntity] = {}
var producing_entities: Array[OwniverseEntity] = []
# TODO to save !!!
var auto_production_rest: Dictionary = {
'H': 0.0,
'S': 0.0,
'auto': 0.0,
}
var evolving_entities: Array[OwniverseEntity] = []
# TODO to save !!!
var batches: Dictionary[String, Array] = {}
# TODO to save !!!
var amount: Dictionary[String, float] = {}
var distribution: Dictionary[String, float] = {}
func add(entity: OwniverseEntity, year: float, amount: float):
var _batch_array: Array = batches[entity.id]
var _from_year: float = year + entity.lifespan - entity.lifespan_delta
var _to_year: float = year + entity.lifespan + entity.lifespan_delta
if _batch_array.size() != 0 and _batch_array[-1].to_year > year + entity.lifespan:
_batch_array[-1].amount += amount
else:
var _batch := {
'from_year': _from_year,
'to_year': _to_year,
'amount': amount,
}
_batch_array.append(_batch)
print('---------')
print(batches)
func process(from_year: float, to_year: float) -> void:
# производим
# эволюционируем
# записываем новые
var _to_produce := {}
var _to_evolute := {}
for _entity_id in batches:
_to_produce[_entity_id] = 0
_to_evolute[_entity_id] = 0
var _new_batch_array := []
for _batch: Dictionary in batches[_entity_id]:
# production
_to_produce[_entity_id] += _batch.amount
# evolution
if _batch.to_year < to_year:
_to_produce[_entity_id] += _batch.amount
else:
var _max_from_year: float = max(from_year, _batch.from_year)
var _min_to_year: float = min(to_year, _batch.to_year)
var _part_of_amount: float = _batch.amount \
* (_min_to_year - _max_from_year) \
/ (_batch.to_year - _batch.from_year)
_to_evolute[_entity_id] += _part_of_amount
var _rest_of_amount: float = _batch.amount - _part_of_amount
if _rest_of_amount > 0:
_new_batch_array.append({
'from_year': _batch._from_year,
'to_year': _batch._to_year,
'amount': _rest_of_amount,
})
# entity level
batches[_entity_id] = _new_batch_array
func produce(from_year: float, to_year: float):
var _time_delta = to_year - from_year
# get entity array for production
for _entity: OwniverseEntity in producing_entities:
var _to_produce: float = 0.0
for _batch: Dictionary in batches[_entity.id]:
_to_produce += _batch.amount
_to_produce *= _time_delta * _entity.product_per_year_amount
if _entity.auto > 0:
var _auto_production_part: float = _entity.auto * _to_produce
_to_produce -= _auto_production_part
auto_production_rest.auto += _auto_production_part
auto_production_rest[_entity.product_per_year_type] += _to_produce
print("auto prod rest: ", auto_production_rest)
auto_production_rest["H"] = produce_hs("H", auto_production_rest["H"])
auto_production_rest["S"] = produce_hs("S", auto_production_rest["S"])
func produce_hs(entity_id: String, value: float) -> float:
var _to_produce: float = floorf(value)
amount[entity_id] += _to_produce
# TODO double double math
if _to_produce > state["energy"]:
_to_produce = state["energy"]
state["energy"] -= _to_produce
return value - _to_produce
func produce_auto() -> float:
return 0.0
func init_entity(entity: OwniverseEntity) -> void:
entities[entity.id] = entity
amount[entity.id] = 0.0
if entity.product_per_year_type != "":
producing_entities.append(entity)
if entity.lifespan > 0:
evolving_entities.append(entity)
batches[entity.id] = []
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
#func _add_float(a: float, b: Array[float]) -> Array[float]:
#if a < FLOAT_THRESHOLD:
#return [a+b[0], 0]
#else:
#return [a+b[0], 0]