Really need help implementing state machine in EasyC Pro

If you have seen my other posts you probably know that I am working on an autonomous sumo robot with 4 ultrasonics and 3 line sensors. I am also using 2 NPR motors (controlled using an H-Bridge and the digital output ports of the vex controller).

I have been working on the coding for that but it has been really long with improperly nested If/If Else statements and has just been somewhat of a mess (you can see it in my other posts). So I started reading more about state machines and figured that that would probably be my best bet if I want to make this successful. This is the basic outline that I want to use

while(true) {
… // sensor sampling code
switch (state) {
case SURVIVE:
… // survive code
break;
case HUNT:
… // hunt code
break;
case TARGET:
… // target code
break;
case ATTACK:
… // attack code
break;
     }
}

this is what i want to accomplish using easyC Pro (keep in mind that I am using 7 inputs and NO servo outputs/readings)



while(loop == 1) {

start ultrasonic1
get ultrasonic1

start ultrasonic2
get ultrasonic2

start ultrasonic3
get ultrasonic3

start ultrasonic4
get ultrasonic4

getanalog1
getanalog2
getanalog3

switch ("state") {  // I don't know what im supposed to do about "state". Is it a variable I am supposed to define? or is it a sensor reading?

case SURVIVE: // Also, do I actually type in the word SURVIVE, and would it be a variable that needs defining? I am really clueless in that matter.
..blah blah blah
break;

case HUNT:
blah blah blah
If (sensor sees robot)
   {case=hunt     // THIS IS WHERE I REALLY NEED HELP. Is there a way I can tell the program to switch to a different case like the way I have it here? I haven't found a function in easyC Pro that will let me switch cases like that. I can't rely on the program to switch to a different case because of a sensor reading since I have too many sensors to do that.
    }
break;

case TARGET:
..blah blah blah
break;

case ATTACK:
..blah blah blah
break;
     }

}

So in essence I am just asking how I can implement that structure in easyC Pro. I am not asking for someone to do the program for me, I just want someone to point me in the right direction or show me which functions/structure I can use in easyC Pro.

Any help is much appreciated as always,

“state” is an integer variable that starts at state=0 or state=1 and is incremented each time you need to change state by doing someting like state++;

Yes, you can embed case statements inside if blocks, just be sure to match the parentheses.