Skip to content

Commit

Permalink
feature: Made FactionData datum into a SmartObject. Its paths can be …
Browse files Browse the repository at this point in the history
…initialized by the Faction AI for convenience. First Faction JSON spec. Diplomancy.
  • Loading branch information
jmalek committed Apr 22, 2024
1 parent 40c196d commit f7fe8c5
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 10 deletions.
Binary file modified GOAI/GOAI.dmb
Binary file not shown.
2 changes: 2 additions & 0 deletions GOAI/GOAI.dme
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// BEGIN_INCLUDE
#include "__goai_library_features.dm"
#include "__spaceman_dmm.dm"
#include "_a_debug_mode.dm"
#include "_b_debug_modules.dm"
#include "_defines.dm"
#include "_paths.dm"
Expand Down Expand Up @@ -120,6 +121,7 @@
#include "integrations\utility_agent\actions\idle.dm"
#include "integrations\utility_agent\actions\request_plan.dm"
#include "integrations\utility_agent\actions\wrapper.dm"
#include "integrations\utility_agent\actions\faction\improve_relations.dm"
#include "integrations\utility_agent\actions\faction\mobspawn.dm"
#include "integrations\utility_agent\actions\mob\coverleap.dm"
#include "integrations\utility_agent\actions\mob\flee.dm"
Expand Down
34 changes: 33 additions & 1 deletion GOAI/_datastructures/faction_data.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
// Faction Registry index
var/registry_index

// Associated Actions
var/list/actionset_files = null


/datum/faction_data/proc/RegisterFaction()
// Registry pattern, to facilitate querying all Factions
Expand Down Expand Up @@ -51,7 +54,7 @@
return mytags


/datum/faction_data/New(var/newname, var/datum/relationships/init_relations = null, var/list/init_tags = null)
/datum/faction_data/New(var/newname, var/datum/relationships/init_relations = null, var/list/init_tags = null, var/list/actionsets = null)
. = ..()

if(newname)
Expand All @@ -63,6 +66,9 @@
if(init_tags)
src.tags = init_tags

if(actionsets)
src.actionset_files = actionsets

// Populate data structures (do not override existing)
src.relations = (src.relations || src.BuildRelations())
src.tags = (src.tags || src.BuildTags())
Expand All @@ -71,3 +77,29 @@
src.RegisterFaction()

return


/* SmartObject */
GOAI_HAS_UTILITY_ACTIONS_BOILERPLATE_VARLIST(/datum/faction_data, actionset_files)

/datum/faction_data/GetUtilityActions(var/requester, var/list/args = null)
var/list/my_action_sets = list()

if(!src.actionset_files)
return my_action_sets

for(var/action_bundle_json_fp in src.actionset_files)
if(!fexists(action_bundle_json_fp))
GOAI_LOG_ERROR("[src.name] - Filepath [action_bundle_json_fp] does not exist - skipping!")
continue

try
var/datum/action_set/myset = ActionSetFromJsonFile(action_bundle_json_fp)
myset.origin = src
my_action_sets.Add(myset)

catch(var/exception/e)
world.Error(e)

return my_action_sets

2 changes: 2 additions & 0 deletions GOAI/_paths.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#define GOAI_DATA_PATH(FP) "goai_data/" + ##FP
#define GOAI_SMARTOBJECT_PATH(FP) "goai_data/smartobject_definitions/" + ##FP
#define GOAI_FACTIONSPEC_PATH(FP) "goai_data/faction_definitions/" + ##FP

#endif

Expand All @@ -12,6 +13,7 @@
// SS13
#define GOAI_DATA_PATH(FP) "code/modules/urist/GOAI/goai_data/" + ##FP
#define GOAI_SMARTOBJECT_PATH(FP) "code/modules/urist/GOAI/goai_data/smartobject_definitions/" + ##FP
#define GOAI_FACTIONSPEC_PATH(FP) "code/modules/urist/GOAI/goai_data/faction_definitions/" + ##FP

#endif

Expand Down
2 changes: 1 addition & 1 deletion GOAI/basics/relationships.dm
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@


/datum/relationships/proc/GetRelationshipByTags(var/list/tags)
if(!(data && tags && tags.len))
if(!(data && tags))
return

var/total_val = 0
Expand Down
1 change: 1 addition & 0 deletions GOAI/dev_sense.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "sofetcher",
"sense_index_key": "sofetcher",
"enabled": 1,
"in_memory_key": "SightCurr",
Expand Down
10 changes: 10 additions & 0 deletions GOAI/goai_data/faction_definitions/debug_faction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Debug Faction",
"tags": [
"debug"
],
"relationships": {},
"actionset_files": [
"faction_base.json"
]
}
6 changes: 3 additions & 3 deletions GOAI/goai_data/smartobject_definitions/faction_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"active": 1,
"actions": [
{
"name": "Print Faction",
"name": "Befriend Faction",
"description": "Does literally nothing. Fallback behavior.",
"handler": "/datum/utility_ai/proc/PrintArg",
"handler": "/datum/utility_ai/proc/ImproveRelations",
"priority": 1,
"context_procs": ["/proc/ctxfetcher_all_faction_ais"],
"context_args": [{
"output_context_key": "printarg"
"output_context_key": "buddyfriendo"
}],
"considerations": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
// Diplomancy - make a target faction friendlier to us. Somehow.
*/

/datum/utility_ai/proc/ImproveRelations(var/datum/ActionTracker/tracker, var/datum/utility_ai/buddyfriendo)
if(isnull(tracker))
RUN_ACTION_DEBUG_LOG("Tracker position is null | <@[src]> | [__FILE__] -> L[__LINE__]")
return

if(isnull(buddyfriendo))
RUN_ACTION_DEBUG_LOG("buddyfriendo is null | <@[src]> | [__FILE__] -> L[__LINE__]")
return

if(tracker.IsStopped())
return

if(isnull(buddyfriendo.brain))
RUN_ACTION_DEBUG_LOG("Target brain is null | <@[src]> | [__FILE__] -> L[__LINE__]")
tracker.SetFailed()
return

if(isnull(src.brain.relations))
src.brain.relations = new()

if(isnull(buddyfriendo.brain.relations))
buddyfriendo.brain.relations = new()

if(prob(75))
var/their_tag = src.name
var/their_amount = (rand(2, 10) / 2)

var/our_tag = buddyfriendo.name
var/our_amount = their_amount + (rand(-2, 2) / 4)

buddyfriendo.brain.relations.Increase(their_tag, their_amount)
src.brain.relations.Increase(our_tag, our_amount)
tracker.SetDone()
return
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
var/spec_file = (filespec || src.factionspec_source)

var/list/factionspec = null // assoc list
if(src.factionspec_source)
if(spec_file)
factionspec = READ_JSON_FILE(spec_file)
to_world_log("Read factionspec [spec_file] for [src] ([json_encode(factionspec)])")

var/faction_name = null
if(factionspec)
Expand All @@ -65,7 +66,32 @@
if(factionspec)
faction_rels = factionspec["relationships"]

var/datum/faction_data/new_faction = new(faction_name, faction_rels, faction_tags)
var/list/actionspecs = null

if(factionspec)
var/list/raw_actionspecs = factionspec["actionset_files"]

if(raw_actionspecs)
actionspecs = list()

for(var/raw_spec_path in raw_actionspecs)
// If the spec does not have the canonical SO location, assume it's a relative path in the SO dir.
var/basepath_idx = findtext(raw_spec_path, GOAI_SMARTOBJECT_PATH(""))
var/abs_spec_path = ((basepath_idx != 1) ? GOAI_SMARTOBJECT_PATH(raw_spec_path) : raw_spec_path)
actionspecs.Add(abs_spec_path)

if(spec_file)
// If the spec does not have the canonical SO location, assume it's a relative path in the SO dir.
var/basepath_idx = findtext(spec_file, GOAI_SMARTOBJECT_PATH(""))

var/abs_spec_path = spec_file
if(!basepath_idx)
abs_spec_path = GOAI_DATA_PATH(spec_file)

factionspec = READ_JSON_FILE(abs_spec_path)
to_world_log("Read factionspec [spec_file] for [src] ([json_encode(factionspec)])")

var/datum/faction_data/new_faction = new(faction_name, faction_rels, faction_tags, actionspecs)

// Note: the pawn may be a weakref, so
// if for whatever reason the faction gets de-registered from a global list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/datum/utility_ai/faction_commander/debug
name = "Debug Faction"

innate_actions_filepaths = list(
factionspec_source = GOAI_FACTIONSPEC_PATH("debug_faction.json")

/*innate_actions_filepaths = list(
GOAI_SMARTOBJECT_PATH("faction_base.json"),
)
)*/
10 changes: 9 additions & 1 deletion GOAI/utility/serde.dm
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@
if(isnull(action_definition))
continue

var/datum/utility_action_template/new_action_template = ActionTemplateFromData(action_definition)
var/datum/utility_action_template/new_action_template = null

try
new_action_template = ActionTemplateFromData(action_definition)

catch(var/exception/e)
world.Error(e)
new_action_template = null

if(isnull(new_action_template))
continue

Expand Down

0 comments on commit f7fe8c5

Please sign in to comment.