Automatic Events


  • Forum Admin

    Automatic events will come to Avalonia and Corleone Online. Here is the first version of the "event_automaticcontrol" which is starting a random event when you say "autoevent":

    function onPlayerSays(pl) {
        if (pl.chat.startsWith("autoevent")) {
            if (pl.adminlevel >= 1)
                this.sendInvitation(pl);
            else
                pl.showmessage("You need to be admin to start events!");
            pl.chat = "";
        }
    }
    
    function setup() {
        this.logStart = "Automatic event: ";
        this.mode = "closed";
        this.minPlayerNr = 2;
        this.eventMaps = [
            {template:"inside_autoevent_water1", x:19, y:44}
        ];
        this.eventMap = "inside_autoevent";
        this.eventMapData = this.eventMaps[Math.floor(Math.random()*this.eventMaps.length)];
    }
    
    function sendInvitation(admin) {
        this.setup();
        let players = Server.searchplayers({});
        if (players.length == 0) {
            echo(this.logStart + "no players found to invite");
            return;
        }
        echo(this.logStart + "inviting " + players.length + " players!");
        if (admin)
            admin.showmessage("The event invitation has been sent to " + players.length + " players!");
        this.mode = "inviting";
        let message = "Would you like to join the event?" +
            " Once it starts you will be automatically put in the event map.";
        this.invitationTime = new Date().getTime();
        this.joinedPlayers = [];
        this.cancelevents("startevent");
        this.scheduleevent(15, "startevent");
        
        let self = this;
        players.forEach(function(pl) {
            pl.showpm(message, null, {type:"invitation"}, function(pl, answer) {
                if (answer == "yes" && self.mode == "inviting" && self.joinedPlayers.indexOf(pl) < 0) {
                    echo(self.logStart+ "player joined: " + pl.name);
                    self.joinedPlayers.push(pl);
                }
            });
        });
    }
    
    function onStartEvent() {
        if (this.joinedPlayers.length == 0) {
            echo(this.logStart + "no players joined");
            this.mode = "closed";
            return;
        }
        if (this.joinedPlayers.length < this.minPlayerNr) {
            echo(this.logStart + "not enough players joined");
            this.joinedPlayers.forEach(function(pl) { pl.showmessage("Sorry not enough players joined the event."); });
            this.mode = "closed";
            return;
        }
    
        // Warp to the event map
        echo(this.logStart + "joined players: " + this.joinedPlayers.length);
        let self = this;
        this.joinedPlayers.forEach(function(pl) {
            pl.setmap(self.eventMap, self.eventMapData.template, self.eventMapData.x, self.eventMapData.y);
        });
        this.scheduleevent(1, "starteventonmap");
    
        this.mode = "closed";
    }
    
    function onStartEventOnMap() {
        // Activate the event
        let npcs = Server.searchnpcs({map:this.eventMap, area:{x:0, y:0, w:100, h:100}});
        npcs.forEach(function(npc) {
            npc.scheduleevent(0.1,"startevent");
        });
    }
    

    In the event map you need to have one NPC with a function "onStartEvent", and in the NPC which controls the end of the event do following to warp the players back to where they came:

        // Warp players outside
        let players = Server.searchplayers({map:this.map});
        players.forEach(function(pl) {
            pl.setmap("outside","unknown",64,64);
            pl.showmessage("The event is over! Thank you for participating.");
        });
    

  • Forum Admin

    An example for the "Event-Control" NPC in the game map, here for Last-Man-Standing:
    The event is started in onStartEvent, then it's checking periodically if it's over, and then announcing the winner and warping players out of the map.

    It's using the new map.eventinfo object to configure if the map should be pvp (pk:true) and if users should be warped out on login (warpdead:true). The eventinfo is basically the same as previously managed events with /editevent

    function onStartEvent() {
        // Set pk mode
        this.map.eventinfo = {pk:true, warpdead:true, ignoretags:true};
    
        let arr = Server.searchPlayers({map:this.map.name});
        arr.forEach(function(pl) {
            pl.showmessage("Last Man Standing! Kill everyone else to win!");
            pl.heal();
        });
        echo(this.map.name + ": started LMS for " + arr.length + " players");
        this.settimeout(1);
    }
    
    function onTimeout() {
        if (!this.map.eventinfo || !this.map.eventinfo.pk)
            return;
    
        // Check who is still playing
        let gamePlayers = [];
        let arr = Server.searchplayers({map:this.map.name});
        arr.forEach(function(pl) {
            if (pl.hp > 0 && pl.clanname != "Admin")
                gamePlayers.push(pl);
        });
        if (gamePlayers.length > 1) {
            this.settimeout(1);
            return;
        }
    
        let message;
        if (gamePlayers.length == 1) {
            let winner = gamePlayers[0];
            message = "The LMS has been won by " + winner.name + "!";
            echo(this.map.name + ": " + message + " (" + winner.id + ")");
            //TODO: give prize
        } else {
            message = "The LMS event finished without a winner!";
            echo(this.map.name + ": " + message);
        }
        Server.message(message);
    
        // Warp players outside
        this.map.eventinfo = null;
        let mapPlayers = Server.searchplayers({map:this.map});
        mapPlayers.forEach(function(pl) {
            pl.setmap("outside","unknown",64,64);
        });
    }
    


  • Very very nice 🙂


Log in to reply