Skip to content

Instantly share code, notes, and snippets.

@jgarlick
Created October 17, 2013 16:25
Show Gist options
  • Save jgarlick/7027932 to your computer and use it in GitHub Desktop.
Save jgarlick/7027932 to your computer and use it in GitHub Desktop.
/*
* The Hive - Cricket Plugin
* Copyright (C) 1999-2001 James Garlick
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/** @file cricket.c
* The Cricket game (work in progress).
* @author James Garlick
*/
/*
.--off-side---------------------------------------. 1 1st slip
| l- cv cp 3m | 2 2nd slip
| p g | 3 3rd slip
| 4 | 4 4th slip
| m- 3 | 5 leg slip
| 2 | cv cover
| s 1 | p point
| bo->> ||-------||ba wk | g gully
| U||-------|| | f forward - short leg
| f 5 | s silly point
| fine | dl deep fine/square leg
| m+ | | 3m 3rd man
| | | l+ long on
| l+ mw dl | l- long off
| sl / | mw mid-wicket
| U / | sl square leg
| square | m+ mid on
`--leg-side---------------------------------------' m- mid off
Bowling: GalaK Batting: Grimace cp cover-point
wk wicketkeeper
@ _ ,,
\\ _ @' ( )_ . _ \\
\\_( )_// / Y | . /--( )_//
| Y/-- /\ / . '// \~ \
|_/ _ / o" ( _\ / . - \
_ //\ | | | . \_\\\ . // \\--,
/_// / | | | . / \ \\\ . \\
/ // /_______|_|_|__________/_/_\ \_______________________________\\______
*/
/* Spinning info:
http://news.bbc.co.uk/sport/hi/english/static/in_depth/cricket/2001/spin_bowling/offbreak.stm
*/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "plugin_headers.h"
PLUGIN_NAME( "Cricket Game" )
PLUGIN_VERSION( "0.0.4" )
PLUGIN_DEPENDENCIES( "" )
PLUGIN_AUTHORS_LIST( "ofLights" )
#define PLAYER_HIGHLIGHT "^Y"
/*
"bo","wk","s","1","2","3","4","3m","g","p","cp","cv","m-","l-","l+","m+","mw","f","sl","dl","5"
*/
int positions[22][2] =
{
/* ba */ { 30, 14 },
/* bo */ { 9, 14 },
/* wk */ { 33, 14 },
/* s */ { 27, 12 },
/* 1 */ { 35, 12 },
/* 2 */ { 35, 10 },
/* 3 */ { 34, 8 },
/* 4 */ { 33, 6 },
/* 3m */ { 42, 2 },
/* g */ { 30, 4 },
/* p */ { 27, 4 },
/* cp */ { 23, 2 },
/* cv */ { 15, 2 },
/* m- */ { 14, 8 },
/* m+ */ { 14, 22 },
/* l- */ { 1, 2 },
/* l+ */ { 1, 26 },
/* mw */ { 22, 26 },
/* f */ { 27, 18 },
/* sl */ { 27, 28 },
/* dl */ { 43, 26 },
/* 5 */ { 32, 18 }
};
char position_names[22][3] =
{ "ba","bo","wk","s","1","2","3","4","3m","g","p","cp","cv","m-","m+","l-","l+","mw","f","sl","dl","5" };
char position_lnames[22][21] =
{ "batting", // 0
"bowling", // 1
"wicketkeeper", // 2
"silly point", // 3
"1st slip", // 4
"2nd slip", // 5
"3rd slip", // 6
"4th slip", // 7
"3rd man", // 8
"gully", // 9
"point", // 10
"cover-point", // 11
"cover", // 12
"mid off", // 13
"mid on", // 14
"long off", // 15
"long on", // 16
"mid-wicket", // 17
"forward - short leg", // 18
"square leg", // 19
"deep fine/square leg", // 20
"leg slip" // 21
};
char fieldmap[20][100] =
{ "^G.--^Roff-side^G---------------------------------------. %s wk wicketkeeper\n",
"^G|^n %sl- %scv %scp %s3m ^G| %s s silly point\n",
"^G|^n %sp %sg ^G| %s 1 1st slip\n",
"^G|^n %s4 ^G| %s 2 2nd slip\n",
"^G|^n %sm- %s3 ^G| %s 3 3rd slip\n",
"^G|^n %s2 ^G| %s 4 4th slip\n",
"^G|^n %ss %s1 ^G| %s 3m 3rd man\n",
"^G|^n %sbo^n->> ||-------||%sba %swk ^G| %s g gully\n",
"^G|^n U||-------|| ^G| %s p point\n",
"^G|^n %sf %s5 ^G| %s cp cover-point\n",
"^G|^n fine ^G| %s cv cover\n",
"^G|^n %sm+ | ^G| %s m- mid off\n",
"^G|^n | ^G| %s m+ mid on\n",
"^G|^n %sl+ %smw %sdl ^G| %s l- long off\n",
"^G|^n %ssl / ^G| %s l+ long on\n",
"^G|^n U / ^G| %s mw mid-wicket\n",
"^G|^n square ^G| %s f forward - short leg\n",
"^G`--^Rleg-side^G---------------------------------------' %s sl square leg\n",
" Batting: ^L%-15.15s^n Bowling: ^L%-15.15s^n %s dl deep fine/square leg\n",
" %-24.24s %-24.24s %s 5 leg slip\n^n"
};
#define BALL_RUBBED ( 1 << 0 )
#define BALL_PICKED ( 1 << 1 )
typedef struct cplayer_s
{
time_t arrival;
int destination;
ulist_t *ul;
} cplayer_t;
struct cricket_s
{
int batting;
int runs; /* number of runs scored */
int bowling;
int balls; /* balls bowled this over */
int fielders[20];
int ballflags;
cplayer_t *players;
ulist_t *users;
};
struct cricket_s *cricket;
void cricketc( user_t *u, int argc, char *argv[ ] )
{
string_t *buf;
int i;
char sballs[30];
char sruns[30];
buf = string_alloc( "" );
for( i = 0; i < 20; i++ )
{
switch( i )
{
case 1 : string_append( buf, fieldmap[i],
( cricket -> fielders[13] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[10] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[9] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[6] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 2 : string_append( buf, fieldmap[i],
( cricket -> fielders[8] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[7] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 3 : string_append( buf, fieldmap[i],
( cricket -> fielders[5] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 4 : string_append( buf, fieldmap[i],
( cricket -> fielders[11] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[4] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 5 : string_append( buf, fieldmap[i],
( cricket -> fielders[3] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 6 : string_append( buf, fieldmap[i],
( cricket -> fielders[1] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[2] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 7 : string_append( buf, fieldmap[i],
( cricket -> bowling ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> batting ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[0] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 9 : string_append( buf, fieldmap[i],
( cricket -> fielders[16] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[19] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 11 : string_append( buf, fieldmap[i],
( cricket -> fielders[12] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 13 : string_append( buf, fieldmap[i],
( cricket -> fielders[14] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[15] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[18] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 14 : string_append( buf, fieldmap[i],
( cricket -> fielders[17] ) ? PLAYER_HIGHLIGHT : "^n",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 18 : string_append( buf, fieldmap[i],
( cricket -> batting ) ? user_get_name( cricket -> batting ) : "No one",
( cricket -> bowling ) ? user_get_name( cricket -> bowling ) : "No one",
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
case 19 : sprintf( sruns, "(%d runs)", cricket -> runs );
sprintf( sballs, "(ball %d of 6)", cricket -> balls );
string_append( buf, fieldmap[i],
sruns,
sballs,
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
default : string_append( buf, fieldmap[i],
( cricket -> fielders[i] ) ? "^L>" : "^n "
);
break;
}
}
command_output( buf -> data );
string_free( buf );
}
int find_position( char *str )
{
int i;
for( i = 0; i < 22; i++ )
if( !strcasecmp( str, position_names[i] ) )
return i;
return -1;
}
char *str_position( int userid )
{
int i;
if( cricket -> batting == userid )
return position_lnames[0];
else if( cricket -> bowling == userid )
return position_lnames[1];
else
{
for( i = 0; i < 20; i++ )
if( cricket -> fielders[i] == userid )
return position_lnames[i+2];
}
return NULL;
}
void cricket_distance( user_t *u, int argc, char *argv[] )
{
int pos, cur;
double dx, dy, dsec;
if( argc < 2 )
{
command_output( "Format: %s <position name>\n", argv[0] );
return;
}
pos = find_position( argv[1] );
if( pos == -1 )
{
command_output( "Unknown position \'%s\', (see \'cricket\' for position names)\n", argv[0] );
return;
}
cur = 0;
dx = ( double ) abs( positions[pos][0] - positions[cur][0] );
dy = ( double ) abs( positions[pos][1] - positions[cur][1] );
dsec = sqrt( ( dx * dx ) + ( dy * dy ) );
command_output( "You are currently at %s [%dx%dy]\n%s is situated at [%dx%dy]\nThat is %dx and %dy\nDistance = %g\n",
position_names[cur], positions[cur][0], positions[cur][1],
position_names[pos], positions[pos][0], positions[pos][1],
positions[pos][0] - positions[cur][0],
positions[pos][1] - positions[cur][1],
dsec
);
}
void cricket_rub( user_t *u, int argc, char *argv[] )
{
if( cricket -> bowling != u -> userid )
{
command_output( "You can not rub the ball unless you are bowling.\n" );
return;
}
if( cricket -> ballflags & BALL_RUBBED )
{
ulist_output( CPUBLIC, u, cricket -> users,
"^Y|||^n %s is caught rubbing his balls!\n",
u -> current_name
);
}
else
{
cricket -> ballflags |= BALL_RUBBED;
command_output( "^Ro^n You rub the ball for smoother flight.\n" );
}
}
void cricket_pick( user_t *u, int argc, char *argv[] )
{
if( cricket -> bowling != u -> userid )
{
command_output( "You can not pick the ball unless you are bowling.\n" );
return;
}
if( cricket -> ballflags & BALL_PICKED )
{
ulist_output( CPUBLIC, u, cricket -> users,
"^Y|||^n %s has ruined the ball.\n",
u -> current_name
);
}
else
{
cricket -> ballflags |= BALL_PICKED;
command_output( "^Ro^n You pick the stiches of the ball for some reason.\n" );
}
}
void cricket_swing( user_t *u, int argc, char *argv[] )
{
if( cricket -> batting != u -> userid )
{
command_output( "You can not swing the bat unless you are batting.\n" );
return;
}
ulist_output( CPUBLIC, u, cricket -> users,
"^Y|||^n %s swings the bat menacingly.\n",
u -> current_name
);
}
void cricket_join( user_t *u, int argc, char *argv[] )
{
int l;
if( ulist_find_by_id( cricket -> users, u -> userid ) )
{
command_output( "You are already playing cricket.\n" );
return;
}
l = ulist_count( cricket -> users );
if( l < 22 )
{
cricket -> users = ulist_add( cricket -> users, u );
if( l == 0 )
cricket -> batting = u -> userid;
else if( l == 1 )
cricket -> bowling = u -> userid;
else
{
l = 0;
while( cricket -> fielders[l] )
l++;
cricket -> fielders[l] = u -> userid;
}
ulist_output( CPUBLIC, NULL, cricket -> users,
"^Y|||^n %s joins the cricket game (%s).\n^n",
u -> current_name,
str_position( u -> userid )
);
command_output( "(Messages starting with ^Y|||^n are sent to everyone playing.)\n" );
}
else
{
/* TODO - look for idle players to replace */
command_output( "The cricket game is full.\n" );
}
}
void cricket_who( user_t *u, int argc, char *argv[] )
{
string_t *buf;
ulist_t *mover;
int user_count;
if( cricket -> users == NULL )
{
command_output( "No one is playing cricket.\n" );
return;
}
buf = string_alloc( "" );
string_append_title( buf, u, FORMAT_LEFT | FORMAT_OTHER, "Cricket" );
user_count = 0;
mover = cricket -> users;
while( mover )
{
string_append( buf, " %-15.15s %-23.23s (%s inactive)\n",
mover -> u -> current_name,
str_position( mover -> u -> userid ),
string_time_short( inactive_time( mover -> u ) )
);
user_count++;
mover = mover -> next;
}
string_append_title( buf, u, FORMAT_LEFT | FORMAT_OTHER, "%d user%s playing",
user_count, ( user_count > 1 ) ? "s" : "" );
command_output( "%s", buf -> data );
string_free( buf );
}
/*
void tick_cricket( )
{
debug( 5, "timer callback in cricket.sub" );
}
*/
void PLUGIN_INIT( plugin_t *plugin )
{
cricket = ALLOC( 1, struct cricket_s );
if( cricket == NULL )
{
debug( 3, "Unable to allocate cricket_s in cricket plugin!" );
return;
}
// tcallback_register( plugin, tick_cricket, 1 );
/*
add_command( "cricket", cricketc, plugin, CMD_GAMES, 1 );
add_subcommand( "cricket", "join", cricket_join, plugin, CMD_GAMES, 0 );
add_subcommand( "cricket", "distance", cricket_distance, plugin, CMD_GAMES, 0 );
add_subcommand( "cricket", "rub", cricket_rub, plugin, CMD_GAMES, 0 );
add_subcommand( "cricket", "pick", cricket_pick, plugin, CMD_GAMES, 0 );
add_subcommand( "cricket", "swing", cricket_swing, plugin, CMD_GAMES, 0 );
add_subcommand( "cricket", "who", cricket_who, plugin, CMD_GAMES, 0 );
*/
command_add( "cricket", cricketc, 1, CMD_GAMES);
command_add( "cricket join", cricket_join, 0, CMD_GAMES);
command_add( "cricket distance", cricket_distance, 1, CMD_GAMES);
command_add( "cricket rub", cricket_rub, 0, CMD_GAMES);
command_add( "cricket pick", cricket_pick, 0, CMD_GAMES);
command_add( "cricket swing", cricket_swing, 0, CMD_GAMES);
command_add( "cricket who", cricket_who, 0, CMD_GAMES);
}
void PLUGIN_FINI( plugin_t *plugin )
{
if( cricket )
{
if( cricket -> users )
ulist_free( cricket -> users );
FREE( cricket );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment