Hogwarts Wand Docs: ../server/calc.c

Wand Sourcecode: ../server/calc.c

//
// calc.c - calculate c_wpic*.txt from wpic*.txt
//
// Copyright (C) 2006  Nathan (Acorn) Pooley
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// 
// 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 (gpl.txt) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
// 
// You can contact the author, Nathan (Acorn) Pooley, by writing
// to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
// or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
//

//
// OBSOLETE! - check out fmtb_parse.c
//

//#@DOC@ OBSOLETE utility for readin & manipulating wpic*.txt files (spell graphs)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define CNT_MAX 1024

int calc(
        FILE *out,
        float *ti, 
        float *xi, 
        float *yi, 
        int cnti)
{
    static float scale_a = 1.0;
    float max_sum = 0.0;
    float lt = ti[0];
    float lx = xi[0];
    float ly = yi[0];
    int   la = -1;
    float   lda = 0.0;
    float   sum = 0.0;
    int c=0;
    int i;
    for (i=0; i<cnti; i++) {
        float t = ti[i];
        float x = xi[i];
        float y = yi[i];
        //float dt = t-lt;
        //float dx = x-lx;
        //float dy = y-ly;
        float a = -1;
        float da;
        int brk = 0;

        if (x < 3.5) {
            a = 0;
            da = x-0.0;
        } else if (x < 11.5) {
            a = 1;
            da = x-8.0;
        } else if (x < 19.5) {
            a = 2;
            da = x-16.0;
        } else if (x < 27.5) {
            a = 3;
            da = x-24.0;
        } else if (x < 34.5) {
            a = 0;
            da = x-32.0;
        } else {
            a = -1;
            da = 0;
        }


        if (a == -1) {
            brk = 1;
        } else if (a == la) {
            float m = 0;
            float lm = 0;
            if (da*da < 9) {
                m = 4- (da<0?-da:da);
            }
            if (lda*lda < 9) {
                lm = 4- (da<0?-da:da);
            }
            sum += (m + lm) * y;
        } else {
            sum = 0;
        }


        max_sum = sum>max_sum ? sum : max_sum;


        if (out) {
            if (la != -1) {
                fprintf(out,"%12.5f %12.5f %12.5f %12.5f %12.5f\n",
                    t,
                    x,y,
                    la * scale_a,
                    sum);
            }
            if (brk) {
                fprintf(out,"B2\n");
                fprintf(out,"B3\n");
            }
            fprintf(out,"%12.5f %12.5f %12.5f %12.5f %12.5f\n",
                t,
                x,y,
                a * scale_a,
                sum);
            if (brk) {
                fprintf(out,"B2\n");
                fprintf(out,"B3\n");
            }
        }

        
        lda = da;
        la = a;
        lt = t;
        lx = x;
        ly = y;
    }

    scale_a = max_sum / 4;

    return c;
}

int getline(FILE *fp, float *t, float *x, float *y)
{
    int c;

    while(1) {
        c = getc(fp);
        while(isspace(c)) {
            c = getc(fp);
        }
        if (c=='#') {
            while(c!='\n' && c!=EOF) {
                c = getc(fp);
            }
            continue;
        }
        if (c==EOF) return 0;
        ungetc(c,fp);
        fscanf(fp,"%f %f %f",t,x,y);
        return 1;
    }

}

int get(FILE *in, float *t, float *x, float *y)
{
    int i;
    for(i=0; i<CNT_MAX && getline(in,t+i,x+i,y+i); i++);
    return i;
}

#if 0
void put(FILE *out, float *t, float *x, float *y, int cnt)
{
    int i;
    for (i=0; i<cnt; i++) {
        fprintf(out,"%12.5f %12.5f %12.5f\n",
            t[i],x[i],y[i]);
    }
}
#endif

int main(int argc, char *argv[])
{
    float ti[CNT_MAX], xi[CNT_MAX],yi[CNT_MAX];
    //float to[CNT_MAX], xo[CNT_MAX],yo[CNT_MAX];
    int cnti=0,cnto=0;
    cnti = get(stdin,ti,xi,yi);
    if (cnti) {
        cnto = calc(0,ti,xi,yi,cnti);
        cnto = calc(stdout,ti,xi,yi,cnti);
    }
    return 0;
}

This file Copyright (C) 2006 by Nathan (Acorn) Pooley
Go to TOP Wand page
Go to Acorn's personal webpage
Go to Hogwarts website: www.gotohogwarts.com
Snout: www.snout.org/game
Gadgets of Justice Unlimited
Snout GC (Wiki)
Snout Wiki
File created by do_doc at Wed May 30 03:25:06 PDT 2007