Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ life bar
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1 2
Posted by
| Alkarindil
Brazil (70 posts) Bio
|
Date
| Fri 01 Jun 2007 09:48 PM (UTC) Amended on Fri 01 Jun 2007 09:58 PM (UTC) by Alkarindil
|
Message
| Hello!
I know a Smaug-based MUD where there is a skill that show-me the mob's life in a bar.. it's something like this:
[>>>>>>>---]
The same bar appears in the player's score screen, in the hp, mn and mv information.
I am trying to make the same thing in my smaug here..
I know php, delphi, vb6, etc, but this is my first time using c++. Is that any default function that draw this bar? How can I use this in a skill??
I'd made a function to the score command, but it doesn't work..
This is the function, the variables are portuguese words (barra = bar; porcento = percent; desenho = draw):
//-----------------
char barra(int A, int B)
{
int porcento;
char desenho;
int i;
porcento = (A*100)/B;
desenho = '[';
for (i = 1; i < 11; ++i){
if (porcento >= (i*10))
desenho += '>';
else
desenho += '-';
}
desenho += ']';
return (desenho);
}
//-----------------
So in the moment I need to use the bar, I use the caracter's hit & max_hit information to return the bar, but I doesn't understand how to use the print functions..
Oh yes, I believe this is relevant: I am using Microsoft Visual C++ 6.
Well, that's it, can you help-me, please?
Thanks everyone! | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Fri 01 Jun 2007 11:55 PM (UTC) |
Message
| The problem is that 'char' is not a string type, it is just a character. So you are taking a character, and adding other characters to it, resulting in a new, single character.
There are two ways to do this:
1. build up the string and return it as a char*, keeping in mind that you will have to free it.
2. pass the CHAR_DATA *ch variable into your function, and use the ch_printf or send_to_char functions to send data to the character. This is the easier solution.
For examples of these, look at any of the functions that send to the character, such as do_mstat, do_time, or even do_score which you are already looking at. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Alkarindil
Brazil (70 posts) Bio
|
Date
| Reply #2 on Sat 02 Jun 2007 02:22 AM (UTC) |
Message
| Holy sh*t!
Now I am really mad with this thing!
How can I return a string!?
That's madness! | Top |
|
Posted by
| Zeno
USA (2,871 posts) Bio
|
Date
| Reply #3 on Sat 02 Jun 2007 02:28 AM (UTC) |
Message
| I believe David just said how to return it. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | Top |
|
Posted by
| Alkarindil
Brazil (70 posts) Bio
|
Date
| Reply #4 on Sat 02 Jun 2007 02:51 AM (UTC) |
Message
| Thanks David!
I'm sorry, I realize this is really stupid, but I really never used a string this way..
Ok. I am trying now to print the result inside the function..
This is my idea.. of course it doesn't work, but I am trying to make it fit..
//--------------------------
int bar_draw(int A, int B)
{
int percent;
char bar [20];
int i;
percent = (A*100)/B;
for (i = 1; i < 11; ++i){
if (percent >= (i*10))
bar += '>';
else
bar += '-';
}
send_to_pager("["+bar+"]\n\r", ch); //this is not right..
return 0;
}
//--------------------------
Now I get two errors:
The most common is "'+=' : left operand must be l-value"
The function to print I believe I will find a way.. but why the "+=" doesn't work?
| Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #5 on Sat 02 Jun 2007 03:41 AM (UTC) |
Message
| These are fundamental issues about using strings in C.
I really suggest doing a C tutorial if these are confusing you.
Basically, "char bar [20]" is an array of characters. You can't just add to it.
Something like this is closer:
char bar [20] = "";
strcat (bar, ">"); // add to the string
Read up on strcat, and strcpy to find out more about working with strings.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Conner
USA (381 posts) Bio
|
Date
| Reply #6 on Sat 02 Jun 2007 04:15 AM (UTC) Amended on Sat 02 Jun 2007 04:16 AM (UTC) by Conner
|
Message
| For what he's after, wouldn't a series of if/else if's work better? ...something along the lines of:
int bar_draw(int A, int B)
{
char *bar[MIL];
int percent;
percent = (A*100)/B;
if( percent >= 100 ) bar = "[>>>>>>>>>>]";
else if( percent >= 90 ) bar = "[>>>>>>>>>-]";
else if( percent >= 80 ) bar = "[>>>>>>>>--]";
else if( percent >= 70 ) bar = "[>>>>>>>---]";
else if( percent >= 60 ) bar = "[>>>>>>----]";
else if( percent >= 50 ) bar = "[>>>>>-----]";
else if( percent >= 40 ) bar = "[>>>>------]";
else if( percent >= 30 ) bar = "[>>>-------]";
else if( percent >= 20 ) bar = "[>>--------]";
else if( percent >= 10 ) bar = "[>---------]";
else bar = "[----------]";
send_to_char( bar, ch );
send_to_char( "\n\r", ch );
return;
}
|
-=Conner=-
--
Come test your mettle in the Land of Legends at telnet://tcdbbs.zapto.org:4000
or, for a little family oriented medieval fun, come join us at The Castle's Dungeon BBS at telnet://tcdbbs.zapto.org
or, if you just want information about either, check our web page at http://tcdbbs.zapto.org | Top |
|
Posted by
| Alkarindil
Brazil (70 posts) Bio
|
Date
| Reply #7 on Sat 02 Jun 2007 08:52 PM (UTC) Amended on Sat 02 Jun 2007 09:02 PM (UTC) by Alkarindil
|
Message
| I am reading some tutorials now, Nick, thanks!
I used Conner sugestion and now I have this:
void bar_draw(int A, int B, CHAR_DATA * ch)
{
int percent;
percent = (A*100)/B;
if( percent >= 100 ) send_to_pager("[>>>>>>>>>>]", ch);
else if(percent >= 90) send_to_pager("[>>>>>>>>>-]", ch);
else if(percent >= 80) send_to_pager("[>>>>>>>>--]", ch);
else if(percent >= 70) send_to_pager("[>>>>>>>---]", ch);
else if(percent >= 60) send_to_pager("[>>>>>>----]", ch);
else if(percent >= 50) send_to_pager("[>>>>>-----]", ch);
else if(percent >= 40) send_to_pager("[>>>>------]", ch);
else if(percent >= 30) send_to_pager("[>>>-------]", ch);
else if(percent >= 20) send_to_pager("[>>--------]", ch);
else if(percent >= 10) send_to_pager("[>---------]", ch);
else send_to_pager("[----------]", ch);
return;
}
//--------------------------
It's working!
At least if whe use "bar_draw(85,100,ch);", it works fine!
Thanks everbody! | Top |
|
Posted by
| Conner
USA (381 posts) Bio
|
Date
| Reply #8 on Sat 02 Jun 2007 11:15 PM (UTC) |
Message
| Glad I was able to help, it doesn't feel like that long ago that I'd have been the one asking instead of helping. It's a nice feeling. :) |
-=Conner=-
--
Come test your mettle in the Land of Legends at telnet://tcdbbs.zapto.org:4000
or, for a little family oriented medieval fun, come join us at The Castle's Dungeon BBS at telnet://tcdbbs.zapto.org
or, if you just want information about either, check our web page at http://tcdbbs.zapto.org | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #9 on Sun 03 Jun 2007 01:45 AM (UTC) Amended on Sun 03 Jun 2007 01:47 AM (UTC) by David Haley
|
Message
| I think it's nicer without the if checks, but that's just me:
[david@thebalrog:~]$ cat test.c
#include <stdio.h>
#include <stdlib.h>
void PrintBar(int cur, int max)
{
int perdec = (10*cur)/max;
int decade;
printf("[");
for (decade = 1; decade <= 10; decade++)
{
if (decade <= perdec) {
printf(">");
}
else {
printf("-");
}
}
printf("]\n");
}
int main(int argc, char** argv)
{
int i;
for (i = 0; i <= 100; i += 5) {
PrintBar(i, 100);
}
}
[david@thebalrog:~]$ gcc test.c -o tester
[david@thebalrog:~]$ ./tester
[----------]
[----------]
[>---------]
[>---------]
[>>--------]
[>>--------]
[>>>-------]
[>>>-------]
[>>>>------]
[>>>>------]
[>>>>>-----]
[>>>>>-----]
[>>>>>>----]
[>>>>>>----]
[>>>>>>>---]
[>>>>>>>---]
[>>>>>>>>--]
[>>>>>>>>--]
[>>>>>>>>>-]
[>>>>>>>>>-]
[>>>>>>>>>>]
Having it handled logically like that makes it easier to adapt to different formats, for instance, instead of doing tenths you could do fifteenths, hundredths, fifths, etc. In fact, you could even have the resolution as another parameter of the function. It would be very easy. I'll leave it as an exercise to the reader. (*chortle* I've always wanted to say that. :-P) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #10 on Sun 03 Jun 2007 01:49 AM (UTC) |
Message
| Also, in this code:
int bar_draw(int A, int B)
{
char *bar[MIL];
int percent;
percent = (A*100)/B;
if( percent >= 100 ) bar = "[>>>>>>>>>>]";
Technically bar here is declared as an array of char*, which is probably not what you want. It is enough to define it as just a char*, that is, "char * bar;".
In fact to be somewhat obsessive about it you would do const char *, because you are not writing into the string but rather assigning to it from the data segment of the program (the string constants "[........]". |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Conner
USA (381 posts) Bio
|
Date
| Reply #11 on Sun 03 Jun 2007 03:17 AM (UTC) |
Message
| Oh, I like the way you've proposed, David, that's slick.
I'm not sure what the difference is between char *bar [MIL]; and char *bar; and const char *bar;
I suggested the way I had because I knew it'd work (from my own experience dealing with a similiar snippet having to do with putting a bar like that into the prompt), though your way without the multiple if checks is much nicer. I didn't know how to do that myself, but now that I can see what you'd done, I'll use your way if I should find myself doing something like this in the future. :) |
-=Conner=-
--
Come test your mettle in the Land of Legends at telnet://tcdbbs.zapto.org:4000
or, for a little family oriented medieval fun, come join us at The Castle's Dungeon BBS at telnet://tcdbbs.zapto.org
or, if you just want information about either, check our web page at http://tcdbbs.zapto.org | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #12 on Sun 03 Jun 2007 06:24 AM (UTC) |
Message
| char *bar [MIL];
An array named bar of MIL char pointers.
char *bar;
A pointer to a character.
const char *bar;
A pointer (that can be changed) to a character (that cannot be changed). So bar++ is legal, but bar[0] = 'a' is not.
Contrast with:
char * const bar
In this case the pointer cannot be changed to point to another location (bar++ is illegal) but you can change the contents in the address it points to (bar[0] = '\0', for example, is legal).
The rule for arrays is that when you have the brackets, you are creating an array of the type before the name. So,
char bar[100]
is an array of 100 characters. 'bar' is a pointer to the beginning of that array.
char * bar
A pointer, that for now points nowhere;
char * bar[100]
One hundred pointers to characters, none of which point anywhere.
Hope this helps. :-) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Conner
USA (381 posts) Bio
|
Date
| Reply #13 on Mon 04 Jun 2007 04:38 AM (UTC) |
Message
| How much it helps, I can't say yet.. I may have to read through that a few times (or even play with it in some code a bit) to fully grasp what you've explained, but it does look like a pretty helpful explaination in general that I suspect I'll be coming back to reference for awhile before I've got it all down myself. ;) |
-=Conner=-
--
Come test your mettle in the Land of Legends at telnet://tcdbbs.zapto.org:4000
or, for a little family oriented medieval fun, come join us at The Castle's Dungeon BBS at telnet://tcdbbs.zapto.org
or, if you just want information about either, check our web page at http://tcdbbs.zapto.org | Top |
|
Posted by
| Alkarindil
Brazil (70 posts) Bio
|
Date
| Reply #14 on Tue 05 Jun 2007 03:27 AM (UTC) |
Message
| Hey Conner, my turn to ask all the things here...
I liked David's idea, but recently I find out that if you do not declare the variables in the top of the function, c++ will get an error.
Horewer, I will try it tomorrow!
;) | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
62,145 views.
This is page 1, subject is 2 pages long: 1 2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top