19 Nov, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
I'm experiencing some difficulty understanding why this isn't working out.
I'm using Lua to run my color conversion, and then appending that output to the buffer.
However, it seems as though something is wrong with the length because it's a few characters short each time.
So, if there's a room full of mobs, they blend together like this:

A cityguard stands heA janitor is walking around, cleaning



/*
* Append onto an output buffer.
*/
void write_to_buffer( DESCRIPTOR_DATA *d, const char *txt, int length )
{

/* color conversion in lua */
char *ctxt;
ctxt = call_lua( global_L, LCALL_TEXT, "do_color", (char*)txt );

/*
* Find length in case caller didn't.
*/
if ( length <= 0 )
length = strlen(ctxt);

/*
* Initial \r\n if needed.
*/
if ( d->outtop == 0 && !d->fcommand ) {
d->outbuf[0] = '\r';
d->outbuf[1] = '\n';
d->outtop = 2;
}

/*
* Expand the buffer as needed.
*/
while ( d->outtop + length >= d->outsize ) {
char *outbuf;

outbuf = alloc_mem( 2 * d->outsize );
strncpy( outbuf, d->outbuf, d->outtop );
free_mem( d->outbuf, d->outsize );
d->outbuf = outbuf;
d->outsize *= 2;
}

/*
* Copy.
*/
strcpy( d->outbuf + d->outtop, ctxt );
d->outtop += length;
return;
}
19 Nov, 2009, Tyche wrote in the 2nd comment:
Votes: 0
If a length > 0 is passed in, then it won't be recalculated.

So change..
# if ( length <= 0 )
# length = strlen(ctxt);
to
# length = strlen(ctxt);

Unless of course something else is broken in the call_lua thing
19 Nov, 2009, JohnnyStarr wrote in the 3rd comment:
Votes: 0
Thanks Tyche, that did the trick.
How well do you know Merc's "act" routine?
Check this out:
<22hp 100ap 110mv>'{yWhat the hell?
You say 'What the hell?'.
" // what is this?
<22hp 100ap 110mv>


There's that weird output now.
19 Nov, 2009, Mudder wrote in the 4th comment:
Votes: 0
How exactly does one blend in a script language like that, and what are the advantages of doing it versus keeping things in the "normal" language?
19 Nov, 2009, Tyche wrote in the 5th comment:
Votes: 0
JohnnyStarr said:
" // what is this?


Why that's A acute, the 2nd letter of the Hungarian alphabet. ;-)

Hexidecimal - C1
Decimal - 193
19 Nov, 2009, JohnnyStarr wrote in the 6th comment:
Votes: 0
Tyche said:
JohnnyStarr said:
" // what is this?


Why that's A acute, the 2nd letter of the Hungarian alphabet. ;-)

Hexidecimal - C1
Decimal - 193


hint taken. :rolleyes:
19 Nov, 2009, JohnnyStarr wrote in the 7th comment:
Votes: 0
Mudder said:
How exactly does one blend in a script language like that, and what are the advantages of doing it versus keeping things in the "normal" language?

Read this.

To understand the benefits, you would need to have some experience programming.
Suffice it to say, it allows you to take advantages of multiple languages which have their own individual strengths.
28 Jan, 2010, Kjwah wrote in the 8th comment:
Votes: 0
So do you get a speed improvement on the color conversion using Lua rather then doing that part in C?
28 Jan, 2010, David Haley wrote in the 9th comment:
Votes: 0
Why is a speed improvement expected here?
28 Jan, 2010, Kjwah wrote in the 10th comment:
Votes: 0
David Haley said:
Why is a speed improvement expected here?


I'm curious to find out why do the color conversion in Lua rather then do it in C?

If I'm not suppose to be curious, sorry, I'll stop posting?
28 Jan, 2010, JohnnyStarr wrote in the 11th comment:
Votes: 0
I don't think run-time speed is the point, look here.
It took me 5 minutes to figure out how to do this. So if you consider how long it would take to write a C color converter, the speed
benefit is the time it takes to write. In the end though, Lua is just tapping into C under the hood, so even if it was "slower" you would never
notice.
28 Jan, 2010, Kjwah wrote in the 12th comment:
Votes: 0
JohnnyStarr said:
I don't think run-time speed is the point, look here.
It took me 5 minutes to figure out how to do this. So if you consider how long it would take to write a C color converter, the speed
benefit is the time it takes to write. In the end though, Lua is just tapping into C under the hood, so even if it was "slower" you would never
notice.


Well, I wasn't really expecting it to be faster or slower, not that it'd be noticeable to the end user anyways. I suppose I worded my question wrong as I was just more curious to find out why do it in Lua rather then do it in C. I guess getting 1-4 hours of sleep a night for two weeks doesn't do much for the brain.

After looking at your Lua code, I can see why you did it in Lua. I'm no Lua expert, I've only recently started messing with it again. Which is where my curiosity came from as I'm embedding Lua into my code also. :)
28 Jan, 2010, David Haley wrote in the 13th comment:
Votes: 0
The main reason to write almost anything in Lua/Python/Ruby/etc. these days is developer efficiency, not computer efficiency. Certain things are simply far easier to write in dynamic languages than in statically typed languages esp. ones such as C++. More modern statically typed languages introduce syntactic convenience over common idioms, like container mapping, filtering, etc. (E.g., list comprehensions in Python)
28 Jan, 2010, elanthis wrote in the 14th comment:
Votes: 0
The basic gist of it, Kjwah, is that some languages make some things way easier to code. Lua is a very high level language with built-in support for certain data structures and types (table and strings and first-class functions and such) and takes some of the work load off of the programmer (automatic memory management, higher-order operators, etc.). Some code that is dead easy and quick to write in Lua is a major pain in the ass to write in C.

I don't think color conversion is a great example (it's not that much harder in C at all; I'd go so far as to call it trivial) but that is just me. For many tasks it would definitely be great to be able to use Lua.

Perhaps the problem with such approaches is that using higher-level languages can make some people lazy. That Lua color conversion script is actually very, very inefficient. It's not because it's Lua, as that same algorithm in C would also be quite inefficient. It's doing a global replace on the entire input string for each possible color, and it cannot support things like {{ to escape the { character. A simple C implementation would be barely more effort, would be orders of magnitude more efficient, and wouldn't have the { escape bug. Granted, you could also do a correct and more efficient version in Lua too, but the correct Lua version would basically be just as complex as the correct C version, IMHO.
28 Jan, 2010, David Haley wrote in the 15th comment:
Votes: 0
elanthis said:
Granted, you could also do a correct and more efficient version in Lua too, but the correct Lua version would basically be just as complex as the correct C version, IMHO.

Eh.

$ cat test.lua 

colors = {}

colors["{x"] = "COLORX"
colors["{y"] = "COLORY"
colors["{{"] = "{"

input = "This{x is {{ my {{x input {ystring"
output = string.gsub(input, "{.", colors)
print(output)


$ lua test.lua
ThisCOLORX is { my {x input COLORYstring
28 Jan, 2010, JohnnyStarr wrote in the 16th comment:
Votes: 0
I never claimed it was complete, in fact if you read the disclaimer of my project, it makes it very clear that it is in fact incomplete.
Kjwah, it seems that trying to post something generally helpful to others comes with some sort of biased criticism. My advice is to
decide for yourself what is more helpful to you, and you alone. After all, the avid hobbiest programmer may be more
concerned with having enough time to have FUN, not write a book on computer science.
28 Jan, 2010, Kjwah wrote in the 17th comment:
Votes: 0
elanthis said:
That Lua color conversion script is actually very, very inefficient. It's not because it's Lua, as that same algorithm in C would also be quite inefficient. Granted, you could also do a correct and more efficient version in Lua too, but the correct Lua version would basically be just as complex as the correct C version, IMHO.


This basically where my curiosity was coming from. Like I said earlier, I'm no expert in Lua but I was thinking it'd be about the same amount of work to do it in C. After I saw your code, I was thinking well, that looks a lot more simple. However, not being the a master of Lua, I wasn't sure how efficient it'd be. Which is why I asked why do it in Lua(In the complete wrong way, sorry I'm a freaking idiot lately). lol

JohnnyStarr said:
Kjwah, it seems that trying to post something generally helpful to others comes with some sort of biased criticism.


Why I don't post here much and pretty much stopped posting in the MUD community.
0.0/17