[Add] Leave me a lone a short while, still working with this.
This commit is contained in:
parent
85bec426ba
commit
20bf55f117
1
.gitignore
vendored
1
.gitignore
vendored
@ -24,5 +24,6 @@
|
|||||||
*.tsv
|
*.tsv
|
||||||
*.pdf
|
*.pdf
|
||||||
*bin/Lephisto
|
*bin/Lephisto
|
||||||
|
*bin/data
|
||||||
|
|
||||||
|
|
||||||
|
33
bin/Makefile
33
bin/Makefile
@ -1,11 +1,11 @@
|
|||||||
DEBUG = 1
|
DEBUG = 1
|
||||||
APPNAME = Lephisto
|
APPNAME = Lephisto
|
||||||
|
|
||||||
OBJS := ../src/main.o \
|
OBJS := $(shell find ../src/ -name '*.c' -print)
|
||||||
../src/physics.o \
|
OBJS := $(OBJS:../src/%.c=../src/%.o)
|
||||||
../src/opengl.o \
|
|
||||||
../src/ship.o \
|
DATA = data
|
||||||
../src/pilot.o
|
DATAFILES = $(shell find ../gfx/ ../dat/ -name '*.png' -o -name '*.xml' -print)
|
||||||
|
|
||||||
CFLAGS = -Wall `sdl-config --cflags` `xml2-config --cflags`
|
CFLAGS = -Wall `sdl-config --cflags` `xml2-config --cflags`
|
||||||
ifdef DEBUG
|
ifdef DEBUG
|
||||||
@ -16,29 +16,18 @@ endif
|
|||||||
|
|
||||||
LDFLAGS = -lm `sdl-config --libs` `xml2-config --libs` -lSDL_image -lGL
|
LDFLAGS = -lm `sdl-config --libs` `xml2-config --libs` -lSDL_image -lGL
|
||||||
|
|
||||||
DOBJS = ship.xml \
|
|
||||||
fleet.xml \
|
|
||||||
outfit.xml \
|
|
||||||
planet.xml \
|
|
||||||
ssys.xml
|
|
||||||
|
|
||||||
%.xml:
|
|
||||||
@sed -e '/^<?xml.*/d' ../dat/$@ >> data
|
|
||||||
|
|
||||||
%.o: ../src/%.c
|
%.o: ../src/%.c
|
||||||
@gcc -c $(CFLAGS) -o $@ $<
|
@gcc -c $(CFLAGS) -o $@ $<
|
||||||
@echo -e "\tCC $@"
|
@echo -e "\tCC $@"
|
||||||
|
|
||||||
all: data $(OBJS)
|
all: data $(OBJS)
|
||||||
@gcc $(LDFLAGS) -o $(APPNAME) $(OBJS)
|
@gcc $(LDFLAGS) -o $(APPNAME) $(OBJS)
|
||||||
@echo -e "\tLD $(APPNAME)"
|
@echo "\tLD $(APPNAME)"
|
||||||
|
|
||||||
data_init:
|
data: $(DATAFILES)
|
||||||
@echo -e '<?xml version="1.0" encoding="UTF-8"?>\n<Data>' > data
|
@echo -e "\tCreating data..\n"
|
||||||
|
@ls -1 $(DATAFILES) | cpio --quiet -o > $(DATA)
|
||||||
data: data_init $(DOBJS)
|
|
||||||
@echo -e '</data>' >> data
|
|
||||||
@echo -e "\tCreating data\n"
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJS) $(APPNAME)
|
@echo -e "\tRemoving data.."
|
||||||
|
rm -rf $(OBJS) $(APPNAME) $(DATA)
|
||||||
|
178
bin/data
178
bin/data
@ -1,178 +0,0 @@
|
|||||||
-e <?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<Data>
|
|
||||||
<Ships>
|
|
||||||
<ship name="Ship">
|
|
||||||
<GFX>../gfx/ship.png</GFX>
|
|
||||||
<class>1</class>
|
|
||||||
<movement>
|
|
||||||
<thrust>240</thrust>
|
|
||||||
<turn>120</turn>
|
|
||||||
<speed>360</speed>
|
|
||||||
</movement>
|
|
||||||
<health>
|
|
||||||
<shield>50</shield>
|
|
||||||
<armor>80</armor>
|
|
||||||
<energy>240</energy>
|
|
||||||
<shield_regen>80</shield_regen>
|
|
||||||
<armor_regen>50</armor_regen>
|
|
||||||
<energy_regen>40</energy_regen>
|
|
||||||
</health>
|
|
||||||
<characteristics>
|
|
||||||
<crew>2</crew>
|
|
||||||
<mass>30</mass>
|
|
||||||
<cap_weapon>20</cap_weapon>
|
|
||||||
<cap_cargo>20</cap_cargo>
|
|
||||||
</caracteristics>
|
|
||||||
<outfits>
|
|
||||||
<outfit quantity='2'>laser</outfit>
|
|
||||||
</outfits>
|
|
||||||
</ship>
|
|
||||||
<ship name="Mr. Test">
|
|
||||||
<GFX>gfx/enemyship.png</GFX>
|
|
||||||
<class>1</class>
|
|
||||||
<movement>
|
|
||||||
<thrust>180</thrust>
|
|
||||||
<turn>100</turn>
|
|
||||||
<speed>260</speed>
|
|
||||||
</movement>
|
|
||||||
<health>
|
|
||||||
<shield>160</shield>
|
|
||||||
<armor>120</armor>
|
|
||||||
<energy>360</energy>
|
|
||||||
<shieldregen>90</shieldregen>
|
|
||||||
<armorregen>60</armorregen>
|
|
||||||
<energyregen>50</energyregen>
|
|
||||||
</health>
|
|
||||||
<characteristics>
|
|
||||||
<crew>9</crew>
|
|
||||||
<mass>130</mass>
|
|
||||||
<cap_weapon>60</cap_weapon>
|
|
||||||
<cap_cargo>40</cap_cargo>
|
|
||||||
</caracteristics>
|
|
||||||
<outfits>
|
|
||||||
</outfits>
|
|
||||||
</ship>
|
|
||||||
</Ships>
|
|
||||||
<Fleets>
|
|
||||||
<fleet name="Test">
|
|
||||||
<faction>2</faction>
|
|
||||||
<pilots>
|
|
||||||
<pilot chance='100'>Mr. Test</pilot>
|
|
||||||
</pilots>
|
|
||||||
</fleet>
|
|
||||||
<fleet name="Merchant Ship">
|
|
||||||
<faction>2</faction>
|
|
||||||
<pilots>
|
|
||||||
<pilot chance='100'>Ship</pilot>
|
|
||||||
</pilots>
|
|
||||||
</fleet>
|
|
||||||
<fleet name="Sml Merchant Convoy">
|
|
||||||
<faction>2</faction>
|
|
||||||
<pilots>
|
|
||||||
<pilot chance='80'>Ship</pilot>
|
|
||||||
<pilot chance='80'>Ship</pilot>
|
|
||||||
<pilot chance='60'>Ship</pilot>
|
|
||||||
<pilot chance='60'>Ship</pilot>
|
|
||||||
</pilots>
|
|
||||||
</fleet>
|
|
||||||
</Fleets>
|
|
||||||
<Outfits>
|
|
||||||
<outfit name="laser">
|
|
||||||
<general>
|
|
||||||
<max>0</max>
|
|
||||||
<type>1</type>
|
|
||||||
<tech>0</tech>
|
|
||||||
</general>
|
|
||||||
<sound>laser</sound>
|
|
||||||
<GFX>
|
|
||||||
<game>laser_green</game>
|
|
||||||
</GFX>
|
|
||||||
<parameters>
|
|
||||||
<parameter>7</parameter>
|
|
||||||
<parameter>7</parameter>
|
|
||||||
<parameter>600</parameter>
|
|
||||||
<parameter>400</parameter>
|
|
||||||
<parameter>18</parameter>
|
|
||||||
<parameter>500</parameter>
|
|
||||||
</parameters>
|
|
||||||
</outfit>
|
|
||||||
</Outfits>
|
|
||||||
<Planets>
|
|
||||||
<planet name="KonoSphere">
|
|
||||||
<pos>
|
|
||||||
<x>10</x>
|
|
||||||
<y>15</y>
|
|
||||||
</pos>
|
|
||||||
<general>
|
|
||||||
<class>1</class>
|
|
||||||
<services>1</services>
|
|
||||||
<tech>0</tech>
|
|
||||||
<commodities>1</commodities>
|
|
||||||
</general>
|
|
||||||
<GFX>KonoSphere</GFX>
|
|
||||||
</planet>
|
|
||||||
<planet name="SaraCraft">
|
|
||||||
<pos>
|
|
||||||
<x>125</x>
|
|
||||||
<y>-345</y>
|
|
||||||
</pos>
|
|
||||||
<general>
|
|
||||||
<class>1</class>
|
|
||||||
<services>1</services>
|
|
||||||
<tech>0</tech>
|
|
||||||
<commodities>1</commodities>
|
|
||||||
</general>
|
|
||||||
<GFX>SaraCraft</GFX>
|
|
||||||
</planet>
|
|
||||||
</Planets>
|
|
||||||
<Systems>
|
|
||||||
<ssys name="SaraSys">
|
|
||||||
<pos>
|
|
||||||
<x>15</x>
|
|
||||||
<y>8</y>
|
|
||||||
</pos>
|
|
||||||
<general>
|
|
||||||
<stars>50</stars>
|
|
||||||
<asteroids>0</asteroids>
|
|
||||||
<interference>0</interference>
|
|
||||||
<faction>2</faction>
|
|
||||||
</general>
|
|
||||||
<planets>
|
|
||||||
<planet>KonoSphere</planet>
|
|
||||||
</planets>
|
|
||||||
<fleets>
|
|
||||||
<fleet chance="100">Test</fleet>
|
|
||||||
<fleet chance="60">Merchant Ship</fleet>
|
|
||||||
<fleet chance="50">Merchant Ship</fleet>
|
|
||||||
<fleet chance="40">Merchant Ship</fleet>
|
|
||||||
<fleet chance="50">Sml Merchant Convoy</fleet>
|
|
||||||
<fleet chance="40">Sml Merchant Convoy</fleet>
|
|
||||||
</fleets>
|
|
||||||
<paths>
|
|
||||||
<path>KonoSys</path>
|
|
||||||
</paths>
|
|
||||||
</ssys>
|
|
||||||
<ssys name="KonoSys">
|
|
||||||
<pos>
|
|
||||||
<x>47</x>
|
|
||||||
<y>33</y>
|
|
||||||
</pos>
|
|
||||||
<general>
|
|
||||||
<stars>27</stars>
|
|
||||||
<asteroids>0</asteroids>
|
|
||||||
<interference>0</interference>
|
|
||||||
<faction>2</faction>
|
|
||||||
</general>
|
|
||||||
<planets>
|
|
||||||
<planet>SaraCraft</planet>
|
|
||||||
</planets>
|
|
||||||
<fleets>
|
|
||||||
<fleet chance="80">Merchant Ship</fleet>
|
|
||||||
<fleet chance="60">Merchant Ship</fleet>
|
|
||||||
</fleets>
|
|
||||||
<paths>
|
|
||||||
<path>SaraSys</path>
|
|
||||||
</paths>
|
|
||||||
</ssys>
|
|
||||||
</Systems>
|
|
||||||
-e </data>
|
|
@ -4,8 +4,8 @@
|
|||||||
<GFX>../gfx/ship.png</GFX>
|
<GFX>../gfx/ship.png</GFX>
|
||||||
<class>1</class>
|
<class>1</class>
|
||||||
<movement>
|
<movement>
|
||||||
<thrust>240</thrust>
|
<thrust>400</thrust>
|
||||||
<turn>120</turn>
|
<turn>960</turn>
|
||||||
<speed>360</speed>
|
<speed>360</speed>
|
||||||
</movement>
|
</movement>
|
||||||
<health>
|
<health>
|
||||||
@ -18,7 +18,7 @@
|
|||||||
</health>
|
</health>
|
||||||
<characteristics>
|
<characteristics>
|
||||||
<crew>2</crew>
|
<crew>2</crew>
|
||||||
<mass>30</mass>
|
<mass>7</mass>
|
||||||
<cap_weapon>20</cap_weapon>
|
<cap_weapon>20</cap_weapon>
|
||||||
<cap_cargo>20</cap_cargo>
|
<cap_cargo>20</cap_cargo>
|
||||||
</caracteristics>
|
</caracteristics>
|
||||||
@ -26,7 +26,7 @@
|
|||||||
<outfit quantity='2'>laser</outfit>
|
<outfit quantity='2'>laser</outfit>
|
||||||
</outfits>
|
</outfits>
|
||||||
</ship>
|
</ship>
|
||||||
<ship name="Mr. Test">
|
<ship name="Miss. Test">
|
||||||
<GFX>gfx/enemyship.png</GFX>
|
<GFX>gfx/enemyship.png</GFX>
|
||||||
<class>1</class>
|
<class>1</class>
|
||||||
<movement>
|
<movement>
|
||||||
@ -52,3 +52,4 @@
|
|||||||
</outfits>
|
</outfits>
|
||||||
</ship>
|
</ship>
|
||||||
</Ships>
|
</Ships>
|
||||||
|
|
||||||
|
BIN
gfx/ship/enemyship.png
Normal file
BIN
gfx/ship/enemyship.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 145 KiB |
BIN
gfx/ship/ship.png
Normal file
BIN
gfx/ship/ship.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 91 KiB |
27
src/joystick.c
Normal file
27
src/joystick.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <SDL.h>
|
||||||
|
#include "def.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "joystick.h"
|
||||||
|
|
||||||
|
static SDL_Joystick* joystick;
|
||||||
|
|
||||||
|
int joystick_init(void) {
|
||||||
|
int indjoystick, numjoysticks, i;
|
||||||
|
indjoystick = 1;
|
||||||
|
|
||||||
|
// Init the SDL subsys.
|
||||||
|
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||||
|
|
||||||
|
// Figure out how many joysticks there are.
|
||||||
|
numjoysticks = SDL_NumJoysticks();
|
||||||
|
LOG("%d joysticks detected", numjoysticks);
|
||||||
|
for(i = 0; i < numjoysticks; i++)
|
||||||
|
LOG("\t\t%d. %s", i, SDL_JoystickName(i));
|
||||||
|
|
||||||
|
// Start using that bitch.
|
||||||
|
LOG("Using joystick %d", indjoystick);
|
||||||
|
joystick = SDL_JoystickOpen(indjoystick);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
2
src/joystick.h
Normal file
2
src/joystick.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
int joystick_init(void);
|
||||||
|
|
51
src/main.c
51
src/main.c
@ -1,6 +1,5 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@ -8,10 +7,14 @@
|
|||||||
#include "physics.h"
|
#include "physics.h"
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
|
#include "player.h"
|
||||||
|
#include "joystick.h"
|
||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
|
|
||||||
static int quit = 0;
|
static int quit = 0;
|
||||||
|
|
||||||
|
static unsigned int time = 0;
|
||||||
|
|
||||||
// Prototypes.
|
// Prototypes.
|
||||||
static void handle_keydown(SDLKey key);
|
static void handle_keydown(SDLKey key);
|
||||||
static void handle_keyup(SDLKey key);
|
static void handle_keyup(SDLKey key);
|
||||||
@ -32,9 +35,22 @@ int main(int argc, const char** argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Input.
|
||||||
|
if(joystick_init())
|
||||||
|
WARN("Error initializing joystick input");
|
||||||
|
|
||||||
// Data loading.
|
// Data loading.
|
||||||
ships_load();
|
ships_load();
|
||||||
|
|
||||||
|
// Testing.
|
||||||
|
unsigned int player_id;
|
||||||
|
player_id = pilot_create(get_ship("Ship"), "Player", NULL, NULL, PILOT_PLAYER);
|
||||||
|
gl_bindCamera(&get_pilot(player_id)->solid->pos);
|
||||||
|
|
||||||
|
pilot_create(get_ship("Miss. Test"), NULL, NULL, NULL, 0);
|
||||||
|
|
||||||
|
time = SDL_GetTicks();
|
||||||
|
|
||||||
// Main looops.
|
// Main looops.
|
||||||
while(!quit) {
|
while(!quit) {
|
||||||
// Event loop.
|
// Event loop.
|
||||||
@ -54,6 +70,7 @@ int main(int argc, const char** argv) {
|
|||||||
update_all();
|
update_all();
|
||||||
}
|
}
|
||||||
// Unload data.
|
// Unload data.
|
||||||
|
pilots_free();
|
||||||
ships_free();
|
ships_free();
|
||||||
gl_exit(); // Kills video output.
|
gl_exit(); // Kills video output.
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
@ -65,6 +82,18 @@ static void handle_keydown(SDLKey key) {
|
|||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
quit = 1;
|
quit = 1;
|
||||||
break;
|
break;
|
||||||
|
case SDLK_a:
|
||||||
|
case SDLK_LEFT:
|
||||||
|
player_setFlag(PLAYER_FLAG_MOV_LEFT);
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
player_setFlag(PLAYER_FLAG_MOV_RIGHT);
|
||||||
|
break;
|
||||||
|
case SDLK_w:
|
||||||
|
case SDLK_UP:
|
||||||
|
player_setFlag(PLAYER_FLAG_MOV_ACC);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -73,14 +102,34 @@ static void handle_keydown(SDLKey key) {
|
|||||||
// Handle keyup events.
|
// Handle keyup events.
|
||||||
static void handle_keyup(SDLKey key) {
|
static void handle_keyup(SDLKey key) {
|
||||||
switch(key) {
|
switch(key) {
|
||||||
|
case SDLK_a:
|
||||||
|
case SDLK_LEFT:
|
||||||
|
player_rmFlag(PLAYER_FLAG_MOV_LEFT);
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
player_rmFlag(PLAYER_FLAG_MOV_RIGHT);
|
||||||
|
break;
|
||||||
|
case SDLK_w:
|
||||||
|
case SDLK_UP:
|
||||||
|
player_rmFlag(PLAYER_FLAG_MOV_ACC);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update all the things.
|
// Update all the things.
|
||||||
|
// Pilots:
|
||||||
|
// -- Think (ai).
|
||||||
|
// -- Solid.
|
||||||
static void update_all(void) {
|
static void update_all(void) {
|
||||||
|
FP dt = (FP)(SDL_GetTicks() - time) / 1000.0;
|
||||||
|
time = SDL_GetTicks();
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
pilots_update(dt);
|
||||||
|
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,8 +22,12 @@
|
|||||||
// Since dt isn't actually differential this gives us an
|
// Since dt isn't actually differential this gives us an
|
||||||
// error, so watch out with big values for dt.
|
// error, so watch out with big values for dt.
|
||||||
// ========================================================
|
// ========================================================
|
||||||
|
#if 0
|
||||||
static void simple_update(Solid* obj, const FP dt) {
|
static void simple_update(Solid* obj, const FP dt) {
|
||||||
|
// Make sure angle doesn't flip.
|
||||||
|
obj->dir += obj->dir_vel/360.0*dt;
|
||||||
|
if(obj->dir > 2*M_PI) obj->dir -= 2*M_PI;
|
||||||
|
if(obj->dir < 0.0) obj->dir += 2*M_PI;
|
||||||
if(obj->force) {
|
if(obj->force) {
|
||||||
Vec2 acc;
|
Vec2 acc;
|
||||||
acc.x = obj->force / obj->mass * COS(obj->dir);
|
acc.x = obj->force / obj->mass * COS(obj->dir);
|
||||||
@ -32,13 +36,14 @@ static void simple_update(Solid* obj, const FP dt) {
|
|||||||
obj->pos.x += acc.x * dt;
|
obj->pos.x += acc.x * dt;
|
||||||
obj->vel.y += acc.y * dt;
|
obj->vel.y += acc.y * dt;
|
||||||
|
|
||||||
obj->pos.x += obj->vel.x * dt + 0.5 * acc.x / obj->mass * dt * dt;
|
obj->pos.x += obj->vel.x * dt + 0.5 * acc.x * dt*dt;
|
||||||
obj->pos.y += obj->vel.y * dt + 0.5 * acc.y / obj->mass * dt * dt;
|
obj->pos.y += obj->vel.y * dt + 0.5 * acc.y * dt*dt;
|
||||||
} else {
|
} else {
|
||||||
obj->pos.x += obj->vel.x * dt;
|
obj->pos.x += obj->vel.x * dt;
|
||||||
obj->pos.y += obj->vel.y * dt;
|
obj->pos.y += obj->vel.y * dt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// ==Runge-Kutta 4th method.===============================
|
// ==Runge-Kutta 4th method.===============================
|
||||||
// d^2 x(t) / d t^2 = a, a = constant(acceleration)
|
// d^2 x(t) / d t^2 = a, a = constant(acceleration)
|
||||||
@ -57,6 +62,11 @@ static void simple_update(Solid* obj, const FP dt) {
|
|||||||
|
|
||||||
#define RK4_N 4
|
#define RK4_N 4
|
||||||
static void rk4_update(Solid* obj, const FP dt) {
|
static void rk4_update(Solid* obj, const FP dt) {
|
||||||
|
// Make sure angle doesn't flip.
|
||||||
|
obj->dir += obj->dir_vel/360.0*dt;
|
||||||
|
if(obj->dir > 2*M_PI) obj->dir -= 2*M_PI;
|
||||||
|
if(obj->dir < 0.0) obj->dir += 2*M_PI;
|
||||||
|
|
||||||
FP h = dt / RK4_N; // Step.
|
FP h = dt / RK4_N; // Step.
|
||||||
|
|
||||||
if(obj->force) { // Force applied on object.
|
if(obj->force) { // Force applied on object.
|
||||||
|
@ -8,7 +8,7 @@ typedef struct {
|
|||||||
|
|
||||||
// Describe any solid in 2D space.
|
// Describe any solid in 2D space.
|
||||||
struct Solid {
|
struct Solid {
|
||||||
FP mass, force, dir; // Properties.
|
FP mass, force, dir, dir_vel; // Properties.
|
||||||
Vec2 vel, pos; // Position/velocity vectors.
|
Vec2 vel, pos; // Position/velocity vectors.
|
||||||
void(*update)(struct Solid*, const FP); // Update method.
|
void(*update)(struct Solid*, const FP); // Update method.
|
||||||
};
|
};
|
||||||
|
75
src/pilot.c
75
src/pilot.c
@ -1,17 +1,38 @@
|
|||||||
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
|
|
||||||
// Stack of pilot id's to assure uniqueness.
|
// Stack of pilot id's to assure uniqueness.
|
||||||
static unsigned int pilot_id = 0;
|
static unsigned int pilot_id = 0;
|
||||||
|
|
||||||
// Stack of pilots - yes, they come in stacks now.
|
// Stack of pilots - yes, they come in stacks now.
|
||||||
static Pilot* pilot_stack = NULL;
|
static Pilot** pilot_stack;
|
||||||
|
static int pilots = 0;
|
||||||
|
|
||||||
|
extern void player_think(Pilot* pilot, const FP dt); // Player.
|
||||||
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);
|
||||||
|
|
||||||
|
// Pull a pilot out of the pilot_stack based on id.
|
||||||
|
Pilot* get_pilot(unsigned int id) {
|
||||||
|
// Regular search.
|
||||||
|
#if 0
|
||||||
|
for(int i = 0; i < pilots; i++)
|
||||||
|
if(pilot_stack[i]->id == id)
|
||||||
|
return pilot_stack[i];
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
|
// Dichotomical search.
|
||||||
|
int i, n;
|
||||||
|
for(i = 0, n = pilots/2; n > 0; n /= 2)
|
||||||
|
i += (pilot_stack[i+n]->id > id) ? 0 : n;
|
||||||
|
return (pilot_stack[i]->id == id) ? pilot_stack[i] : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Render the pilot.
|
// Render the pilot.
|
||||||
static void pilot_render(Pilot* pilot) {
|
static void pilot_render(Pilot* pilot) {
|
||||||
int sprite;
|
int sprite;
|
||||||
@ -46,13 +67,11 @@ static void pilot_update(Pilot* pilot, const FP dt) {
|
|||||||
// flags : Tweaking the pilot.
|
// flags : Tweaking the pilot.
|
||||||
// ========================================================
|
// ========================================================
|
||||||
void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec2* pos, const int flags) {
|
void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec2* pos, const int flags) {
|
||||||
pilot->id = pilot_id++; // New unique pilot id based on pilot_id.
|
pilot->id = ++pilot_id; // New unique pilot id based on pilot_id, Can't be 0.
|
||||||
|
|
||||||
pilot->ship = ship;
|
pilot->ship = ship;
|
||||||
if(name == NULL)
|
|
||||||
pilot->name = ship->name;
|
pilot->name = strdup((name == NULL) ? ship->name:name);
|
||||||
else
|
|
||||||
pilot->name = name;
|
|
||||||
|
|
||||||
pilot->solid = solid_create(ship->mass, vel, pos);
|
pilot->solid = solid_create(ship->mass, vel, pos);
|
||||||
|
|
||||||
@ -60,24 +79,48 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, const Vec2* vel, const Vec
|
|||||||
pilot->shield = ship->shield;
|
pilot->shield = ship->shield;
|
||||||
pilot->energy = ship->energy;
|
pilot->energy = ship->energy;
|
||||||
|
|
||||||
if(flags & PILOT_PLAYER)
|
if(flags & PILOT_PLAYER) {
|
||||||
pilot->think = NULL; // Players don't need to thing! :P
|
pilot->think = (void*)player_think; // Players don't need to thing! :P
|
||||||
|
pilot->think = NULL;
|
||||||
|
} else
|
||||||
|
pilot->think = NULL;
|
||||||
|
|
||||||
pilot->update = pilot_update;
|
pilot->update = pilot_update;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new pilot - Params are same as pilot_init.
|
// Create a new pilot - Params are same as pilot_init. Return pilot's id.
|
||||||
Pilot* pilot_create(Ship* ship, char* name, const Vec2* vel, const Vec2* pos, const int flags) {
|
unsigned int pilot_create(Ship* ship, char* name, const Vec2* vel, const Vec2* pos, const int flags) {
|
||||||
Pilot* dyn = MALLOC_L(Pilot);
|
Pilot* dyn = MALLOC_L(Pilot);
|
||||||
assert(dyn != NULL);
|
if(dyn == NULL) {
|
||||||
|
WARN("Unable to allocate memory.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
pilot_init(dyn, ship, name, vel, pos, flags);
|
pilot_init(dyn, ship, name, vel, pos, flags);
|
||||||
return dyn;
|
|
||||||
|
// Add to the stack.
|
||||||
|
pilot_stack = realloc(pilot_stack, ++pilots*sizeof(Pilot*));
|
||||||
|
pilot_stack[pilots-1] = dyn;
|
||||||
|
return dyn->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free the prisoned pilot!
|
// Free the prisoned pilot!
|
||||||
void pilot_free(Pilot* pilot) {
|
void pilots_free(void) {
|
||||||
solid_free(pilot->solid);
|
int i;
|
||||||
free(pilot);
|
for(i = 0; i < pilots; i++) {
|
||||||
pilot = NULL;
|
solid_free(pilot_stack[i]->solid);
|
||||||
|
free(pilot_stack[i]->name);
|
||||||
|
free(pilot_stack[i]);
|
||||||
|
}
|
||||||
|
free(pilot_stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all pilots.
|
||||||
|
void pilots_update(FP dt) {
|
||||||
|
int i;
|
||||||
|
for(i = pilots-1; i >= 0; i--) {
|
||||||
|
if(pilot_stack[i]->think != NULL)
|
||||||
|
pilot_stack[i]->think(pilot_stack[i]);
|
||||||
|
pilot_stack[i]->update(pilot_stack[i], dt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
src/pilot.h
16
src/pilot.h
@ -6,8 +6,7 @@
|
|||||||
#define PILOT_PLAYER 1 // Pilot is a player.
|
#define PILOT_PLAYER 1 // Pilot is a player.
|
||||||
|
|
||||||
struct Pilot {
|
struct Pilot {
|
||||||
struct Pilot* next;
|
unsigned int id; // Pilots id.
|
||||||
int id; // Pilot's id.
|
|
||||||
char* name; // Pilot's name (if unique).
|
char* name; // Pilot's name (if unique).
|
||||||
|
|
||||||
Ship* ship; // Pilots ship.
|
Ship* ship; // Pilots ship.
|
||||||
@ -19,15 +18,22 @@ struct Pilot {
|
|||||||
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.
|
void (*think)(struct Pilot*); // AI thinking for the pilot.
|
||||||
|
|
||||||
unsigned int flags; // Used for AI etc.
|
unsigned int properties; // Used for AI etc.
|
||||||
};
|
};
|
||||||
typedef struct Pilot Pilot;
|
typedef struct Pilot Pilot;
|
||||||
|
|
||||||
|
Pilot* get_pilot(unsigned int id);
|
||||||
|
|
||||||
|
// Creation.
|
||||||
void pilot_init(Pilot* dest, Ship* ship, char* name,
|
void pilot_init(Pilot* dest, Ship* ship, char* name,
|
||||||
const Vec2* vel, const Vec2* pos, const int flags);
|
const Vec2* vel, const Vec2* pos, const int flags);
|
||||||
|
|
||||||
Pilot* pilot_create(Ship* ship, char* name, const Vec2* vel,
|
unsigned int pilot_create(Ship* ship, char* name, const Vec2* vel,
|
||||||
const Vec2* pos, const int flags);
|
const Vec2* pos, const int flags);
|
||||||
|
|
||||||
void pilot_free(Pilot* src);
|
// Cleanup.
|
||||||
|
void pilot_free(void);
|
||||||
|
|
||||||
|
// Update.
|
||||||
|
void pilots_update(FP dt);
|
||||||
|
|
||||||
|
33
src/player.c
Normal file
33
src/player.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "def.h"
|
||||||
|
#include "pilot.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "player.h"
|
||||||
|
|
||||||
|
static unsigned int player_flags = PLAYER_FLAG_NULL;
|
||||||
|
|
||||||
|
// To be used in pilot.c
|
||||||
|
void player_think(Pilot* player, const FP dt) {
|
||||||
|
player->solid->dir_vel = 0.0;
|
||||||
|
if(player_isFlag(PLAYER_FLAG_MOV_LEFT))
|
||||||
|
player->solid->dir_vel += player->ship->turn;
|
||||||
|
if(player_isFlag(PLAYER_FLAG_MOV_RIGHT))
|
||||||
|
player->solid->dir_vel -= player->ship->turn;
|
||||||
|
|
||||||
|
player->solid->force = (player_isFlag(PLAYER_FLAG_MOV_ACC)) ? player->ship->thrust : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flag manipulationz.
|
||||||
|
int player_isFlag(unsigned int flag) {
|
||||||
|
return player_flags & flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
void player_setFlag(unsigned int flag) {
|
||||||
|
if(!player_isFlag(flag))
|
||||||
|
player_flags |= flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
void player_rmFlag(unsigned int flag) {
|
||||||
|
if(player_isFlag(flag))
|
||||||
|
player_flags ^= flag;
|
||||||
|
}
|
||||||
|
|
11
src/player.h
Normal file
11
src/player.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define PLAYER_FLAG_NULL (1<<0)
|
||||||
|
#define PLAYER_FLAG_MOV_LEFT (1<<1)
|
||||||
|
#define PLAYER_FLAG_MOV_RIGHT (1<<2)
|
||||||
|
#define PLAYER_FLAG_MOV_ACC (1<<3)
|
||||||
|
|
||||||
|
int player_isFlag(unsigned int flag);
|
||||||
|
void player_setFlag(unsigned int flag);
|
||||||
|
void player_rmFlag(unsigned int flag);
|
||||||
|
|
20
src/ship.c
20
src/ship.c
@ -4,6 +4,8 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
|
|
||||||
|
#define MAX_PATH_NAME 20 // Maximum size of the path.
|
||||||
|
|
||||||
#define XML_NODE_START 1
|
#define XML_NODE_START 1
|
||||||
#define XML_NODE_TEXT 3
|
#define XML_NODE_TEXT 3
|
||||||
#define XML_NODE_CLOSE 15
|
#define XML_NODE_CLOSE 15
|
||||||
@ -12,22 +14,28 @@
|
|||||||
#define XML_ID "Ships" // XML section identifier.
|
#define XML_ID "Ships" // XML section identifier.
|
||||||
#define XML_SHIP "ship"
|
#define XML_SHIP "ship"
|
||||||
|
|
||||||
|
#define SHIP_DATA "../dat/ship.xml"
|
||||||
|
#define SHIP_GFX "../gfx/ship/
|
||||||
|
|
||||||
static Ship* ship_stack = NULL;
|
static Ship* ship_stack = NULL;
|
||||||
static int ships;
|
static int ships;
|
||||||
|
|
||||||
// Get a ship based on it's name.
|
// Get a ship based on it's name.
|
||||||
Ship* get_ship(const char* name) {
|
Ship* get_ship(const char* name) {
|
||||||
Ship* tmp = ship_stack;
|
Ship* tmp = ship_stack;
|
||||||
while(tmp != NULL)
|
int i;
|
||||||
if(strcmp((tmp++)->name, name)==0) break;
|
for(i = 0; i < ships; i++)
|
||||||
|
if(strcmp((tmp+i)->name, name)==0) break;
|
||||||
|
|
||||||
return tmp;
|
return tmp+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ship* ship_parse(xmlNodePtr node) {
|
Ship* ship_parse(xmlNodePtr node) {
|
||||||
xmlNodePtr cur;
|
xmlNodePtr cur;
|
||||||
Ship* tmp = CALLOC_L(Ship);
|
Ship* tmp = CALLOC_L(Ship);
|
||||||
|
|
||||||
|
char str[MAX_PATH_NAME] = "\0";
|
||||||
|
|
||||||
tmp->name = (char*)xmlGetProp(node, (xmlChar*)"name");
|
tmp->name = (char*)xmlGetProp(node, (xmlChar*)"name");
|
||||||
|
|
||||||
node = node->xmlChildrenNode;
|
node = node->xmlChildrenNode;
|
||||||
@ -35,9 +43,11 @@ Ship* ship_parse(xmlNodePtr node) {
|
|||||||
while((node = node->next)) {
|
while((node = node->next)) {
|
||||||
if(strcmp((char*)node->name, "GFX")==0) {
|
if(strcmp((char*)node->name, "GFX")==0) {
|
||||||
cur = node->children;
|
cur = node->children;
|
||||||
if(strcmp((char*)cur->name, "text")==0)
|
if(strcmp((char*)cur->name, "text")==0) {
|
||||||
|
snprintf(str, sizeof(cur->content)+4, "../gfx/%s", (char*)cur->content);
|
||||||
tmp->gfx_ship = gl_newSprite((char*)cur->content, 6, 6);
|
tmp->gfx_ship = gl_newSprite((char*)cur->content, 6, 6);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if(strcmp((char*)node->name, "class")==0) {
|
else if(strcmp((char*)node->name, "class")==0) {
|
||||||
cur = node->children;
|
cur = node->children;
|
||||||
if(strcmp((char*)cur->name, "text")==0)
|
if(strcmp((char*)cur->name, "text")==0)
|
||||||
@ -85,6 +95,8 @@ Ship* ship_parse(xmlNodePtr node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tmp->thrust *= tmp->mass; // Helps keep number sane.
|
||||||
|
|
||||||
DEBUG("Loaded ship '%s'", tmp->name);
|
DEBUG("Loaded ship '%s'", tmp->name);
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
4
utils/pack/Makefile
Normal file
4
utils/pack/Makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
CFLAGS += -g
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(CC) $(CFLAGS) -o pack pack.c
|
BIN
utils/pack/pack
Executable file
BIN
utils/pack/pack
Executable file
Binary file not shown.
75
utils/pack/pack.c
Normal file
75
utils/pack/pack.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <fcntl.h> // creat() and co.
|
||||||
|
#include <sys/stat.h> // S_IRUSR
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define USAGE "Usage: %s output input\n", argv[0]
|
||||||
|
#define PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
|
||||||
|
|
||||||
|
const uint64_t magic = 0x25524573; // sER%
|
||||||
|
|
||||||
|
off_t getfilesize(const char* filename);
|
||||||
|
int fileexists(const char* filename);
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
switch(argc) {
|
||||||
|
case 1:
|
||||||
|
printf("Missing output file\n");
|
||||||
|
case 2:
|
||||||
|
printf("Missing input file/s\n\n");
|
||||||
|
goto usage;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i, namesize;
|
||||||
|
int outfd, infd;
|
||||||
|
uint32_t nfiles = (uint32_t)argc - 2;
|
||||||
|
|
||||||
|
for(namesize = 0; i < nfiles; i++) {
|
||||||
|
// Make sure file exists before writing.
|
||||||
|
if(!fileexists(argv[i+2])) {
|
||||||
|
printf("File %s does not exist!\n", argv[i+2]);
|
||||||
|
goto failure;
|
||||||
|
}
|
||||||
|
namesize += strlen(argv[i+2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the output file.
|
||||||
|
outfd = creat(argv[1], PERMS);
|
||||||
|
if(outfd == -1) goto failure;
|
||||||
|
|
||||||
|
// Magic number.
|
||||||
|
write(outfd, &magic, sizeof(magic));
|
||||||
|
|
||||||
|
close(outfd);
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
usage:
|
||||||
|
printf(USAGE);
|
||||||
|
failure:
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grab file size.
|
||||||
|
off_t getfilesize(const char* filename) {
|
||||||
|
struct stat file;
|
||||||
|
if(!stat(filename, &file))
|
||||||
|
return file.st_size;
|
||||||
|
|
||||||
|
printf("Unable to get filesize of %s\n", filename);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Does the file exist?
|
||||||
|
int fileexists(const char* filename) {
|
||||||
|
struct stat file;
|
||||||
|
if(!stat(filename, &file))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user