and this is sort of what I'm thinking for the whole user variable system
#include "ship/ship.h"
class variable{
public:
virtual float value(ship*shipp=NULL) = 0;
};
class constant:public variable{
float val;
public:
constant(float in):val(in){};
float value(ship*shipp=NULL){return val;};
};
class operate:public variable{
protected:
variable *operand1, *operand2;
public:
virtual float value(ship*shipp=NULL) = 0;
operate(variable*op1, variable*op2){operand1 = op1; operand2 = op2;}
};
class add:public operate{
public:
add(variable*op1, variable*op2):operate(op1,op2){}
float value(ship*shipp=NULL){return operand1->value(shipp) + operand2->value(shipp);}
};
class sub:public operate{
public:
sub(variable*op1, variable*op2):operate(op1,op2){}
float value(ship*shipp=NULL){return operand1->value(shipp) - operand2->value(shipp);};
};
class mult:public operate{
public:
mult(variable*op1, variable*op2):operate(op1,op2){}
float value(ship*shipp=NULL){return operand1->value(shipp) * operand2->value(shipp);};
};
class div:public operate{
public:
div(variable*op1, variable*op2):operate(op1,op2){}
float value(ship*shipp=NULL){return operand1->value(shipp) / operand2->value(shipp);};
};
class expression:public variable{
int n_var; //number of variables in the expression
variable** var; //an array of pointers, each points to a diferent subclass
variable *head; //the head of the expression
variable* parse_next(char**);
public:
expression(char*);
~expression(){for(int i = 0;i float value(ship*shipp=NULL);
}
/*
variable* con = new constant(1.0f);
variable* op = new add(con,con);
*/
#include "graphics/materials/variables.h"
expression::expression(char* in_str):n_var(0),var(NULL),head(NULL){
while(*in_str){
}
}
variable* expression::parse_next(char**in_str){
//do some parseing
//get the string into pre-fix notation
char ship_idnt[] = "ship:";
char op_char[] = "+-*/"
if(!strcmp(in_str,ship_idnt)){
//it's a ship variable
}else{
//it's a constant, or an opporateor
if(strchr(op_char, **in_str)){
//it must be an operator
}else{
//it must be a constant
}
}
}
float expression::value(ship*shipp=NULL){
}
I need to make a class that takes a string, allocates enough variable pointers, allocates each pointer to the corect type, and while doing that passes the corect pointers to all operate type variables.
to set up the variable cascade corectly it will need to convert to prefix notation.
each new ship variable type would requier a new class be defined for example ship:thrust would be
class thrust:public variable{
public:
float value(ship*shipp=NULL){if(shipp)return ship->what_ever_the_member_was; else return 0.0f;};
};
global variables like fov would just ignore the ship parameter