29 Oct, 2009, Skol wrote in the 1st comment:
Votes: 0
Rofl, I was writing out my crafting experience per craft level and thought man I do NOT want to do all of this math for this array of ints…

So I added it to my fishing code as an argument (it'll be the first 'craft' type skill).
else if (!str_prefix (arg, "experience"))
{
char temp[128], buf[MSL];
int i, col, xp, total;

buf[0] = '\0';
for (i= 0, col = 0, total=0, xp=80; i<101;i++)
{
sprintf (temp, " %8d,", total);
strcat (buf, temp);
if (++col % 5 == 0)
{
if (col % 10 == 0)
{
sprintf (temp, "// %d", i+1);
strcat (buf, temp);
}

strcat (buf, "\r\n");
}
total += xp;
xp = xp * 1.1;
}
strcat (buf, "\r\n");
send_to_char (buf, ch);
return;
}


Which, with a little help, yielded:
static const int craft_exp_level [] = 
{
0, 80, 168, 264, 369,
484, 610, 748, 899, 1065,// 10
1247, 1447, 1667, 1909, 2175,
2467, 2788, 3141, 3529, 3955,// 20
4423, 4937, 5502, 6123, 6806,
7557, 8383, 9291, 10289, 11386,// 30
12592, 13918, 15376, 16979, 18742,
20681, 22813, 25158, 27737, 30573,// 40
33692, 37122, 40895, 45045, 49610,
54631, 60154, 66229, 72911, 80261,// 50
88346, 97239, 107021, 117781, 129617,
142636, 156956, 172708, 190035, 209094,// 60
230058, 253118, 278484, 306386, 337078,
370839, 407976, 448826, 493761, 543189,// 70
597559, 657366, 723153, 795518, 875119,
962680, 1058997, 1164945, 1281487, 1409683,// 80
1550698, 1705814, 1876441, 2064130, 2270587,
2497689, 2747501, 3022294, 3324566, 3657065,// 90
4022813, 4425135, 4867689, 5354498, 5889987,
6479024, 7126964, 7839698, 8623705, 9486112,// 100
10434759
}
(101, levels of it, level 1 is 0)

Anyone else do this kind of stuff to write your code if it involves computation and arrays like that? I felt kind of… dirty ;p.
29 Oct, 2009, KaVir wrote in the 2nd comment:
Votes: 0
I did something similar for creating tables of subclasses for my muds wiki. I got fed up of writing them all out by hand, so I got the mud to generate a big block of text that I could just copy and paste.
29 Oct, 2009, Igabod wrote in the 3rd comment:
Votes: 0
Hey, that's why calculators were invented. You can do the math yourself, but it's easier and less time consuming with a computer/calculator/program, especially with large numbers like you have there. Nothing to be ashamed of, in fact it's rather an ingenious method of doing it IMHO.
29 Oct, 2009, Runter wrote in the 4th comment:
Votes: 0
Code writing code isn't a very novel concept. Well, maybe in C. I would suggest learning a simple scripting language to generate your C code if you actually do that very often. :p

I think a more interesting question would have been how do you generate the C function given an approximate table. :evil:
29 Oct, 2009, David Haley wrote in the 5th comment:
Votes: 0
I've often written code to generate code, but as Runter said usually the generator was in a scripting language. There's nothing wrong with it, although depending on what you're doing you might be generating a table when you could leave it as a formula in your code or something like that.

You can also think of it as compiling: given a simple and abbreviated representation of something, you need to generate a more explicit representation of something that you can feed to another system. I did this quite a bit when generating game rules that needed to be expressed in a logical language; the game generation was parametrized but the resulting structure had clear patterns that were far easier to just generate automatically.
29 Oct, 2009, Skol wrote in the 6th comment:
Votes: 0
Lol yeah I could have done it in Javascript or such a bit faster heh.
I just happened to be in that window and went, hm, //blah, and I had them all nicely formatted and all heh.
29 Oct, 2009, Tyche wrote in the 7th comment:
Votes: 0
David Haley said:
There's nothing wrong with it, although depending on what you're doing you might be generating a table when you could leave it as a formula in your code or something like that.


I probably would have left it as a calculated function in the code.
30 Oct, 2009, Koron wrote in the 8th comment:
Votes: 0
Tyche said:
I probably would have left it as a calculated function in the code.

This.
If you want to add more later, leaving it as the formula would save you the hassle of recalculating the table with the new additions.

Still, having the mud do your work for you is always a good idea. Well played!
10 Nov, 2009, Impacatus wrote in the 9th comment:
Votes: 0
What scripting language would be good for things like this? I've tried to automate the more repetitive parts of my code before, but I always find it takes too long to write the program.
10 Nov, 2009, Runter wrote in the 10th comment:
Votes: 0
Ruby would be a good choice.
10 Nov, 2009, David Haley wrote in the 11th comment:
Votes: 0
When I mentioned this:
I said:
I did this quite a bit when generating game rules that needed to be expressed in a logical language; the game generation was parametrized but the resulting structure had clear patterns that were far easier to just generate automatically.

I did it in Lua or Ruby depending on my mood at the time, and/or who else I was working with.

It requires a certain degree of comfort, though; if you're not proficient enough with the scripting language you might as well write the code by hand and use editor-fu (copy/paste, replacing, regular expressions, etc.) instead of scripting-fu.
0.0/11