New OOTS products from CafePress
New OOTS t-shirts, ornaments, mugs, bags, and more
Results 1 to 3 of 3

Thread: Loot Calculator

  1. - Top - End - #1
    Troll in the Playground
     
    Gralamin's Avatar

    Join Date
    Feb 2005

    Default Loot Calculator

    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 Blog posts on this 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:
    Spoiler
    Show
    input1
    Code:
    GP: X
    Art: Y
    Items: I
      Level A - Player Z
      Level B - Player Y
      Level C - Sell
      etc.


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


    And here are necessary files:
    Spoiler
    Show
    Makefile
    Code:
    #
    # 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


    Spoiler
    Show
    Loot.h
    Code:
    /* 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);


    Spoiler
    Show

    Loot.c
    Code:
    #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.
    Last edited by Gralamin; 2009-11-01 at 12:15 AM.

  2. - Top - End - #2
    Troll in the Playground
     
    herrhauptmann's Avatar

    Join Date
    Jun 2007

    Default Re: Loot Calculator

    Is there a way to put that on the TI-83 calculator?

  3. - Top - End - #3
    Troll in the Playground
     
    Gralamin's Avatar

    Join Date
    Feb 2005

    Default Re: Loot Calculator

    Quote Originally Posted by herrhauptmann View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •