1 #include "commsScriptInterface.h"
2 #include "spaceObjects/cpuShip.h"
3 #include "spaceObjects/playerSpaceship.h"
4 static CommsScriptInterface* comms_script_interface = NULL;
5 
setCommsMessage(lua_State * L)6 static int setCommsMessage(lua_State* L)
7 {
8     if (!comms_script_interface)
9         return 0;
10     comms_script_interface->setCommsMessage(luaL_checkstring(L, 1));
11     return 0;
12 }
13 
addCommsReply(lua_State * L)14 static int addCommsReply(lua_State* L)
15 {
16     if (!comms_script_interface)
17         return 0;
18 
19     ScriptSimpleCallback callback;
20     int idx = 2;
21     convert<ScriptSimpleCallback>::param(L, idx, callback);
22     comms_script_interface->addCommsReply(luaL_checkstring(L, 1), callback);
23     return 0;
24 }
25 
commsSwitchToGM(lua_State * L)26 static int commsSwitchToGM(lua_State* L)
27 {
28     if (!comms_script_interface)
29         return 0;
30 
31     comms_script_interface->switchToGM();
32     return 0;
33 }
34 
35 /// setCommsMessage(message)
36 /// Sets the message/reply shown to the comms officer.
37 /// Not setting the message leads to "no reply" (when trying to open comms)
38 /// or a dialog with the message "?" (in a reply).
39 REGISTER_SCRIPT_FUNCTION(setCommsMessage);
40 /// addCommsReply(message, function)
41 /// Add an reply option for communications.
42 /// Within the callback function, `comms_source` and `comms_target` are available.
43 /// Deprecated: In a CommsScript, `player` can be used for `comms_source`.
44 /// (In a CommsFunction, only `comms_source` is provided.)
45 /// Instead of using the globals, the callback function can take two parameters.
46 /// Example: addCommsReply(message, function(comms_source, comms_target) ... end)
47 REGISTER_SCRIPT_FUNCTION(addCommsReply);
48 /// Use this function from a communication callback function to switch the current
49 /// communication from scripted to a GM based chat.
50 REGISTER_SCRIPT_FUNCTION(commsSwitchToGM);
51 
openCommChannel(P<PlayerSpaceship> ship,P<SpaceObject> target)52 bool CommsScriptInterface::openCommChannel(P<PlayerSpaceship> ship, P<SpaceObject> target)
53 {
54     string script_name = target->comms_script_name;
55     comms_script_interface = this;
56 
57     reply_callbacks.clear();
58 
59     this->ship = ship;
60     this->target = target;
61 
62     if (scriptObject)
63         scriptObject->destroy();
64     scriptObject = nullptr;
65     has_message = false;
66 
67     if (script_name != "")
68     {
69         scriptObject = new ScriptObject();
70         // consider "player" deprecated, but keep it for a long time
71         scriptObject->registerObject(ship, "player");
72         scriptObject->registerObject(ship, "comms_source");
73         scriptObject->registerObject(target, "comms_target");
74         scriptObject->run(script_name);
75     }else if (target->comms_script_callback.isSet())
76     {
77         target->comms_script_callback.getScriptObject()->registerObject(ship, "comms_source");
78         target->comms_script_callback.getScriptObject()->registerObject(target, "comms_target");
79         target->comms_script_callback.call<void>(ship, target);
80     }
81     comms_script_interface = nullptr;
82     return has_message;
83 }
84 
commChannelMessage(int32_t message_id)85 void CommsScriptInterface::commChannelMessage(int32_t message_id)
86 {
87     comms_script_interface = this;
88 
89     if (message_id >= 0 && message_id < int(reply_callbacks.size()) && ship && target)
90     {
91         ScriptSimpleCallback callback = reply_callbacks[message_id];
92         if (!scriptObject)
93         {
94             target->comms_script_callback.getScriptObject()->registerObject(ship, "comms_source");
95             target->comms_script_callback.getScriptObject()->registerObject(target, "comms_target");
96         }
97         reply_callbacks.clear();
98         callback.call<void>(ship, target);
99     }
100 
101     comms_script_interface = nullptr;
102 }
103 
setCommsMessage(string message)104 void CommsScriptInterface::setCommsMessage(string message)
105 {
106     has_message = true;
107     ship->setCommsMessage(message);
108 }
109 
addCommsReply(string message,ScriptSimpleCallback callback)110 void CommsScriptInterface::addCommsReply(string message, ScriptSimpleCallback callback)
111 {
112     comms_script_interface->ship->addCommsReply(reply_callbacks.size(), message);
113     reply_callbacks.push_back(callback);
114 }
115 
switchToGM()116 void CommsScriptInterface::switchToGM()
117 {
118     ship->switchCommsToGM();
119 }
120