PDA

View Full Version : Loot Calculator



Gralamin
2009-11-01, 12:02 AM
Some people have been asking for this. So, I threw together some C code in a more logical fashion. This has been built for linux, but it should be pretty simple to turn it into windows compatible (Probably just copy the .h and .c into a compiler).

This was also written quickly, and hasn't been bugtested since I'm currently upgrading my Linux Distribution. It also requires two command line arguments (Two different files). I wrote two (http://gralamin.blogspot.com/2009/10/designing-loot-calculator.html) Blog posts on this (http://gralamin.blogspot.com/2009/10/designing-loot-calculator-part-2.htmll) for other coders.

The base code is made for 4e, but its pretty simple to turn into 3.5 (I in fact have left ways to do so in the code).

This requires two input files, entered into the program as command line arguments. input1 should look like:
input1
GP: X
Art: Y
Items: I
Level A - Player Z
Level B - Player Y
Level C - Sell
etc.

input2 should look like:
Input2
Players: n
Name-1
Name-2
...
Name-n.

And here are necessary files:
Makefile
#
# Creates loot executable. 'make clean' will rm all object files,
# the executable and a core file.
#
loot: loot.o
gcc -Wall -std=c99 -o loot loot.o -lm

loot.o: loot.c loot.h
gcc -Wall -std=c99 -c loot.c

clean:
-rm -f *.o loot core

Loot.h
/* Contains macros, structures, and functions related to loot.c */
#ifndef LOOT_H
#define LOOT_H

/* The expected number of command line arguments. */
#define NUM_ARGS 2

/* The Maximum length for an input line. */
#define LINE_LEN 256

/* The maximum length of a character name. */
#define NAME_MAX 128

/* Constant used to adjust item prices. 0.5 for 3.5, 0.2 for 4e.*/
#define SELL_CONSTANT 0.2

/* Structures */

/* Structure definition for player */
typdef struct {
char name[NAME_MAX];
double goldPieces;
}Player;

/* Function declarations for loot.c functions.
* See comments in loot.c for more information.
*/

/* Reads data from input, sends to output. */
void readInput(FILE* inputData, FILE* inputPlayers);

/* Takes read data, outputs. */
void outputData(Player* playerList[]);

/* Gets the gold Equivalence of an item of a certain level (4e) */
int getPriceofItem(int level);


Loot.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "loot.h"

int main(int argc, char* argv[])
{
FILE* input1;
FILE* input2;

if (argc == NUM_ARGS +1)
{
input1 = fopen(argv[1], "r");
input2 = fopen(argv[2], "r");

if(input1 && input2 != NULL)
{
readInput(input1, input2);

if(fclose(input1) != EOF && fclose(input2) != EOF)
{
return EXIT_SUCCESS;
}
}
}
return EXIT_FAILURE;
}

/* Reads in values, creates players, assigns items to players, output */
void readInput(FILE* inputData, FILE* inputPlayers)
{
char line[LINE_LEN+1];
char command[LINE_LEN+1];
char command2[LINE_LEN+1];
double *goldIn;
double goldTotal = 0;
int players = 0;
int curPlayer = 0;
Player* playerList;
int *itemsIn;
int curItemLevel;

memset(line, 0, LINE_LEN+1);
memset(command, 0, LINE_LEN+1);
memset(command2, 0, LINE_LEN+1);

while (fgets(line, LINE_LEN+1, inputPlayers) != NULL)
{
if (sscanf(line, "%s %d", command, &players) == 2 &&
strncmp(command, "Players:", 8) == 0)
{
playerList = malloc(players*sizeof(Player));
for(int z = 0; z < players; z++)
{
playerList[z].name = strncpy(playerList[z].name, "");
playerList[z].goldPieces = 0.0;
}
}
if (sscanf(line, "%s", command) == 1 && players > 0)
{
playerList[curPlayer].name = strncpy(playerList[curPlayer].name, command, NAME_MAX);
curPlayer++;
}

memset(line, 0, LINE_LEN+1);
memset(command, 0, LINE_LEN+1);
memset(command2, 0, LINE_LEN+1);
}

while (fgets(line, LINE_LEN+1, inputData) != NULL)
{
if (sscanf(line, "%s %lf", command, goldIn) == 2 &&
strncmp(command, "GP:", 3) == 0)
{
goldTotal += goldIn;
}
if (sscanf(line, "%s %lf", command, goldIn) == 2 &&
strncmp(command, "Art:", 4) == 0)
{
goldTotal += goldIn;
}
if (sscanf(line, "%s %d", command, itemsIn) == 2 &&
strncmp(command, "Items:", 6) == 0)
{
// Not actually doing anything with this currently.
}
//The Following two commands are 4e commands.
if (sscanf(line, "%s %d - %s", command, itemsIn, command2) == 3 &&
strncmp(command, "Level", 5) == 0 && strncmp(command2, "Sell", 4) == 0)
{
goldTotal += getPriceofItem(*itemsIn) * SELL_CONSTANT;
}
if (sscanf(line, "%s %d - %s %d", command, itemsIn, command2, &curPlayer) == 4 &&
strncmp(command, "Level", 5) == 0 && strncmp(command2, "Player", 4) == 0)
{
playerList[curPlayer].goldPieces -= getPriceofItem(*itemsIn)* SELL_CONSTANT;
}
/* The following commands are 3.5 commands.
if (sscanf(line, "%d - %s", itemsIn, command2) == 2 &&
strncmp(command2, "Sell", 4) == 0)
{
goldTotal += *itemsIn * SELL_CONSTANT;
}
if (sscanf(line, "%d - %s %d", itemsIn, command2, &curPlayer) == 4 &&
strncmp(command, "Level", 5) == 0 && strncmp(command2, "Player", 4) == 0)
{
playerList[curPlayer].goldPieces -= *itemsIn * SELL_CONSTANT;
}
*/

memset(line, 0, LINE_LEN+1);
memset(command, 0, LINE_LEN+1);
memset(command2, 0, LINE_LEN+1);
}
goldTotal = goldTotal / players;
for(int i = 0; i < players; i++)
{
playerList[i].goldPieces+=goldTotal;
}

outputData(playerList);
}

/* Takes in a list of players, and outputs the data to stdout */
void outputData(Player* playerList)
{
int size = sizeof(playerList)/sizeof(Player);

for(int i = 0; i < size; i++)
{
printf("%s :\n GP %lf", playerList[i].name, playerList[i].goldPieces);
printf("\n\n");
}
}

/* Gets the price of an item of some level, and returns it */
int getPriceofItem(int level)
{
int basePrices[5] = {360, 520, 680, 840, 1000}
int allPrices[30];
allPrices[0] = basePrice[0];
allPrices[1] = basePrice[1];
allPrices[2] = basePrice[2];
allPrices[3] = basePrice[3];
allPrices[4] = basePrice[4];

for(int i = 5; i < 30; i++)
{
allPrices[i] = allPrices[i-5]*5;
}
return allPrices[level - 1];
}

I'm sure this has bugs, but I won't be able to test for a while, so if another coder in the playground wants to fix the code, they may go right ahead.

herrhauptmann
2009-11-01, 12:15 AM
Is there a way to put that on the TI-83 calculator?

Gralamin
2009-11-01, 12:16 AM
Is there a way to put that on the TI-83 calculator?

This C code? No. Could you do something similar for TI-83? Yes, though you will have some severe limitations because of your variable limits.