feat: added legacy code
This commit is contained in:
commit
6dc00a542d
53
attrib.c
Normal file
53
attrib.c
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#include "string.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "ctype.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "futil.h"
|
||||||
|
|
||||||
|
char* g_attribs=(char*)0;
|
||||||
|
const char** g_attrib_index=(const char**)0;
|
||||||
|
int g_attribs_count=0;
|
||||||
|
int g_attribs_len=0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* load and index an attributes file
|
||||||
|
*/
|
||||||
|
int load_attrib_file(const char* file) {
|
||||||
|
FILE* stream=fopen(file,"r");
|
||||||
|
g_attribs_len=file_len(stream);
|
||||||
|
g_attribs_count=fcountc(stream,',')+1;
|
||||||
|
g_attribs=calloc(g_attribs_len,sizeof(char)+1);
|
||||||
|
g_attrib_index=calloc(sizeof(char*),g_attribs_count);
|
||||||
|
const char** ind_writer = g_attrib_index;
|
||||||
|
int n='\0';
|
||||||
|
char* writer=g_attribs;
|
||||||
|
*ind_writer=writer;
|
||||||
|
++ind_writer;
|
||||||
|
while((n=fgetc(stream))!=EOF) {
|
||||||
|
char c=(char)n;
|
||||||
|
printf("%c",c);
|
||||||
|
switch(c) {
|
||||||
|
case ',':
|
||||||
|
*writer='\0';
|
||||||
|
if(isspace((char)fpeek(stream)))
|
||||||
|
*ind_writer=writer+2;
|
||||||
|
else
|
||||||
|
*ind_writer=writer+1;
|
||||||
|
++ind_writer;
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
case '\n':
|
||||||
|
*writer=' ';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
*writer=c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++writer;
|
||||||
|
}
|
||||||
|
*writer='\0';
|
||||||
|
for(int i=0;i<g_attribs_count;++i) {
|
||||||
|
printf("%d) %s\n",i+1,g_attrib_index[i]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
11
attrib.h
Normal file
11
attrib.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef _attrib_h
|
||||||
|
#define _attrib_h
|
||||||
|
|
||||||
|
extern char* g_attribs;
|
||||||
|
extern int g_attribs_count;
|
||||||
|
extern int g_attribs_len;
|
||||||
|
|
||||||
|
int load_attribs(const char*);
|
||||||
|
int load_attrib_file(const char*);
|
||||||
|
|
||||||
|
#endif /* _attrib_h */
|
2
compile.sh
Executable file
2
compile.sh
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/bash
|
||||||
|
gcc *.c -o dice --std=c99 -g
|
59
dice.c
Normal file
59
dice.c
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
int rolld(int d) {
|
||||||
|
return rand() % d + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rollnd(int n, int d, int* i) {
|
||||||
|
int total=0;
|
||||||
|
while(n-->0) {
|
||||||
|
int roll = rolld(d);
|
||||||
|
total += roll;
|
||||||
|
if(i!=(void*)0)
|
||||||
|
*(i++) = roll;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
int process_dice_format(const char* format,int* num,int* die) {
|
||||||
|
int dummy; // dummy int for if either of the out adresses is 0
|
||||||
|
if(num==(void*)0)num=&dummy;
|
||||||
|
if(die==(void*)0)die=&dummy;
|
||||||
|
char buf[10];
|
||||||
|
memset(buf,0,10); // clear buffer
|
||||||
|
char* bufw=buf;
|
||||||
|
const char* next=format;
|
||||||
|
while(*next!='\0') {
|
||||||
|
if(*next=='d') { // argument is d in <num>d<die> format
|
||||||
|
if(next==format) // if there is no die count given, assume 1
|
||||||
|
*num=1;
|
||||||
|
else
|
||||||
|
*num=atoi(buf); // store buffered string as number
|
||||||
|
memset(buf,0,10); // clear buffer
|
||||||
|
bufw=buf; // reset buffer walker
|
||||||
|
next++; // skip
|
||||||
|
}
|
||||||
|
*bufw=*next; // buffer next character
|
||||||
|
bufw++;next++; // increment walker and next character
|
||||||
|
}
|
||||||
|
*die=atoi(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void roll_and_print(int n, int d) {
|
||||||
|
int* out_rolls=malloc(sizeof(int)*n);
|
||||||
|
int result=rollnd(n,d,out_rolls);
|
||||||
|
printf(" %d (%dd%d) ",result,n,d);
|
||||||
|
// only print inbetween steps if there are any
|
||||||
|
if(n>1) {
|
||||||
|
for(int i=0;i<n;++i) {
|
||||||
|
printf("%d%s",out_rolls[i],i==n-1?"\n":"+");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
free(out_rolls);
|
||||||
|
}
|
9
dice.h
Normal file
9
dice.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef _dice_h
|
||||||
|
#define _dice_h
|
||||||
|
|
||||||
|
extern int rolld(int d);
|
||||||
|
extern int rollnd(int n,int d,int* i);
|
||||||
|
extern int process_dice_format(const char* format,int* num,int* die);
|
||||||
|
extern void roll_and_print(int n, int d);
|
||||||
|
|
||||||
|
#endif /* _dice_h */
|
28
flags.c
Normal file
28
flags.c
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#include "memory.h"
|
||||||
|
#include "ctype.h"
|
||||||
|
#include "dice.h"
|
||||||
|
#include "attrib.h"
|
||||||
|
|
||||||
|
int f_interactive=0;
|
||||||
|
int f_num=0;
|
||||||
|
int f_die=0;
|
||||||
|
int g_argc=0;
|
||||||
|
const char** g_argv=(void*)0;
|
||||||
|
|
||||||
|
int process_arg(int argind) {
|
||||||
|
if(argind>=g_argc)return 0;
|
||||||
|
const char* arg=g_argv[argind];
|
||||||
|
char buf[10];
|
||||||
|
memset(buf,0,10);
|
||||||
|
if(strcmp(arg,"-i")==0) {
|
||||||
|
f_interactive=1;
|
||||||
|
return process_arg(argind+1);
|
||||||
|
} else if(strcmp(arg, "-a")==0) {
|
||||||
|
load_attrib_file(g_argv[argind+1]);
|
||||||
|
return process_arg(argind+2);
|
||||||
|
} else if(isdigit(arg[0])||arg[0]=='d') {
|
||||||
|
process_dice_format(arg,&f_num,&f_die);
|
||||||
|
return process_arg(argind+1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
12
flags.h
Normal file
12
flags.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef _flags_h
|
||||||
|
#define _flags_h
|
||||||
|
|
||||||
|
extern int f_interactive;
|
||||||
|
extern int f_num;
|
||||||
|
extern int f_die;
|
||||||
|
extern int g_argc;
|
||||||
|
extern const char** g_argv;
|
||||||
|
|
||||||
|
extern int process_arg(int argind);
|
||||||
|
|
||||||
|
#endif /* _flags_h */
|
32
futil.c
Normal file
32
futil.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
long file_len(FILE* file) {
|
||||||
|
fpos_t pos;
|
||||||
|
fgetpos(file,&pos);
|
||||||
|
fseek(file,0,SEEK_END);
|
||||||
|
long len=ftell(file);
|
||||||
|
fsetpos(file,&pos);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
long fcountc(FILE* file, char c) {
|
||||||
|
int i = 0;
|
||||||
|
long t = 0;
|
||||||
|
fpos_t strt;
|
||||||
|
fgetpos(file,&strt);
|
||||||
|
while((i=fgetc(file))!=EOF) {
|
||||||
|
if((char)i==c)
|
||||||
|
++t;
|
||||||
|
}
|
||||||
|
fsetpos(file, &strt);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fpeek(FILE* file) {
|
||||||
|
fpos_t p;
|
||||||
|
fgetpos(file,&p);
|
||||||
|
int v = fgetc(file);
|
||||||
|
fsetpos(file,&p);
|
||||||
|
return v;
|
||||||
|
}
|
22
futil.h
Normal file
22
futil.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef _futils_h
|
||||||
|
#define _futils_h
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return the length of a text file
|
||||||
|
*/
|
||||||
|
long file_len(FILE* file);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* count the number of occurences of c in file
|
||||||
|
* starting from the current stream position
|
||||||
|
*/
|
||||||
|
long fcountc(FILE* file, char c);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* peek the next value of file
|
||||||
|
*/
|
||||||
|
int fpeek(FILE* file);
|
||||||
|
|
||||||
|
#endif /* _futils_h */
|
19
interactive.c
Normal file
19
interactive.c
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "dice.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "flags.h"
|
||||||
|
|
||||||
|
int run_interactive() {
|
||||||
|
int running = 1;
|
||||||
|
char buf[24];
|
||||||
|
while(running) {
|
||||||
|
printf("> ");
|
||||||
|
memset(buf,0,10);
|
||||||
|
scanf("%s",buf);
|
||||||
|
if(strcmp(buf,"quit")==0||strcmp(buf,"exit")==0||strcmp(buf,"q")==0) return 0;
|
||||||
|
process_dice_format(buf,&f_num,&f_die);
|
||||||
|
if(f_num!=0&&f_die!=0||buf[0]=='\0')
|
||||||
|
roll_and_print(f_num, f_die);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
6
interactive.h
Normal file
6
interactive.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef _interactive_h
|
||||||
|
#define _interactive_h
|
||||||
|
|
||||||
|
extern int run_interactive();
|
||||||
|
|
||||||
|
#endif /* _interactive_h */
|
19
main.c
Normal file
19
main.c
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#include "time.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "dice.h"
|
||||||
|
#include "flags.h"
|
||||||
|
#include "interactive.h"
|
||||||
|
|
||||||
|
int main(int argc,const char* argv[]) {
|
||||||
|
srand(time(NULL));
|
||||||
|
g_argv=argv;
|
||||||
|
g_argc=argc;
|
||||||
|
process_arg(1);
|
||||||
|
// if there are valid dice to roll
|
||||||
|
if(f_die>0&&f_num>0) {
|
||||||
|
roll_and_print(f_num,f_die);
|
||||||
|
}
|
||||||
|
if(f_interactive==1||argc==1) {
|
||||||
|
return run_interactive();
|
||||||
|
}
|
||||||
|
}
|
8
pc.c
Normal file
8
pc.c
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#include "pc.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
int new_character(pc_t* pc, const char* name) {
|
||||||
|
memset(pc,0,sizeof(pc_t));
|
||||||
|
strncpy(pc->name,name,64);
|
||||||
|
return 0;
|
||||||
|
}
|
13
pc.h
Normal file
13
pc.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef _pc_h
|
||||||
|
#define _pc_h
|
||||||
|
|
||||||
|
typedef struct pc_t {
|
||||||
|
char name[64];
|
||||||
|
int dexterity,strength,constitution,intelligence,wisdom;
|
||||||
|
const char* spells[64];
|
||||||
|
const char* traits[64];
|
||||||
|
} pc_t;
|
||||||
|
|
||||||
|
int new_character(pc_t* pc, const char* name);
|
||||||
|
|
||||||
|
#endif /* _pc_h */
|
Loading…
Reference in a new issue