Skip to content

Commit

Permalink
Merge pull request #44 from NosotrosNueces/faster-plants
Browse files Browse the repository at this point in the history
v0.2 Proper sample bot implementation
  • Loading branch information
JDongian committed Oct 19, 2014
2 parents 6f4d6dd + 1475bc8 commit 468d4f0
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 38 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ CC = clang
SA = scan-build
_LIB_FILES = marshal.c protocol.c bot.c client.c handlers.c api.c pipe.c
_TEST_FILES = packet_test.c protocol_test.c test_runner.c
_SAMPLE_FILES = bot_runner.c sample_defender.c
LIB_DIR = src
TEST_DIR = test
SAMPLE_DIR = sample
LIB_FILES= $(patsubst %,$(LIB_DIR)/%,$(_LIB_FILES))
TEST_FILES= $(patsubst %,$(TEST_DIR)/%,$(_TEST_FILES))
SAMPLE_FILES= $(patsubst %,$(SAMPLE_DIR)/%,$(_SAMPLE_FILES))
CFLAGS=-Wall --std=gnu99 -Wfatal-errors -lpthread -g
tests: bin
$(CC) -o bin/tests $(LIB_FILES) $(TEST_FILES) -I $(LIB_DIR) $(CFLAGS)
./bin/tests
sample: bin
$(CC) -o bin/sample_bot src/sample_bot.c $(LIB_FILES) $(CFLAGS)
build: bin
$(CC) -o bin/sample_defender $(SAMPLE_FILES) $(LIB_FILES) -I $(LIB_DIR) $(CFLAGS)
bin:
mkdir bin
check:
$(SA) make build
clean:
rm -f *.o core
rm -f bin/*
rebuild: clean build
23 changes: 23 additions & 0 deletions sample/bot_runner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <time.h>
#include "sample_defender.h"
#include "client.h"
#include "protocol.h"
#include "bot.h"
#include "api.h"

#define SERVER_NAME "10.10.2.16"
#define DEFAULT_SERVER_PORT 25565
#define NUM_BOTS 1

int main(int argc, char *argv[]) {
bot_t *bots[NUM_BOTS];

bots[0] = defender_init("plants");

client_run(bots, NUM_BOTS);
for (int i = 0; i < NUM_BOTS; i++) {
free_bot(bots[i]);
}
return 0;
}
100 changes: 100 additions & 0 deletions sample/sample_defender.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdio.h>
#include <time.h>
#include "bot.h"
#include "protocol.h"
#include "api.h"

#define SERVER_NAME "10.10.2.16"
#define DEFAULT_SERVER_PORT 25565

typedef struct entity_node {
vint32_t entity;
struct entity_node *next;
} entity_node_t;

entity_node_t targets;

typedef struct bot_globals {
entity_node_t targets;
} bot_globals_t;

void insert_target(bot_t *bot, vint32_t eid) {
pthread_mutex_lock(&bot->bot_mutex);
entity_node_t *p = &((bot_globals_t *)bot->item)->targets;
while (p->next) {
p = p->next;
}
p->entity = eid;
p->next = calloc(1, sizeof(entity_node_t));
pthread_mutex_unlock(&bot->bot_mutex);
}

void remove_target(bot_t *bot, vint32_t eid) {
pthread_mutex_lock(&bot->bot_mutex);
entity_node_t *p = &((bot_globals_t *)bot->item)->targets;
while (p->next) {
if (p->next->entity == eid) {
p->next = p->next->next;
}
p = p->next;
}
pthread_mutex_unlock(&bot->bot_mutex);
}

bool exists_target(bot_t *bot, vint32_t eid) {
bool ret = false;
pthread_mutex_lock(&bot->bot_mutex);
entity_node_t *p = &((bot_globals_t *)bot->item)->targets;
while (p->next) {
if (p->entity == eid) {
ret = true;
break;
}
p = p->next;
}
pthread_mutex_unlock(&bot->bot_mutex);
return ret;
}

void protect(bot_t *bot, vint32_t eid) {
send_play_serverbound_player_look(bot, 0, 0, true);
send_play_serverbound_entity_use(bot, eid, 1, 0, 0, 0);
}

void entity_handler(bot_t *bot, void *vp) {
play_clientbound_spawn_mob_t *p = (play_clientbound_spawn_mob_t *) vp;
if (p->type >= 50) insert_target(bot, p->entity_id);
}

void entity_move_handler(bot_t *bot, void *vp) {
play_clientbound_entity_move_t *p = (play_clientbound_entity_move_t *) vp;
if (exists_target(bot, p->entity_id)) protect(bot, p->entity_id);
}

void entity_status_handler(bot_t *bot, void *vp) {
play_clientbound_entity_status_t *p = (play_clientbound_entity_status_t *) vp;
if (p->status == 3) remove_target(bot, p->entity_id);
}

void defender_main(void *vbot) {
bot_t *bot = (bot_t *)vbot;

while(1) {
msleep(500);
send_play_serverbound_player_status(bot, 0);
}
}

bot_t *defender_init(char *name) {
bot_t *bot = init_bot(name, *defender_main);
bot->item = calloc(1, sizeof(bot_globals_t));

register_defaults(bot);
register_event(bot, PLAY, 0x0F, entity_handler);
register_event(bot, PLAY, 0x15, entity_move_handler);
register_event(bot, PLAY, 0x1A, entity_status_handler);

login(bot, SERVER_NAME, DEFAULT_SERVER_PORT);

return bot;
}
7 changes: 7 additions & 0 deletions sample/sample_defender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __SAMPLE_DEFENDER_H__
#define __SAMPLE_DEFENDER_H__
#include "bot.h"

bot_t *defender_init(char *name);

#endif /* __SAMPLE_DEFENDER_H__ */
16 changes: 7 additions & 9 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@
//bot_t *bot_net[MAX_BOTS];

uint32_t num_bots;
bot_t *bot_list;
bot_t **bot_list;
pthread_t *bot_threads;
pthread_t *receivers;
pthread_t *callbackers;
pipe_t **pipes;

void client_run(bot_t *, uint32_t);

void *receiver(void *index);
void *callbacker(void *index);
void *bot_thread(void *bot);

void client_run(bot_t *bots, uint32_t num) {
void client_run(bot_t **bots, uint32_t num) {
// create 1 thread for receiving packets, and 1 for each bot
uint64_t i;
num_bots = num;
Expand All @@ -55,7 +53,7 @@ void client_run(bot_t *bots, uint32_t num) {
// create all the bot threads
bot_threads = calloc(num, sizeof (pthread_t));
for(i = 0; i < num; i++)
pthread_create(bot_threads + i, NULL, bot_thread, bot_list + i);
pthread_create(bot_threads + i, NULL, bot_thread, bot_list[i]);

// wait for all threads to finish
// TODO: support for exit codes
Expand All @@ -79,8 +77,8 @@ void *bot_thread(void *bot) {
}

void *receiver(void *index) {
int i = (int) index;
bot_t *bot = &bot_list[i];
int i = (uint64_t) index;
bot_t *bot = bot_list[i];
int ready;
struct pollfd fds;
fds.fd = bot->_data->socketfd;
Expand All @@ -106,8 +104,8 @@ void *receiver(void *index) {
}

void *callbacker(void *index) {
int i = (int) index;
bot_t *bot = &bot_list[i];
int i = (uint64_t) index;
bot_t *bot = bot_list[i];

int bytes_read;
protocol_dummy_t *data;
Expand Down
2 changes: 1 addition & 1 deletion src/client.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "bot.h"

void client_run(bot_t *, uint32_t);
void client_run(bot_t **, uint32_t);

void *bot_thread(void *);

Expand Down
25 changes: 0 additions & 25 deletions src/sample_bot.c

This file was deleted.

0 comments on commit 468d4f0

Please sign in to comment.