Skip to content

Instantly share code, notes, and snippets.

@Fulgen301
Last active July 26, 2024 14:47
Show Gist options
  • Save Fulgen301/53ead9e8482d31b28668f450b7fa5ca5 to your computer and use it in GitHub Desktop.
Save Fulgen301/53ead9e8482d31b28668f450b7fa5ca5 to your computer and use it in GitHub Desktop.
Sections

Sections

CreateSection

Description

Creates a new section from the given data. The section is loaded asynchronously, and the engine calls the passed specified callback in the given object when all clients have loaded the section.

Syntax

int CreateSection(any data, string callback, object obj);

Parameters

data

Either a string, which is the name of the section template (e.g. "Foo" for SectFoo.c4g) to be loaded, or a map describing the Scenario.txt options for the new empty section.

Currently, only the Landscape subkey is supported, with the following properties:

  • BottomOpen (bool)
  • TopOpen (bool)
  • LeftOpen (bool)
  • RightOpen (bool)
  • AutoScanSideOpen (bool)
  • SkyDef (string)
  • SkyDefFade (array of six integers)
  • NoSky (bool)
  • Gravity (C4SVal)
  • MapWdt (C4SVal)
  • MapHgt (C4SVal)
  • MapZoom (C4SVal)
  • KeepMapCreator (bool)
  • SkyScrollMode (int)
  • FoWRes (int)
  • ShadeMaterials (bool)

Gravity, MapWdt, MapHgt and MapZoom can be specified as either an array of four integers [Std, Rnd, Min, Max], of which the final value is calculated as BoundBy(Std + Random(2 * Rnd + 1) - Rnd, Min, Max), or as a single integer, which is directly used as the value.

callback

A callback function to be called when the section has finished loading. The callback function receives the new section number as first parameter and whether the section has loaded successfully on all clients as second parameter.

The calback function is always called in the section context from which the call to CreateSection occured; in case that section has been deleted in the meantime, it is called in the first section.

obj

[opt] Object in which the callback should be called. Can be nil in local calls.

Returns

The new section's number. Note that the section doesn't exist yet at that time, as it is loaded asynchronously.

Example

func LoadHappySection()
{
    CreateSection("Happy", "SectionLoaded");
}

func SectionLoaded(int newSection, bool success)
{
    if (!success)
    {
        Log("No happy section :(");
        return;
    }

    SetObjectContext(nil);
    SetSectionContext(newSection);

    for (int i = 0; i < 100; ++i)
    {
        PlaceAnimal(WIPF);
    }
}

Loads the Happy section from a template and places 100 wipfs in it once it is loaded.

func CreatePrison()
{
    CreateSection({
        Landscape = {
            MapWdt = [100, 3, 50, 200],
            MapHgt = 300,
            MapZoom = 1,
            Gravity = 50
        }
    }, "GotPrison");
}

func GotPrison(int newSection, bool success)
{
    if (!success)
    {
        Log("No prison");
        return;
    }

    for (var obj in FindObjects(Find_OCF(OCF_CrewMember)))
    {
        obj->SetPosition(0, 0);
        obj->SetSection(newSection);
    }
}

Creates an empty section with variable width and fixed height and half gravity and moves all crew members into it. They are moved to 0, 0 to ensure they are in the new section's bounds.

RemoveSection

Description

Removes the section with the given number. Note that section number 0 cannot be removed.

Syntax

bool RemoveSection(int section);

Parameters

section

The section to remove.

Returns

true if the section has been successfully removed, false otherwise.

GetSectionCount

Description

Returns the number of existing sections.

Syntax

int GetSectionCount();

Returns

The number of existing sections.

GetSectionByIndex

Description

Gets the section by the given index. As section numbers aren't contiguous, this is the only way to iterate over sections.

Syntax

int GetSectionByIndex(int index);

Returns

The number of the section at the given index, or -1 if none exists.

GetSection

Description

Gets the section of a given object.

Syntax

int GetSection(object obj);

Parameters

obj

[opt] The object ot get the section from. Can be nil in local calls.

Returns

The section of the given object, or nil if none was passed.

SetSection

Description

Sets the section of a given object. Attached and contained objects are moved with the object they are attached to / contained in.

Syntax

bool SetSection(int section, object obj);

Parameters

section

The section to move the object to.

obj

[opt] The object to move. Can be nil in local calls.

Returns

true if the section exists, false otherwise.

GetSectionContext

Description

Gets the section context of the calling function.

Syntax

int GetSectionContext();

Returns

The section context of the calling function. If the function executes with an object context, this is equivalent to a call to GetSection().

SetSectionContext

Description

Sets the section context of the calling function. If the function executes with an object context, calling this function throws an error.

Syntax

int SetSectionContext(int section);

Parameters

section

The section to set as new section context.

Returns

The old section context, or nil if section does not refer to a valid section.

Example

// a flint that spawns a monster in every section on hit
protected func Hit()
{
    var that = this;
    SetObjectContext(nil);

    for (int i = 0; i < GetSectionCount(); ++i)
    {
        SetSectionContext(GetSectionByIndex(i));
        PlaceAnimal(MONS);
    }
    
    SetObjectContext(that);
    RemoveObject();
}

SetObjectContext

Description

Sets the object context of the currently executing function. The equivalent getter function is this. This function throws an error if it is called in a global context (= not a definition / object call) or if the defintion of obj isn't equal to the definition of this.

Syntax

void SetObjectContext(object obj);

Parameters

obj

The new object context to set, or nil.

AddBackgroundSection

Description

Adds a section to a background section, creating a parent-child relationship between them where the child section is rendered on top of the parent section.

Syntax

bool AddBackgroundSection(int section, int backgroundSection, int x, int y, int width, int height);

Parameters

section

The child section - the section to attach to a background section.

backgroundSection

The parent section - the section to which the child section is attached to.

x

The x coordinate in the background section at which the child section is attached to.

y

The y coordinate in the background section at which the child section is attached to.

width

The width of the rectangle in the background section in which the child section is rendered.

height

The height of the rectangle in the background section in which the child section is rendered.

Returns

true if all sections have been found and the bounds don't exceed the background section's bounds. false if the section already has a different background section as parent than the one specified.

SetSectionPosition

Description

Updates the section's position within the background section.

Syntax

bool SetSectionPosition(int section, int x, int y);

Parameters

section

The child section to move.

x

The new x coordinate in the background section at which the child section should be attached to.

y

The new y coordinate in the background section at which the child section should be attached to.

Returns

true if the section is valid and the bounds don't exceed the parent section's bounds, false otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment