Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-27-2008, 03:21 AM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Music Controller

A script that controls what song is played, and when.


Graal Script Code:
//#CLIENTSIDE
function onCreated()
{
  
this.songList = new TStaticVar();
  
  
/*
    this.songList.SONGNAME = { ARG1,
      { ARG2 }
    };
    ARG1 = determines if the song should stop playing when
           the players level name isn't in ARG2.
    ARG2 = an array with the levels the song should be played
           on, or what the level name should begin with.
           
           ~~~ EXAMPLE ~~~
           
    this.songList.("My Song Name") = { false,
      { "mylevelname.nw", "mylevelname2.nw", "my_gmap_" }
    };
  */
}

function 
onPlayerEnters()
{
  
temp.songList this.songList.getDynamicVarNames();
  for (
temp.songtemp.songList) {
    
temp.data this.songList.(@temp.song);
    
temp.song temp.song ".mp3";
    
temp.file getmusicfilename();
    
    
// Is temp.data[1] the players current level?
    
if (player.level.name in temp.data[1]) {
      if (
fileExists(temp.song)) playlooped(temp.song);
    }
    
// Ah well... is the player on a gmap, or does temp.data[1]
    // contain what the players level start with?
    
else if (isonmap || onSpecialLevel(temp.data[1])) {
      for (
temp.leveltemp.data[1]) {
        if (
player.level.name.starts(temp.level)) {
          if (
fileExists(temp.song)) playlooped(temp.song);
          break;
        }
      }
    }
    
    
// Should the song play in this level?
    
if (isMusicPlaying() && temp.file.ends(temp.song)) {
      if (
temp.data[0] == true && !(player.level.name in temp.data[1]) && !onSpecialLevel()) stopMusic();
    }
  }
}

function 
onSpecialLevel(levels)
{  
  for (
temp.levellevels) {
    if (
temp.level.name.ends(".nw")
     || 
temp.level.name.ends(".graal")) continue;
    
    if (
player.level.name.starts(temp.level)) return temp.level;
  }

As always, please, use the forums reputation system to show if you like the script or not.
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
  #2  
Old 06-27-2008, 02:59 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
As a firm believer in code readability of irrational proportions, I must object to the ambiguity of temp.data[i].
Reply With Quote
  #3  
Old 06-27-2008, 07:29 PM
TheStan TheStan is offline
Stan?
Join Date: May 2008
Location: U.S.
Posts: 100
TheStan is on a distinguished road
Send a message via AIM to TheStan Send a message via MSN to TheStan
Excellent script Ziro. Tho' I might not be using the ENTIRE script. I do plan on using the format you used to list your songs. :O
Reply With Quote
  #4  
Old 06-27-2008, 07:54 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by DrakilorP2P View Post
As a firm believer in code readability of irrational proportions, I must object to the ambiguity of temp.data[i].
Yes, his code is horribly formatted, but it's personal preference of course.

Looks cool, I wish Graal offered more control for music, it's been brought up many times.
__________________
Reply With Quote
  #5  
Old 01-30-2010, 05:36 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Here's a new and much much better version of it:

Graal Script Code:
//#CLIENTSIDE
function onCreated()
{
  
this.songList = new TStaticVar();
  
  
// Music file stored locally
  
this.songList.("My Example Music.mp3") = {
    { 
"mylevel.nw""my_gmap" }
  };
  
  
// Streamed song
  
this.songList.("My Example Music 2.mp3") = {
    { 
"mylevel2.nw""my_gmap2" },
    
"http://www.myexamplewebsite.com/music/"
  
};
}

function 
onPlayerEnters()
{
  
// Get the list of songs
  
temp.songList this.songList.getDynamicVarNames();
  
  for (
temp.songtemp.songList) {
    
// Data specific to this song
    
temp.data this.songList.(@temp.song);
    
// Is the song being streamed or played locally?
    
temp.streamed = (temp.data[1] == "" false true);
    
temp.volumetype = (temp.streamed "radiovolume" "mp3volume");
    
// Song name
    
temp.songpath = (temp.streamed temp.data[1] @ temp.song temp.song);
    
temp.printname extractfilebase(temp.song);
    
// File name
    
temp.currentsong extractfilename(getmusicfilename());
    
    
// If the current song isn't supposed to play in this area,
    // check if it's currently playing and stop it, then continue
    // with the next song.
    
if (!(player.level.name in temp.data[0])
     && !
onSpecialLevel(temp.data[0])) {
      if (
temp.currentsong == temp.song) {
        
player.chat "Stopped playing \"" temp.printname "\"...";
        
stopMusic();
      }
      continue;
    }
    
    
// Is the player already playing this song?
    
if (temp.currentsong == temp.song)
      break;
    else if (
temp.currentsong != temp.song)
      
stopMusic();
    
    
// If this song isn't streamed, does the player have it?
    
if (temp.streamed)
      
temp.fileexists true;
    else
      
temp.fileexists fileExists(temp.song);
    
// If he hasn't, warn about it and then stop.
    
if (!temp.fileexists) {
      
player.chat "Couldn't play \"" temp.printname "\"! Download from Start > Install Packages > Audio/Music";
      break;
    }
    
    
// Ok, let's see if volume is enabled or not!
    
temp.volumeenabled = (makevar("$pref::audio::" temp.volumetype) == false true);
    
// If it's not, warn about it and then stop.
    
if (!temp.volumeenabled) {
      
player.chat =  "Couldn't play \"" temp.printname "\"! Make sure your" SPC temp.volumetype SPC "is turned on in F3 options!";
      break;
    }
    
    
playlooped(temp.songpath);
    
player.chat "Playing \"" temp.printname "\"...";
  }
}

function 
onSpecialLevel(levels)
{  
  for (
temp.levellevels) {
    if (
temp.level.name.ends(".nw")
     || 
temp.level.name.ends(".graal"))
      continue;
    
    if (
player.level.name.starts(temp.level))
      return 
temp.level;
  }

__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:15 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.