naev 0.10.4
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
77 /* Get the pilot. */
78 p = pilot_get( pilot );
80
81 /* Make sure pilot exists. */
82 if (p == NULL)
83 return -1;
84
85 /* Make sure pilot in range. */
86 if (!pilot_isFlag(p, PILOT_HAILING) &&
87 pilot_inRangePilot( player.p, p, NULL ) <= 0) {
88 player_message(_("#rTarget is out of communications range"));
89 return -1;
90 }
91
92 /* Must not be jumping. */
93 if (pilot_isFlag(p, PILOT_HYPERSPACE)) {
94 player_message(_("#%c%s#r is jumping and can't respond"), c, p->name);
95 return 0;
96 }
97
98 /* Must not be disabled. */
99 if (pilot_isFlag(p, PILOT_DISABLED)) {
100 player_message(_("#%c%s#r does not respond"), c, p->name);
101 return 0;
102 }
103
104 /* Check for player faction (escorts). */
105 if (p->faction == FACTION_PLAYER) {
107 return 0;
108 }
109
110 /* Set up for the comm_get* functions. */
111 ai_setPilot( p );
112
113 /* Have pilot stop hailing. */
114 pilot_rmFlag( p, PILOT_HAILING );
115
116 /* Don't close automatically. */
117 comm_commClose = 0;
118
119 /* Run specific hail hooks on hailing pilot. */
120 HookParam hparam[] = {
121 { .type = HOOK_PARAM_PILOT,
122 .u = { .lp = p->id } },
123 { .type = HOOK_PARAM_SENTINEL } };
124 if (pilot_canTarget( p )) {
125 hooks_runParam( "hail", hparam );
126 pilot_runHook( p, PILOT_HOOK_HAIL );
127 }
128
129 /* Check to see if pilot wants to communicate. */
130 msg = comm_getString( p, "comm_no" );
131 if (msg != NULL) {
132 if (comm_commClose==0)
133 player_messageRaw( msg );
134 return 0;
135 }
136
137 /* Run generic hail hooks on all pilots. */
138 pltstk = pilot_getAll();
139 for (int i=0; i<array_size(pltstk); i++)
140 ai_hail( pltstk[i] );
141
142 /* Close window if necessary. */
143 if (comm_commClose) {
144 p = NULL;
145 comm_spob = NULL;
146 comm_commClose = 0;
147 return 0;
148 }
149
150 /* Set up environment first time. */
151 if (comm_env == LUA_NOREF) {
152 comm_env = nlua_newEnv();
154
155 size_t bufsize;
156 char *buf = ndata_read( COMM_PATH, &bufsize );
157 if (nlua_dobufenv(comm_env, buf, bufsize, COMM_PATH) != 0) {
158 WARN( _("Error loading file: %s\n"
159 "%s\n"
160 "Most likely Lua file has improper syntax, please check"),
161 COMM_PATH, lua_tostring(naevL,-1));
162 free(buf);
163 return -1;
164 }
165 free(buf);
166 }
167
168 comm_open = 1;
169
170 /* Run Lua. */
171 nlua_getenv(naevL, comm_env,"comm");
172 lua_pushpilot(naevL, p->id);
173 if (nlua_pcall(comm_env, 1, 0)) { /* error has occurred */
174 WARN( _("Comm: '%s'"), lua_tostring(naevL,-1));
175 lua_pop(naevL,1);
176 }
177
178 comm_open = 0;
179
180 return 0;
181}
182
189int comm_openSpob( Spob *spob )
190{
191 /* Must not be disabled. */
192 if (!spob_hasService(spob, SPOB_SERVICE_INHABITED)) {
193 player_message(_("%s does not respond."), spob_name(spob));
194 return 0;
195 }
196
197 /* Lua stuff. */
198 if (spob->lua_comm != LUA_NOREF) {
199 comm_open = 1;
200 spob_luaInitMem( spob );
201 lua_rawgeti(naevL, LUA_REGISTRYINDEX, spob->lua_comm); /* f */
202 if (nlua_pcall( spob->lua_env, 0, 0 )) {
203 WARN(_("Spob '%s' failed to run '%s':\n%s"), spob->name, "comm", lua_tostring(naevL,-1));
204 lua_pop(naevL,1);
205 }
206 comm_open = 0;
207 return 0;
208 }
209
210 player_message(_("%s does not respond."), spob_name(spob));
211 return 0;
212}
213
227static const char* comm_getString( const Pilot *p, const char *str )
228{
229 const char *ret;
230
231 if (p->ai == NULL)
232 return NULL;
233
234 /* Get memory table. */
235 nlua_getenv(naevL, p->ai->env, "mem");
236
237 /* Get str message. */
238 lua_getfield(naevL, -1, str );
239 if (!lua_isstring(naevL, -1))
240 ret = NULL;
241 else
242 ret = lua_tostring(naevL, -1);
243 lua_pop(naevL, 2);
244
245 return ret;
246}
void ai_hail(Pilot *recipient)
Triggers the hail() function in the pilot's AI.
Definition: ai.c:906
void ai_setPilot(Pilot *p)
Sets the pilot for further AI calls.
Definition: ai.c:405
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:227
int comm_openSpob(Spob *spob)
Opens a communication dialogue with a spob.
Definition: comm.c:189
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(Pilot *e)
Open a dialog for the player to issue a command to an escort.
Definition: escort.c:357
void player_messageRaw(const char *str)
Adds a mesg to the queue to be displayed on screen.
Definition: gui.c:293
void player_message(const char *fmt,...)
Adds a mesg to the queue to be displayed on screen.
Definition: gui.c:330
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
Definition: hook.c:967
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:760
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
Definition: nlua_pilot.c:495
char pilot_getFactionColourChar(const Pilot *p)
Gets the faction colour char, works like faction_getColourChar but for a pilot.
Definition: pilot.c:1125
Pilot * pilot_get(unsigned int id)
Pulls a pilot out of the pilot_stack based on ID.
Definition: pilot.c:589
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition: pilot.c:83
int pilot_canTarget(const Pilot *p)
Same as pilot_validTarget but without the range check.
Definition: pilot.c:243
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:242
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:73
static const double c[]
Definition: rng.c:264
const char * spob_name(const Spob *p)
Gets the translated name of a spob.
Definition: space.c:1705
void spob_luaInitMem(const Spob *spob)
Initializes the memory fo a spob.
Definition: space.c:1931
The actual hook parameter.
Definition: hook.h:35
HookParamType type
Definition: hook.h:36
The representation of an in-game pilot.
Definition: pilot.h:210
Pilot * p
Definition: player.h:101
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition: space.h:88
int lua_comm
Definition: space.h:143
nlua_env lua_env
Definition: space.h:134
char * name
Definition: space.h:90