Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reaction Codex Fix for Unstable Ingredients #859

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions code/__HELPERS/cmp.dm
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,7 @@ GLOBAL_VAR_INIT(cmp_field, "name")
/// Orders lists by the size of lists in their contents
/proc/cmp_list_length(list/A, list/B)
return length(A) - length(B)

/// Orders codex entries by name alphabetically
/proc/cmp_codex_name(datum/codex_entry/a, datum/codex_entry/b)
return sorttext(b.name, a.name)
76 changes: 57 additions & 19 deletions code/controllers/subsystem/codex.dm
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ SUBSYSTEM_DEF(codex)
string = replacetextEx(string, linkRegex.match, replacement)
return string

/// Returns a codex entry for the given query. May return a list if multiple are found, or null if none.
/datum/controller/subsystem/codex/proc/get_codex_entry(entry)
if(isatom(entry))
var/atom/entity = entry
. = entity.get_specific_codex_entry()
if(.)
return
return entries_by_path[entity.type] || get_entry_by_string(entity.name)

if(isdatum(entry))
entry = entry:type
if(ispath(entry))
return entries_by_path[entry]
if(istext(entry))
Expand All @@ -74,13 +78,40 @@ SUBSYSTEM_DEF(codex)
/datum/controller/subsystem/codex/proc/get_entry_by_string(string)
return entries_by_string[codex_sanitize(string)]

/// Presents a codex entry to a mob. If it receives a list of entries, it will prompt them to choose one.
/datum/controller/subsystem/codex/proc/present_codex_entry(mob/presenting_to, datum/codex_entry/entry)
if(entry && istype(presenting_to) && presenting_to.client)
var/datum/browser/popup = new(presenting_to, "codex", "Codex", nheight=425) //"codex\ref[entry]"
var/entry_data = entry.get_codex_body(presenting_to)
popup.set_content(parse_links(jointext(entry_data, null), presenting_to))
popup.open()

if(!entry || !istype(presenting_to) || !presenting_to.client)
return

if(islist(entry))
present_codex_search(presenting_to, entry)
return

var/datum/browser/popup = new(presenting_to, "codex", "Codex", nheight=425) //"codex\ref[entry]"
var/entry_data = entry.get_codex_body(presenting_to)
popup.set_content(parse_links(jointext(entry_data, null), presenting_to))
popup.open()

#define CODEX_ENTRY_LIMIT 10
/// Presents a list of codex entries to a mob.
/datum/controller/subsystem/codex/proc/present_codex_search(mob/presenting_to, list/entries, search_query)
var/list/codex_data = list()
codex_data += "<h3><b>[all_entries.len] matches</b>[search_query ? " for '[search_query]'" : ""]:</h3>"

if(LAZYLEN(entries) > CODEX_ENTRY_LIMIT)
codex_data += "Showing first <b>[CODEX_ENTRY_LIMIT]</b> entries. <b>[all_entries.len - 5] result\s</b> omitted.</br>"
codex_data += "<table width = 100%>"

for(var/i = 1 to min(entries.len, CODEX_ENTRY_LIMIT))
var/datum/codex_entry/entry = entries[i]
codex_data += "<tr><td>[entry.name]</td><td><a href='?src=\ref[SScodex];show_examined_info=\ref[entry];show_to=\ref[presenting_to]'>View</a></td></tr>"
codex_data += "</table>"

var/datum/browser/popup = new(presenting_to, "codex-search", "Codex Search") //"codex-search"
popup.set_content(codex_data.Join())
popup.open()

#undef CODEX_ENTRY_LIMIT
/datum/controller/subsystem/codex/proc/get_guide(category)
var/datum/codex_category/cat = codex_categories[category]
. = cat?.guide_html
Expand All @@ -91,25 +122,32 @@ SUBSYSTEM_DEF(codex)
return list()

searching = codex_sanitize(searching)

if(!searching)
return list()
if(!search_cache[searching])
var/list/results
if(entries_by_string[searching])
results = list(entries_by_string[searching])
else
results = list()
for(var/entry_title in entries_by_string)
var/datum/codex_entry/entry = entries_by_string[entry_title]
if(findtext(entry.name, searching) || findtext(entry.lore_text, searching) || findtext(entry.mechanics_text, searching) || findtext(entry.antag_text, searching))
results |= entry
search_cache[searching] = sortTim(results, GLOBAL_PROC_REF(cmp_name_asc))
return search_cache[searching]

. = search_cache[searching]
if(.)
return .

var/list/results = list()
if(entries_by_string[searching])
results = entries_by_string[searching]
else
for(var/datum/codex_entry/entry as anything in all_entries)
if(findtext(entry.name, searching))
results.Insert(1, entry) // If it's in the name, put it at the front of the list.

else if(findtext(entry.lore_text, searching) || findtext(entry.mechanics_text, searching) || findtext(entry.antag_text, searching))
results += entry

search_cache[searching] = sortTim(results, GLOBAL_PROC_REF(cmp_name_asc))
. = search_cache[searching]

/datum/controller/subsystem/codex/Topic(href, href_list)
. = ..()
if(!. && href_list["show_examined_info"] && href_list["show_to"])
var/mob/showing_mob = locate(href_list["show_to"])
var/mob/showing_mob = locate(href_list["show_to"])
if(!istype(showing_mob))
return
var/atom/showing_atom = locate(href_list["show_examined_info"])
Expand Down
91 changes: 47 additions & 44 deletions code/modules/codex/categories/_codex_category.dm
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,64 @@
/datum/codex_category/proc/Populate()
SHOULD_CALL_PARENT(TRUE)

if(length(items))
var/lore_text = "<div style='text-align:center'>[desc]</div>" + "<hr>"
if(guide_name && guide_html)
lore_text += "This category has <span codexlink='Guide to [capitalize(guide_name || name)]'>an associated guide</span>.<hr>"
if(!length(items))
return

items = sortTim(items, GLOBAL_PROC_REF(cmp_text_asc), TRUE)
var/list/links = list()
for(var/item as anything in items)
var/datum/codex_entry/item_entry = SScodex.get_entry_by_string(item)
if(!item_entry)
stack_trace("Invalid item supplied to codex category Populate(): [item]")
continue
var/starter = uppertext(copytext(strip_improper(item_entry.name), 1, 2))
LAZYADD(links[starter], "<l>[item_entry.name]</l>")
LAZYDISTINCTADD(item_entry.categories, src)
var/lore_text = "<div style='text-align:center'>[desc]</div>" + "<hr>"
if(guide_name && guide_html)
lore_text += "This category has <span codexlink='Guide to [capitalize(guide_name || name)]'>an associated guide</span>.<hr>"

var/list/link_cells = list()
for(var/letter in GLOB.alphabet_upper)
if(length(links[letter]))
link_cells += "<td style='width:100%'><b><center>[letter]</center></b>\n<hr>\n<br>\n[jointext(links[letter], "\n<br>\n")]</td>\n"
items = sortTim(items, GLOBAL_PROC_REF(cmp_codex_name))

var/list/link_table = list("<table style='width:100%'>")
var/link_counter = 0
for(var/i = 1 to length(link_cells))
if(link_counter == 0)
link_table += "<tr style='width:100%'>"
link_table += link_cells[i]
if(link_counter == link_columns)
link_table += "</tr>"
link_counter++
if(link_counter > link_columns)
link_counter = 0
var/list/links = list()
for(var/datum/codex_entry/item_entry as anything in items)
if(!istype(item_entry))
stack_trace("Invalid item supplied to codex category Populate(): [item_entry]")
continue

if(link_counter != link_columns)
var/starter = uppertext(copytext(strip_improper(item_entry.name), 1, 2))
LAZYADD(links[starter], "<l>[item_entry.name]</l>")
LAZYDISTINCTADD(item_entry.categories, src)

var/list/link_cells = list()
for(var/letter in GLOB.alphabet_upper)
if(length(links[letter]))
link_cells += "<td style='width:100%'><b><center>[letter]</center></b>\n<hr>\n<br>\n[jointext(links[letter], "\n<br>\n")]</td>\n"

var/list/link_table = list("<table style='width:100%'>")
var/link_counter = 0
for(var/i = 1 to length(link_cells))
if(link_counter == 0)
link_table += "<tr style='width:100%'>"
link_table += link_cells[i]
if(link_counter == link_columns)
link_table += "</tr>"
link_table += "</table>"
link_counter++
if(link_counter > link_columns)
link_counter = 0

lore_text += jointext(link_table, "\n")
var/datum/codex_entry/entry = new(
_display_name = "[name] (category)",
_lore_text = lore_text
)
// Categorize the categories.
var/datum/codex_category/categories/cats_cat = SScodex.codex_categories[/datum/codex_category/categories]
LAZYDISTINCTADD(entry.categories, cats_cat)
LAZYDISTINCTADD(cats_cat.items, entry.name)
if(link_counter != link_columns)
link_table += "</tr>"
link_table += "</table>"

lore_text += jointext(link_table, "\n")
var/datum/codex_entry/entry = new(
_display_name = "[name] (category)",
_lore_text = lore_text
)
// Categorize the categories.
var/datum/codex_category/categories/cats_cat = SScodex.codex_categories[/datum/codex_category/categories]
LAZYDISTINCTADD(entry.categories, cats_cat)
LAZYDISTINCTADD(cats_cat.items, entry)

if(guide_html)
var/datum/codex_entry/entry = new(
var/datum/codex_entry/guide_entry = new(
_display_name = "Guide to [capitalize(guide_name || name)]",
_mechanics_text = guide_html,
_disambiguator = "guide"
)
LAZYDISTINCTADD(entry.categories, src)
LAZYDISTINCTADD(guide_entry.categories, src)
// It's a guide so we track it.
var/datum/codex_category/guides/guides_cat = SScodex.codex_categories[/datum/codex_category/guides]
LAZYDISTINCTADD(entry.categories, guides_cat)
LAZYDISTINCTADD(guides_cat.items, entry.name)
LAZYDISTINCTADD(guide_entry.categories, guides_cat)
LAZYDISTINCTADD(guides_cat.items, guide_entry)
2 changes: 1 addition & 1 deletion code/modules/codex/categories/gases.dm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
_mechanics_text = jointext(material_info, null)
)

items |= entry.name
items += entry

return ..()
54 changes: 51 additions & 3 deletions code/modules/codex/categories/reagents.dm
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
for(var/datum/reagent/reactant as anything in reaction.required_reagents)
reactant_values += "[reaction.required_reagents[reactant]]u [lowertext(initial(reactant.name))]"


var/list/catalysts = list()

for(var/datum/reagent/catalyst as anything in reaction.required_catalysts)
catalysts += "[reaction.required_catalysts[catalyst]]u [lowertext(initial(catalyst.name))]"

var/datum/reagent/unstable_min = null
var/datum/reagent/unstable_max = null

for(var/datum/reagent/chemical as anything in reaction.required_catalysts + reaction.required_reagents + reaction.results)
if(chemical.unstable_temperature)
if(!chemical.unstable_cold && (!unstable_min || unstable_min.unstable_temperature > chemical.unstable_temperature))
unstable_min = chemical
if(chemical.unstable_cold && (!unstable_max || unstable_max.unstable_temperature < chemical.unstable_temperature))
unstable_max = chemical

if(length(catalysts))
production_strings += "- [jointext(reactant_values, " + ")] (catalysts: [jointext(catalysts, ", ")]): [reaction.results[R]]u [lowertext(initial(R.name))]"
Expand All @@ -38,18 +46,58 @@

production_strings += "- Optimal temperature: [KELVIN_TO_CELSIUS(reaction.optimal_temp)]C ([reaction.optimal_temp]K)"

if (reaction.overheat_temp < NO_OVERHEAT)
if (unstable_min && unstable_min.unstable_temperature < reaction.overheat_temp)
production_strings += "- Overheat temperature: [KELVIN_TO_CELSIUS(unstable_min.unstable_temperature)]C ([unstable_min.unstable_temperature]K)"
else if (reaction.overheat_temp < NO_OVERHEAT)
production_strings += "- Overheat temperature: [KELVIN_TO_CELSIUS(reaction.overheat_temp)]C ([reaction.overheat_temp]K)"

if (unstable_max)
production_strings += "- Overcooled temperature: [KELVIN_TO_CELSIUS(unstable_max.unstable_temperature)]C ([unstable_max.unstable_temperature]K)"

if (reaction.required_temp > 0)
production_strings += "- Required temperature: [KELVIN_TO_CELSIUS(reaction.required_temp)]C ([reaction.required_temp]K)"


if(reaction.thermic_constant != 0)
// lifted outta modules/reagents/chemistry/holder.dm
var/thermic_string = ""
var/thermic = reaction.thermic_constant
if(reaction.reaction_flags & REACTION_HEAT_ARBITARY)
thermic *= 100 //Because arbitary is a lower scale
switch(thermic)
if(-INFINITY to -1500)
thermic_string = "overwhelmingly endothermic"
if(-1500 to -1000)
thermic_string = "extremely endothermic"
if(-1000 to -500)
thermic_string = "strongly endothermic"
if(-500 to -200)
thermic_string = "moderately endothermic"
if(-200 to -50)
thermic_string = "endothermic"
if(-50 to 0)
thermic_string = "weakly endothermic"
if(0 to 50)
thermic_string = "weakly exothermic"
if(50 to 200)
thermic_string = "exothermic"
if(200 to 500)
thermic_string = "moderately exothermic"
if(500 to 1000)
thermic_string = "strongly exothermic"
if(1000 to 1500)
thermic_string = "extremely exothermic"
if(1500 to INFINITY)
thermic_string = "overwhelmingly exothermic"
production_strings += "- The reaction is [thermic_string]"

if(length(production_strings))
if(!entry.mechanics_text)
entry.mechanics_text = "It can be produced as follows:<br>"
else
entry.mechanics_text += "<br><br>It can be produced as follows:<br>"
entry.mechanics_text += jointext(production_strings, "<br>")

items += entry.name
items += entry

return ..()
2 changes: 1 addition & 1 deletion code/modules/codex/categories/surgery.dm
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@
_mechanics_text = jointext(info, null)
)

items |= entry.name
items += entry

return ..()
2 changes: 1 addition & 1 deletion code/modules/codex/categories/uncategorized.dm
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
for(var/datum/codex_entry/entry as anything in SScodex.all_entries)
if(!length(entry.categories))
LAZYADD(entry.categories, src)
items |= entry.name
items += entry
return ..()
41 changes: 12 additions & 29 deletions code/modules/codex/codex_client.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/client
var/codex_cooldown = FALSE
var/const/max_codex_entries_shown = 10

/client/verb/search_codex(searching as text)

Expand All @@ -22,36 +21,20 @@

codex_cooldown = world.time + 1 SECONDS

var/list/all_entries = SScodex.retrieve_entries_for_string(searching)
var/list/found_entries = SScodex.retrieve_entries_for_string(searching)
if(mob && mob.mind && !length(mob.mind.antag_datums))
all_entries = all_entries.Copy() // So we aren't messing with the codex search cache.
for(var/datum/codex_entry/entry in all_entries)
found_entries = found_entries.Copy() // So we aren't messing with the codex search cache.
for(var/datum/codex_entry/entry in found_entries)
if(entry.antag_text && !entry.mechanics_text && !entry.lore_text)
all_entries -= entry

//Put entries with match in the name first
for(var/datum/codex_entry/entry in all_entries)
if(findtext(entry.name, searching))
all_entries -= entry
all_entries.Insert(1, entry)

if(LAZYLEN(all_entries) == 1)
SScodex.present_codex_entry(mob, all_entries[1])
else
if(LAZYLEN(all_entries) > 1)
var/list/codex_data = list("<h3><b>[all_entries.len] matches</b> for '[searching]':</h3>")
if(LAZYLEN(all_entries) > max_codex_entries_shown)
codex_data += "Showing first <b>[max_codex_entries_shown]</b> entries. <b>[all_entries.len - 5] result\s</b> omitted.</br>"
codex_data += "<table width = 100%>"
for(var/i = 1 to min(all_entries.len, max_codex_entries_shown))
var/datum/codex_entry/entry = all_entries[i]
codex_data += "<tr><td>[entry.name]</td><td><a href='?src=\ref[SScodex];show_examined_info=\ref[entry];show_to=\ref[mob]'>View</a></td></tr>"
codex_data += "</table>"
var/datum/browser/popup = new(mob, "codex-search", "Codex Search") //"codex-search"
popup.set_content(codex_data.Join())
popup.open()
else
found_entries -= entry

switch(LAZYLEN(found_entries))
if(null)
to_chat(src, span_alert("The codex reports <b>no matches</b> for '[searching]'."))
if(1)
SScodex.present_codex_entry(mob, found_entries[1])
else
SScodex.present_codex_search(mob, found_entries, searching)

/client/verb/list_codex_entries()

Expand All @@ -69,7 +52,7 @@
codex_cooldown = world.time + 1 SECONDS

var/datum/browser/popup = new(mob, "codex", "Codex Index") //"codex-index"
var/datum/codex_entry/nexus = SScodex.get_entry_by_string("nexus")
var/datum/codex_entry/nexus = SScodex.get_codex_entry(/datum/codex_entry/nexus)
var/list/codex_data = list(nexus.get_codex_header(mob).Join(), "<h2>Codex Entries</h2>")
codex_data += "<table width = 100%>"

Expand Down
Loading
Loading