diff --git a/src/ai.c b/src/ai.c new file mode 100644 index 0000000..30f41a5 --- /dev/null +++ b/src/ai.c @@ -0,0 +1,31 @@ +// Woot, LUA!!!!!! +#include +#include +#include + +#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! + } +} + diff --git a/src/ai.h b/src/ai.h new file mode 100644 index 0000000..a5f3029 --- /dev/null +++ b/src/ai.h @@ -0,0 +1,5 @@ +#pragma once + +int ai_init(void); +void ai_exit(void); + diff --git a/src/def.h b/src/def.h index fae8351..3b93acc 100644 --- a/src/def.h +++ b/src/def.h @@ -3,7 +3,8 @@ #define MALLOC_L(type)(malloc(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; diff --git a/src/opengl.c b/src/opengl.c index 31f6468..32049dc 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -1,5 +1,6 @@ #include #include +#include #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, diff --git a/src/pilot.c b/src/pilot.c index f064657..029109c 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -3,6 +3,7 @@ #include #include +#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; } diff --git a/src/pilot.h b/src/pilot.h index 48016f6..8fb3e0f 100644 --- a/src/pilot.h +++ b/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; diff --git a/src/player.c b/src/player.c index 720de13..c3bb3a7 100644 --- a/src/player.c +++ b/src/player.c @@ -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; }