cassowary

From NWN Lexicon
Jump to navigationJump to search
Nwnee logo.jpg Note: This article documents Neverwinter Nights: Enhanced Edition new content or changes/updates/fixes to 1.69 functions. These are all listed under the category and patches pages.

A reference to an cassowary in the module. Similar to object these are shared - so making changes to a given created cassowary will alter it for anyone else accessing it later.

If you're not sure what a cassowary is, it's not the bird but instead a type of solver. NWN uses them internally to resize the newer GUI windows. You can use them to do solving by inputting data in, and ask it to give you data out.


Example

This very simple example is based off this cassowary example page that uses Python. Note unlike the Python library NWN has not got weights (only strengths, although these can be user defined to be anything) and variables do not need to be defined by themselves before use in a cassowary constraint.

Since it is good practice to not carry on with an error, additional error checking has been added which stops the script and debug will write to the script log in case something goes wrong.

A cut down version is further down.

// Basic Cassowary Test
void Debug(string sDebug);

void main()
{
    // Generate cassowary
    cassowary cTest;
    string sResult;

    Debug("Cassowary Script Start");

    // Some constraints
    sResult = CassowaryConstrain(cTest, "middle == (left + right) / 2");
    if(sResult != "")
    {
        Debug("Cassowary error: " + sResult);
        return;
    }

    sResult = CassowaryConstrain(cTest, "right == left + 10");
    if(sResult != "")
    {
        Debug("Cassowary error: " + sResult);
        return;
    }

    sResult = CassowaryConstrain(cTest, "right <= 100");
    if(sResult != "")
    {
        Debug("Cassowary error: " + sResult);
        return;
    }

    sResult = CassowaryConstrain(cTest, "left >= 0");
    if(sResult != "")
    {
        Debug("Cassowary error: " + sResult);
        return;
    }

    // Debug the entire thing
    Debug("Full debug of cassowary and left/middle/right calculated values: ");
    Debug("CassowaryDebug: " + CassowaryDebug(cTest));

    // Results should be 90, 95 and 100
    Debug("Left: " + FloatToString(CassowaryGetValue(cTest, "left")));
    Debug("Middle: " + FloatToString(CassowaryGetValue(cTest, "middle")));
    Debug("Right: " + FloatToString(CassowaryGetValue(cTest, "right")));

    Debug("Cassowary Setting middle to 45.0");

    // Suggest middle is 45
    CassowarySuggestValue(cTest, "middle", 45.0);

    // Debug the entire thing
    Debug("Full debug of cassowary and left/middle/right calculated values: ");
    Debug("CassowaryDebug: " + CassowaryDebug(cTest));

    // Results should now be 40, 45 and 50
    Debug("Left: " + FloatToString(CassowaryGetValue(cTest, "left")));
    Debug("Middle: " + FloatToString(CassowaryGetValue(cTest, "middle")));
    Debug("Right: " + FloatToString(CassowaryGetValue(cTest, "right")));


    Debug("Cassowary Script End");
}

// Debug a string
void Debug(string sDebug)
{
    WriteTimestampedLogEntry("[DEBUG] " + sDebug);
    SendMessageToPC(GetFirstPC(), "[DEBUG] " + sDebug);
}

This cut down version may be easier to read. There is no real debug except the final results.

// Cutdown example
void main()
{
    // Generate cassowary
    cassowary cTest;

    // Some constraints
    CassowaryConstrain(cTest, "middle == (left + right) / 2");
    CassowaryConstrain(cTest, "right == left + 10");
    CassowaryConstrain(cTest, "right <= 100");
    CassowaryConstrain(cTest, "left >= 0");

    // Results should be 90, 95 and 100
    SendMessageToPC(GetFirstPC(), "Left: " + FloatToString(CassowaryGetValue(cTest, "left")) +
                                  "Middle: " + FloatToString(CassowaryGetValue(cTest, "middle")) +
                                  "Right: " + FloatToString(CassowaryGetValue(cTest, "right")));

    // Suggest middle is 45
    CassowarySuggestValue(cTest, "middle", 45.0);

    // Results should now be 40, 45 and 50
    SendMessageToPC(GetFirstPC(), "Left: " + FloatToString(CassowaryGetValue(cTest, "left")) +
                                  "Middle: " + FloatToString(CassowaryGetValue(cTest, "middle")) +
                                  "Right: " + FloatToString(CassowaryGetValue(cTest, "right")));
}


See Also

functions:

GetLocalCassowary, SetLocalCassowary, DeleteLocalCassowary, CassowaryReset, CassowaryConstrain, CassowarySuggestValue, CassowaryGetValue, CassowaryDebug