Compare commits
5 Commits
0f4153099c
...
207ea9b245
Author | SHA1 | Date |
---|---|---|
Elias Fleckenstein | 207ea9b245 | |
Elias Fleckenstein | 125e7eca1a | |
Elias Fleckenstein | 8b50cd53c8 | |
Elias Fleckenstein | e1be7203f4 | |
Elias Fleckenstein | 9d3ecc266d |
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
all: dungeon plugins
|
all: dungeon plugins
|
||||||
|
|
||||||
dungeon: dungeon.c
|
dungeon: dungeon.c
|
||||||
cc -g -o dungeon dungeon.c -ldl -D_GNU_SOURCE
|
cc -g -o dungeon dungeon.c -ldl
|
||||||
|
|
||||||
include plugins/*/Makefile
|
include plugins/*/Makefile
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ static void *load_plugin(const char *name)
|
||||||
char library_name[1 + 1 + 7 + 1 + len + 1 + len + 1 + 2 + 1];
|
char library_name[1 + 1 + 7 + 1 + len + 1 + len + 1 + 2 + 1];
|
||||||
sprintf(library_name, "./plugins/%s/%s.so", name, name);
|
sprintf(library_name, "./plugins/%s/%s.so", name, name);
|
||||||
|
|
||||||
void *handle = dlmopen(LM_ID_BASE, library_name, RTLD_NOW | RTLD_GLOBAL);
|
void *handle = dlopen(library_name, RTLD_NOW | RTLD_GLOBAL);
|
||||||
|
|
||||||
if (! handle) {
|
if (! handle) {
|
||||||
printf("%s\n", dlerror());
|
printf("%s\n", dlerror());
|
||||||
|
|
|
@ -42,7 +42,8 @@ static void spawn_apple(int x, int y, enum mg_context ctx)
|
||||||
__attribute__((constructor)) static void init()
|
__attribute__((constructor)) static void init()
|
||||||
{
|
{
|
||||||
register_air_function((struct generator_function) {
|
register_air_function((struct generator_function) {
|
||||||
.chance = 25,
|
.corridor_chance = 25,
|
||||||
|
.room_chance = 50,
|
||||||
.callback = &spawn_apple,
|
.callback = &spawn_apple,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
plugins/cherry/cherry.so: plugins/cherry/cherry.c plugins/game/game.h plugins/score/score.h plugins/inventory/inventory.h
|
plugins/cherry/cherry.so: plugins/cherry/cherry.c plugins/game/game.h plugins/score/score.h plugins/inventory/inventory.h plugins/loot/loot.h
|
||||||
cc -g -shared -fpic -o plugins/cherry/cherry.so plugins/cherry/cherry.c
|
cc -g -shared -fpic -o plugins/cherry/cherry.so plugins/cherry/cherry.c
|
||||||
|
|
||||||
PLUGINS := ${PLUGINS} plugins/cherry/cherry.so
|
PLUGINS := ${PLUGINS} plugins/cherry/cherry.so
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
#include "../score/score.h"
|
#include "../score/score.h"
|
||||||
#include "../inventory/inventory.h"
|
#include "../inventory/inventory.h"
|
||||||
|
#include "../loot/loot.h"
|
||||||
|
|
||||||
static bool use_cherry(struct itemstack *stack)
|
static bool use_cherry(struct itemstack *stack)
|
||||||
{
|
{
|
||||||
|
@ -62,8 +63,16 @@ static void spawn_cherry(int x, int y, enum mg_context ctx)
|
||||||
__attribute__((constructor)) static void init()
|
__attribute__((constructor)) static void init()
|
||||||
{
|
{
|
||||||
register_air_function((struct generator_function) {
|
register_air_function((struct generator_function) {
|
||||||
.chance = 100,
|
.corridor_chance = 100,
|
||||||
|
.room_chance = 100,
|
||||||
.callback = &spawn_cherry,
|
.callback = &spawn_cherry,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
register_loot((struct loot) {
|
||||||
|
.item = &cherry_item,
|
||||||
|
.chance = 2,
|
||||||
|
.min = 3,
|
||||||
|
.max = 10,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef _CHERRY_H_
|
||||||
|
#define _CHERRY_H_
|
||||||
|
|
||||||
|
#include "../inventory/inventory.h"
|
||||||
|
|
||||||
|
extern struct item cherry_item;
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,3 +1,4 @@
|
||||||
game
|
game
|
||||||
score
|
score
|
||||||
inventory
|
inventory
|
||||||
|
loot
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
plugins/fireball/fireball.so: plugins/fireball/fireball.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h
|
plugins/fireball/fireball.so: plugins/fireball/fireball.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/loot/loot.h
|
||||||
cc -g -shared -fpic -o plugins/fireball/fireball.so plugins/fireball/fireball.c
|
cc -g -shared -fpic -o plugins/fireball/fireball.so plugins/fireball/fireball.c
|
||||||
|
|
||||||
PLUGINS := ${PLUGINS} plugins/fireball/fireball.so
|
PLUGINS := ${PLUGINS} plugins/fireball/fireball.so
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
game
|
game
|
||||||
movement
|
movement
|
||||||
inventory
|
inventory
|
||||||
|
loot
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
#include "../movement/movement.h"
|
#include "../movement/movement.h"
|
||||||
#include "../inventory/inventory.h"
|
#include "../inventory/inventory.h"
|
||||||
|
#include "../loot/loot.h"
|
||||||
|
|
||||||
struct fireball_data
|
struct fireball_data
|
||||||
{
|
{
|
||||||
|
@ -113,4 +114,11 @@ __attribute__((constructor)) static void init()
|
||||||
.count = 7,
|
.count = 7,
|
||||||
.meta = NULL,
|
.meta = NULL,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
register_loot((struct loot) {
|
||||||
|
.item = &fireball_item,
|
||||||
|
.chance = 3,
|
||||||
|
.min = 4,
|
||||||
|
.max = 7,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -334,7 +334,9 @@ static void mapgen_set_air(int x, int y, enum mg_context ctx)
|
||||||
for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
|
for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
|
||||||
struct generator_function *func = ptr->element;
|
struct generator_function *func = ptr->element;
|
||||||
|
|
||||||
if (rand() % func->chance == 0)
|
int chance = ctx == MG_CTX_CORRIDOR ? func->corridor_chance : func->room_chance;
|
||||||
|
|
||||||
|
if (chance && rand() % chance == 0)
|
||||||
func->callback(x, y, ctx);
|
func->callback(x, y, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,8 @@ enum mg_context
|
||||||
|
|
||||||
struct generator_function
|
struct generator_function
|
||||||
{
|
{
|
||||||
int chance;
|
int corridor_chance;
|
||||||
|
int room_chance;
|
||||||
void (*callback)(int x, int y, enum mg_context ctx);
|
void (*callback)(int x, int y, enum mg_context ctx);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
plugins/loot/loot.so: plugins/loot/loot.c plugins/loot/loot.h plugins/game/game.h plugins/inventory/inventory.h
|
||||||
|
cc -g -shared -fpic -o plugins/loot/loot.so plugins/loot/loot.c
|
||||||
|
|
||||||
|
PLUGINS := ${PLUGINS} plugins/loot/loot.so
|
|
@ -0,0 +1,2 @@
|
||||||
|
game
|
||||||
|
inventory
|
|
@ -0,0 +1,63 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../loot/loot.h"
|
||||||
|
#include "../game/game.h"
|
||||||
|
|
||||||
|
static struct list *loot_list = NULL;
|
||||||
|
|
||||||
|
void register_loot(struct loot loot)
|
||||||
|
{
|
||||||
|
loot_list = add_element(loot_list, make_buffer(&loot, sizeof(struct loot)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void loot_step(struct entity *self, struct entity_step_data stepdata)
|
||||||
|
{
|
||||||
|
if (stepdata.dx == 0 && stepdata.dy == 0) {
|
||||||
|
for (struct list *ptr = loot_list; ptr != NULL; ptr = ptr->next) {
|
||||||
|
struct loot *loot = ptr->element;
|
||||||
|
if (rand() % loot->chance == 0) {
|
||||||
|
inventory_add(&player_inventory, (struct itemstack) {
|
||||||
|
.item = loot->item,
|
||||||
|
.count = loot->min + (loot->max > loot->min ? rand() % (loot->max - loot->min + 1) : 0),
|
||||||
|
.meta = NULL,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self->remove = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct entity loot_entity = {
|
||||||
|
.name = "loot",
|
||||||
|
.x = 0,
|
||||||
|
.y = 0,
|
||||||
|
.color = {0},
|
||||||
|
.use_color = false,
|
||||||
|
.texture = "🎁",
|
||||||
|
.remove = false,
|
||||||
|
.meta = NULL,
|
||||||
|
.health = 1,
|
||||||
|
.max_health = 1,
|
||||||
|
.collide_with_entities = false,
|
||||||
|
|
||||||
|
.on_step = &loot_step,
|
||||||
|
.on_collide = NULL,
|
||||||
|
.on_collide_with_entity = NULL,
|
||||||
|
.on_spawn = NULL,
|
||||||
|
.on_remove = NULL,
|
||||||
|
.on_death = NULL,
|
||||||
|
.on_damage = NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void spawn_loot(int x, int y, enum mg_context ctx)
|
||||||
|
{
|
||||||
|
spawn(loot_entity, x, y, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((constructor)) static void init()
|
||||||
|
{
|
||||||
|
register_air_function((struct generator_function) {
|
||||||
|
.corridor_chance = 0,
|
||||||
|
.room_chance = 250,
|
||||||
|
.callback = &spawn_loot,
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef _LOOT_H_
|
||||||
|
#define _LOOT_H_
|
||||||
|
|
||||||
|
#include "../inventory/inventory.h"
|
||||||
|
|
||||||
|
struct loot
|
||||||
|
{
|
||||||
|
struct item *item;
|
||||||
|
int chance;
|
||||||
|
int min;
|
||||||
|
int max;
|
||||||
|
};
|
||||||
|
|
||||||
|
void register_loot(struct loot loot);
|
||||||
|
|
||||||
|
#endif
|
|
@ -68,7 +68,8 @@ static void spawn_monster(int x, int y, enum mg_context ctx)
|
||||||
__attribute__((constructor)) static void init()
|
__attribute__((constructor)) static void init()
|
||||||
{
|
{
|
||||||
register_air_function((struct generator_function) {
|
register_air_function((struct generator_function) {
|
||||||
.chance = 50,
|
.corridor_chance = 50,
|
||||||
|
.room_chance = 200,
|
||||||
.callback = &spawn_monster,
|
.callback = &spawn_monster,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ static char *level_symbol = NULL;
|
||||||
static size_t level_symbol_len = 0;
|
static size_t level_symbol_len = 0;
|
||||||
static double score_timer = 0.0;
|
static double score_timer = 0.0;
|
||||||
static double level_timer = 0.0;
|
static double level_timer = 0.0;
|
||||||
|
static struct list *on_level_up = NULL;
|
||||||
|
|
||||||
static void level_up()
|
static void level_up()
|
||||||
{
|
{
|
||||||
|
@ -23,6 +24,9 @@ static void level_up()
|
||||||
get_roman_numeral(level, &level_symbol, &level_symbol_len);
|
get_roman_numeral(level, &level_symbol, &level_symbol_len);
|
||||||
|
|
||||||
level_timer = 2.0;
|
level_timer = 2.0;
|
||||||
|
|
||||||
|
for (struct list *ptr = on_level_up; ptr != NULL; ptr = ptr->next)
|
||||||
|
((void (*)(int)) ptr->element)(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_score(int s)
|
void add_score(int s)
|
||||||
|
@ -47,13 +51,18 @@ int get_level()
|
||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void register_on_level_up(void (*callback)(int new_level))
|
||||||
|
{
|
||||||
|
on_level_up = add_element(on_level_up, callback);
|
||||||
|
}
|
||||||
|
|
||||||
static void render_score(struct winsize ws)
|
static void render_score(struct winsize ws)
|
||||||
{
|
{
|
||||||
int bar_flash = clamp(score_timer * 255, 0, 255);
|
int bar_flash = clamp(score_timer * 255, 0, 255);
|
||||||
set_color((struct color) {bar_flash, 255, bar_flash}, true);
|
set_color((struct color) {bar_flash, 255, bar_flash}, true);
|
||||||
|
|
||||||
int level_flash = clamp(level_timer * 255, 0, 255);
|
int level_flash = clamp(level_timer * 255, 0, 255);
|
||||||
set_color((struct color) {255, 255, 255 - level_flash}, false);
|
set_color((struct color) {level_flash, 128 + (level_flash / 2), level_flash}, false);
|
||||||
|
|
||||||
size_t level_len = level_symbol_len > 0 ? 6 + level_symbol_len + 1 : 0;
|
size_t level_len = level_symbol_len > 0 ? 6 + level_symbol_len + 1 : 0;
|
||||||
char level_display[level_len];
|
char level_display[level_len];
|
||||||
|
|
|
@ -4,5 +4,6 @@
|
||||||
void add_score(int s);
|
void add_score(int s);
|
||||||
int get_score();
|
int get_score();
|
||||||
int get_level();
|
int get_level();
|
||||||
|
void register_on_level_up(void (*callback)(int new_level));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/recharge/recharge.h
|
plugins/sword/sword.so: plugins/sword/sword.c plugins/game/game.h plugins/movement/movement.h plugins/inventory/inventory.h plugins/recharge/recharge.h plugins/loot/loot.h
|
||||||
cc -g -shared -fpic -o plugins/sword/sword.so plugins/sword/sword.c
|
cc -g -shared -fpic -o plugins/sword/sword.so plugins/sword/sword.c
|
||||||
|
|
||||||
PLUGINS := ${PLUGINS} plugins/sword/sword.so
|
PLUGINS := ${PLUGINS} plugins/sword/sword.so
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "../movement/movement.h"
|
#include "../movement/movement.h"
|
||||||
#include "../inventory/inventory.h"
|
#include "../inventory/inventory.h"
|
||||||
#include "../recharge/recharge.h"
|
#include "../recharge/recharge.h"
|
||||||
|
#include "../loot/loot.h"
|
||||||
|
|
||||||
static bool use_broken_sword(struct itemstack *stack)
|
static bool use_broken_sword(struct itemstack *stack)
|
||||||
{
|
{
|
||||||
|
@ -73,4 +74,11 @@ __attribute__((constructor)) static void init()
|
||||||
.run_if_dead = false,
|
.run_if_dead = false,
|
||||||
.callback = &handle_e,
|
.callback = &handle_e,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
register_loot((struct loot) {
|
||||||
|
.chance = 7,
|
||||||
|
.item = &sword,
|
||||||
|
.min = 1,
|
||||||
|
.max = 1,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue