Debian Codegraph @

plugins/dm.stimresponse/SRPropertySaver.cpp

raw
1 #include "SRPropertySaver.h"
2
3 #include "iregistry.h"
4 #include "entitylib.h"
5 #include "string/convert.h"
6 #include "gamelib.h"
7 #include <iostream>
8
9 SRPropertySaver::SRPropertySaver(Entity* target, SREntity::KeyList& keys) :
10 _target(target),
11 _keys(keys)
12 {}
13
14 void SRPropertySaver::visit(StimResponse& sr) {
15 std::string prefix = game::current::getValue<std::string>(GKEY_STIM_RESPONSE_PREFIX);
16 std::string suffix = "_" + string::to_string(sr.getIndex());
17
18 // Now cycle through the possible key names and see if we have a match
19 for (unsigned int i = 0; i < _keys.size(); i++) {
20 // Check if the property carries a non-empty value
21 std::string value = sr.get(_keys[i].key);
22
23 // The class of this S/R
24 std::string srClass = sr.get("class");
25
26 // Check if the property matches for the class type
27 if (!value.empty() && _keys[i].classes.find(srClass, 0) != std::string::npos) {
28 std::string fullKey = prefix + _keys[i].key + suffix;
29
30 if (sr.inherited() && sr.isOverridden(_keys[i].key)) {
31 // Save the key
32 _target->setKeyValue(fullKey, value);
33 }
34 else if (!sr.inherited()) {
35 // Save the key, if it hasn't been inherited and not overridden
36 _target->setKeyValue(fullKey, value);
37 }
38 }
39 }
40
41 // If we have a Response, save the response effects to the spawnargs
42 if (sr.get("class") == "R") {
43 std::string responseEffectPrefix =
44 game::current::getValue<std::string>(GKEY_RESPONSE_EFFECT_PREFIX);
45
46 // Re-index the effect map before saving
47 sr.sortEffects();
48
49 // Retrieve the effect map and save them one after the other
50 StimResponse::EffectMap& effects = sr.getEffects();
51
52 for (StimResponse::EffectMap::iterator i = effects.begin();
53 i != effects.end();
54 i++)
55 {
56 ResponseEffect& effect = i->second;
57
58 // Save the effect declaration ("sr_effect_1_1")
59 std::string key = prefix + responseEffectPrefix +
60 string::to_string(sr.getIndex()) + "_" + string::to_string(i->first);
61
62 if (effect.isInherited() && effect.nameIsOverridden()) {
63 // Overridden name, save it
64 _target->setKeyValue(key, effect.getName());
65 }
66 else if (!effect.isInherited()) {
67 // Non-inherited name, save it
68 _target->setKeyValue(key, effect.getName());
69 }
70
71 // Now save the arguments
72 ResponseEffect::ArgumentList& args = effect.getArguments();
73
74 for (ResponseEffect::ArgumentList::iterator a = args.begin();
75 a != args.end(); a++)
76 {
77 int argIndex = a->first;
78 std::string argValue = a->second.value;
79
80 // Construct the argument key ("sr_effect_3_2_arg4")
81 std::string argKey = key + "_arg" + string::to_string(argIndex);
82
83 if (effect.isInherited() && effect.argIsOverridden(argIndex)) {
84 // This is an overridden argument, save it
85 _target->setKeyValue(argKey, argValue);
86 }
87 else if (!effect.isInherited()) {
88 // A non-inherited argument, save it
89 _target->setKeyValue(argKey, argValue);
90 }
91 }
92
93 if (effect.isInherited() && effect.activeIsOverridden()) {
94 // An overridden state, save the spawnarg in every case
95 _target->setKeyValue(key + "_state", effect.isActive() ? "1" : "0");
96 }
97 else if (!effect.isInherited()) {
98 // A non-inherited state, save it only if necessary (inactive)
99 if (!effect.isActive()) {
100 _target->setKeyValue(key + "_state", "0");
101 }
102 }
103 }
104 }
105 }