A good way to sort and present commands...
#10

I came up with this, feel free to complete/use it:
pawn Code:
#include <a_samp>

main() {
    new str[128], type, which, qty;

    GetThingFromString("nothing", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("hp 20", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("armour 50", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("deagle 2000", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("mp5", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("infernus", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("$9000", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
    GetThingFromString("BAM BOM HELLO", type, which, qty);
    GetStringFromThing(str, type, which, qty), print(str);
   
    new idx = 0, i = 0;
   
    while (GetThingFromString("hp 100, armour 50, mp5, deagle, infernus", type, which, qty, idx)) {
        GetStringFromThing(str, type, which, qty);
        printf("thing %d: %s", i++, str);
    }
}

enum e_THING_TYPES {
    THING_INVALID = -1,
   
    THING_NOTHING,
    THING_HEALTH,
    THING_ARMOUR,
    THING_WEAPON,
    THING_VEHICLE,
    THING_MONEY,
   
    // not implemented:
    THING_AMMO,
    THING_JETPACK,
    THING_SKIN,
    THING_COLOR,
    THING_FIGHTING_STYLE
}

static stock const gs_ThingName[][] = {"nothing", "health", "armour", "weapon", "vehicle", "money"};

static stock const gs_WeaponSearchNames[48][20 char] = {
    {!"fists"              }, {!"brassknuckles"     }, {!"golfclub"         }, //  0,  1,  2
    {!"nightsticknitestick"}, {!"knife"             }, {!"bat"              }, //  3,  4,  5
    {!"shovel"             }, {!"poolcue"           }, {!"katana"           }, //  6,  7,  8
    {!"chainsaw"           }, {!"dildo"             }, {!"vibrator"         }, //  9, 10, 11
    {!"vibrator"           }, {!"vibrator"          }, {!"flowers"          }, // 12, 13, 14
    {!"cane"               }, {!"grenades"          }, {!"teargas"          }, // 15, 16, 17
    {!"molotov"            }, {!""                  }, {!""                 }, // 18,      
    {!""                   }, {!"colt459mm"         }, {!"silencedpistol9mm"}, //     22, 23
    {!"deserteagledeagle"  }, {!"pumpregularshotgun"}, {!"sawnssawnoffs"    }, // 24, 25, 26
    {!"combatspas12"       }, {!"mac10uzi"          }, {!"mp5"              }, // 27, 28, 29
    {!"ak47ak-47"          }, {!"m4"                }, {!"tec9"             }, // 30, 31, 32
    {!"counrtyriflecuntgun"}, {!"sniperrifle"       }, {!"rocketlauncher"   }, // 33, 34, 35
    {!"heatseekingrpg"     }, {!"flamethrower"      }, {!"minigun"          }, // 36, 37, 38
    {!"satchel"            }, {!"detonator"         }, {!"spraycan"         }, // 39, 40, 41
    {!"extinguisher"       }, {!"camera"            }, {!"nightvision"      }, // 42, 43, 44
    {!"infrared"           }, {!"parachute"         }, {!"pistol"           }  // 45, 46, 47
};

static stock const gs_VehicleNames[212][18 char] = {
    {!"Landstalker"     }, {!"Bravura"        }, {!"Buffalo"          }, {!"Linerunner"       }, // 400, 401, 402, 403
    {!"Perrenial"       }, {!"Sentinel"       }, {!"Dumper"           }, {!"Firetruck"        }, // 404, 405, 406, 407
    {!"Trashmaster"     }, {!"Stretch"        }, {!"Manana"           }, {!"Infernus"         }, // 408, 409, 410, 411
    {!"Voodoo"          }, {!"Pony"           }, {!"Mule"             }, {!"Cheetah"          }, // 412, 413, 414, 415
    {!"Ambulance"       }, {!"Leviathan"      }, {!"Moonbeam"         }, {!"Esperanto"        }, // 416, 417, 418, 419
    {!"Taxi"            }, {!"Washington"     }, {!"Bobcat"           }, {!"Mr Whoopee"       }, // 420, 421, 422, 423
    {!"BF Injection"    }, {!"Hunter"         }, {!"Premier"          }, {!"Enforcer"         }, // 424, 425, 426, 427
    {!"Securicar"       }, {!"Banshee"        }, {!"Predator"         }, {!"Bus"              }, // 428, 429, 430, 431
    {!"Rhino"           }, {!"Barracks"       }, {!"Hotknife"         }, {!"Trailer 1"        }, // 432, 433, 434, 435
    {!"Previon"         }, {!"Coach"          }, {!"Cabbie"           }, {!"Stallion"         }, // 436, 437, 438, 439
    {!"Rumpo"           }, {!"RC Bandit"      }, {!"Romero"           }, {!"Packer"           }, // 440, 441, 442, 443
    {!"Monster"         }, {!"Admiral"        }, {!"Squalo"           }, {!"Seasparrow"       }, // 444, 445, 446, 447
    {!"Pizzaboy"        }, {!"Tram"           }, {!"Trailer 2"        }, {!"Turismo"          }, // 448, 449, 450, 451
    {!"Speeder"         }, {!"Reefer"         }, {!"Tropic"           }, {!"Flatbed"          }, // 452, 453, 454, 455
    {!"Yankee"          }, {!"Caddy"          }, {!"Solair"           }, {!"RC Van"           }, // 456, 457, 458, 459
    {!"Skimmer"         }, {!"PCJ-600"        }, {!"Faggio"           }, {!"Freeway"          }, // 460, 461, 462, 463
    {!"RC Baron"        }, {!"RC Raider"      }, {!"Glendale"         }, {!"Oceanic"          }, // 464, 465, 466, 467
    {!"Sanchez"         }, {!"Sparrow"        }, {!"Patriot"          }, {!"Quad"             }, // 468, 469, 470, 471
    {!"Coastguard"      }, {!"Dinghy"         }, {!"Hermes"           }, {!"Sabre"            }, // 472, 473, 474, 475
    {!"Rustler"         }, {!"ZR-350"         }, {!"Walton"           }, {!"Regina"           }, // 476, 477, 478, 479
    {!"Comet"           }, {!"BMX"            }, {!"Burrito"          }, {!"Camper"           }, // 480, 481, 482, 483
    {!"Marquis"         }, {!"Baggage"        }, {!"Dozer"            }, {!"Maverick"         }, // 484, 485, 486, 487
    {!"News Chopper"    }, {!"Rancher"        }, {!"FBI Rancher"      }, {!"Virgo"            }, // 488, 489, 490, 491
    {!"Greenwood"       }, {!"Jetmax"         }, {!"Hotring"          }, {!"Sandking"         }, // 492, 493, 494, 495
    {!"Blista"          }, {!"Police Maverick"}, {!"Boxville"         }, {!"Benson"           }, // 496, 497, 498, 499
    {!"Mesa"            }, {!"RC Goblin"      }, {!"Hotring A"        }, {!"Hotring B"        }, // 500, 501, 502, 503
    {!"Bloodring Banger"}, {!"Rancher"        }, {!"Super GT"         }, {!"Elegant"          }, // 504, 505, 506, 507
    {!"Journey"         }, {!"Bike"           }, {!"Mountain Bike"    }, {!"Beagle"           }, // 508, 509, 510, 511
    {!"Cropdust"        }, {!"Stuntplane"     }, {!"Tanker"           }, {!"Roadtrain"        }, // 512, 513, 514, 515
    {!"Nebula"          }, {!"Majestic"       }, {!"Buccaneer"        }, {!"Shamal"           }, // 516, 517, 518, 519
    {!"Hydra"           }, {!"FCR-900"        }, {!"NRG-500"          }, {!"HPV1000"          }, // 520, 521, 522, 523
    {!"Cement Truck"    }, {!"Tow Truck"      }, {!"Fortune"          }, {!"Cadrona"          }, // 524, 525, 526, 527
    {!"FBI Truck"       }, {!"Willard"        }, {!"Forklift"         }, {!"Tractor"          }, // 528, 529, 530, 531
    {!"Combine"         }, {!"Feltzer"        }, {!"Remington"        }, {!"Slamvan"          }, // 532, 533, 534, 535
    {!"Blade"           }, {!"Freight"        }, {!"Streak"           }, {!"Vortex"           }, // 536, 537, 538, 539
    {!"Vincent"         }, {!"Bullet"         }, {!"Clover"           }, {!"Sadler"           }, // 540, 541, 542, 543
    {!"Firetruck LA"    }, {!"Hustler"        }, {!"Intruder"         }, {!"Primo"            }, // 544, 545, 546, 547
    {!"Cargobob"        }, {!"Tampa"          }, {!"Sunrise"          }, {!"Merit"            }, // 548, 549, 550, 551
    {!"Utility"         }, {!"Nevada"         }, {!"Yosemite"         }, {!"Windsor"          }, // 552, 553, 554, 555
    {!"Monster"         }, {!"Monster"        }, {!"Uranus"           }, {!"Jester"           }, // 556, 557, 558, 559
    {!"Sultan"          }, {!"Stratum"        }, {!"Elegy"            }, {!"Raindance"        }, // 560, 561, 562, 563
    {!"RC Tiger"        }, {!"Flash"          }, {!"Tahoma"           }, {!"Savanna"          }, // 564, 565, 566, 567
    {!"Bandito"         }, {!"Freight Flat"   }, {!"Streak Carriage"  }, {!"Kart"             }, // 568, 569, 570, 571
    {!"Mower"           }, {!"Duneride"       }, {!"Sweeper"          }, {!"Broadway"         }, // 572, 573, 574, 575
    {!"Tornado"         }, {!"AT-400"         }, {!"DFT-30"           }, {!"Huntley"          }, // 576, 577, 578, 579
    {!"Stafford"        }, {!"BF-400"         }, {!"Newsvan"          }, {!"Tug"              }, // 580, 581, 582, 583
    {!"Trailer 3"       }, {!"Emperor"        }, {!"Wayfarer"         }, {!"Euros"            }, // 584, 585, 586, 587
    {!"Hotdog"          }, {!"Club"           }, {!"Freight Carriage" }, {!"Trailer 3"        }, // 588, 589, 590, 591
    {!"Andromada"       }, {!"Dodo"           }, {!"RC Cam"           }, {!"Launch"           }, // 592, 593, 594, 595
    {!"Police LS"       }, {!"Police SF"      }, {!"Police LV"        }, {!"Police Ranger"    }, // 596, 597, 598, 599
    {!"Picador"         }, {!"S.W.A.T. Van"   }, {!"Alpha"            }, {!"Phoenix"          }, // 600, 601, 602, 603
    {!"Glendale"        }, {!"Sadler"         }, {!"Luggage Trailer A"}, {!"Luggage Trailer B"}, // 604, 605, 606, 607
    {!"Stair Trailer"   }, {!"Boxville"       }, {!"Farm Plow"        }, {!"Utility Trailer"  }  // 608, 609, 610, 611
};

static stock FindWeaponID(const input[]) {
    new search[20], i = 0;
   
    while (0 < input[i] <= ' ') {
        i++;
    }
   
    if (!input[i]) {
        return -1;
    }
   
    while (0 < input[i] > ' ' && input[i] != ',') {
        i++;
    }
   
    strmid(search, input, 0, i);
   
    for (i = 0; i < sizeof(gs_WeaponSearchNames); i++) {
        if (!gs_WeaponSearchNames[i][0]) {
            continue;
        }
       
        if (strfind(gs_WeaponSearchNames[i], search, true) != -1) {
            return i;
        }
    }
   
    return -1;
}

static stock FindVehicleID(const input[]) {
    new match = INVALID_VEHICLE_ID, match_len;
   
    for (new i = 0; i < sizeof(gs_VehicleNames); i++) {
        if (strfind(gs_VehicleNames[i], input, true) != -1) {
            if (match != INVALID_VEHICLE_ID) {
                new len = strlen(gs_VehicleNames[i]);
               
                if (len < match_len) {
                    match = 400 + i;
                    match_len = len;
                }
            } else {
                match = 400 + i;
                match_len = strlen(gs_VehicleNames[i]);
            }
        }
    }
   
    return match;
}

stock GetThingFromString(const input[], &type, &which, &quantity, &index = 0) {
    new id;
   
    type = -1;
   
    while (0 < input[index] <= ' ') {
        index++;
    }
   
    if (!input[index]) {
        return 0;
    }
   
    if (!strcmp(input, "nothing", true)) {
        type = THING_NOTHING;
    } else if (!strcmp(input[index], "health", true, 6) ||
               !strcmp(input[index], "hp", true, 2)) {
        type = THING_HEALTH;
       
        if (tolower(input[index + 1]) == 'p') {
            index += 2;
        } else {
            index += 6;
        }
       
        quantity = strval(input[index]);
    } else if (!strcmp(input[index], "armour", true, 6) ||
               !strcmp(input[index], "armor", true, 5)) {
        type = THING_ARMOUR;
       
        if (tolower(input[index + 4]) == 'r') {
            index += 5;
        } else {
            index += 6;
        }
       
        quantity = strval(input[index]);
    } else if (input[index] == '$') {
        type = THING_MONEY;
       
        quantity = strval(input[index + 1]);
    } else if (-1 != (id = FindWeaponID(input[index]))) {
        type = THING_WEAPON;
        which = id;
        quantity = -1;
       
        while (input[index] && input[index] > ' ' && input[index] != ',') {
            index++;
        }
       
        if (input[index] && input[index] != ',') {
            quantity = strval(input[index]);
        }
    } else if (INVALID_VEHICLE_ID != (id = FindVehicleID(input[index]))) {
        type = THING_VEHICLE;
        which = id;
        quantity = -1;
    } else {
        return 0;
    }
   
    while (input[index] && input[index] != ',') {
        index++;
    }
   
    if (input[index] == ',') {
        index++;
    }
   
    return 1;
}

stock GivePlayerThing(playerid, type, which, quantity = 0) {
    // TODO
}

stock GetStringFromThing(output[], thing, which, quantity = 0, maxlength = sizeof(output)) {
    output[0] = '\0';
   
    switch (thing) {
        case THING_NOTHING: {
            strcat(output, "Nothing", maxlength);
        }
           
        case THING_HEALTH: {
            format(output, maxlength, "%d health", quantity);
        }
           
        case THING_ARMOUR: {
            format(output, maxlength, "%d armour", quantity);
        }
           
        case THING_WEAPON: {
            GetWeaponName(which, output, maxlength);
           
            if (quantity > -1) {
                format(output, maxlength, "%s with %d ammo", output, quantity);
            }
        }
           
        case THING_VEHICLE: {
            which -= 400;
           
            if (0 <= which < sizeof(gs_VehicleNames)) {
                strunpack(output, gs_VehicleNames[which], maxlength);
            } else {
                strcat(output, "Invalid vehicle", maxlength);
            }
        }
           
        case THING_MONEY: {
            format(output, maxlength, "$%d", quantity);
        }
           
        default: {
            strcat(output, "Invalid thing", maxlength);
        }
    }
}
Output:
Code:
[14:42:25] Nothing
[14:42:25] 20 health
[14:42:25] 50 armour
[14:42:25] Desert Eagle with 2000 ammo
[14:42:25] MP5
[14:42:25] Infernus
[14:42:25] $9000
[14:42:25] Invalid thing
[14:42:25] thing 0: 100 health
[14:42:25] thing 1: 50 armour
[14:42:25] thing 2: MP5
[14:42:25] thing 3: Desert Eagle
[14:42:25] thing 4: Infernus
Reply


Messages In This Thread
A good way to sort and present commands... - by Scenario - 30.07.2013, 04:02
Re: A good way to sort and present commands... - by JimmyCh - 30.07.2013, 11:44
Re: A good way to sort and present commands... - by Vince - 30.07.2013, 12:38
Re: A good way to sort and present commands... - by Scenario - 30.07.2013, 15:04
Re: A good way to sort and present commands... - by MP2 - 30.07.2013, 15:47
Re: A good way to sort and present commands... - by Kar - 30.07.2013, 16:25
Re: A good way to sort and present commands... - by Vince - 30.07.2013, 16:30
Re: A good way to sort and present commands... - by Scenario - 30.07.2013, 17:03
Re: A good way to sort and present commands... - by Slice - 05.08.2013, 09:10
Re: A good way to sort and present commands... - by Slice - 05.08.2013, 12:35
Re: A good way to sort and present commands... - by legodude - 06.08.2013, 09:55
Re: A good way to sort and present commands... - by Slice - 06.08.2013, 10:05
Re: A good way to sort and present commands... - by RealCop228 - 06.08.2013, 15:07
Re: A good way to sort and present commands... - by Slice - 06.08.2013, 16:59
Re: A good way to sort and present commands... - by RealCop228 - 06.08.2013, 17:09
Re: A good way to sort and present commands... - by [XST]O_x - 06.08.2013, 20:11
Re: A good way to sort and present commands... - by Macluawn - 06.08.2013, 20:24
Re: A good way to sort and present commands... - by Lorenc_ - 09.08.2013, 07:43
Re: A good way to sort and present commands... - by PinkFloydLover - 10.01.2014, 07:01

Forum Jump:


Users browsing this thread: 5 Guest(s)