Reactions done
This commit is contained in:
parent
44e39ded34
commit
bd87ccf871
7 changed files with 63 additions and 7 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -32,6 +32,13 @@ func add_substance(data: RuntimeSubstanceData) -> void:
|
||||||
recalculate_temperature()
|
recalculate_temperature()
|
||||||
inventory_changed.emit(inventory.values())
|
inventory_changed.emit(inventory.values())
|
||||||
|
|
||||||
|
func add_substance_no_callback(data: RuntimeSubstanceData) -> void:
|
||||||
|
if data.substance.scientific_name in inventory:
|
||||||
|
inventory[data.substance.scientific_name].add(data)
|
||||||
|
else:
|
||||||
|
inventory[data.substance.scientific_name] = data
|
||||||
|
|
||||||
|
|
||||||
func add_inventory(substances: Array[RuntimeSubstanceData]) -> void:
|
func add_inventory(substances: Array[RuntimeSubstanceData]) -> void:
|
||||||
for sub in substances:
|
for sub in substances:
|
||||||
add_substance(sub)
|
add_substance(sub)
|
||||||
|
|
@ -84,6 +91,21 @@ func extract_solid(amount: float) -> Array[RuntimeSubstanceData]:
|
||||||
inventory_changed.emit(inventory.values())
|
inventory_changed.emit(inventory.values())
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
func extract_substance(key: String, extract_amount: float) -> RuntimeSubstanceData:
|
||||||
|
var substance: RuntimeSubstanceData = inventory[key]
|
||||||
|
var result_substance: RuntimeSubstanceData = substance.duplicate()
|
||||||
|
if substance.amount >= extract_amount:
|
||||||
|
result_substance.amount = extract_amount
|
||||||
|
inventory[result_substance.substance.scientific_name].amount -= extract_amount
|
||||||
|
else:
|
||||||
|
inventory[result_substance.substance.scientific_name].amount = 0
|
||||||
|
if inventory[result_substance.substance.scientific_name].amount == 0:
|
||||||
|
inventory.erase(result_substance.substance.scientific_name)
|
||||||
|
|
||||||
|
inventory_changed.emit(inventory.values())
|
||||||
|
|
||||||
|
return result_substance
|
||||||
|
|
||||||
func sort_by_amount(a: RuntimeSubstanceData, b: RuntimeSubstanceData) -> bool:
|
func sort_by_amount(a: RuntimeSubstanceData, b: RuntimeSubstanceData) -> bool:
|
||||||
return a.amount < b.amount
|
return a.amount < b.amount
|
||||||
|
|
||||||
|
|
@ -108,7 +130,35 @@ func recalculate_temperature() -> void:
|
||||||
mean_temperature = temperature_sum / len(inventory)
|
mean_temperature = temperature_sum / len(inventory)
|
||||||
|
|
||||||
func check_for_reactions() -> void:
|
func check_for_reactions() -> void:
|
||||||
print(ReactionRegistry.find_appropriate_reaction(inventory.values().map(runtime_to_substance),mean_temperature))
|
var substances: Array[Substance] = []
|
||||||
|
substances.resize(len(inventory))
|
||||||
func runtime_to_substance(sub: RuntimeSubstanceData) -> Substance:
|
var temp_inv: Array[RuntimeSubstanceData] = inventory.values()
|
||||||
return sub.substance
|
for i in range(len(temp_inv)):
|
||||||
|
substances[i] = temp_inv[i].substance
|
||||||
|
|
||||||
|
var found_reaction = ReactionRegistry.find_appropriate_reaction(substances,mean_temperature)
|
||||||
|
|
||||||
|
if found_reaction == null:
|
||||||
|
return
|
||||||
|
|
||||||
|
var reagents: Dictionary[StringName, ReactionSubstance] = found_reaction.input_substances_dict()
|
||||||
|
var reaction_mass: float = INF
|
||||||
|
for key in reagents:
|
||||||
|
var reagent_mass: float = inventory[key].amount/reagents[key].coefficient
|
||||||
|
reaction_mass = min(reagent_mass,reaction_mass)
|
||||||
|
|
||||||
|
for key in reagents:
|
||||||
|
extract_substance(key,reaction_mass * reagents[key].coefficient)
|
||||||
|
|
||||||
|
for sub in found_reaction.output_substances:
|
||||||
|
var data: RuntimeSubstanceData = RuntimeSubstanceData.new()
|
||||||
|
|
||||||
|
data.amount = sub.coefficient * reaction_mass
|
||||||
|
data.substance = sub.substance
|
||||||
|
data.temperature = mean_temperature
|
||||||
|
|
||||||
|
add_substance_no_callback(data)
|
||||||
|
check_for_reactions()
|
||||||
|
recalculate_temperature()
|
||||||
|
inventory_changed.emit(inventory.values())
|
||||||
|
print(inventory)
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,17 @@ func _ready() -> void:
|
||||||
reactions.append(load(REACTIONS_DIRECTORY + file))
|
reactions.append(load(REACTIONS_DIRECTORY + file))
|
||||||
|
|
||||||
func find_appropriate_reaction(input_substances: Array[Substance], temperature: float) -> Reaction:
|
func find_appropriate_reaction(input_substances: Array[Substance], temperature: float) -> Reaction:
|
||||||
|
var has_all: Callable = func(compared_array: Array[ReactionSubstance]) -> bool:
|
||||||
|
for el in compared_array:
|
||||||
|
if not el.substance in input_substances:
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
var filter_func: Callable = func(reaction: Reaction) -> bool:
|
var filter_func: Callable = func(reaction: Reaction) -> bool:
|
||||||
if reaction.reaction_temperature != -1:
|
if reaction.reaction_temperature != -1:
|
||||||
return (reaction.input_substances == input_substances) and temperature >= reaction.reaction_temperature
|
return (has_all.call(reaction.input_substances)) and temperature >= reaction.reaction_temperature
|
||||||
else:
|
else:
|
||||||
return reaction.input_substances == input_substances
|
return has_all.call(reaction.input_substances)
|
||||||
|
|
||||||
var filtered: Array[Reaction] = reactions.filter(filter_func)
|
var filtered: Array[Reaction] = reactions.filter(filter_func)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class_name Reaction
|
||||||
@export var reaction_temperature: float = -1
|
@export var reaction_temperature: float = -1
|
||||||
|
|
||||||
func input_substances_dict() -> Dictionary[StringName, ReactionSubstance]:
|
func input_substances_dict() -> Dictionary[StringName, ReactionSubstance]:
|
||||||
var result = {}
|
var result: Dictionary[StringName, ReactionSubstance] = {}
|
||||||
|
|
||||||
for el in input_substances:
|
for el in input_substances:
|
||||||
result[el.substance.scientific_name] = el
|
result[el.substance.scientific_name] = el
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue