naev 0.11.5
comm.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
11#include "naev.h"
14#include "comm.h"
15
16#include "ai.h"
17#include "array.h"
18#include "commodity.h"
19#include "dialogue.h"
20#include "escort.h"
21#include "hook.h"
22#include "log.h"
23#include "ndata.h"
24#include "nlua.h"
25#include "opengl.h"
26#include "pilot.h"
27#include "player.h"
28#include "rng.h"
29#include "toolkit.h"
30
31#define BUTTON_WIDTH 80
32#define BUTTON_HEIGHT 30
34#define GRAPHIC_WIDTH 256
35#define GRAPHIC_HEIGHT 256
37static Spob *comm_spob = NULL;
38static int comm_commClose = 0;
39static nlua_env comm_env = LUA_NOREF;
40static int comm_open = 0;
41
42/*
43 * Prototypes.
44 */
45/* Static. */
46static const char* comm_getString( const Pilot *p, const char *str );
47
51int comm_isOpen (void)
52{
53 return comm_open;
54}
55
59void comm_queueClose (void)
60{
62}
63
70int comm_openPilot( unsigned int pilot )
71{
72 const char *msg;
73 char c;
74 Pilot *p;
75 Pilot *const* pltstk;
76 AIMemory oldmem;
77
78 /* Get the pilot. */
79 p = pilot_get( pilot );
81
82 /* Make sure pilot exists. */
83 if (p == NULL)
84 return -1;
85
86 /* Make sure pilot in range. */
87 if (!pilot_isFlag(p, PILOT_HAILING) &&
88 pilot_inRangePilot( player.p, p, NULL ) <= 0) {
89 player_message(_("#rTarget is out of communications range"));
90 return -1;
91 }
92
93 /* Must not be jumping. */
94 if (pilot_isFlag(p, PILOT_HYPERSPACE)) {
95 player_message(_("#%c%s#r is jumping and can't respond"), c, p->name);
96 return 0;
97 }
98
99 /* Must not be disabled. */
100 if (pilot_isFlag(p, PILOT_DISABLED)) {
101 player_message(_("#%c%s#r does not respond"), c, p->name);
102 return 0;
103 }
104
105 /* Check for player faction (escorts). */
106 if (p->faction == FACTION_PLAYER) {
108 return 0;
109 }
110
111 /* Set up for the comm_get* functions. */
112 oldmem = ai_setPilot( p );
113
114 /* Have pilot stop hailing. */
115 pilot_rmFlag( p, PILOT_HAILING );
116
117 /* Don't close automatically. */
118 comm_commClose = 0;
119
120 /* Run specific hail hooks on hailing pilot. */
121 if (pilot_canTarget( p )) {
122 HookParam hparam[] = {
123 { .type = HOOK_PARAM_PILOT,
124 .u = { .lp = p->id } },
125 { .type = HOOK_PARAM_SENTINEL } };
126 hooks_runParam( "hail", hparam );
127 pilot_runHook( p, PILOT_HOOK_HAIL );
128 }
129
130 /* Check to see if pilot wants to communicate. */
131 msg = comm_getString( p, "comm_no" );
132 if (msg != NULL) {
133 if (comm_commClose==0)
134 player_messageRaw( msg );
135 ai_unsetPilot( oldmem );
136 return 0;
137 }
138
139 /* Run generic hail hooks on all pilots. */
140 pltstk = pilot_getAll();
141 for (int i=0; i<array_size(pltstk); i++)
142 ai_hail( pltstk[i] );
143
144 /* Close window if necessary. */
145 if (comm_commClose) {
146 comm_spob = NULL;
147 comm_commClose = 0;
148 ai_unsetPilot( oldmem );
149 return 0;
150 }
151
152 /* Set up environment first time. */
153 if (comm_env == LUA_NOREF) {
154 comm_env = nlua_newEnv();
156
157 size_t bufsize;
158 char *buf = ndata_read( COMM_PATH, &bufsize );
159 if (nlua_dobufenv(comm_env, buf, bufsize, COMM_PATH) != 0) {
160 WARN( _("Error loading file: %s\n"
161 "%s\n"
162 "Most likely Lua file has improper syntax, please check"),
163 COMM_PATH, lua_tostring(naevL,-1));
164 free(buf);
165 ai_unsetPilot( oldmem );
166 return -1;
167 }
168 free(buf);
169 }
170
171 comm_open = 1;
172
173 /* Run Lua. */
174 nlua_getenv(naevL, comm_env,"comm");
175 lua_pushpilot(naevL, p->id);
176 if (nlua_pcall(comm_env, 1, 0)) { /* error has occurred */
177 WARN( _("Comm: '%s'"), lua_tostring(naevL,-1));
178 lua_pop(naevL,1);
179 }
180
181 ai_unsetPilot( oldmem );
182 comm_open = 0;
183
184 return 0;
185}
186
193int comm_openSpob( Spob *spob )
194{
195 /* Don't close automatically. */
196 comm_commClose = 0;
197
198 /* Run hail_spob hook. */
199 HookParam hparam[] = {
200 { .type = HOOK_PARAM_SPOB,
201 .u = { .la = spob_index( spob ) } },
202 { .type = HOOK_PARAM_SENTINEL } };
203 hooks_runParam( "hail_spob", hparam );
204
205 /* Close window if necessary. */
206 if (comm_commClose) {
207 comm_spob = NULL;
208 comm_commClose = 0;
209 return 0;
210 }
211
212 /* Lua stuff. */
213 if (spob->lua_comm != LUA_NOREF) {
214 comm_open = 1;
215 spob_luaInitMem( spob );
216 lua_rawgeti(naevL, LUA_REGISTRYINDEX, spob->lua_comm); /* f */
217 if (nlua_pcall( spob->lua_env, 0, 1 )) {
218 WARN(_("Spob '%s' failed to run '%s':\n%s"), spob->name, "comm", lua_tostring(naevL,-1));
219 lua_pop(naevL,1);
220 }
221 else {
222 int commed = lua_toboolean(naevL,-1);
223 lua_pop(naevL,1);
224 if (commed) {
225 comm_open = 0;
226 return 0;
227 }
228 }
229 comm_open = 0;
230 }
231
232 player_message(_("%s does not respond."), spob_name(spob));
233 return 0;
234}
235
249static const char* comm_getString( const Pilot *p, const char *str )
250{
251 const char *ret;
252
253 if (p->ai == NULL)
254 return NULL;
255
256 /* Get memory table. */
257 nlua_getenv(naevL, p->ai->env, "mem");
258
259 /* Get str message. */
260 lua_getfield(naevL, -1, str );
261 if (!lua_isstring(naevL, -1))
262 ret = NULL;
263 else
264 ret = lua_tostring(naevL, -1);
265 lua_pop(naevL, 2);
266
267 return ret;
268}
void ai_unsetPilot(AIMemory oldmem)
Finishes setting up a pilot.
Definition ai.c:426
AIMemory ai_setPilot(Pilot *p)
Sets the pilot for further AI calls.
Definition ai.c:414
void ai_hail(Pilot *recipient)
Triggers the hail() function in the pilot's AI.
Definition ai.c:983
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
int comm_openPilot(unsigned int pilot)
Opens the communication dialogue with a pilot.
Definition comm.c:70
static const char * comm_getString(const Pilot *p, const char *str)
Gets a string from the pilot's memory.
Definition comm.c:249
int comm_openSpob(Spob *spob)
Opens a communication dialogue with a spob.
Definition comm.c:193
static Spob * comm_spob
Definition comm.c:37
int comm_isOpen(void)
Check to see if the comm window is open.
Definition comm.c:51
void comm_queueClose(void)
Queues a close command when possible.
Definition comm.c:59
static int comm_commClose
Definition comm.c:38
static nlua_env comm_env
Definition comm.c:39
int escort_playerCommand(const Pilot *e)
Open a dialog for the player to issue a command to an escort.
Definition escort.c:362
void player_messageRaw(const char *str)
Adds a mesg to the queue to be displayed on screen.
Definition gui.c:298
void player_message(const char *fmt,...)
Adds a mesg to the queue to be displayed on screen.
Definition gui.c:335
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
Definition hook.c:979
Header file with generic functions and naev-specifics.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
Definition ndata.c:154
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition nlua.c:798
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition nlua_pilot.c:563
char pilot_getFactionColourChar(const Pilot *p)
Gets the faction colour char, works like faction_getColourChar but for a pilot.
Definition pilot.c:1080
Pilot * pilot_get(unsigned int id)
Pulls a pilot out of the pilot_stack based on ID.
Definition pilot.c:620
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition pilot.c:94
int pilot_canTarget(const Pilot *p)
Same as pilot_validTarget but without the range check.
Definition pilot.c:254
int pilot_inRangePilot(const Pilot *p, const Pilot *target, double *dist2)
Check to see if a pilot is in sensor range of another.
Definition pilot_ew.c:244
int pilot_runHook(Pilot *p, int hook_type)
Tries to run a pilot hook if he has it.
Definition pilot_hook.c:106
Player_t player
Definition player.c:74
static const double c[]
Definition rng.c:264
int spob_index(const Spob *p)
Gets the ID of a spob.
Definition space.c:1099
const char * spob_name(const Spob *p)
Gets the translated name of a spob.
Definition space.c:1752
void spob_luaInitMem(const Spob *spob)
Initializes the memory fo a spob.
Definition space.c:1986
Represents a temporary pilot memory. For use with ai_setPilot and ai_unsetPilot.
Definition ai.h:58
The actual hook parameter.
Definition hook.h:38
The representation of an in-game pilot.
Definition pilot.h:217
Pilot * p
Definition player.h:101
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:89
int lua_comm
Definition space.h:147
nlua_env lua_env
Definition space.h:138
char * name
Definition space.h:91