1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright (C) 1999-2005 Open Source Telecom Corporation.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// ccScript. If you copy code from other releases into a copy of GNU
// ccScript, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU ccScript, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
#include "script3.h"
#include <cmath>
#include <climits>
#include <cstdlib>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace ccscript3Extension {
using namespace std;
using namespace ost;
class MathMethods : public ScriptMethods
{
public:
bool scrRandom(void);
bool scrSeed(void);
};
class MathChecks : public ScriptChecks
{
public:
const char *chkRandom(Line *line, ScriptImage *img);
const char *chkSeed(Line *line, ScriptImage *img);
};
class MathRuntime : public ScriptBinder
{
public:
MathRuntime();
};
static Script::Define runtime[] = {
{"seed", false, (Script::Method)&MathMethods::scrSeed,
(Script::Check)&MathChecks::chkSeed},
{"random", false, (Script::Method)&MathMethods::scrRandom,
(Script::Check)&MathChecks::chkRandom},
{NULL, false, NULL, NULL}};
static long fRandom(long *values, unsigned prec)
{return (long)(((double)(*values)) * (rand()/(RAND_MAX + 0.)));};
static long fSine(long *values, unsigned prec)
{return (long)(sin(values[0] * M_PI / values[1]) * values[1]);}
static long fCosine(long *values, unsigned prec)
{return (long)(cos(values[0] * M_PI / values[1]) * values[1]);}
static long fTangent(long *values, unsigned prec)
{return (long)(tan(values[0] * M_PI / values[1]) * values[1]);};
static long fArcTangent(long *values, unsigned prec)
{return (long)(atan(values[0] * M_PI / values[1]) * values[1]);};
static long fPi(long *values, unsigned prec)
{
char pi[10];
strcpy(pi, "3141592653");
pi[prec + 1] = 0;
return atol(pi);
}
static long fAbs(long *values, unsigned prec)
{
if(*values < 0)
return -*values;
else
return *values;
}
static long fMin(long *values, unsigned prec)
{
if(values[0] < values[1])
return values[0];
return values[1];
}
static long fMax(long *values, unsigned prec)
{
if(values[0] > values[1])
return values[0];
return values[1];
}
static long fLimit(long *values, unsigned prec)
{
if(values[0] < values[1])
return values[1];
if(values[0] > values[2])
return values[2];
return values[0];
}
static long fRound(long *values, unsigned prec)
{
if(!values[1])
return values[0];
return (values[0] / values[1]) * values[1];
}
static long fSqrt(long *values, unsigned prec)
{
double sq = sqrt(ScriptMethods::getDouble(*values, prec));
return ScriptMethods::getRealValue(sq, prec);
}
static long fInt(long *values, unsigned prec)
{
return ScriptMethods::getInteger(*values, prec) * ScriptMethods::getTens(prec);
}
static MathRuntime math;
MathRuntime::MathRuntime() : ScriptBinder()
{
addFunction("abs", 1, &fAbs);
addFunction("pi", 0, &fPi);
addFunction("min", 2, &fMin);
addFunction("max", 2, &fMax);
addFunction("round", 2, &fRound);
addFunction("limit", 3, &fLimit);
addFunction("int", 1, &fInt);
addFunction("sqrt", 1, &fSqrt);
addFunction("sine", 2, &fSine);
addFunction("cosine", 2, &fCosine);
addFunction("tangent", 2, &fTangent);
addFunction("arctangent", 2, &fArcTangent);
addFunction("random", 1, &fRandom);
bind(runtime);
}
const char *MathChecks::chkRandom(Line *line, ScriptImage *img)
{
const char *cp = getMember(line);
if(cp) {
if(atoi(++cp) < 1)
return "random member must be integer range";
}
else if(!findKeyword(line, "range"))
return "random requires range keyword or member";
if(!useKeywords(line, "=range=seed=offset=min=max=reroll=count"))
return "unknown or invalid keyword used";
return chkAllVars(line, img);
}
const char *MathChecks::chkSeed(Line *line, ScriptImage *img)
{
if(getMember(line))
return "seed does not use members";
if(line->argc > 1)
return "invalid number of arguments for seed";
return NULL;
}
bool MathMethods::scrSeed(void)
{
const char *cp = getValue(NULL);
char buf[12];
time_t now;
if(!cp) {
snprintf(buf, sizeof(buf), "%ld", (long)time(&now));
cp = buf;
}
srand(atoi(cp));
skip();
return true;
}
bool MathMethods::scrRandom(void)
{
const char *cp = getMember();
unsigned range = 0, count = 1, roll;
int val, offset = 0, min = 1, max = 0, reroll = 0;
Symbol *sym;
const char *errmsg = NULL;
char buf[12];
if(cp)
range = atoi(cp);
cp = getKeyword("range");
if(cp)
range = atoi(cp);
if(!range) {
error("random-range-invalid");
return true;
}
cp = getKeyword("count");
if(cp)
count = atoi(cp);
cp = getKeyword("seed");
if(cp)
srand(atoi(cp));
cp = getKeyword("offset");
if(cp)
offset = atoi(cp);
cp = getKeyword("min");
if(cp)
min = atoi(cp);
cp = getKeyword("max");
if(cp)
max = atoi(cp);
cp = getKeyword("reroll");
if(cp)
reroll = atoi(cp);
while(NULL != (cp = getOption(NULL))) {
sym = mapSymbol(cp, 11);
if(!sym) {
errmsg = "symbol-invalid";
continue;
}
if(sym->type == symINITIAL)
sym->type = symNUMBER;
retry:
val = offset;
roll = count;
while(roll--)
val += 1 + (int)(((double)(range)) * rand()/(RAND_MAX + 1.0));
if(val < min)
val = min;
if(val <= reroll)
goto retry;
if(val > max && max)
val = max;
snprintf(buf, sizeof(buf), "%d", val);
commit(sym, buf);
}
if(errmsg)
error(errmsg);
else
skip();
return true;
}
};
|