naev 0.12.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"
14
15#include "nlua_camera.h"
16
17#include "camera.h"
18#include "conf.h"
19#include "nlua_pilot.h"
20#include "nlua_vec2.h"
21#include "nluadef.h"
22#include "player.h"
23#include "spfx.h"
24
25/* Camera methods. */
26static int camL_set( lua_State *L );
27static int camL_pos( lua_State *L );
28static int camL_get( lua_State *L );
29static int camL_setZoom( lua_State *L );
30static int camL_getZoom( lua_State *L );
31static int camL_shake( lua_State *L );
32
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
48int nlua_loadCamera( nlua_env env )
49{
50 nlua_register( env, "camera", cameraL_methods, 0 );
51 return 0;
52}
53
63
81static int camL_set( lua_State *L )
82{
83 Pilot *p;
84 vec2 *vec;
85 double x, y, d;
86 int hard_over, speed;
87
88 /* Handle arguments. */
89 p = NULL;
90 vec = NULL;
91 if ( lua_ispilot( L, 1 ) ) {
92 LuaPilot lp = lua_topilot( L, 1 );
93 p = pilot_get( lp );
94 if ( p == NULL )
95 return 0;
96 vec = &p->solid.pos;
97 } else if ( lua_isvector( L, 1 ) )
98 vec = lua_tovector( L, 1 );
99 else if ( lua_isnoneornil( L, 1 ) ) {
100 if ( player.p != NULL ) {
101 p = player.p;
102 vec = &player.p->solid.pos;
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
171static int camL_setZoom( lua_State *L )
172{
173 double zoom = luaL_optnumber( L, 1, -1. );
174 int hard_over = lua_toboolean( L, 2 );
175 double speed = luaL_optnumber( L, 3, conf.zoom_speed );
176
177 /* Handle arguments. */
178 if ( zoom > 0. ) {
179 zoom = 1.0 / zoom;
180 cam_zoomOverride( 1 );
181 if ( hard_over ) {
182 cam_setZoom( zoom );
183 cam_setZoomTarget( zoom, speed );
184 } else
185 cam_setZoomTarget( zoom, speed );
186 } else {
187 cam_zoomOverride( 0 );
188 cam_setZoomTarget( 1., speed );
189 }
190 return 0;
191}
192
201static int camL_getZoom( lua_State *L )
202{
203 lua_pushnumber( L, 1.0 / cam_getZoom() );
204 lua_pushnumber( L, 1.0 / conf.zoom_far );
205 lua_pushnumber( L, 1.0 / conf.zoom_near );
206 return 3;
207}
208
218static int camL_shake( lua_State *L )
219{
220 double amplitude = luaL_optnumber( L, 1, 1. );
221 spfx_shake( amplitude );
222 return 0;
223}
void cam_setZoom(double zoom)
Sets the camera zoom.
Definition camera.c:77
void cam_zoomOverride(int enable)
Overrides the zoom system.
Definition camera.c:65
void cam_setTargetPilot(unsigned int follow, int soft_over)
Sets the target to follow.
Definition camera.c:158
void cam_setTargetPos(double x, double y, int soft_over)
Sets the camera target to a position.
Definition camera.c:194
void cam_getPos(double *x, double *y)
Gets the camera position.
Definition camera.c:122
double cam_getZoom(void)
Gets the camera zoom.
Definition camera.c:101
void cam_setZoomTarget(double zoom, double speed)
Sets the camera zoom target.
Definition camera.c:90
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:39
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:48
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:81
LuaPilot lua_topilot(lua_State *L, int ind)
Lua bindings to interact with pilots.
Definition nlua_pilot.c:535
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
Definition nlua_pilot.c:591
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:640
Player_t player
Definition player.c:77
static const double d[]
Definition rng.c:263
void spfx_shake(double mod)
Increases the current rumble level.
Definition spfx.c:966
The representation of an in-game pilot.
Definition pilot.h:263
Represents a 2d vector.
Definition vec2.h:45
double y
Definition vec2.h:47
double x
Definition vec2.h:46