naev 0.11.5
nlua_camera.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_camera.h"
16
17#include "camera.h"
18#include "conf.h"
19#include "log.h"
20#include "nlua_pilot.h"
21#include "nlua_vec2.h"
22#include "nluadef.h"
23#include "player.h"
24#include "spfx.h"
25
26/* Camera methods. */
27static int camL_set( lua_State *L );
28static int camL_pos( lua_State *L );
29static int camL_get( lua_State *L );
30static int camL_setZoom( lua_State *L );
31static int camL_getZoom( lua_State *L );
32static int camL_shake( lua_State *L );
33static const luaL_Reg cameraL_methods[] = {
34 { "get", camL_get },
35 { "set", camL_set },
36 { "pos", camL_pos },
37 { "setZoom", camL_setZoom },
38 { "getZoom", camL_getZoom },
39 { "shake", camL_shake },
40 {0,0}
41};
49int nlua_loadCamera( nlua_env env )
50{
51 nlua_register(env, "camera", cameraL_methods, 0);
52 return 0;
53}
54
79static int camL_set( lua_State *L )
80{
81 Pilot *p;
82 vec2 *vec;
83 double x, y, d;
84 int hard_over, speed;
85
86 /* Handle arguments. */
87 p = NULL;
88 vec = NULL;
89 if (lua_ispilot(L,1)) {
90 LuaPilot lp = lua_topilot(L,1);
91 p = pilot_get( lp );
92 if (p==NULL)
93 return 0;
94 vec = &p->solid.pos;
95 }
96 else if (lua_isvector(L,1))
97 vec = lua_tovector(L,1);
98 else if (lua_isnoneornil(L,1)) {
99 if (player.p != NULL) {
100 p = player.p;
101 vec = &player.p->solid.pos;
102 }
103 }
104 else
105 NLUA_INVALID_PARAMETER(L,1);
106 hard_over = !lua_toboolean(L,2);
107 cam_getPos( &x, &y );
108 if (vec != NULL)
109 d = MOD( vec->x-x, vec->y-y );
110 else
111 d = 5000.;
112 speed = luaL_optinteger(L,3,MIN(2000.,d));
113
114 /* Set the camera. */
115 if (p != NULL)
116 cam_setTargetPilot( p->id, hard_over*speed );
117 else if (vec != NULL)
118 cam_setTargetPos( vec->x, vec->y, hard_over*speed );
119 return 0;
120}
121
130static int camL_get( lua_State *L )
131{
132 double x, y;
133 cam_getPos( &x, &y );
134 lua_pushnumber( L, x );
135 lua_pushnumber( L, y );
136 lua_pushnumber( L, 1.0/cam_getZoom() );
137 return 3;
138}
139
146static int camL_pos( lua_State *L )
147{
148 vec2 v;
149 double x, y;
150 cam_getPos( &x, &y );
151 vec2_cset( &v, x, y );
152 lua_pushvector( L, v );
153 return 1;
154}
155
168static int camL_setZoom( lua_State *L )
169{
170 double zoom = luaL_optnumber(L,1,-1.);
171 int hard_over = lua_toboolean(L,2);
172 double speed = luaL_optnumber(L,3,conf.zoom_speed);
173
174 /* Handle arguments. */
175 if (zoom > 0.) {
176 zoom = 1.0 / zoom;
177 cam_zoomOverride( 1 );
178 if (hard_over) {
179 cam_setZoom( zoom );
180 cam_setZoomTarget( zoom, speed );
181 }
182 else
183 cam_setZoomTarget( zoom, speed );
184 }
185 else {
186 cam_zoomOverride( 0 );
187 cam_setZoomTarget( 1., speed );
188 }
189 return 0;
190}
191
200static int camL_getZoom( lua_State *L )
201{
202 lua_pushnumber( L, 1.0/cam_getZoom() );
203 lua_pushnumber( L, 1.0/conf.zoom_far );
204 lua_pushnumber( L, 1.0/conf.zoom_near );
205 return 3;
206}
207
217static int camL_shake( lua_State *L )
218{
219 double amplitude = luaL_optnumber(L,1,1.);
220 spfx_shake( amplitude );
221 return 0;
222}
void cam_setZoom(double zoom)
Sets the camera zoom.
Definition camera.c:73
void cam_zoomOverride(int enable)
Overrides the zoom system.
Definition camera.c:61
void cam_setTargetPilot(unsigned int follow, int soft_over)
Sets the target to follow.
Definition camera.c:145
void cam_setTargetPos(double x, double y, int soft_over)
Sets the camera target to a position.
Definition camera.c:182
void cam_getPos(double *x, double *y)
Gets the camera position.
Definition camera.c:118
double cam_getZoom(void)
Gets the camera zoom.
Definition camera.c:97
void cam_setZoomTarget(double zoom, double speed)
Sets the camera zoom target.
Definition camera.c:86
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:40
static int camL_get(lua_State *L)
Gets the x/y position and zoom of the camera.
static int camL_shake(lua_State *L)
Makes the camera shake.
static int camL_pos(lua_State *L)
Gets the camera position.
static int camL_getZoom(lua_State *L)
Gets the camera zoom.
static const luaL_Reg cameraL_methods[]
Definition nlua_camera.c:33
int nlua_loadCamera(nlua_env env)
Loads the camera library.
Definition nlua_camera.c:49
static int camL_setZoom(lua_State *L)
Sets the camera zoom.
static int camL_set(lua_State *L)
Lua bindings to interact with the Camera.
Definition nlua_camera.c:79
LuaPilot lua_topilot(lua_State *L, int ind)
Lua bindings to interact with pilots.
Definition nlua_pilot.c:522
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
Definition nlua_pilot.c:578
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
Definition nlua_vec2.c:161
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
Definition nlua_vec2.c:119
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition nlua_vec2.c:145
Pilot * pilot_get(unsigned int id)
Pulls a pilot out of the pilot_stack based on ID.
Definition pilot.c:620
Player_t player
Definition player.c:74
static const double d[]
Definition rng.c:273
void spfx_shake(double mod)
Increases the current rumble level.
Definition spfx.c:898
The representation of an in-game pilot.
Definition pilot.h:217
Solid solid
Definition pilot.h:227
double zoom_speed
Definition conf.h:136
double zoom_far
Definition conf.h:134
double zoom_near
Definition conf.h:135
Pilot * p
Definition player.h:101
vec2 pos
Definition physics.h:49
Represents a 2d vector.
Definition vec2.h:32
double y
Definition vec2.h:34
double x
Definition vec2.h:33