2026-03-30 00:48:20 +03:00
|
|
|
extends Node
|
|
|
|
|
class_name Owniverse
|
|
|
|
|
|
2026-08-25 00:26:31 +03:00
|
|
|
#var state: State = State.new()
|
2026-07-20 00:11:44 +03:00
|
|
|
|
2026-05-05 23:49:12 +03:00
|
|
|
var entities: Dictionary[String, OwniverseEntity] = {}
|
2026-07-10 20:36:44 +03:00
|
|
|
var entity_groups: Dictionary[String, Array] = {}
|
|
|
|
|
var entity_event_groups: Dictionary[String, Array] = {}
|
2026-05-05 23:49:12 +03:00
|
|
|
|
|
|
|
|
var items_to_update: Dictionary[String, float] = {}
|
|
|
|
|
|
2026-07-05 12:23:33 +03:00
|
|
|
var auto_production: Array = []
|
|
|
|
|
var locked_entities: Array = []
|
|
|
|
|
|
2026-05-05 23:49:12 +03:00
|
|
|
var cumulative_amount: Dictionary[String, float] = {}
|
2026-06-03 23:26:44 +03:00
|
|
|
var current_amount: Dictionary[String, float] = {}
|
2026-05-05 23:49:12 +03:00
|
|
|
|
2026-06-24 23:25:55 +03:00
|
|
|
|
2026-08-27 23:04:56 +03:00
|
|
|
# ------------------------------------
|
2026-08-25 00:26:31 +03:00
|
|
|
|
|
|
|
|
var state: Dictionary = {
|
|
|
|
|
'datetime': 0.0,
|
|
|
|
|
'year': 0.0,
|
|
|
|
|
'speed': 1,
|
|
|
|
|
'speed_in_year': 1,
|
|
|
|
|
'energy': 1E14,
|
2026-08-27 23:04:56 +03:00
|
|
|
'batches': {},
|
|
|
|
|
'amount': {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stats: Dictionary = {
|
|
|
|
|
'total_amount': {},
|
2026-08-25 00:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:
|
2026-08-27 23:04:56 +03:00
|
|
|
|
2026-08-25 00:26:31 +03:00
|
|
|
var new_owniverse := Owniverse.new()
|
|
|
|
|
_add_time_delta(new_owniverse, delta)
|
2026-08-27 23:04:56 +03:00
|
|
|
# produce and evolute
|
|
|
|
|
new_owniverse.state = state
|
|
|
|
|
new_owniverse.stats = stats
|
|
|
|
|
new_owniverse.entities = entities
|
|
|
|
|
|
|
|
|
|
#.locked_entities = locked_entities
|
|
|
|
|
|
2026-08-25 00:26:31 +03:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2026-08-28 00:07:18 +03:00
|
|
|
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-08-25 00:26:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-08-28 00:07:18 +03:00
|
|
|
# ----------
|
2026-07-20 00:11:44 +03:00
|
|
|
|
2026-05-26 23:21:44 +03:00
|
|
|
|
2026-07-05 12:23:33 +03:00
|
|
|
#region auto production and evolution
|
2026-06-24 23:25:55 +03:00
|
|
|
|
2026-08-25 00:26:31 +03:00
|
|
|
func _produce_automatically(year_delta: float) -> void:
|
2026-07-20 00:11:44 +03:00
|
|
|
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
|
2026-07-20 00:11:44 +03:00
|
|
|
_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
|
|
|
|
|
|
2026-07-20 00:11:44 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2026-07-10 20:36:44 +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
|
|
|
|
|
|
2026-08-28 00:07:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#-----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-21 17:27:31 +03:00
|
|
|
func produce(item_id: String, amount: float) -> void:
|
2026-08-02 22:56:54 +03:00
|
|
|
var _entity: OwniverseEntity = entities[item_id]
|
2026-05-05 23:49:12 +03:00
|
|
|
|
2026-06-03 23:26:44 +03:00
|
|
|
var entity_transaction = {}
|
|
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
for elem in _entity.cost:
|
|
|
|
|
if current_amount[elem] < amount * _entity.cost[elem]:
|
2026-06-03 23:26:44 +03:00
|
|
|
return
|
2026-08-02 22:56:54 +03:00
|
|
|
if _entity.cost[elem] == 0: continue
|
2026-06-03 23:26:44 +03:00
|
|
|
|
2026-08-02 22:57:58 +03:00
|
|
|
entity_transaction[elem] = - amount * _entity.cost[elem]
|
2026-06-03 23:26:44 +03:00
|
|
|
|
|
|
|
|
for elem in entity_transaction:
|
2026-06-21 17:27:31 +03:00
|
|
|
_process_enitity(elem, entity_transaction[elem])
|
|
|
|
|
_process_enitity(item_id, amount)
|
2026-08-25 00:26:31 +03:00
|
|
|
#state.add(_entity, current_year, amount)
|
2026-05-05 23:49:12 +03:00
|
|
|
|
|
|
|
|
|
2026-08-28 00:07:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-05-26 23:21:44 +03:00
|
|
|
func automated_production(_delta: float) -> void:
|
|
|
|
|
for entity_id in auto_production:
|
2026-07-19 15:41:44 +03:00
|
|
|
var _entity = entities[entity_id]
|
|
|
|
|
var _amount_to_produce = int(_entity.amount * _entity.product_per_year_amount * _delta)
|
2026-05-05 23:49:12 +03:00
|
|
|
|
|
|
|
|
|
2026-06-03 23:26:44 +03:00
|
|
|
func get_available_to_produce(item_id: String) -> float:
|
2026-06-21 17:27:31 +03:00
|
|
|
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
|
2026-06-03 23:26:44 +03:00
|
|
|
|
2026-05-05 23:49:12 +03:00
|
|
|
# add to all lists
|
2026-06-21 17:27:31 +03:00
|
|
|
func _process_enitity(entity_id: String, amount: float) -> void:
|
2026-06-03 23:26:44 +03:00
|
|
|
if amount == 0: return
|
2026-06-21 17:27:31 +03:00
|
|
|
_count_amount(entity_id, amount)
|
2026-05-05 23:49:12 +03:00
|
|
|
|
|
|
|
|
|
2026-06-21 17:27:31 +03:00
|
|
|
func _count_amount(entity_id: String, amount: float) -> void:
|
2026-06-24 23:25:55 +03:00
|
|
|
if amount == 0: return
|
|
|
|
|
|
2026-06-21 17:27:31 +03:00
|
|
|
current_amount[entity_id] += amount
|
|
|
|
|
var entity: OwniverseEntity = entities[entity_id]
|
2026-06-03 23:26:44 +03:00
|
|
|
if entity.group_as != "":
|
|
|
|
|
current_amount[entity.group_as] += amount
|
|
|
|
|
|
2026-06-24 23:25:55 +03:00
|
|
|
if amount < 0: return
|
2026-06-03 23:26:44 +03:00
|
|
|
|
2026-06-21 17:27:31 +03:00
|
|
|
cumulative_amount[entity_id] += amount
|
2026-05-26 23:21:44 +03:00
|
|
|
if entity.group_as != "":
|
|
|
|
|
cumulative_amount[entity.group_as] += amount
|
2026-05-05 23:49:12 +03:00
|
|
|
|
2026-06-24 23:25:55 +03:00
|
|
|
items_to_update[entity_id] = current_amount[entity_id]
|
2026-05-05 23:49:12 +03:00
|
|
|
|
2026-05-26 23:21:44 +03:00
|
|
|
|
2026-05-05 23:49:12 +03:00
|
|
|
#endregion
|
|
|
|
|
|
2026-07-05 12:23:33 +03:00
|
|
|
#region Time
|
2026-04-08 00:23:22 +03:00
|
|
|
|
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
|
|
|
|
2026-07-10 20:36:44 +03:00
|
|
|
|
|
|
|
|
#region Init save load routines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func load_data() -> bool:
|
2026-08-27 23:04:56 +03:00
|
|
|
var _game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
|
|
|
|
|
if _game_data == {}:
|
2026-07-10 20:36:44 +03:00
|
|
|
return false
|
|
|
|
|
|
2026-08-27 23:04:56 +03:00
|
|
|
if !_game_data.has_all(["cumulative_amount"
|
2026-08-25 00:26:31 +03:00
|
|
|
, "current_amount"
|
2026-08-02 22:56:54 +03:00
|
|
|
#, "entity_batches"
|
2026-08-25 00:26:31 +03:00
|
|
|
#, "year",
|
2026-07-30 22:06:18 +03:00
|
|
|
]):
|
2026-07-10 20:36:44 +03:00
|
|
|
return false
|
|
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
#entity_batches = game_data["entity_batches"]
|
2026-08-27 23:04:56 +03:00
|
|
|
cumulative_amount = _game_data["cumulative_amount"]
|
|
|
|
|
current_amount = _game_data["current_amount"]
|
2026-08-28 00:07:18 +03:00
|
|
|
#print("loaded ", _game_data)
|
|
|
|
|
state = _game_data["state"]
|
|
|
|
|
stats = _game_data["stats"]
|
2026-07-10 20:36:44 +03:00
|
|
|
|
|
|
|
|
for item in current_amount:
|
|
|
|
|
items_to_update[item] = current_amount[item]
|
2026-08-27 23:04:56 +03:00
|
|
|
#print("loaded: ", _game_data)
|
|
|
|
|
#print("state: ", state)
|
|
|
|
|
#print("stats: ", stats)
|
2026-07-10 20:36:44 +03:00
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func save_data() -> void:
|
2026-08-27 23:04:56 +03:00
|
|
|
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-25 00:26:31 +03:00
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
#game_data["entity_batches"] = entity_batches
|
2026-08-27 23:04:56 +03:00
|
|
|
SaveManager.save_data("owniverse", "qwerty", _game_data)
|
2026-08-28 00:07:18 +03:00
|
|
|
#print("saved ", _game_data)
|
2026-07-10 20:36:44 +03:00
|
|
|
|
|
|
|
|
|
2026-08-27 23:04:56 +03:00
|
|
|
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-05-26 23:21:44 +03:00
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
current_amount[_entity_id] = 0
|
2026-08-27 23:04:56 +03:00
|
|
|
state.amount[_entity_id] = 0
|
2026-08-02 22:56:54 +03:00
|
|
|
cumulative_amount[_entity_id] = 0
|
2026-08-27 23:04:56 +03:00
|
|
|
stats.total_amount[_entity_id] = 0
|
2026-08-02 22:56:54 +03:00
|
|
|
if _entity.group_as != "":
|
|
|
|
|
current_amount[_entity.group_as] = 0
|
2026-08-27 23:04:56 +03:00
|
|
|
state.amount[_entity.group_as] = 0
|
|
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
cumulative_amount[_entity.group_as] = 0
|
2026-08-27 23:04:56 +03:00
|
|
|
stats.total_amount[_entity.group_as] = 0
|
2026-05-26 23:21:44 +03:00
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
if _entity.unlock != {}:
|
|
|
|
|
locked_entities.append(_entity)
|
2026-08-27 23:04:56 +03:00
|
|
|
_result.locked_items.append(_entity.id)
|
2026-08-02 22:56:54 +03:00
|
|
|
|
2026-08-25 00:26:31 +03:00
|
|
|
#state.init_entity(_entity)
|
2026-05-26 23:21:44 +03:00
|
|
|
|
2026-08-02 22:56:54 +03:00
|
|
|
if _entity.product_per_year_type != "":
|
|
|
|
|
auto_production.append(_entity)
|
|
|
|
|
_auto_production.append(_entity.id)
|
2026-07-10 20:36:44 +03:00
|
|
|
|
|
|
|
|
# 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-07-10 20:36:44 +03:00
|
|
|
|
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)
|
2026-08-27 23:04:56 +03:00
|
|
|
return _result
|
2026-07-10 20:36:44 +03:00
|
|
|
|
|
|
|
|
#endregion
|