[Add] Initial Lua infrastructure.

This commit is contained in:
Allanis 2013-02-02 00:39:48 +00:00
parent dd5cbe614b
commit 838bbcd2ab
7 changed files with 73 additions and 5 deletions

31
src/ai.c Normal file
View 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!
}
}

5
src/ai.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
int ai_init(void);
void ai_exit(void);

View File

@ -3,7 +3,8 @@
#define MALLOC_L(type)(malloc(sizeof(type))) #define MALLOC_L(type)(malloc(sizeof(type)))
#define CALLOC_L(type)(calloc(1, sizeof(type))) #define CALLOC_L(type)(calloc(1, sizeof(type)))
#define ABS(X) ((X<0)?-X:X) #define ABS(X) ((X<0)?-X:X)
#define FABS(X) ((X<0.)?-X:X)
typedef float FP; typedef float FP;

View File

@ -1,5 +1,6 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_image.h> #include <SDL_image.h>
#include <math.h>
#include "def.h" #include "def.h"
#include "log.h" #include "log.h"
#include "opengl.h" #include "opengl.h"
@ -196,6 +197,10 @@ void gl_free(gl_texture* texture) {
// Blit the sprite at given position. // Blit the sprite at given position.
void gl_blitSprite(gl_texture* sprite, Vec2* pos, const int sx, const int sy) { 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); glMatrixMode(GL_TEXTURE);
glPushMatrix(); glPushMatrix();
glTranslatef(sprite->sw * (FP)(sx)/sprite->rw, glTranslatef(sprite->sw * (FP)(sx)/sprite->rw,

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "def.h"
#include "log.h" #include "log.h"
#include "pilot.h" #include "pilot.h"
@ -13,7 +14,8 @@ static unsigned int pilot_id = 0;
static Pilot** pilot_stack; static Pilot** pilot_stack;
static int pilots = 0; 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_update(Pilot* pilot, const FP dt);
static void pilot_render(Pilot* pilot); 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); pilot->solid = solid_create(ship->mass, vel, pos);
// Max shields armor.
pilot->armor = ship->armor; pilot->armor = ship->armor;
pilot->shield = ship->shield; pilot->shield = ship->shield;
pilot->energy = ship->energy; pilot->energy = ship->energy;
// Initially idle.
pilot->action = NULL;
if(flags & PILOT_PLAYER) { if(flags & PILOT_PLAYER) {
pilot->think = (void*)player_think; // Players don't need to thing! :P pilot->think = (void*)player_think; // Players don't need to thing! :P
pilot->properties |= PILOT_PLAYER; pilot->properties |= PILOT_PLAYER;
player = pilot; player = pilot;
} else } else
pilot->think = NULL; pilot->think = ai_think;
pilot->update = pilot_update; pilot->update = pilot_update;
} }

View File

@ -5,10 +5,27 @@
#define PILOT_PLAYER 1 // Pilot is a player. #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 { struct Pilot {
unsigned int id; // Pilots id. unsigned int id; // Pilots id.
char* name; // Pilot's name (if unique). char* name; // Pilot's name (if unique).
// Object characteristics.
Ship* ship; // Pilots ship. Ship* ship; // Pilots ship.
Solid* solid; // Associated solid (physics). Solid* solid; // Associated solid (physics).
@ -16,9 +33,12 @@ struct Pilot {
FP armor, shield, energy; FP armor, shield, energy;
void (*update)(struct Pilot*, const FP); // Update the pilot. 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. 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; typedef struct Pilot Pilot;

View File

@ -101,7 +101,7 @@ static void handle_keyup(SDLKey key) {
case SDLK_UP: case SDLK_UP:
case SDLK_w: case SDLK_w:
player_acc -= (FP)(1<<15); player_acc -= (FP)(1<<15);
break; //break;
default: default:
break; break;
} }