Results 1 to 5 of 5

Thread: Zen's Script Gallery

  1. #1

    Zen's Script Gallery

    Below is a basic instance implementation for anyone who might be interested. It's not very complex, but beginners may find it interesting. Please note it does not support gmap or multi-level instances, though this would be a relatively simple modification. Maybe a good challenge for someone looking to learn?

    To implement this, replace some of the constants with default paths/levels.

    Control-Instance (NPC)

    PHP Code:
    /**
        Instance control npc.  Manages instances, and handles creation/destruction.
    */

    //Set instance level path, default exit level (along with x, y coords)
    function onCreated() {
      
    this.path "Instance Path";
      
    this.defaultExit "Default Exit Level";
      
    this.exitX 30;
      
    this.exitY 1;
      
    destroyInstances();
    }

    /**
        Params:
          levelName: String
          levelPath: String
          data: Any
          className: String

        Ret:
          id: String

        Creates new instance level and instance object.  Stores object in memory
        indexed by levelname (id).  New instance is constructed with standard
        instance class and user-defined instance class if provided.
    */
    public function createInstance(levelNamelevelPathdataclassName) {
      if (
    levelName == NULL) return;

      
    temp.id levelName.substring(0levelName.length() - 3) @ int(timevar2) @ ".nw";

      if (
    this.(@ id) != NULL) {
        
    sleep(1);
        
    temp.id createInstance(levelNamelevelPathdataclassName);
        return 
    id;
      }

      
    copyFile(levelPath levelNamethis.path id);
      
    this.(@ id) = new TStaticVar();
      
    this.(@ id).join("Instance");
      
    this.(@ id).init(iddata);
      
    temp.npc findLevel(id).putnpc2(3030"");
      
    temp.npc.join("Instance_Level");
      if (
    className != NULL) {
        
    temp.cnpc findLevel(id).putnpc2(3030"");
        
    temp.cnpc.join("" className "");
      }
      return 
    id;
    }

    /**
        Params:
          id: String
    */
    public function destroyInstance(id) {
      if (
    fileexists(this.path id)) {
        if (
    this.(@ id).playerCount 0this.(@ id).removePlayers(this.defaultExitthis.exitXthis.exitY);
        
    clearNPCs(id);
        
    deletefile(this.path id);
        if (
    this.(@ id) != NULLthis.(@ id).destroy();
      }
    }

    function 
    clearNPCs(levelName) {
      
    temp.level findLevel(levelName);
      for (
    temp.npc level.npcs) {
        
    npc.destroy();
      }
    }

    /**
        Params:
          id: String

        Ret:
          instance: Object
    */
    public function getInstanceByID(id) {
      return 
    this.(@ id);
    }

    //Destroy all instances when restarting server
    function destroyInstances() {
      
    temp.folder.loadfolder(this.path "*.nw"false);
      for (
    temp.temp.folder) {
        
    destroyInstance(temp.f);
      }
    }

    function 
    copyFile(srcdest) {
      
    temp.contents.loadstring(src);
      
    temp.contents.savestring(dest0);

    Instance_Creator (Class)

    PHP Code:
    function init(levelNamelevelPathdataclassName) {
      
    temp.npc findNPC("Control-Instance");
      
    this.id npc.createInstance(levelNamelevelPathdataclassName);
    }

    function 
    warpPlayer(plxy) {
      
    findplayer(pl).setlevel2(this.idxy);
    }

    function 
    warpPlayers(plsxy) {
      for (
    temp.pl pls) {
        
    findplayer(temp.pl).setlevel2(this.idxy);
      }
    }

    function 
    warpPlayerRandom(plxydxdy) {
      
    findplayer(pl).setlevel2(this.idrandom(-dxdx), random(-dydy));
    }

    function 
    warpPlayersRandom(plsxydxdy) {
      for (
    temp.pl pls) {
        
    findplayer(temp.pl).setlevel2(this.idrandom(-dxdx), random(-dydy));
      }

    Instance_Level (Class)

    PHP Code:
    // Get instance object
    function onCreated() {
      
    temp.npc findNPC("Control-Instance");
      
    this.instance npc.getInstanceByID(this.level.name);
      
    this.initiated false;
      
    init();
    }

    function 
    init() {
      if (
    findlevel(this.instance.getLevelName()).players.size() == 0) {
        
    sleep(5);
        
    init();
      } else {
        
    scheduleevent(30"UpdateInstance"NULL);
      }
    }

    //Check if players remain in instance, destroy if not
    function onUpdateInstance() {
      if (
    findlevel(this.instance.getLevelName()).players.size() == 0) {
        
    temp.npc findNPC("Control-Instance");
        
    npc.destroyInstance(this.level.name);
      }
      
    scheduleevent(30"UpdateInstance"NULL);

    Instance (Class)

    PHP Code:
    /**
        Params:
          levelName: String
          data: any
    */
    public function init(levelNamedata) {
      
    this.data data;
      
    this.levelName levelName;
    }

    // Returns current player count on level
    public function getPlayerCount() {
      return 
    arraylen(findLevel(this.levelName).players);
    }

    // Returns level name
    public function getLevelName() {
      return 
    this.levelName;
    }

    // Returns instance data
    public function getData() {
      return 
    this.data;
    }

    // Sets instance data
    public function setData(d) {
      
    this.data d;
    }

    // Removes all players from level
    public function removePlayers(destxy) {
      
    temp.lvl findLevel(this.levelName);
      for (
    temp.pl temp.lvl.players) {
        
    temp.pl.setlevel2(destxy);
      }

    Now this code really isn't great, there are several design inconsistencies, etc. It's also not useful for servers atm as I'd imagine most wouldn't want to just have single level instances. I'll update this soon with a better implementation! May still be of use to someone looking to build an instance system.
    Last edited by Grief Hero; 05-03-2019 at 04:53 PM. Reason: Made code easier to read.

  2. #2
    Sheikh bin sheikh Sottocapo imaginary's Avatar
    Join Date
    Jan 2014
    Location
    Golden desert with no camels roaming around
    Posts
    11,851
    I think you don’t know how to post pics or codes

  3. #3
    Global Moderator Grief Hero's Avatar
    Join Date
    Jun 2013
    Location
    n/a
    Posts
    1,223
    I modified your original post to use PHP tags instead of CODE, as it'll actually highlight the syntax somewhat accordingly. It also seems your posts are being deleted automatically cause you're reposting the same content multiple times.


    I saw. I conquered. I came.


  4. #4
    Quote Originally Posted by Grief Hero View Post
    I modified your original post to use PHP tags instead of CODE, as it'll actually highlight the syntax somewhat accordingly. It also seems your posts are being deleted automatically cause you're reposting the same content multiple times.
    Ah my bad, appreciate it. Originally uploaded code that neglected to clear NPC's on instance deletion, modified it so I figure most of the code was the same throughout. Thanks for letting me know!
    Last edited by Grief Hero; 05-03-2019 at 10:17 PM.

  5. #5
    I finished a carry/throw/place system for various objects the other day, and wanted to post it here again as reference for people looking to learn scripting. As a note, a lot of the functionality from this isn't just plug and play; for example, you would need to add checks if the player is carrying anything in a custom combat implementation/etc. Likewise you will need your own grab implementation for this code to work, here it is though, much of it will still be useful even though parts may require modification; also, credit where credit is due, some of the Carryable object was not written by me, basically all aside from onActionStoppedGrabbing and init;

    Carry (Weapon)
    PHP Code:
    function onActionServerSidecmdp1p2p3p4 ) {
      
    //Spawn object back into level
      
    switch ( cmd ) {
        case 
    "Spawn_Object":
          
    temp.npc putnpc2(p1p2"");
          for (
    temp.p4) {
            
    temp.npc.join(temp.c);
          }
          
    temp.npc.image p3;
        break;
      }
    }

    //#CLIENTSIDE
    function onCreated() {
      
    ObjectCarry this;
      
    player.carrying false;
    }
      
    function 
    onActionClientSidecmdp1p2p3 ) {
      
    //Set player/weapon attributes and carry object
      
    switch ( cmd ) {
        case 
    "Object_Carry":
          
    this.objectType p1;
          
    this.classlist p3;
          
    player.attr[20] = p2;
          
    this.onCarryObject();
        break;
      }
    }

    function 
    onCarryObject() {
      
    freezeplayer(1);
      
    setani("YOUR LIFT GANI HERE"nil);
      
    replaceani("idle""YOUR IDLE GANI HERE");
      
    replaceani("walk""YOUR WALK GANI HERE");
      
    player.carrying true;
    }

    function 
    onActionProjectile2(p1p2p3p4) {
      
    //When player object projectile reaches end of path, spawn an npc on the level
      
    if (p4 != player.account ) return;
      
    triggerServer("gui"this.name"Spawn_Object"p1 1p2p3this.classlist);
    }

    public function 
    onThrowObject() {
     
    //Make sure the player isn't throwing on a wall before throwing.
      
    temp.dx = (9.91 vecx(player.dir) + 0.5) / 20;
      
    temp.dy = (9.91 vecy(player.dir) + 1) / 20;
      
    temp.player.x;
      
    temp.player.y;
      
      for (
    temp.0temp.20temp.+= 1) {
        
    temp.+= temp.dx;
        
    temp.+= temp.dy;
        
        if (
    onWall(temp.xtemp.y)) {
          return;
        }
      }
      
    temp.XY = {player.vecx(player.dir), player.vecy(player.dir) - 1};


      
    setshootparams(player.attr[20], player.account);
      
    shoot(temp.XY[0], temp.XY[1], 1pi/* (player.dir 1), pi/40.9"YOUR PROJECTILE GANI HERE"player.attr[20]);

      
    setani("YOUR THROW GANI HERE"nil);
      
    replaceani("idle""idle");
      
    replaceani("walk""walk");
      
    player.carrying false;
    }

    public function 
    onPlaceObject() {
      
    //Place the object instead of throwing
      
    temp.player.vecx(player.dir);
      
    temp.player.vecy(player.dir);
      
    triggerServer("weapon"this.name"Spawn_Object"temp.0.5temp.1player.attr[20], this.classlist);
      
    setani("YOUR PLACE GANI HERE"nil);
      
    replaceani("idle""idle");
      
    replaceani("walk""walk");
      
    player.carrying false;
    }

    function 
    GraalControl.onKeyDown(keyCodekeyName) {
      if ( 
    keyName == "A" ) {
        if ( 
    player.carrying ) {
         
    //Throw the object in front of the player.
          
    this.onThrowObject();
        }
      } else
      if ( 
    keyName == "S" ) {
        if ( 
    player.carrying ) {
         
    //Place object in front of player.
          
    this.onPlaceObject();
        }
      }

    Carryable (Class)
    PHP Code:
    function onPlayerChats() {
      if ( !
    clientr.isStaff ) return;
      if ( 
    player.chat == ":killobjects" 
        
    this.destroy();
    }

    function 
    onCreated() setshape(13232);
      
    function 
    init(type) {
      
    this.type type;
      
    this.grabbed false;
    }

    function 
    onActionStoppedGrabbing() {
      if (!
    this.grabbed) {
        
    this.grabbed true;
        
    player.triggerclient("weapon""Carry""Object_Carry"this.typethis.imagethis.joinedclasses);
        
    this.destroy();
      }
    }

    //#CLIENTSIDE
    function onCreated() setshape(13232); 
    To elaborate a bit on what's going on. This is a system to define NPC objects as carryable. NPC's, when initiated, should have a type parameter (so you can select gani's of different sizes for objects of varying sizes). Replace "YOUR X GANI HERE" with "YOUR GANI PREFIX" @ this.type to achieve that effect. Likewise, make sure gani's use player attribute 20, and the projectile gani's use attr1. Also, onActionGrabbed and onActionStoppedGrabbing are custom events specified in your grab implementation. Hope someone finds this useful, feel free to comment questions/crtiticism!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •