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 02-02-2008, 04:03 PM
Chompy Chompy is offline
»\(║_o)/»
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
My weird file parser?

PHP Code:
/*  scripted by Chompy
      
      # prefix = notes
      $ prefix = global variables (ie. author)
      
      $ variables Always on top!

      All spaces must be double spaced!
        (Not in params/variables though!)
        
      Global vars can only contain 1 member/value,
        it's a string, not an array
*/
function onCreted() {
  
this.global_variables = {"author"};
}
public function 
compile(infomode) {
  
temp.lines cleanfile(infomode);
  
temp.global = findglobalvars(lines);
  
  for(
temp.: global) {
    if (
g[0in this.global_variables) {
      
this.global.(@ g[0]) = g[1];
    }
  }
  
this.info.variables 0;
  
this.info.id 0;
  for (
temp.line lines.subarray(global.size() - 1)) {
    
//lets jump over the global vars
    
temp.tokens line.tokenize("  ");
    
temp.str convertParamsToString(tokens.subarray(3));
    
temp.arr convertStringToArray(str);
    
    
this.data.(@ tokens[1]) = arr;
    
this.info.variables.add({tokens[2], tokens[1]});
    
this.info.id.add({tokens[0], tokens[1]});
  }
  
this.info.iscompiled true;
}
public function 
findidbyname(str) {
  if (
this.info.iscompiled) {
    for(
temp.key this.info.id) {
      if (
key[1] == str) return key[0];
    }
  }return -
1;
}
public function 
findvarbyid(id) {
  if (
this.info.iscompiled) {
    for(
temp.key this.info.id) {
      if (
key[0] == id) return this.data.(@key[1]);
    }
  }return 
0;
}
public function 
findvarbyname(name) {
  if (
this.info.iscompiled) {
    for(
temp.key this.info.variables) {
      if (
key[0] == name) return this.data.(@key[1]);
    }
  }return 
0;
}
function 
convertStringToArray(s) {
  
out 0;
  for(
temp.s.tokenize(",")) out.add(a);
  return 
out;
}
function 
convertParamsToString(p) {
  
temp.out 0;
  
temp.ar 0;
  for(
temp.p) {
    if (
a.starts("|")) out a.substring(1a.length() - 1);
    else if (
a.starts("'")) {
      
ar 1;
      
out @= " " a.substring(1a.length() - 1);
      continue;
    }else if (
a.ends("|")) {
      
out @= " " a.substring(0a.length() - 1);
    }else if (
a.ends(",") && !a.ends("',")) {
      
out @= " " a.substring(0a.length() - 1) @ ",";
    }
    if (
ar) {
      if (
a.ends(".'|")) {
        
ar 0;
        
out out.substring(0out.length() - 1);
        break;
      }else if (
a.ends("',")) {
        
ar 0;
        
out @= " " a.substring(0a.length() - 3);
        
out @= a.substring(a.pos("',") - 11) @ ",";
      }else 
out @= " " a;
    }
  }
  return 
out;
}
function 
cleanfile(filemode) {
  
temp.fakefile 0;
  if (
modefakefile.loadlines(file);
  else 
fakefile file;
  if (
fakefile.type() == 3) {
    
temp.out 0;
    for(
temp.line fakefile) {
      if (!(
line.starts("#")) && line != "" && !(line.length() in {04})) {
        
out.add(line);
      }
    }
    return 
out;
  }
  return 
0;
}
function 
findglobalvars(lines) {
  
temp.out = {-1};
  
temp.0;
  for(
temp.line lines) {
    if (
line.starts("$")) {
      
out[0] = i;
      
out.add({line.tokenize("  ")[0].substring(1), line.tokenize("  ")[1]});
    }
    
++;
  }
  return 
out;

For example, put this in a class named.. err... maybe 'test_file'?

Now, we need a test file, or an array with members responding to the lines that the file has:

NPC Code:

# Global variables listed below
$author Chompy
# Skills listed below
# ID GLOBALNAME VARNAME |VARIABLES/PARAMS|
1 SKILL_RUN run |Running, 'Will allow you to run.', 1, 1.5|
2 SKILL_JUMP jump |Jump, 'Will allow you to jump.'|




Now, how the functions work, example:

PHP Code:
function onCreated() {
  
join("test_file");

  
compile("mud/testfile.txt"true);

  
/*  First param is either the filepath (param2 must be true then), or
       an actual array (Read above code tags and param2 must be false).
       Basicly what it does is reading the array/file, then convert it into  
       readable variables. How to read them; Example: */

  
echo(this.global.author); // Chompy
  
echo(this.info.variables); // "run,SKILL_RUN","jump,SKILL_JUMP"
  
echo(this.info.id); // "1,SKILL_RUN","2,SKILL_JUMP"
  
echo(this.data.SKILL_RUN); // Running,"Will allow you to run.",1,1.5
  
echo(this.data.SKILL_JUMP); // Jump,"Will allow you to jump."

  
echo(findvarbyid(1)); // Running,"Will allow you to run.",1,1.5
  
echo(findvarbyname("run")); // Running,"Will allow you to run.",1,1.5
  
echo(findidbyname("SKILL_RUN")); // 1
  
  
if (this.info.iscompiled) {
    echo(
"The file has been compiled through compile()!");
    
// echoes: The file has been compiled through compile()!
  
}

AND, I was bored once again . Enjoy.. my boredness?
__________________
Reply With Quote
  #2  
Old 02-02-2008, 06:03 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
Very nice. I have no clue what it does, but it looks wicked. rep++ in like 8 hours.
__________________
Reply With Quote
  #3  
Old 03-16-2008, 11:55 PM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
function onCreted isNotSpelledRight on line 14 "function onCreted() {"
lol.
Awesome stuff
__________________
Trying to be nice now...
Reply With Quote
  #4  
Old 03-17-2008, 12:02 AM
Chompy Chompy is offline
»\(║_o)/»
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by bscharff View Post
function onCreted isNotSpelledRight on line 14 "function onCreted() {"
lol.
Awesome stuff
Lol, well, I edited it on the go though xD So I might of done a typo there, since when I make stuff I use the onCreated event, and when I'm done then make the function for the parsing itself and make it return instead of echo'ing
__________________
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 12:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.