Welcome to the Builder Academy

Question void need help and question

More
30 Sep 2017 19:04 - 30 Sep 2017 19:29 #6932 by JTP
Code:
void perform_raceelf() { struct char_data *ch; for (ch = character_list; ch; ch = ch->next) { if ((GET_RACE(ch) == RACE_ELF) && (GET_POS(ch) != POS_FIGHTING || GET_POS(ch) != POS_MORTALLYW || GET_POS(ch) != POS_INCAP) && (GET_HIT(ch) < GET_MAX_HIT(ch))) { GET_HIT(ch) += 4 + GET_LEVEL(ch) /5; send_to_char(ch, "Your wounds heal on their own.\r\n"); act("$n's wounds heal on their own.", TRUE, ch, 0, 0, TO_ROOM); } } } How does above code look ? I never know when to use ( ) inside an if, it compiles with and without them and works. But i guess there is a reason why they are needed ?? Also the above code works BUT it a char is almost at max health, then the last time it fires it brings them above there max health 5008/5000..How can that be prevented ? Ideas to write it "smarter" is also welcome.
Last edit: 30 Sep 2017 19:29 by JTP.

Please Log in or Create an account to join the conversation.

More
01 Oct 2017 07:35 #6934 by thomas
Replied by thomas on topic void need help and question
This isn't too shabby :)

An if statement is followed by an expression in parenthesis, and then the code to run if the expression evaluates to true (or !=0 in C). This code can be either a single line (so it just follows the expression) or a block of code (so it is wrapped in a new scope with {}'s).

So a little info about the expression. The expression uses boolean logic to determine if the total is true or false. This means that A && B is true if both A and B is true. It means that A || B is true if either A or B is true. It means that A && (B || C) is true if A is true and one or more of B and C is true. It matters how the parenthesis is are place. (A && B) || C - the same statement as before, but with different placing of the parenthesis - is true if A and B are true, or C is.

Because boolean logic is hard (at least, it's usually where I find my bugs), it pays off to try and simplify those expressions. The same logic can typically be represented by a series of if statements:
Code:
void perform_raceelf() { struct char_data *ch; for (ch = character_list; ch; ch = ch->next) { if (GET_RACE(ch) != RACE_ELF) // note - this is the opposite check of what you used. continue; // We're filtering out instead of including. this statement means, done with this char, see next. if (GET_POS(ch) == POS_FIGHTING || GET_POS(ch) == POS_MORTALLYW || GET_POS(ch) == POS_INCAP) continue; if (GET_HIT(ch) >= GET_MAX_HIT(ch)) continue; // all who pass the above checks are elves with less than max hitpoints who are not fighting or dying. GET_HIT(ch) += 4 + GET_LEVEL(ch) /5; if (GET_HIT(ch) > GET_MAX_HIT(ch)) GET_HIT(ch) = GET_MAX_HIT(ch); send_to_char(ch, "Your wounds heal on their own.\r\n"); act("$n's wounds heal on their own.", TRUE, ch, 0, 0, TO_ROOM); } }

A side effect of structuring your loops this way is that it is easy to add another check (ie. level, class, etc.) because each section of the code does one particular thing.

Please Log in or Create an account to join the conversation.

More
01 Oct 2017 07:41 #6935 by JTP
Replied by JTP on topic void need help and question
Thanks. I like the idea of filtering out alot more Then what i started out with. Suppose i dont need a dead check ?

Please Log in or Create an account to join the conversation.

More
01 Oct 2017 10:04 #6936 by thomas
Replied by thomas on topic void need help and question
good point. I'd probably add it in the check for position.

Please Log in or Create an account to join the conversation.

More
01 Oct 2017 10:09 #6938 by zusuk
Replied by zusuk on topic void need help and question
I could only write the code the way Thomas did it.... my brain breaks when I try to evaluate long logical expressions ;p

Website
www.luminariMUD.com

Main Game Port
luminariMUD.com:4100

Please Log in or Create an account to join the conversation.

Time to create page: 0.197 seconds