Difference between revisions of "Ohms Law Calc"

From Second Life Wiki
Jump to navigation Jump to search
m (User:Fox Diller/ohmsLawCalc moved to Ohms Law Calc: Use Wiki folder)
m
Line 197: Line 197:
}
}
</pre>
</pre>
{{LSLC|Library}}{{LSLC|Examples}}
{{LSLC|Examples}}
{{#vardefine:sort|Ohm's Law Calculator}}
{{#vardefine:sort|Ohm's Law Calculator}}

Revision as of 04:49, 7 September 2007

Ohm's Law Calculator

///////////////////////////////////////////////////////////////////////
// Sprott-Shaw Degree College //// Ohm's Law Calculator Script ////////
///////////////// Copyright (C) 2007  Fox Diller //////////////////////
///////////////////////////////////////////////////////////////////////
//                                                                   //
// This program is free software; you can redistribute it and/or     //
// modify it under the terms of the GNU General Public License       //
// as published by the Free Software Foundation; either version 2    //
// of the License, or any later version.                             //
//                                                                   //
// This program is distributed in the hope that it will be useful,   //
// but WITHOUT ANY WARRANTY; without even the implied warranty of    // 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     //
// GNU General Public License for more details.                      //
//                                                                   //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the                     //
// Free Software Foundation, Inc.,                                   //
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.     //
//                                                                   //
//// Released under the GPL //////////////////// March 24th, 2007 /////
///////////////////////////////////////////////////////////////////////
////////////////////////////// Coded by: Fox Diller ///////////////////
///////////////////////////////////////////////////////////////////////

// [GLOBAL VARIABLES] // CHANGEABLE--
integer  dialog_chan                        = 42;
integer  input_chan                         = 0;
integer input_level                         = 0;
list    inputs;

// [CONSTANTS] // PLZ DO NO CHANGE...
string  CALC_NAME                           = "Ohm's Law Calculator";
string  CALC_DESC                           = "Sprott-Shaw Degree College - Magrathean Technologies";

string  SELECTION;

list    MENU_INITIAL                        = [ "Watts", "Ohms", "Volts", "Amps" ];

list    LIST_WATTS                          = [ "Volts", "Amps", "Ohms" ];
list    LIST_AMPS                           = [ "Volts", "Ohms", "Watts" ];
list    LIST_OHMS                           = [ "Volts", "Amps", "Watts" ];
list    LIST_VOLTS                          = [ "Amps", "Ohms", "Watts" ];

list    LIST_IN_USE;

// [LISTEN KEYS] //
integer DIALOG_LISTEN_KEY;
integer INPUT_LISTEN_KEY;

// [RUNTIME VARIABLES] //
key     OBJECT_KEY;
string  OBJECT_NAME;
key     OBJECT_OWNER_KEY;
string  OBJECT_OWNER_NAME;

  

// [CUSTOM FUNCTIONS] //
// + [MAIN MATH FUNCTIONS] //
float getWatts(float volts, float amps, float ohms)
{
    // llOwnerSay("V:" + (string)volts + " - A:" + (string)amps + " - R:" + (string)ohms); - DEBUG
    
    float result;
    
    if (volts == 0)
        result = llPow(amps, 2.0) * ohms;
    else if (amps == 0)
        result = llPow(volts, 2.0) / ohms;
    else if (ohms == 0)
        result = volts * amps;
    
    return result;
}

float getAmps(float volts, float ohms, float watts)
{
    float result;
    
    if (volts == 0)
        result = llSqrt((watts / ohms));
    else if (ohms == 0)
        result = watts / volts;
    else if (watts == 0)
        result =  volts / ohms;
    
    return result;
}

float getOhms(float volts, float amps, float watts)
{
    float result;
    
    if (volts == 0)
        result = watts / llPow(amps, 2.0);
    else if (amps == 0)
        result = llPow(volts, 2.0) / watts;
    else if (watts == 0)
        result = volts / amps;
    
    return result;
}

float getVolts(float amps, float ohms, float watts)
{
    float result;
    
    if (watts == 0)
        result = amps * ohms;
    else if (amps == 0)
        result = llSqrt((watts * ohms));
    else if (ohms == 0)
        result = watts / amps;
    
    return result;
}

// + [RUNTIME FUNCTIONS] //
initRunTimeVars()
{
    OBJECT_NAME         = CALC_NAME;
    OBJECT_KEY          = llGetKey();
    OBJECT_OWNER_KEY    = llGetOwner();
    OBJECT_OWNER_NAME   = llKey2Name(OBJECT_OWNER_KEY);   
}

// [DEFAULT BEGIN STATE] //
default
{
    state_entry()
    {
        // [Build Runtime Variables.] //
        initRunTimeVars();
        
        llSetObjectName(CALC_NAME);
        llSetObjectDesc(CALC_DESC);
        
        DIALOG_LISTEN_KEY = llListen(dialog_chan, OBJECT_OWNER_NAME, OBJECT_OWNER_KEY, "");
        
        // llSay(0, "Calculator Online!");
    }

    touch_start(integer total_number)
    {
        llDialog(llDetectedKey(0), "What value would you like to find out?", MENU_INITIAL, dialog_chan);        
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == dialog_chan)
        {
            INPUT_LISTEN_KEY = llListen(input_chan, OBJECT_OWNER_NAME, OBJECT_OWNER_KEY, "");
            llSay(0, "Please enter the values to find the [ "+message+" ], type 0 for null value.");
           
            if (message == "Watts")
                LIST_IN_USE = LIST_WATTS;
            else if (message == "Volts")
                LIST_IN_USE = LIST_VOLTS;
            else if (message == "Amps")
                LIST_IN_USE = LIST_AMPS;
            else if (message == "Ohms")
                LIST_IN_USE = LIST_OHMS;
                
            SELECTION = message;
                
            llSay(0, llList2String(LIST_IN_USE, input_level)+":");
        } 
        else
        {
            input_level++;
            
            inputs += message;
            
            if (input_level == 3)
            {
                // llOwnerSay(llList2CSV(inputs)); - DEBUG
                
                if (SELECTION == "Watts")
                    llSay(0, SELECTION + ": " + (string)getWatts(llList2Float(inputs, 0), llList2Float(inputs, 1), llList2Float(inputs, 2)));
                else if (SELECTION == "Volts")
                    llSay(0, SELECTION + ": " + (string)getVolts(llList2Float(inputs, 0), llList2Float(inputs, 1), llList2Float(inputs, 2)));
                else if (SELECTION == "Amps")
                    llSay(0, SELECTION + ": " + (string)getAmps(llList2Float(inputs, 0), llList2Float(inputs, 1), llList2Float(inputs, 2)));
                else if (SELECTION == "Ohms")
                    llSay(0, SELECTION + ": " + (string)getOhms(llList2Float(inputs, 0), llList2Float(inputs, 1), llList2Float(inputs, 2)));
                    
                llResetScript();
            }
            
            llSay(0, llList2String(LIST_IN_USE, input_level)+":");
        }
    }
}