Files
owniverse/world/owniverse/owniverse.gd
T

272 lines
7.2 KiB
GDScript
Raw Normal View History

2026-03-30 00:48:20 +03:00
extends Node
class_name Owniverse
#var state: State = State.new()
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] = {}
2026-07-05 12:23:33 +03:00
var auto_production: Array = []
var locked_entities: Array = []
var cumulative_amount: Dictionary[String, float] = {}
var current_amount: Dictionary[String, float] = {}
2026-06-24 23:25:55 +03:00
# ------------------------------------
var state: Dictionary = {
'datetime': 0.0,
'year': 0.0,
'speed': 1,
'speed_in_year': 1,
'energy': 1E14,
'batches': {},
'amount': {},
}
var stats: Dictionary = {
'total_amount': {},
}
var speed: int:
get:
return state.speed
set(value):
state.speed = clampi(value, 1, Global.SPEED_LIMIT)
state.speed_in_years = pow(10, state.speed - 1)
func evolute(delta: float) -> Owniverse:
var new_owniverse := Owniverse.new()
_add_time_delta(new_owniverse, delta)
# produce and evolute
new_owniverse.state = state
new_owniverse.stats = stats
new_owniverse.entities = entities
#.locked_entities = locked_entities
return new_owniverse
func _add_time_delta(new_owniverse: Owniverse, delta: float):
new_owniverse.state.speed_in_year = state.speed_in_year
new_owniverse.state.datetime = state.datetime \
+ delta * new_owniverse.state.speed_in_year
new_owniverse.state.year = floorf(new_owniverse.state.datetime)
func produce_hs(entity_id: String, value: float) -> float:
var _to_produce: float = floorf(value)
_to_produce = min(_to_produce, state.energy)
state.energy -= _to_produce
state.amount[entity_id] += _to_produce
stats.total_amount[entity_id] += _to_produce
return _to_produce
# ----------
2026-07-05 12:23:33 +03:00
#region auto production and evolution
2026-06-24 23:25:55 +03:00
func _produce_automatically(year_delta: float) -> void:
var _cycle_production = {"S": 0.0, "H": 0.0}
2026-06-24 23:25:55 +03:00
for entity: OwniverseEntity in auto_production:
if current_amount[entity.id] == 0:
2026-08-02 22:57:58 +03:00
continue
_cycle_production[entity.product_per_year_type] += \
2026-06-24 23:25:55 +03:00
current_amount[entity.id] * entity.product_per_year_amount * year_delta
for item in _cycle_production:
2026-08-02 22:57:58 +03:00
_count_amount(item, _cycle_production[item])
2026-06-24 23:25:55 +03:00
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
2026-06-24 23:25:55 +03:00
#endregion
#region Production
#-----
func produce(item_id: String, amount: float) -> void:
2026-08-02 22:56:54 +03:00
var _entity: OwniverseEntity = entities[item_id]
var entity_transaction = {}
2026-08-02 22:56:54 +03:00
for elem in _entity.cost:
if current_amount[elem] < amount * _entity.cost[elem]:
return
2026-08-02 22:56:54 +03:00
if _entity.cost[elem] == 0: continue
2026-08-02 22:57:58 +03:00
entity_transaction[elem] = - amount * _entity.cost[elem]
for elem in entity_transaction:
_process_enitity(elem, entity_transaction[elem])
_process_enitity(item_id, amount)
#state.add(_entity, current_year, 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:
2026-06-24 23:25:55 +03:00
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
2026-06-24 23:25:55 +03:00
if amount < 0: return
cumulative_amount[entity_id] += amount
if entity.group_as != "":
cumulative_amount[entity.group_as] += amount
2026-06-24 23:25:55 +03:00
items_to_update[entity_id] = current_amount[entity_id]
#endregion
2026-07-05 12:23:33 +03:00
#region Time
2026-06-24 23:25:55 +03:00
2026-07-05 12:23:33 +03:00
#endregion
2026-06-24 23:25:55 +03:00
#region Init save load routines
func load_data() -> bool:
var _game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
if _game_data == {}:
return false
if !_game_data.has_all(["cumulative_amount"
, "current_amount"
2026-08-02 22:56:54 +03:00
#, "entity_batches"
#, "year",
]):
return false
2026-08-02 22:56:54 +03:00
#entity_batches = game_data["entity_batches"]
cumulative_amount = _game_data["cumulative_amount"]
current_amount = _game_data["current_amount"]
#print("loaded ", _game_data)
state = _game_data["state"]
stats = _game_data["stats"]
for item in current_amount:
items_to_update[item] = current_amount[item]
#print("loaded: ", _game_data)
#print("state: ", state)
#print("stats: ", stats)
return true
func save_data() -> void:
var _game_data: Dictionary = {}
_game_data["cumulative_amount"] = cumulative_amount
_game_data["current_amount"] = current_amount
_game_data["state"] = state
_game_data["stats"] = stats
2026-08-02 22:56:54 +03:00
#game_data["entity_batches"] = entity_batches
SaveManager.save_data("owniverse", "qwerty", _game_data)
#print("saved ", _game_data)
func init_enitities() -> Dictionary:
var _result := {
'locked_items' : [],
}
2026-08-02 22:56:54 +03:00
var _auto_production := []
for _entity_id in Global.OwniverseEntities:
var _entity = OwniverseEntity.new(Global.OwniverseEntities[_entity_id])
entities[_entity_id] = _entity
2026-08-02 22:56:54 +03:00
current_amount[_entity_id] = 0
state.amount[_entity_id] = 0
2026-08-02 22:56:54 +03:00
cumulative_amount[_entity_id] = 0
stats.total_amount[_entity_id] = 0
2026-08-02 22:56:54 +03:00
if _entity.group_as != "":
current_amount[_entity.group_as] = 0
state.amount[_entity.group_as] = 0
2026-08-02 22:56:54 +03:00
cumulative_amount[_entity.group_as] = 0
stats.total_amount[_entity.group_as] = 0
2026-08-02 22:56:54 +03:00
if _entity.unlock != {}:
locked_entities.append(_entity)
_result.locked_items.append(_entity.id)
2026-08-02 22:56:54 +03:00
#state.init_entity(_entity)
2026-08-02 22:56:54 +03:00
if _entity.product_per_year_type != "":
auto_production.append(_entity)
_auto_production.append(_entity.id)
# groups
2026-08-02 22:56:54 +03:00
if _entity.group_as != "":
if !entity_groups.has(_entity.group_as):
entity_groups[_entity.group_as] = []
entity_groups[_entity.group_as].append(_entity_id)
2026-08-02 22:56:54 +03:00
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)
#evolution.init_batches(_auto_production)
return _result
#endregion