Beispiel #1
0
func MessThingIndex(state *lua.State) int {
	log.Println("HEY WE MADE IT")
	printStackTypes(state)

	state.GetMetaTable(1)
	state.LGetMetaTable(ThingMetaTableName)
	isThing := state.RawEqual(-1, -2)
	state.Pop(2)
	if !isThing {
		log.Println("!!! OMG ARG #1 IS NOT A MESS.THING !!!")
	}

	fieldName := state.CheckString(2)
	log.Println("Arg #2 checks out, it's a string")
	thing := checkThing(state, 1)
	log.Println("So we're tryin'a look up", fieldName, "on thing", thing.Id)

	if member, ok := MessThingMembers[fieldName]; ok {
		return member(state, thing)
	}

	// That wasn't one of our members, so look it up in our Table.
	if data, ok := thing.Table[fieldName]; ok {
		// TODO: instead of pushing a whole map if the script asks for one, maybe we should use another kind of userdata that tracks the name & can access its submembers until the script asks for the leaf (or a non-existent branch)?
		pushValue(state, data)
		return 1
	}

	// uh... I guess we didn't do anything, so...?
	return 0
}
Beispiel #2
0
func MessThingTellallMethod(state *lua.State, thing *Thing) int {
	state.PushGoFunction(func(state *lua.State) int {
		place := checkThing(state, 1)
		text := state.CheckString(2)

		// If arg 3 is present, it should be a table of Things to exclude.
		excludes := make(map[ThingId]bool)
		if 2 < state.GetTop() {
			if !state.IsTable(3) {
				state.ArgError(3, "expected `table` for exclude argument if present")
			}
			numExcludes := int(state.ObjLen(3))
			for i := 0; i < numExcludes; i++ {
				state.RawGeti(3, i+1)
				exclude := checkThing(state, -1)
				excludes[exclude.Id] = true
			}
		}

		for _, content := range place.GetContents() {
			if excludes[content.Id] {
				continue
			}
			if content.Client != nil {
				content.Client.Send(text)
			}
		}

		return 0
	})
	return 1
}
Beispiel #3
0
func MessThingTellMethod(state *lua.State, thing *Thing) int {
	state.PushGoFunction(func(state *lua.State) int {
		thing := checkThing(state, 1)
		text := state.CheckString(2)

		if thing.Client != nil {
			thing.Client.Send(text)
		}
		state.Pop(2) // ( udataThing strText -- )
		return 0
	})
	return 1
}
Beispiel #4
0
func MessThingFindinsideMethod(state *lua.State, thing *Thing) int {
	state.PushGoFunction(func(state *lua.State) int {
		thing := checkThing(state, 1)
		text := state.CheckString(2)
		if text == "" {
			state.ArgError(2, "cannot find empty string")
		}

		otherThing := thing.FindInside(text)
		if otherThing == nil {
			return 0
		}
		pushValue(state, otherThing.Id)
		return 1
	})
	return 1
}
Beispiel #5
0
func MessThingPronounsubMethod(state *lua.State, thing *Thing) int {
	state.PushGoFunction(func(state *lua.State) int {
		thing := checkThing(state, 1)
		text := state.CheckString(2)

		for code, pronoun := range thing.Pronouns() {
			lowerCode := fmt.Sprintf(`%%%s`, code)
			upperCode := fmt.Sprintf(`%%%s`, strings.ToUpper(code))
			text = strings.Replace(text, lowerCode, pronoun, -1)
			text = strings.Replace(text, upperCode, strings.ToTitle(pronoun), -1)
		}

		text = strings.Replace(text, `%n`, thing.Name, -1)
		text = strings.Replace(text, `%N`, thing.Name, -1)

		state.Pop(2)           // ( udataThing str -- )
		state.PushString(text) // ( -- str' )
		return 1
	})
	return 1
}
Beispiel #6
0
func (p *Plugin) apiOn(l *lua.State) int {
	event := strings.ToLower(l.CheckString(1))
	function := luar.NewLuaObject(l, 2)
	p.listeners[event] = append(p.listeners[event], function)
	return 0
}