Welcome to the Builder Academy

Question Converting a set of checks to a switch/case function

More
27 Sep 2025 17:23 #10908 by wlessard1
I have in fight.c in the HIT function weapon flares.
What I want to do, as I want to add other things to this is to convert it to a switch/case function.
Code:
here is the existing checks for flares....     /* Add flare damage */     if (AFF_FLAGGED(ch, AFF_FIREFLARE)) {       int success = rand_number(1, 100);       if (success <= (GET_LEVEL(ch) + 50)) {         int dam_mod = rand_number(10, 25);         dam += dam_mod;         send_to_char(ch, "\trYour weapon flares with \tRsearing fire\tr doing %d damage!\tn\r\n", dam_mod);       }     }   /* Add Cold damage */   if (AFF_FLAGGED(ch, AFF_COLDFLARE)) { int success = rand_number(1, 100); if (success <= (GET_LEVEL(ch) + 50)) {   int dam_mod = rand_number(10, 25);   dam += dam_mod;     send_to_char(ch, "\tbYour weapon flares with \tBBone Chilling Cold\tb doing %d damage!\tn\r\n", dam_mod);        }      }   /* Add Shock/Lightning damage */   if (AFF_FLAGGED(ch, AFF_SHOCKFLARE)) { int success = rand_number(1, 100); if (CONFIG_DEBUG_MODE) {   send_to_char(ch, "Debug: Shock Flare Success = %d\r\n", success); } if (success <= (GET_LEVEL(ch) + 50)) {   int dam_mod = rand_number(10, 25);   dam += dam_mod;     send_to_char(ch, "\tbYour weapon flares with a \tCSearing Lightning Strike\tc doing %d damage!\tn\r\n", dam_mod);        }      }

I have tried converting to a switch/case function but I am missing something. Flares no longer work.
Code:
/* Handle weapon effect flags flares */ if (wielded && GET_OBJ_TYPE(wielded) == ITEM_WEAPON) {   int i;   /* Loop through OBJ_AFFECT bits to find active effects */   for (i = 0; i < MAX_OBJ_AFFECT; i++) {     if (!IS_SET_AR(GET_OBJ_AFFECT(wielded), i)) continue;     switch (i) {       case AFF_FIREFLARE:         if (rand_number(1, 100) <= (GET_LEVEL(ch) + 50)) {           int dam_mod = rand_number(10, 25);           dam += dam_mod;           send_to_char(ch, "\trYour %s flares with \tRsearing fire\tr doing %d damage!\tn\r\n",                        GET_OBJ_SHORT(wielded), dam_mod);         }         break;       case AFF_COLDFLARE:         if (rand_number(1, 100) <= (GET_LEVEL(ch) + 50)) {           int dam_mod = rand_number(10, 25);           dam += dam_mod;           send_to_char(ch, "\tbYour %s flares with \tBBone Chilling Cold\tb doing %d damage!\tn\r\n",                        GET_OBJ_SHORT(wielded), dam_mod);         }         break;       case AFF_SHOCKFLARE:         if (rand_number(1, 100) <= (GET_LEVEL(ch) + 50)) {           int dam_mod = rand_number(10, 25);           dam += dam_mod;           if (CONFIG_DEBUG_MODE) {             send_to_char(ch, "Debug: Shock Flare Success\r\n");           }           send_to_char(ch, "\tbYour %s flares with a \tCSearing Lightning Strike\tc doing %d damage!\tn\r\n",                        GET_OBJ_SHORT(wielded), dam_mod);         }         break;       /* Add more cases for other effects, e.g., AFF_DEMON_BANE */       default:         if (CONFIG_DEBUG_MODE) {           send_to_char(ch, "Debug: Unhandled weapon affect bit %d on %s\r\n",                        i, GET_OBJ_SHORT(wielded));         }         break;     }   } }


Thanks for any help.

Bramage

Just a guy coding a mud at home for no reason but the challenge.

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

More
29 Sep 2025 23:17 #10915 by thomas
You're changing the logic a bit here. When going from AFF_FLAGGED(ch, AFF_FIREFLARE) to checking if the object is affected, you'll need to use the OBJAFF_FLAGGED(wielded, AFF_FIREFLARE) macro instead. I'm not sure it does what you want, though.

Why do you want to make it into a switch case?

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

More
01 Oct 2025 10:07 #10921 by wlessard1
I thought I had posted it... finally got it going the right way.
Code:
    /* Add flare damage effects */     int get_flare_affect(struct obj_data *wielded) {       if (!wielded) return 0;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_FIREFLARE)) return AFF_FIREFLARE;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_COLDFLARE)) return AFF_COLDFLARE;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_SHOCKFLARE)) return AFF_SHOCKFLARE;      return 0;     }   int flare = get_flare_affect(wielded);   switch (flare) {     case AFF_FIREFLARE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         int dam_mod = rand_number(10, 25);         dam += dam_mod;         send_to_char(ch, "\trYour weapon flares with \tRsearing fire\tr doing %d extra damage!\tn\r\n", dam_mod);       }       break;     case AFF_COLDFLARE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         int dam_mod = rand_number(10, 25);         dam += dam_mod;         send_to_char(ch, "\tbYour weapon flares with \tBBone Chilling Cold\tb doing %d extra damage!\tn\r\n", dam_mod);       }       break;     case AFF_SHOCKFLARE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         int dam_mod = rand_number(10, 25);         dam += dam_mod;         send_to_char(ch, "\tbYour weapon flares with a \tCSearing Lightning Strike\tc doing %d extra damage!\tn\r\n",                      dam_mod);       }       break;     default:       break; /* No flare */   }   /* Bane Effects Bramage */    int get_bane_affect(struct obj_data *wielded, struct char_data *victim) {      if (!wielded) return 0;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_UNDEAD_BANE) && strcmp(npc_races[(int)GET_CLASS(victim)], "Undead") == 0) return AFF_UNDEAD_BANE;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_DEMON_BANE) && strcmp(npc_races[(int)GET_CLASS(victim)], "Demon") == 0) return AFF_UNDEAD_BANE;      if (wielded && IS_SET_AR(GET_OBJ_AFFECT(wielded), AFF_DRAGON_BANE) && strcmp(npc_races[(int)GET_CLASS(victim)], "Dragon") == 0) return AFF_UNDEAD_BANE;      return 0;    }   int bane = get_bane_affect(wielded, victim);   switch (bane) {     case AFF_UNDEAD_BANE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         dam *= 2;         send_to_char(ch, "\tbYour Undead Bane Weapon does %d damage!\tn\r\n", dam);       }       break;     case AFF_DEMON_BANE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         dam *= 3;       send_to_char(ch, "\tbYour Demon Bane Weapon does %d damage!\tn\r\n", dam);       }       break;     case AFF_DRAGON_BANE:       if (rand_number(1, 101) <= GET_LEVEL(ch) + 25) {         dam *= 3;       send_to_char(ch, "\tbYour Dragon Bane Weapon does %d damage!\tn\r\n", dam);       }       break;     default:       break; /* No Bane */   }

This is working. I mainly had to just make the switch/case for both as they didn't have the same calls.

Big problem was making sure that it called the object affect not the affect on the player. 

Putting the check on the player caused it to not flare right in a dual wield situation so this takes care of dual wield. The code is pretty much exactly the same for Dual Wield function but it has dwielded instead of wielded to make sure of the check looking at the dual wielded weapon.

It aint perfect, I aint no real coder but I try.

Just a guy coding a mud at home for no reason but the challenge.
The following user(s) said Thank You: thomas

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

Time to create page: 0.185 seconds