In the case of your oil problem we are dealing with a bunch of ranges (with no gaps between the ranges) - so to tackle this let's first picture each "range" as a bucket:
- We go into bucket 0 if there is no oil
- Otherwise if there is <5 oil go into bucket 1
- else <12.5 bucket 2
- ...and so on...
The buckets have integer values and identify the range - perfect for switch case stuff (and some nifty tricks too). All we need now is some way to represent the ranges and find out the appropriate bucket of some number. Thankfully, doing this is not too tricky - Firstly we shall define the ranges in a global array, so adding extra buckets is easy:
float[] oilRanges = { 0.0f, 5.0f, 12.5f };
The values in this array represent the (inclusive) upper bound of the bucket. Next we shall make a function that will work out which bucket (range index) a float value lands in:
int findOilRangeIndex(float value)
{
//For each upper bound of the range of our buckets...
for(int i=0; i<int(oilRanges.length()); i++)
//If the value is less than this upper bound, then this is the correct bucket.
if(value <= oilRanges[i]) return i;
//No matching bucket - return -1 to indicate an error.
return -1;
}
The above code will loop through upper bound in the array and determine which bucket the value lies in. It is worth noting here that this approach assumes the ranges have no gaps and the upper bounds are specified in ascending order in the global array.
With the tough part out the way...
void someFunction()
{
int bucket = findOilRangeIndex(GetPlayerLampOil());
switch(bucket)
{
case 0:
//Some code for oil=0
break;
case 1:
//Some code for 0<oil<=5
break;
case 2:
//Some code for 5<oil<=12.5
break;
default:
//Error case where the oil is outside of the buckets
}
}
In the case of you example of just wanting to alter what the output of setmessage is there is a far more compact way of going about the problem. I notice your entries start as OilQuestDone1 (for oil=0) and go OilQuestDone2, OilQuestDone3... - you can simply display the correct message and avoid writing the whole switch case stuff by adding 1 to the bucket id and adding this number to the end of the entry:
void someFunction()
{
int entry = findOilRangeIndex(GetPlayerLampOil()) + 1;
SetMessage("map2_messages", "OilQuestDone" + entry, 5.0f);
}
Which is far more compact when you get past two or three ranges.
Please not code provided here has been written in the forum editor and is untested. It should work but i could have made a typo at somepoint 
.