[Add] Initial Lua infrastructure.
This commit is contained in:
parent
dd5cbe614b
commit
838bbcd2ab
31
src/ai.c
Normal file
31
src/ai.c
Normal file
@ -0,0 +1,31 @@
|
||||
// Woot, LUA!!!!!!
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#include "def.h"
|
||||
#include "log.h"
|
||||
#include "pilot.h"
|
||||
#include "ai.h"
|
||||
|
||||
static lua_State* L = NULL;
|
||||
|
||||
int ai_init(void) {
|
||||
L = luaL_newstate();
|
||||
if(L == NULL)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ai_exit(void) {
|
||||
lua_close(L);
|
||||
}
|
||||
|
||||
// Heart of hearts of the ai!! Brains of the pilot.
|
||||
void ai_think(Pilot* pilot) {
|
||||
if(pilot->action == NULL) {
|
||||
// Idle git!
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#define CALLOC_L(type)(calloc(1, sizeof(type)))
|
||||
|
||||
#define ABS(X) ((X<0)?-X:X)
|
||||
#define FABS(X) ((X<0.)?-X:X)
|
||||
|
||||
typedef float FP;
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <math.h>
|
||||
#include "def.h"
|
||||
#include "log.h"
|
||||
#include "opengl.h"
|
||||
@ -196,6 +197,10 @@ void gl_free(gl_texture* texture) {
|
||||
|
||||
// Blit the sprite at given position.
|
||||
void gl_blitSprite(gl_texture* sprite, Vec2* pos, const int sx, const int sy) {
|
||||
// Don't bother drawing if offscreen -- waste of cycles.
|
||||
if(fabs(pos->x - gl_camera->x) > gl_screen.w / 2 + sprite->sw / 2 ||
|
||||
fabs(pos->y-gl_camera->y) > gl_screen.h / 2 + sprite->sh / 2)
|
||||
return;
|
||||
glMatrixMode(GL_TEXTURE);
|
||||
glPushMatrix();
|
||||
glTranslatef(sprite->sw * (FP)(sx)/sprite->rw,
|
||||
|
10
src/pilot.c
10
src/pilot.c
@ -3,6 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "def.h"
|
||||
#include "log.h"
|
||||
#include "pilot.h"
|
||||
|
||||
@ -13,7 +14,8 @@ static unsigned int pilot_id = 0;
|
||||
static Pilot** pilot_stack;
|
||||
static int pilots = 0;
|
||||
|
||||
extern void player_think(Pilot* pilot, const FP dt); // Player.
|
||||
extern void player_think(Pilot* pilot, const FP dt); // Player.c
|
||||
extern void ai_think(Pilot* pilot); // Ai.c
|
||||
static void pilot_update(Pilot* pilot, const FP dt);
|
||||
static void pilot_render(Pilot* pilot);
|
||||
|
||||
@ -75,16 +77,20 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec
|
||||
|
||||
pilot->solid = solid_create(ship->mass, vel, pos);
|
||||
|
||||
// Max shields armor.
|
||||
pilot->armor = ship->armor;
|
||||
pilot->shield = ship->shield;
|
||||
pilot->energy = ship->energy;
|
||||
|
||||
// Initially idle.
|
||||
pilot->action = NULL;
|
||||
|
||||
if(flags & PILOT_PLAYER) {
|
||||
pilot->think = (void*)player_think; // Players don't need to thing! :P
|
||||
pilot->properties |= PILOT_PLAYER;
|
||||
player = pilot;
|
||||
} else
|
||||
pilot->think = NULL;
|
||||
pilot->think = ai_think;
|
||||
|
||||
pilot->update = pilot_update;
|
||||
}
|
||||
|
22
src/pilot.h
22
src/pilot.h
@ -5,10 +5,27 @@
|
||||
|
||||
#define PILOT_PLAYER 1 // Pilot is a player.
|
||||
|
||||
// =========================================================
|
||||
// AI:
|
||||
// Ai is based on an action list which contains the current
|
||||
// action (FIFO). Actions will run the appropriate Lua code.
|
||||
// =========================================================
|
||||
typedef enum { ACT_ATTACK, ACT_TRAVEL, ACT_BRAKE } action_type;
|
||||
|
||||
// Actions.
|
||||
struct Action {
|
||||
struct Action* next;
|
||||
action_type type;
|
||||
void* target;
|
||||
};
|
||||
typedef struct Action Action;
|
||||
|
||||
// Primary pilot structure.
|
||||
struct Pilot {
|
||||
unsigned int id; // Pilots id.
|
||||
char* name; // Pilot's name (if unique).
|
||||
|
||||
// Object characteristics.
|
||||
Ship* ship; // Pilots ship.
|
||||
Solid* solid; // Associated solid (physics).
|
||||
|
||||
@ -16,9 +33,12 @@ struct Pilot {
|
||||
FP armor, shield, energy;
|
||||
|
||||
void (*update)(struct Pilot*, const FP); // Update the pilot.
|
||||
void (*think)(struct Pilot*); // AI thinking for the pilot.
|
||||
|
||||
unsigned int properties; // Used for AI etc.
|
||||
|
||||
// AI.
|
||||
void (*think)(struct Pilot*); // Ai thinking for the pilot.
|
||||
Action* action; // Current action.
|
||||
};
|
||||
typedef struct Pilot Pilot;
|
||||
|
||||
|
@ -101,7 +101,7 @@ static void handle_keyup(SDLKey key) {
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
player_acc -= (FP)(1<<15);
|
||||
break;
|
||||
//break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user