Compare commits

...

7 Commits

  1. 2
      .gitignore
  2. 12
      Makefile
  3. 157
      character.c
  4. 16
      character.h
  5. 23
      chargen.c
  6. 8
      chargen.h

2
.gitignore vendored

@ -0,0 +1,2 @@
chargen
*.o

@ -1,9 +1,11 @@
CC=cc LIBS=
LIBS=-lm CFLAGS=-Wall -Wextra -Wno-missing-field-initializers -std=c99 -pedantic -O
server: all:
${CC} -o chargen ${LIBS} chargen.c
all: server generator chargen: chargen.o character.o
${CC} -o chargen ${LIBS} $^ $>
all: chargen
.PHONY: all .PHONY: all

@ -1,13 +1,29 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#include "chargen.h"
#include "character.h" #include "character.h"
unsigned long profs;
unsigned long languages;
char otherprofs[100];
char* background;
char* race;
char* class;
char* classes[] = {"Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard" };
char* races[] = { "Dragonborn", "Dwarf", "Elf", "Gnome", "Half-Elf", "Halfling", "Half-Orc", "Human", "Tiefling" };
char* backgrounds[] = { "Acolyte", "Charlatan", "Criminal", "Entertainer", "Folk Hero", "Guild Artisan", "Hermit", "Noble", "Outlander", "Sage", "Sailor", "Soldier", "Urchin" };
int stats[6];
int mods[6];
int hp;
int dieroll(int num,int sides){ int dieroll(int num,int sides){
int total = 0; int total = 0;
for(num;num>0;num--){ while (num > 0) {
total = total + arc4random_uniform(sides)+1; total = total + arc4random_uniform(sides)+1;
num--;
} }
return total; return total;
} }
@ -16,7 +32,7 @@ void addLanguage(long lang){
if((lang & languages ) == 0){ if((lang & languages ) == 0){
languages = languages + lang; languages = languages + lang;
}else{ }else{
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
} }
} }
@ -24,7 +40,7 @@ void addProf(long prof){
if((prof & profs) == 0){ if((prof & profs) == 0){
profs = profs + prof; profs = profs + prof;
}else{ }else{
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
} }
} }
@ -45,10 +61,9 @@ void classProfs(unsigned long* classprofs, int count, int num){
classprofs[i]=local; classprofs[i]=local;
} }
//Grab the first N elements, based on the class's value, but first, if we have more profs than available profs, get some random ones //Grab the first N elements, based on the class's value, but first, if we have more profs than available profs, get some random ones
if(num > count){ while (num > count){
for(count;num > count;count--){ addProf(1lu << arc4random_uniform(18));
addProf(exp2l(arc4random_uniform(18))); count--;
}
} }
for(int i=0;i<=num;i++){ for(int i=0;i<=num;i++){
addProf(classprofs[num]); addProf(classprofs[num]);
@ -65,9 +80,9 @@ void genClass(){
classProfs(barbskills, 6, 2); classProfs(barbskills, 6, 2);
break; break;
case BARD:; case BARD:;
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
strlcat(otherprofs, "Three Musical Instruments, ", sizeof(otherprofs)); strlcat(otherprofs, "Three Musical Instruments, ", sizeof(otherprofs));
hp = 8 + mods[CON]; hp = 8 + mods[CON];
break; break;
@ -160,7 +175,7 @@ void genRace(){
if(subrace == 0){ if(subrace == 0){
race = "High Elf"; race = "High Elf";
stats[INT]+=1; stats[INT]+=1;
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
}else if(subrace == 1){ }else if(subrace == 1){
race = "Wood Elf"; race = "Wood Elf";
stats[WIS]+=1; stats[WIS]+=1;
@ -186,9 +201,9 @@ void genRace(){
stats[CHA]+=2; stats[CHA]+=2;
stats[arc4random_uniform(4)]+=1; stats[arc4random_uniform(4)]+=1;
stats[arc4random_uniform(4)]+=1; stats[arc4random_uniform(4)]+=1;
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
addProf(exp2l(arc4random_uniform(18))); addProf(1lu << arc4random_uniform(18));
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
addLanguage(ELVISH); addLanguage(ELVISH);
break; break;
case HALFLING: case HALFLING:
@ -216,7 +231,7 @@ void genRace(){
stats[INT]+=1; stats[INT]+=1;
stats[WIS]+=1; stats[WIS]+=1;
stats[CHA]+=1; stats[CHA]+=1;
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
break; break;
case TIEFLING: case TIEFLING:
stats[INT]+=1; stats[INT]+=1;
@ -236,8 +251,8 @@ void genBackground(){
case ACOLYTE: case ACOLYTE:
addProf(INSIGHT); addProf(INSIGHT);
addProf(RELIGION); addProf(RELIGION);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
break; break;
case CHARLATAN: case CHARLATAN:
addProf(DECEPTION); addProf(DECEPTION);
@ -266,32 +281,32 @@ void genBackground(){
case GUILDARTISAN: case GUILDARTISAN:
addProf(INSIGHT); addProf(INSIGHT);
addProf(PERSUASION); addProf(PERSUASION);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
strlcat(otherprofs, "Artisan's Tools, ", sizeof(otherprofs)); strlcat(otherprofs, "Artisan's Tools, ", sizeof(otherprofs));
break; break;
case HERMIT: case HERMIT:
addProf(MEDICINE); addProf(MEDICINE);
addProf(RELIGION); addProf(RELIGION);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
strlcat(otherprofs, "Herbalism Kit, ", sizeof(otherprofs)); strlcat(otherprofs, "Herbalism Kit, ", sizeof(otherprofs));
break; break;
case NOBLE: case NOBLE:
addProf(HISTORY); addProf(HISTORY);
addProf(PERSUASION); addProf(PERSUASION);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
strlcat(otherprofs, "Gaming Set, ", sizeof(otherprofs)); strlcat(otherprofs, "Gaming Set, ", sizeof(otherprofs));
break; break;
case OUTLANDER: case OUTLANDER:
addProf(ATHLETICS); addProf(ATHLETICS);
addProf(SURVIVAL); addProf(SURVIVAL);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
strlcat(otherprofs, "Musical Instrument, ", sizeof(otherprofs)); strlcat(otherprofs, "Musical Instrument, ", sizeof(otherprofs));
break; break;
case SAGE: case SAGE:
addProf(ARCANA); addProf(ARCANA);
addProf(HISTORY); addProf(HISTORY);
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
addLanguage(exp2l(arc4random_uniform(15))); addLanguage(1lu << arc4random_uniform(15));
break; break;
case SAILOR: case SAILOR:
addProf(ATHLETICS); addProf(ATHLETICS);
@ -317,127 +332,125 @@ void genBackground(){
} }
} }
char * genChar(char* output){ size_t genChar(char* output, size_t maxlen){
int i;
profs = 0; profs = 0;
languages = 0; languages = 0;
for(int i = 0; i <= 100; i++){ otherprofs[0] = '\0';
otherprofs[i] = '\0'; for(i=0; i < 6; i++){
stats[i] = dieroll(3,6);
mods[i] = (stats[i]/2)-5;
} }
int i;
genRace(); genRace();
genClass(); genClass();
genBackground(); genBackground();
for(i=0; i < 6; i++){ snprintf(output, maxlen, "Race = %s\r\nBackground = %s\r\nClass = %s\r\nStr = %d %d\r\nDex = %d %d\r\nCon = %d %d\r\nInt = %d %d\r\nWis = %d %d\r\nCha = %d %d\r\n\r\nHP: %d\r\n", race, background, class, stats[STR], mods[STR], stats[DEX], mods[DEX], stats[CON], mods[CON], stats[INT], mods[INT], stats[WIS], mods[WIS], stats[CHA], mods[CHA], hp);
stats[i] = dieroll(3,6);
mods[i] = floor((stats[i]/2)-5);
}
snprintf(output, 512, "Race = %s\r\nBackground = %s\r\nClass = %s\r\nStr = %d %d\r\nDex = %d %d\r\nCon = %d %d\r\nInt = %d %d\r\nWis = %d %d\r\nCha = %d %d\r\n\r\nHP: %d\r\n", race, background, class, stats[STR], mods[STR], stats[DEX], mods[DEX], stats[CON], mods[CON], stats[INT], mods[INT], stats[WIS], mods[WIS], stats[CHA], mods[CHA], hp);
strlcat(output,"Proficiences:\r\n",512); strlcat(output,"Proficiences:\r\n",maxlen);
if((ATHLETICS & profs) != 0){ if((ATHLETICS & profs) != 0){
strlcat(output,"Athletics\r\n",512); strlcat(output,"Athletics\r\n",maxlen);
} }
if((ACROBATICS & profs) != 0){ if((ACROBATICS & profs) != 0){
strlcat(output,"Acrobatics\r\n",512); strlcat(output,"Acrobatics\r\n",maxlen);
} }
if((SLEIGHTOFHAND & profs) != 0){ if((SLEIGHTOFHAND & profs) != 0){
strlcat(output,"Sleight of Hand\r\n",512); strlcat(output,"Sleight of Hand\r\n",maxlen);
} }
if((STEALTH & profs) != 0){ if((STEALTH & profs) != 0){
strlcat(output,"Stealth\r\n",512); strlcat(output,"Stealth\r\n",maxlen);
} }
if((ARCANA & profs) != 0){ if((ARCANA & profs) != 0){
strlcat(output,"Arcana\r\n",512); strlcat(output,"Arcana\r\n",maxlen);
} }
if((HISTORY & profs) != 0){ if((HISTORY & profs) != 0){
strlcat(output,"History\r\n",512); strlcat(output,"History\r\n",maxlen);
} }
if((INVESTIGATION & profs) != 0){ if((INVESTIGATION & profs) != 0){
strlcat(output,"Investigation\r\n",512); strlcat(output,"Investigation\r\n",maxlen);
} }
if((NATURE & profs) != 0){ if((NATURE & profs) != 0){
strlcat(output,"Nature\r\n",512); strlcat(output,"Nature\r\n",maxlen);
} }
if((RELIGION & profs) != 0){ if((RELIGION & profs) != 0){
strlcat(output,"Regligion\r\n",512); strlcat(output,"Regligion\r\n",maxlen);
} }
if((ANIMALHANDLING & profs) != 0){ if((ANIMALHANDLING & profs) != 0){
strlcat(output,"Animal Handling\r\n",512); strlcat(output,"Animal Handling\r\n",maxlen);
} }
if((INSIGHT & profs) != 0){ if((INSIGHT & profs) != 0){
strlcat(output,"Insight\r\n",512); strlcat(output,"Insight\r\n",maxlen);
} }
if((MEDICINE & profs) != 0){ if((MEDICINE & profs) != 0){
strlcat(output,"Medicine\r\n",512); strlcat(output,"Medicine\r\n",maxlen);
} }
if((PERCEPTION & profs) != 0){ if((PERCEPTION & profs) != 0){
strlcat(output,"Perception\r\n",512); strlcat(output,"Perception\r\n",maxlen);
} }
if((SURVIVAL & profs) != 0){ if((SURVIVAL & profs) != 0){
strlcat(output,"Survival\r\n",512); strlcat(output,"Survival\r\n",maxlen);
} }
if((DECEPTION & profs) != 0){ if((DECEPTION & profs) != 0){
strlcat(output,"Deception\r\n",512); strlcat(output,"Deception\r\n",maxlen);
} }
if((INTIMIDATION & profs) != 0){ if((INTIMIDATION & profs) != 0){
strlcat(output,"Intimidation\r\n",512); strlcat(output,"Intimidation\r\n",maxlen);
} }
if((PERFORMANCE & profs) != 0){ if((PERFORMANCE & profs) != 0){
strlcat(output,"Performance\r\n",512); strlcat(output,"Performance\r\n",maxlen);
} }
if((PERSUASION & profs) != 0){ if((PERSUASION & profs) != 0){
strlcat(output,"Persuaion\r\n",512); strlcat(output,"Persuaion\r\n",maxlen);
} }
strlcat(output,"\r\nLanguages:\r\nCommon\r\n",512); strlcat(output,"\r\nLanguages:\r\nCommon\r\n",maxlen);
if((ABYSSAL & languages) != 0){ if((ABYSSAL & languages) != 0){
strlcat(output,"Abyssal\r\n",512); strlcat(output,"Abyssal\r\n",maxlen);
} }
if((CELESTIAL & languages) != 0){ if((CELESTIAL & languages) != 0){
strlcat(output,"Celestial\r\n",512); strlcat(output,"Celestial\r\n",maxlen);
} }
if((DRACONIC & languages) != 0){ if((DRACONIC & languages) != 0){
strlcat(output,"Draconic\r\n",512); strlcat(output,"Draconic\r\n",maxlen);
} }
if((DEEPSPEECH & languages) != 0){ if((DEEPSPEECH & languages) != 0){
strlcat(output,"Deep Speech\r\n",512); strlcat(output,"Deep Speech\r\n",maxlen);
} }
if((INFERNAL & languages) != 0){ if((INFERNAL & languages) != 0){
strlcat(output,"Infernal\r\n",512); strlcat(output,"Infernal\r\n",maxlen);
} }
if((PRIMORDIAL & languages) != 0){ if((PRIMORDIAL & languages) != 0){
strlcat(output,"Primordial\r\n",512); strlcat(output,"Primordial\r\n",maxlen);
} }
if((SYLVAN & languages) != 0){ if((SYLVAN & languages) != 0){
strlcat(output,"Sylvan\r\n",512); strlcat(output,"Sylvan\r\n",maxlen);
} }
if((UNDERCOMMON & languages) != 0){ if((UNDERCOMMON & languages) != 0){
strlcat(output,"Undercommon\r\n",512); strlcat(output,"Undercommon\r\n",maxlen);
} }
if((DWARVISH & languages) != 0){ if((DWARVISH & languages) != 0){
strlcat(output,"Dwarvish\r\n",512); strlcat(output,"Dwarvish\r\n",maxlen);
} }
if((ELVISH & languages) != 0){ if((ELVISH & languages) != 0){
strlcat(output,"Elvish\r\n",512); strlcat(output,"Elvish\r\n",maxlen);
} }
if((GIANT & languages) != 0){ if((GIANT & languages) != 0){
strlcat(output,"Giant\r\n",512); strlcat(output,"Giant\r\n",maxlen);
} }
if((GNOMISH & languages) != 0){ if((GNOMISH & languages) != 0){
strlcat(output,"Gnome\r\n",512); strlcat(output,"Gnome\r\n",maxlen);
} }
if((GOBLIN & languages) != 0){ if((GOBLIN & languages) != 0){
strlcat(output,"Goblin\r\n",512); strlcat(output,"Goblin\r\n",maxlen);
} }
if((HALFLISH & languages) != 0){ if((HALFLISH & languages) != 0){
strlcat(output,"Halfling\r\n",512); strlcat(output,"Halfling\r\n",maxlen);
} }
if((ORC & languages) != 0){ if((ORC & languages) != 0){
strlcat(output,"Orcish\r\n",512); strlcat(output,"Orcish\r\n",maxlen);
} }
strlcat(output,"\r\nOther Profs: ",512); strlcat(output,"\r\nOther Profs: ",maxlen);
strlcat(output,otherprofs,512); strlcat(output,otherprofs,maxlen);
strlcat(output,"\r\n",512); strlcat(output,"\r\n",maxlen);
return output; return strlen(output);
} }

@ -79,19 +79,3 @@
#define INT 3 #define INT 3
#define WIS 4 #define WIS 4
#define CHA 5 #define CHA 5
unsigned long profs;
unsigned long languages;
char otherprofs[100];
char* background;
char* race;
char* class;
char* classes[] = {"Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard" };
char* races[] = { "Dragonborn", "Dwarf", "Elf", "Gnome", "Half-Elf", "Halfling", "Half-Orc", "Human", "Tiefling" };
char* backgrounds[] = { "Acolyte", "Charlatan", "Criminal", "Entertainer", "Folk Hero", "Guild Artisan", "Hermit", "Noble", "Outlander", "Sage", "Sailor", "Soldier", "Urchin" };
int stats[6];
int mods[6];
int hp;
char* genChar();

@ -11,7 +11,12 @@
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include "character.c" #include "chargen.h"
enum {
TCP_MAXLEN = 1024,
UDP_MAXLEN = 512,
};
void usage_and_die(const char *myname, const char *fmt, ...) void usage_and_die(const char *myname, const char *fmt, ...)
{ {
@ -148,9 +153,6 @@ int main(int argc, char **argv)
if (num_packet + num_listen == 0) if (num_packet + num_listen == 0)
errx(EXIT_FAILURE, "No listeners to run on!\n"); errx(EXIT_FAILURE, "No listeners to run on!\n");
if (pledge("stdio inet error", NULL) == -1)
warn("pledge");
while(1) while(1)
{ {
fd_set r, w; fd_set r, w;
@ -249,9 +251,9 @@ int main(int argc, char **argv)
printf("Responding to packet from %s:%s...\n", hbuf, sbuf); printf("Responding to packet from %s:%s...\n", hbuf, sbuf);
//rval = sendto(s, "chargen data\r\n", 14, 0, sa, len); //rval = sendto(s, "chargen data\r\n", 14, 0, sa, len);
char character[512]; char character[UDP_MAXLEN];
genChar(character); size_t datalen = genChar(character, sizeof character);
rval = sendto(s, character, 512, 0, sa, len); rval = sendto(s, character, datalen, 0, sa, len);
if (rval == -1) if (rval == -1)
warn("sendto"); warn("sendto");
} }
@ -286,9 +288,10 @@ int main(int argc, char **argv)
printf("WTF? i=%u num_stream=%u stream_fds[i]=%d s=%d\n", i, num_stream, stream_fds[i], s); printf("WTF? i=%u num_stream=%u stream_fds[i]=%d s=%d\n", i, num_stream, stream_fds[i], s);
continue; continue;
} }
char character[512]; char character[TCP_MAXLEN];
genChar(character); size_t datalen = genChar(character, sizeof character);
if (write(s, character, 512) == -1) /* TODO: Detect short writes and save the rest of the buffer for later */
if (write(s, character, datalen) == -1)
{ {
warn("write"); warn("write");
printf("Closing stream connection\n"); printf("Closing stream connection\n");

@ -0,0 +1,8 @@
#ifndef H_CHARGEN
#define H_CHARGEN
#include <stddef.h>
size_t genChar(char *, size_t);
#endif
Loading…
Cancel
Save