Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Classes... (https://forums.graalonline.com/forums/showthread.php?t=49814)

Termina_Owner 12-22-2003 05:07 AM

Classes...
 
Erm... To think of it, classes are things that could be added to almost most NPCs... I've been scripting for a couple of years (6 to be exact), and well, I just wanted to show some nifty tricks to shorten scripts down (I rather an 100 line script then an 800 one)..

Weapon Key Touches:

When firing a weapon put:
setarray this.keydown,10;
for (i=0;i<10;i++)
this.keydown[i] = (i == 4) ? 1 : 0;

Then, in the timeout loop, put keys();
then, outside everything, put:

function keys(){
for (i=0;i<10;i++)
this.keydown[i] = keydown(i) ? this.keydown[i] + 1 : 0;
}

That's for recording how much time you have a key down (The i == 4 thing is to avoid D being pressed twice in the same second, and thus to close the weapon selction or whatnot, you must release and press it again).

Here's another trick...

setstring commands,heal,die,unstick me;

command = lindexof(#c,commands);
if (command == 0) playerhearts = playerfullhearts;
if (command == 1) playerhearts = 0;
if (command == 2) warpto localstartarea.nw,30,30;

Simple tricks like that reduce the ammount of lines, or at least the ammount of space taken (repeatedly using strequals(#c,...)). And it is much more reliable if you want to make some more dimensional commands along those lines...

setstring commands,buy,add,remove,withdraw,deposit;
tokenize #c;
command = lindexof(#t(0),commands);
if (command < 1){ // User Commands
so-so;
} else { // Admin Commands
so-so;
}

That would be pretty useful if you want to make some player-oriented shops, or at least something of the kind.

Well.. These are two parts that I find useful right now... I'll tell you more later (I just came back from a party)

Termina_Owner 12-22-2003 06:36 AM

Okay... Here's another decent trick:

If you have a lot more options on your server, but don't want to end up with twenty differant set of varialbes... I'll show you some Binary Convertor and how it could help. For you who doesn't know binary, I'll explain shortly: Binary is a number composed of 0's and 1's...
1111 = 1 + 2 + 4 + 8 = 15
So basically, if you have number 15, and want to put it in binary, you'd have "1111" as number. How would this be useful?

Lets say you have 10 possible on and off options, but don't want a string to look like:
option=1,0,1,0,0,1,0,1,1,1
you could have it in a nice interger that hackers would have a hard time guessing..

Convert BINARY to DIGIT
(incoming: put "binary" string as a binary list ^ As with option)
function bintodig(){
for (i=0;i<sarraylen(binary);i++){
value = strtofloat(#I(binary,i));
number = (value == 1) ? number + 2^i : number;
}
}
(outcoming: variable "number")
--
(incoming: put variable "number" in)
function digtobin(){
num = number;
len = 0;
for (i=0;len == 0;i++){
len = num < 2^i ? i : 0;
}
setstring binary,;
for (i=(len-1);i>=0;i--){
value = number < 2^i ? 0 : 1;
insertstring binary,0,#v(value);
number = (number < 2^i) ? number : number - 2^i;
}
}
(output: String List "Binary")

... There...
Basically, if you want to test that, you could do this:

if (playerchats){
number = #v(strtofloat(#c));
digtobin();
message #s(binary);
}

^ if you say "15", it'll give you 1,1,1,1. If you say 16, it'll give you 0,0,0,0,1. And so on... This could shorten your script, however it could also never be placed anywhere at all.

Hope this helps someone... :P

- Rance Vicious

ForgottenLegacy 12-22-2003 07:22 AM

Can you please put that in CODE tags?
NPC Code:
Like this


Termina_Owner 12-22-2003 07:37 AM

When firing a weapon put:
NPC Code:

setarray this.keydown,10;
for (i=0;i<10;i++)
this.keydown[i] = (i == 4) ? 1 : 0;



Then, in the timeout loop, put keys();
then, outside everything, put:

NPC Code:

function keys(){
for (i=0;i<10;i++)
this.keydown[i] = keydown(i) ? this.keydown[i] + 1 : 0;
}



NPC Code:

command = lindexof(#c,commands);
if (command == 0) playerhearts = playerfullhearts;
if (command == 1) playerhearts = 0;
if (command == 2) warpto localstartarea.nw,30,30;


NPC Code:

setstring commands,buy,add,remove,withdraw,deposit;
tokenize #c;
command = lindexof(#t(0),commands);
if (command < 1){ // User Commands
so-so;
} else { // Admin Commands
so-so;
}



\----\

Binary to Digit:
NPC Code:

function bintodig(){
for (i=0;i<sarraylen(binary);i++){
value = strtofloat(#I(binary,i));
number = (value == 1) ? number + 2^i : number;
}
}



Digit to Binary:
NPC Code:

function digtobin(){
num = number;
len = 0;
for (i=0;len == 0;i++)
len = num < 2^i ? i : 0;
setstring binary,;
for (i=(len-1);i>=0;i--){
value = number < 2^i ? 0 : 1;
insertstring binary,0,#v(value);
number = (number < 2^i) ? number : number - 2^i;
}
}



Want to Test it out?
NPC Code:

if (playerchats){
number = #v(strtofloat(#c));
digtobin();
message #s(binary);
}

function digtobin(){
num = number;
len = 0;
for (i=0;len == 0;i++)
len = num < 2^i ? i : 0;
setstring binary,;
for (i=(len-1);i>=0;i--){
value = number < 2^i ? 0 : 1;
insertstring binary,0,#v(value);
number = (number < 2^i) ? number : number - 2^i;
}
}



Well... Hope that helps.

osrs 12-22-2003 03:20 PM

Erm, nice gift. :|

tlf288 12-22-2003 05:30 PM

nice. i really like the binary functions.

Termina_Owner 12-22-2003 11:32 PM

The binary ones should have been added to Graal a long time ago, but then again, I don't think much people think in binary anymore... It's a simple reproduction of what it would be in Graal's programming language. Erm...

Ahh... Another trick, that's pretty simple that I see little people use:

NPC Code:

if (weaponfired){
if (this.on == 0){
this.on = 1;
timeout = 0.05;
}
if (this.on == 1) this.on = 0;
}



Whereas that could just be used as:
NPC Code:

if (weaponfired){
this.on = (this.on + 1)%2;
timeout = 0.05;
}


or
NPC Code:

if (weaponfired){
this.on = this.on == 0 ? 1 : 0;
timeout = 0.05;
}


or
NPC Code:

if (weaponfired){
this.on = abs(this.on - 1);
timeout = 0.05;
}



There is many ways of doing it... I personally rather the:
NPC Code:

this.on = (this.on + 1)%2;



Think of any other way that goes in one line?

- Rance Vicious

ZeLpH_MyStiK 12-23-2003 12:22 AM

heh, tnx, I think =/

osrs 12-23-2003 01:25 AM

You probably posted all of them.

Python523 12-23-2003 01:33 AM

Quote:

Originally posted by Termina_Owner
stuff
eww, %
this.on = 1 - this.on;

Lance 12-23-2003 02:01 AM

Quote:

Originally posted by Python523


eww, %
this.on = 1 - this.on;

You beat me to it. :)

Termina_Owner 12-23-2003 10:10 AM

Ohh.. this.on = 1 - this.on;... Haven't thought of that one! Nice. I don't mind %'s as much... Learnt to like them so much I use it... Seems reliable to me, isn't it? Anyhow, most people don't understand it.

Erm... New things?

Ahh... I'll be making some tutorial on string-based weapon system... I made one before, on my forum, however it got deleted ;P

- RV

Hevaricubed 12-23-2003 06:53 PM

NPC Code:
NPC Code:

// NPC made by R0bin
if (playerchats&&startswith(dec,#c)) {
tokenize #c;
if (strlen(#t(1)) < 10) {
a=strtofloat(#t(1));
setplayerprop #c,;
message Converting #v(a) into binary!;
sleep 0.5;
setstring d,;
setstring bin,;
b=a;
while (b!=0) {
b=int(a/2);
c=a%2;
setstring d,#v(c)#s(d);
a=b;
}
message #s(d);
} else {
message Want to lag graal?;
}
}




NPC Code:
NPC Code:

if (playerchats&&startswith(bin,#c)) {
setstring str.a,#c;
setplayerprop #c,;
setarray arr.b,strlen(#s(str.a));
message Converting #s(str.a) into decimal!;
sleep 0.5;
arr.b[0] = 1;
for (i=0;i<strlen(#s(str.a));i++;) {
if (i > 0) arr.b[i] = arr.b[i-1]*2;
}
k=strlen(#s(str.a));
for (i=0;i<arraylen(arr.b);i++;) {
k--;
l=strtofloat(#e(k,1,#s(str.a)));
m=l*arr.b[i];
j=j+m;
}
message #v(j);
}




Just incase you dont understand all that

? :

stuff :P

Termina_Owner 12-23-2003 08:55 PM

? :


flag ? True Result : False Result;

Like:
NPC Code:

if (playerenters){
a = 1;
if (a == 1){
a = 1;
} else if (a != 1){
a = 2;
}
message #v(a);
}


is the same as:
NPC Code:

if (playerenters){
a = 1;
a = a == 1 ? 1 : 2;
message #v(a);
}



:P For those who doesn't know.

Nice technique though Hev... Haven't thought of it like that, but I guess they also work.

zell12 12-23-2003 09:15 PM

Wow, I am never taking cal.

Termina_Owner 12-23-2003 10:20 PM

lol... This is old school for me... Damn Math class never taught me something new, so I learnt all this myself. I kinda with I didn't, because now the teacher's threatening me to send me to low math (Even though I have an above average mark) because I speak too much, or listen to her too little. (Talk about Attention Freaks.)

Luigi1 12-23-2003 11:36 PM

Quote:

Originally posted by osrs
Erm, nice gift. :|
It's almost Christmas. ;)

Termina_Owner 12-23-2003 11:51 PM

*shifts eyes from left to right to left again*

Erm.. Christmas... Where's my present! :P Merry Christmas People!


All times are GMT +2. The time now is 02:31 PM.

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