Ticket #1555: cheats.2.patch

File cheats.2.patch, 7.0 KB (added by O.Davoodi, 12 years ago)

new version

  • gui/session/messages.js

     
    99const MAX_NUM_NOTIFICATION_LINES = 3;
    1010var notifications = [];
    1111var notificationsTimers = [];
     12var cheatList = parseJSONData("simulation/data/cheats.json").Cheats;
    1213
    1314// Notifications
    1415function handleNotifications()
     
    195196    }
    196197
    197198    var message = escapeText(msg.text);
    198 
     199   
    199200    var formatted;
    200201
     202    var isCheat = false;
     203   
    201204    switch (msg.type)
    202205    {
    203206    case "connect":
     
    207210        formatted = "[color=\"" + playerColor + "\"]" + username + "[/color] has left the game.";
    208211        break;
    209212    case "message":
    210         console.write("<" + username + "> " + message);
    211         formatted = "<[color=\"" + playerColor + "\"]" + username + "[/color]> " + message;
     213        if (!g_IsNetworked)
     214        {
     215            for (var i = 0; i < cheatList.length; i++)
     216            {
     217                if (message.indexOf(cheatList[i].Name)>-1)
     218                {
     219                    if (cheatList[i].IsNumbered)
     220                    {
     221                        var number = message.substr(cheatList[i].Name.length+1, message.length-1).valueOf();
     222                        if (!(number > 0))
     223                            number=cheatList[i].DefaultNumber;
     224                    }
     225                    else
     226                    {
     227                        var number = undefined;
     228                    }
     229                    Engine.PostNetworkCommand({"type": "cheat", "action": cheatList[i].Action, "number": number , "selected": g_Selection.toList()});
     230                    isCheat = true;
     231                }
     232            }
     233        }
     234        if (!isCheat)
     235        {
     236            console.write("<" + username + "> " + message);
     237            formatted = "<[color=\"" + playerColor + "\"]" + username + "[/color]> " + message;
     238        }
    212239        break;
    213240    default:
    214241        error("Invalid chat message '" + uneval(msg) + "'");
  • simulation/components/Player.js

     
    2828    this.startCam = undefined;
    2929    this.controlAllUnits = false;
    3030    this.isAI = false;
     31    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     32    if (!cmpPlayerMan)
     33        return;
     34    var numNAI = 1;
     35    for (var i = 0; i < cmpPlayerMan.GetNumPlayers; i++)
     36    {
     37        var playerEnt = cmpPlayerMan.GetPlayerByID(i);
     38        if (!playerEnt.IsAI)
     39            ++numNAI;
     40    }
     41    if ((numNAI == 1)||(this.IsAI))
     42    {
     43        this.cheatsEnabled = true;
     44    }
     45    else
     46    {
     47        this.cheatsEnabled = false;
     48    }
    3149};
    3250
    3351Player.prototype.SetPlayerID = function(id)
     
    382400    }
    383401};
    384402
     403Player.prototype.Cheat = function(input)
     404{
     405    if (this.cheatsEnabled)
     406    {
     407        if (input.action == "addfood")
     408        {
     409            this.AddResource("food", input.number);
     410        }
     411        else if (input.action == "addwood")
     412        {
     413            this.AddResource("wood", input.number);
     414        }
     415        else if (input.action == "addmetal")
     416        {
     417            this.AddResource("metal", input.number);
     418        }
     419        else if (input.action == "addstone")
     420        {
     421            this.AddResource("stone", input.number);
     422        }
     423        else if (input.action == "revealmap")
     424        {
     425            var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     426            cmpRangeManager.SetLosRevealAll(-1, true);
     427        }
     428        else if (input.action == "maxpopulation")
     429        {
     430            this.popBonuses += 500;
     431        }
     432        else if (input.action == "changemaxpopulation")
     433        {
     434            this.maxPop = 500;
     435        }
     436        else if (input.action == "controlallunits")
     437        {
     438            this.SetControlAllUnits(true);
     439        }
     440        else if (input.action == "convertunit")
     441        {
     442            for each (var ent in input.selected)
     443            {
     444                var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
     445                cmpOwnership.SetOwner(this.playerID);
     446            }
     447        }
     448        else if (input.action == "killunits")
     449        {
     450            for each (var ent in input.selected)
     451            {
     452                var cmpHealth = Engine.QueryInterface(ent, IID_Health);
     453                if (cmpHealth)
     454                    cmpHealth.Kill();
     455                else
     456                    Engine.DestroyEntity(ent);
     457            }
     458        }
     459        else if (input.action == "defeatplayer")
     460        {
     461            var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     462            if (!cmpPlayerMan)
     463                return;
     464            var playerEnt = cmpPlayerMan.GetPlayerByID(input.number);
     465            if (playerEnt == INVALID_ENTITY)
     466                return;
     467            Engine.PostMessage(playerEnt, MT_PlayerDefeated, { "playerId": input.number } );
     468        }
     469        else if (input.action == "heroes")
     470        {   
     471            var spawn = Engine.QueryInterface(input.selected[0], IID_TrainingQueue);
     472            if (!spawn)
     473                return;
     474            for (var i = 0; i < input.number; i++)
     475            {
     476                var templates = ["units/cart_hero_hamilcar", "units/cart_hero_hannibal", "units/cart_hero_maharbal",
     477                                 "units/rome_hero_marcellus", "units/rome_hero_maximus", "units/rome_hero_scipio",
     478                                 "units/pers_hero_cyrus", "units/pers_hero_darius", "units/pers_hero_xerxes"];
     479                spawn.SpawnUnits (templates[i%9],1, null);
     480            }
     481        }
     482        if (this.name.indexOf(" the Cheater")==-1)
     483            this.name = this.name + " the Cheater";
     484    }
     485};
     486
    385487Engine.RegisterComponentType(IID_Player, "Player", Player);
  • simulation/data/cheats.json

     
     1{
     2    "Cheats":
     3    [
     4        {
     5            "Name": "i want pizza",
     6            "Action": "addfood",
     7            "IsNumbered": true,
     8            "DefaultNumber": 1000
     9        },
     10        {
     11            "Name": "bring me my axe",
     12            "Action": "addwood",
     13            "IsNumbered": true,
     14            "DefaultNumber": 1000
     15        },
     16        {
     17            "Name": "your money or your life",
     18            "Action": "addmetal",
     19            "IsNumbered": true,
     20            "DefaultNumber": 1000
     21        },
     22        {
     23            "Name": "isee a mountain here",
     24            "Action": "addstone",
     25            "IsNumbered": true,
     26            "DefaultNumber": 1000
     27        },
     28        {
     29            "Name": "jame jam",
     30            "Action": "revealmap",
     31            "IsNumbered": false
     32        },
     33        {
     34            "Name": "the hive master",
     35            "Action": "maxpopulation",
     36            "IsNumbered": false
     37        },
     38        {
     39            "Name": "TARDIS",
     40            "Action": "changemaxpopulation",
     41            "IsNumbered": false
     42        },
     43        {
     44            "Name": "iwanttopwnthem",
     45            "Action": "heroes",
     46            "IsNumbered": true,
     47            "DefaultNumber": 1
     48        },
     49        {
     50            "Name": "wololo",
     51            "Action": "convertunit",
     52            "IsNumbered": false
     53        },
     54        {
     55            "Name": "black death",
     56            "Action": "killunits",
     57            "IsNumbered": false
     58        },
     59        {
     60            "Name": "hehehehehehehehe",
     61            "Action": "defeatplayer",
     62            "IsNumbered": true,
     63            "DefaultNumber": 2
     64        },
     65        {
     66            "Name": "pandoras box",
     67            "Action": "controlallunits",
     68            "IsNumbered": false
     69        }
     70    ]
     71}
  • simulation/helpers/Commands.js

     
    3131    case "debug-print":
    3232        print(cmd.message);
    3333        break;
    34 
     34   
    3535    case "chat":
    3636        var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    3737        cmpGuiInterface.PushNotification({"type": "chat", "player": player, "message": cmd.message});
    3838        break;
    3939       
     40    case "cheat":
     41        cmpPlayer.Cheat(cmd);
     42        break;
     43       
    4044    case "quit":
    4145        // Let the AI exit the game for testing purposes
    4246        var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);