naev 0.10.4
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_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 );
32static const luaL_Reg cameraL_methods[] = {
33 { "set", camL_set },
34 { "get", camL_get },
35 { "setZoom", camL_setZoom },
36 { "getZoom", camL_getZoom },
37 { "shake", camL_shake },
38 {0,0}
39};
47int nlua_loadCamera( nlua_env env )
48{
49 nlua_register(env, "camera", cameraL_methods, 0);
50 return 0;
51}
52
77static int camL_set( lua_State *L )
78{
79 Pilot *p;
80 vec2 *vec;
81 double x, y, d;
82 int hard_over, speed;
83
84 /* Handle arguments. */
85 p = NULL;
86 vec = NULL;
87 if (lua_ispilot(L,1)) {
88 LuaPilot lp = lua_topilot(L,1);
89 p = pilot_get( lp );
90 if (p==NULL)
91 return 0;
92 vec = &p->solid->pos;
93 }
94 else if (lua_isvector(L,1))
95 vec = lua_tovector(L,1);
96 else if (lua_isnoneornil(L,1)) {
97 if (player.p != NULL) {
98 p = player.p;
99 vec = &player.p->solid->pos;
100 }
101 }
102 else
103 NLUA_INVALID_PARAMETER(L);
104 hard_over = !lua_toboolean(L,2);
105 cam_getPos( &x, &y );
106 if (vec != NULL)
107 d = MOD( vec->x-x, vec->y-y );
108 else
109 d = 5000.;
110 speed = luaL_optinteger(L,3,MIN(2000.,d));
111
112 /* Set the camera. */
113 if (p != NULL)
114 cam_setTargetPilot( p->id, hard_over*speed );
115 else if (vec != NULL)
116 cam_setTargetPos( vec->x, vec->y, hard_over*speed );
117 return 0;
118}
119
126static int camL_get( lua_State *L )
127{
128 vec2 v;
129 cam_getPos( &v.x, &v.y );
130 lua_pushvector( L, v );
131 return 1;
132}
133
146static int camL_setZoom( lua_State *L )
147{
148 double zoom = luaL_optnumber(L,1,-1.);
149 int hard_over = lua_toboolean(L,2);
150 double speed = luaL_optnumber(L,3,conf.zoom_speed);
151
152 /* Handle arguments. */
153 if (zoom > 0.) {
154 zoom = 1.0 / zoom;
155 cam_zoomOverride( 1 );
156 if (hard_over) {
157 cam_setZoom( zoom );
158 cam_setZoomTarget( zoom, speed );
159 }
160 else
161 cam_setZoomTarget( zoom, speed );
162 }
163 else {
164 cam_zoomOverride( 0 );
165 cam_setZoomTarget( 1., speed );
166 }
167 return 0;
168}
169
178static int camL_getZoom( lua_State *L )
179{
180 lua_pushnumber( L, 1.0/cam_getZoom() );
181 lua_pushnumber( L, 1.0/conf.zoom_far );
182 lua_pushnumber( L, 1.0/conf.zoom_near );
183 return 3;
184}
185
195static int camL_shake( lua_State *L )
196{
197 double amplitude = luaL_optnumber(L,1,1.);
198 spfx_shake( amplitude );
199 return 0;
200}
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 camera position.
Definition: nlua_camera.c:126
static int camL_shake(lua_State *L)
Makes the camera shake.
Definition: nlua_camera.c:195
static int camL_getZoom(lua_State *L)
Gets the camera zoom.
Definition: nlua_camera.c:178
static const luaL_Reg cameraL_methods[]
Definition: nlua_camera.c:32
int nlua_loadCamera(nlua_env env)
Loads the camera library.
Definition: nlua_camera.c:47
static int camL_setZoom(lua_State *L)
Sets the camera zoom.
Definition: nlua_camera.c:146
static int camL_set(lua_State *L)
Lua bindings to interact with the Camera.
Definition: nlua_camera.c:77
LuaPilot lua_topilot(lua_State *L, int ind)
Lua bindings to interact with pilots.
Definition: nlua_pilot.c:453
int lua_ispilot(lua_State *L, int ind)
Checks to see if ind is a pilot.
Definition: nlua_pilot.c:510
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
Definition: nlua_vec2.c:155
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
Definition: nlua_vec2.c:113
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
Pilot * pilot_get(unsigned int id)
Pulls a pilot out of the pilot_stack based on ID.
Definition: pilot.c:589
Player_t player
Definition: player.c:73
static const double d[]
Definition: rng.c:273
void spfx_shake(double mod)
Increases the current rumble level.
Definition: spfx.c:883
The representation of an in-game pilot.
Definition: pilot.h:210
Solid * solid
Definition: pilot.h:220
double zoom_speed
Definition: conf.h:137
double zoom_far
Definition: conf.h:135
double zoom_near
Definition: conf.h:136
Pilot * p
Definition: player.h:101
vec2 pos
Definition: physics.h:22
Represents a 2d vector.
Definition: vec2.h:32
double y
Definition: vec2.h:34
double x
Definition: vec2.h:33