Skip to content

Commit

Permalink
Merge branches 'master-cosmetic' and 'feature-bot-name' of git://repo…
Browse files Browse the repository at this point in the history
….or.cz/0verkill
  • Loading branch information
marex committed May 16, 2010
2 parents 7b2e8dd + b9baf7b commit 0f852cd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
103 changes: 56 additions & 47 deletions bot.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,42 +81,44 @@ unsigned char *screen2,*screen2_a;
#endif


int in_signal_handler=0;
int level_sprites_start;
int level_number=-1;
static int in_signal_handler=0;
static int level_sprites_start;
static int level_number=-1;

/* my health, armor, frags, deaths, ammo, ... and ID */
unsigned char health,armor;
unsigned int frags,deaths;
unsigned short ammo[ARMS];
unsigned char current_weapon;
unsigned char weapons;
int my_id;
static unsigned char health,armor;
static unsigned int frags,deaths;
static unsigned short ammo[ARMS];
static unsigned char current_weapon;
static unsigned char weapons;
static int my_id;
/* for use with the -n option */
static char *static_name=NULL;

/* connection with server */
int connected=0;
static int connected=0;

int console_ok=1;

/* networking */
int fd; /* socket */
struct sockaddr_in server; /* server address */
static struct sockaddr_in server; /* server address */

/* objects */
struct object_list objects;
static struct object_list objects;
struct object_list *last_obj;
struct it* hero;
static struct it* hero;

unsigned long_long game_start_offset; /* time difference between game start on this machine and on server */
static unsigned long_long game_start_offset; /* time difference between game start on this machine and on server */


struct /* keyboard status */
static struct /* keyboard status */
{
unsigned char right,left,jump,creep,speed,fire,weapon,down_ladder;
}keyboard_status;


char *names[N_NAMES]={
static char *names[N_NAMES]={
"Terminator",
"Jack The Ripper",
"Rambo",
Expand All @@ -137,11 +139,11 @@ char *names[N_NAMES]={
"Eraser"
};

int direction=0; /* 0=stop, 1=left, 2=right */
int const1,const2,const3,const4;
unsigned short port=DEFAULT_PORT;
char *host;
int priority;
static int direction=0; /* 0=stop, 1=left, 2=right */
static int const1,const2,const3,const4;
static unsigned short port=DEFAULT_PORT;
static char *host;
static int priority;
/* 0=nothing
* 1=kill player
* 2=find rifle
Expand All @@ -161,14 +163,14 @@ int priority;
#define can_see(a,b) (a<add_int(hero->x,CAN_SEE_X)&&a>sub_int(hero->x,CAN_SEE_X)&&b>sub_int(hero->y,CAN_SEE_Y)&&b<add_int(hero->y,CAN_SEE_Y))


int odds(int p)
static int odds(int p)
{
return (random()%1000)<p;
}


/* free all before exit */
void clear_memory(void)
static void clear_memory(void)
{
struct object_list *o;

Expand All @@ -181,7 +183,7 @@ void clear_memory(void)


/* shut down the client */
void shut_down(int a)
static void shut_down(int a)
{
if (a)
{
Expand All @@ -194,7 +196,7 @@ void shut_down(int a)


/* find address of server and fill the server address structure */
char * find_server(char *name,unsigned short port)
static char * find_server(char *name,unsigned short port)
{
struct hostent *h;

Expand All @@ -209,7 +211,7 @@ char * find_server(char *name,unsigned short port)


/* initialize socket */
char * init_socket(void)
static char * init_socket(void)
{
fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(fd<0)return "Can't get socket.\n";
Expand All @@ -220,7 +222,7 @@ char * init_socket(void)
#define MAX_COUNT 32

/* send quit request to server */
void send_quit(void)
static void send_quit(void)
{
char p;
fd_set rfds;
Expand All @@ -246,7 +248,7 @@ void send_quit(void)


/* initiate connection with server */
char * contact_server(int color, char *name)
static char * contact_server(int color, char *name)
{
static char packet[256];
int l=strlen(name)+1;
Expand All @@ -265,8 +267,11 @@ char * contact_server(int color, char *name)
packet[2]=VERSION_MAJOR;
packet[3]=VERSION_MINOR;
packet[4]=color;
if (l>MAX_NAME_LEN) {l=MAX_NAME_LEN; packet[5+MAX_NAME_LEN]='\0';}
memcpy(packet+5,name,l);

printf("Using name \"%s\".\n", packet+5);

send_packet(packet,l+5,(struct sockaddr*)(&server),my_id,0);


Expand Down Expand Up @@ -336,7 +341,7 @@ char * contact_server(int color, char *name)


/* I want to be born again */
void send_reenter_game(void)
static void send_reenter_game(void)
{
char packet;
packet=P_REENTER_GAME;
Expand All @@ -345,7 +350,7 @@ void send_reenter_game(void)


/* send chat message */
void send_message(char *msg)
static void send_message(char *msg)
{
static char packet[MAX_MESSAGE_LENGTH + 2];
int len;
Expand All @@ -359,7 +364,7 @@ void send_message(char *msg)


/* send end of game to server */
void send_keyboard(void)
static void send_keyboard(void)
{
char packet[3];
packet[0]=P_KEYBOARD;
Expand All @@ -375,7 +380,7 @@ void send_keyboard(void)
}


void reset_keyboard(void)
static void reset_keyboard(void)
{
keyboard_status.left=0;
keyboard_status.right=0;
Expand All @@ -388,7 +393,7 @@ void reset_keyboard(void)
}


void test_object(struct it *obj)
static void test_object(struct it *obj)
{
if (obj==hero)return;
if (!can_see(obj->x,obj->y))return;
Expand Down Expand Up @@ -491,7 +496,7 @@ void test_object(struct it *obj)


/* destroys all objects except hero (before level change) */
void clean_memory(void)
static void clean_memory(void)
{
struct object_list *o;

Expand All @@ -502,7 +507,7 @@ void clean_memory(void)
}


void change_level(void)
static void change_level(void)
{
char *LEVEL;
char txt[256];
Expand All @@ -523,7 +528,7 @@ void change_level(void)


/* recompute object positions */
void update_game(void)
static void update_game(void)
{
struct object_list *p;
int w,h;
Expand Down Expand Up @@ -640,7 +645,7 @@ void update_game(void)


/* returns number of read bytes */
int process_packet(char *packet,int l)
static int process_packet(char *packet,int l)
{
int a,n=l;

Expand Down Expand Up @@ -964,7 +969,7 @@ int process_packet(char *packet,int l)


/* read packet from socket */
void read_data(void)
static void read_data(void)
{
fd_set rfds;
struct timeval tv;
Expand All @@ -988,7 +993,7 @@ void read_data(void)


/* handle fatal signal (sigabrt, sigsegv, ...) */
void signal_handler(int signum)
static void signal_handler(int signum)
{

if (connected)send_quit();
Expand All @@ -1007,26 +1012,26 @@ void signal_handler(int signum)


/* print command line help */
void print_help(void)
static void print_help(void)
{
printf(
"0verkill bot"
".\n"
"(c)2000 Brainsoft\n"
"Usage: bot [-h] -a <server address> [-p <port>]"
"Usage: bot [-h] -a <server address> [-p <port>] [-n <name>]"
"\n"
);
}


void parse_command_line(int argc,char **argv)
static void parse_command_line(int argc,char **argv)
{
int a;
char *e;

while(1)
{
a=getopt(argc,argv,"hp:a:");
a=getopt(argc,argv,"hp:a:n:");
switch(a)
{
case EOF:
Expand All @@ -1044,6 +1049,10 @@ void parse_command_line(int argc,char **argv)
host=optarg;
break;

case 'n':
static_name=optarg;
break;

case 'p':
port=(unsigned short)strtoul(optarg,&e,10);
if (*e){ERROR("Error: Decimal number expected.\n");EXIT(1);}
Expand All @@ -1053,13 +1062,13 @@ void parse_command_line(int argc,char **argv)
}


char * select_name(void)
static char * select_name(void)
{
return names[(random())%N_NAMES];
return static_name? static_name:names[(random())%N_NAMES];
}


void where2go(void)
static void where2go(void)
{
if (!direction)
{
Expand All @@ -1076,7 +1085,7 @@ void where2go(void)
}


void action(void)
static void action(void)
{
if (hero->status & S_DEAD)
send_reenter_game(); /* respawn */
Expand Down
6 changes: 6 additions & 0 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ static void read_data(void)
send_packet(packet,2,(struct sockaddr*)(&client),0,last_player->member.id);
break;
}
if (strlen(packet+5) > MAX_NAME_LEN)
{
snprintf(txt,256,"Name too long, shortening it to %i characters\n",MAX_NAME_LEN);
message(txt,2);
packet[5+MAX_NAME_LEN]='\0';
}
if (is_playername_in_use(packet+5))
{
snprintf(txt,256,"Name \"%s\" already in use. Player refused.\n",packet+5);
Expand Down

0 comments on commit 0f852cd

Please sign in to comment.