Exemple #1
0
func main() {
	var L *golua.State

	L = golua.NewState()
	L.OpenLibs()

	L.GetField(golua.LUA_GLOBALSINDEX, "print")
	L.PushString("Hello World!")
	L.Call(1, 0)

	L.PushGoFunction(test)
	L.PushGoFunction(test)
	L.PushGoFunction(test)
	L.PushGoFunction(test)

	L.PushGoFunction(test2)
	L.PushInteger(42)
	L.Call(1, 0)

	L.Call(0, 0)
	L.Call(0, 0)
	L.Call(0, 0)

	L.Close()
}
Exemple #2
0
func main() {
	var L *lua.State
	L = lua.NewState()
	L.OpenLibs()

	rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(Userdata{})))
	var ptr *Userdata
	ptr = (*Userdata)(rawptr)
	ptr.a = 2
	ptr.b = 3

	fmt.Println(ptr)

	rawptr2 := L.ToUserdata(-1)
	ptr2 := (*Userdata)(rawptr2)

	fmt.Println(ptr2)
}
Exemple #3
0
func main() {

	var L *lua.State

	L = lua.NewState()
	defer L.Close()
	L.OpenLibs()

	currentPanicf := L.AtPanic(nil)
	currentPanicf = L.AtPanic(currentPanicf)
	newPanic := func(L1 *lua.State) int {
		fmt.Println("I AM PANICKING!!!")
		return currentPanicf(L1)
	}

	L.AtPanic(newPanic)

	//force a panic
	L.PushNil()
	L.Call(0, 0)
}
Exemple #4
0
Fichier : lua.go Projet : vron/fm
func initLua() {
	// Create a new log file
	f, e := os.Create("session.lua")
	if e != nil {
		log.Fatal(e)
	}
	sesLog = log.New(f, "", 0)
	shell = log.New(os.Stdout, "", 0)

	sesLog.Println("-- Session started ", time.Now())

	L := lua.NewState()
	L.OpenLibs()
	luastate = L

	// Register all our functions
	register(L)

	// We should now capture all input
	go readInput(L)
}
Exemple #5
0
func (a *Ai) setupLuaState() {
	base.CheckPathCasing(a.path)
	prog, err := ioutil.ReadFile(a.path)
	if err != nil {
		base.Error().Printf("Unable to load ai file %s: %v", a.path, err)
		return
	}
	a.Prog = string(prog)
	a.watcher.Watch(a.path)
	a.L = lua.NewState()
	a.L.OpenLibs()
	switch a.kind {
	case game.EntityAi:
		a.addEntityContext()
		a.loadUtils("entity")
		if a.ent.Side() == game.SideHaunt {
			a.loadUtils("denizen_entity")
		}
		if a.ent.Side() == game.SideExplorers {
			a.loadUtils("intruder_entity")
		}

	case game.MinionsAi:
		a.addMinionsContext()
		a.loadUtils("minions")

	case game.DenizensAi:
		a.addDenizensContext()
		a.loadUtils("denizens")

	case game.IntrudersAi:
		a.addIntrudersContext()
		a.loadUtils("intruders")

	default:
		panic("Unknown ai kind")
	}
	// Add this to all contexts
	a.L.Register("print", func(L *lua.State) int {
		var res string
		n := L.GetTop()
		for i := -n; i < 0; i++ {
			res += game.LuaStringifyParam(L, i) + " "
		}
		base.Log().Printf("Ai(%p): %s", a, res)
		return 0
	})
	a.L.Register("randN", func(L *lua.State) int {
		n := L.GetTop()
		if n == 0 || !L.IsNumber(-1) {
			L.PushInteger(0)
			return 1
		}
		val := L.ToInteger(-1)
		if val <= 0 {
			base.Error().Printf("Can't call randN with a value <= 0.")
			return 0
		}
		L.PushInteger(rand.Intn(val) + 1)
		return 1
	})
	a.L.DoString(a.Prog)
}
Exemple #6
0
func startGameScript(gp *GamePanel, path string, player *Player, data map[string]string) {
	// Clear out the panel, now the script can do whatever it wants
	player.Script_path = path
	gp.AnchorBox = gui.MakeAnchorBox(gui.Dims{1024, 768})
	base.Log().Printf("startGameScript")
	if !filepath.IsAbs(path) {
		path = filepath.Join(base.GetDataDir(), "scripts", filepath.FromSlash(path))
	}

	// The game script runs in a separate go routine and functions that need to
	// communicate with the game will do so via channels - DUH why did i even
	// write this comment?
	prog, err := ioutil.ReadFile(path)
	if err != nil {
		base.Error().Printf("Unable to load game script file %s: %v", path, err)
		return
	}
	gp.script = &gameScript{}
	base.Log().Printf("script = %p", gp.script)

	gp.script.L = lua.NewState()
	gp.script.L.OpenLibs()
	gp.script.L.SetExecutionLimit(25000)
	gp.script.L.NewTable()
	LuaPushSmartFunctionTable(gp.script.L, FunctionTable{
		"ChooserFromFile":                   func() { gp.script.L.PushGoFunctionAsCFunction(chooserFromFile(gp)) },
		"StartScript":                       func() { gp.script.L.PushGoFunctionAsCFunction(startScript(gp, player)) },
		"SaveGameState":                     func() { gp.script.L.PushGoFunctionAsCFunction(saveGameState(gp)) },
		"LoadGameState":                     func() { gp.script.L.PushGoFunctionAsCFunction(loadGameState(gp)) },
		"DoExec":                            func() { gp.script.L.PushGoFunctionAsCFunction(doExec(gp)) },
		"SelectEnt":                         func() { gp.script.L.PushGoFunctionAsCFunction(selectEnt(gp)) },
		"FocusPos":                          func() { gp.script.L.PushGoFunctionAsCFunction(focusPos(gp)) },
		"SelectHouse":                       func() { gp.script.L.PushGoFunctionAsCFunction(selectHouse(gp)) },
		"LoadHouse":                         func() { gp.script.L.PushGoFunctionAsCFunction(loadHouse(gp)) },
		"SaveStore":                         func() { gp.script.L.PushGoFunctionAsCFunction(saveStore(gp, player)) },
		"ShowMainBar":                       func() { gp.script.L.PushGoFunctionAsCFunction(showMainBar(gp, player)) },
		"SpawnEntityAtPosition":             func() { gp.script.L.PushGoFunctionAsCFunction(spawnEntityAtPosition(gp)) },
		"GetSpawnPointsMatching":            func() { gp.script.L.PushGoFunctionAsCFunction(getSpawnPointsMatching(gp)) },
		"SpawnEntitySomewhereInSpawnPoints": func() { gp.script.L.PushGoFunctionAsCFunction(spawnEntitySomewhereInSpawnPoints(gp)) },
		"IsSpawnPointInLos":                 func() { gp.script.L.PushGoFunctionAsCFunction(isSpawnPointInLos(gp)) },
		"PlaceEntities":                     func() { gp.script.L.PushGoFunctionAsCFunction(placeEntities(gp)) },
		"RoomAtPos":                         func() { gp.script.L.PushGoFunctionAsCFunction(roomAtPos(gp)) },
		"SetLosMode":                        func() { gp.script.L.PushGoFunctionAsCFunction(setLosMode(gp)) },
		"GetAllEnts":                        func() { gp.script.L.PushGoFunctionAsCFunction(getAllEnts(gp)) },
		"DialogBox":                         func() { gp.script.L.PushGoFunctionAsCFunction(dialogBox(gp)) },
		"PickFromN":                         func() { gp.script.L.PushGoFunctionAsCFunction(pickFromN(gp)) },
		"SetGear":                           func() { gp.script.L.PushGoFunctionAsCFunction(setGear(gp)) },
		"BindAi":                            func() { gp.script.L.PushGoFunctionAsCFunction(bindAi(gp)) },
		"SetVisibility":                     func() { gp.script.L.PushGoFunctionAsCFunction(setVisibility(gp)) },
		"EndPlayerInteraction":              func() { gp.script.L.PushGoFunctionAsCFunction(endPlayerInteraction(gp)) },
		"GetLos":                            func() { gp.script.L.PushGoFunctionAsCFunction(getLos(gp)) },
		"SetVisibleSpawnPoints":             func() { gp.script.L.PushGoFunctionAsCFunction(setVisibleSpawnPoints(gp)) },
		"SetCondition":                      func() { gp.script.L.PushGoFunctionAsCFunction(setCondition(gp)) },
		"SetPosition":                       func() { gp.script.L.PushGoFunctionAsCFunction(setPosition(gp)) },
		"SetHp":                             func() { gp.script.L.PushGoFunctionAsCFunction(setHp(gp)) },
		"SetAp":                             func() { gp.script.L.PushGoFunctionAsCFunction(setAp(gp)) },
		"RemoveEnt":                         func() { gp.script.L.PushGoFunctionAsCFunction(removeEnt(gp)) },
		"PlayAnimations":                    func() { gp.script.L.PushGoFunctionAsCFunction(playAnimations(gp)) },
		"PlayMusic":                         func() { gp.script.L.PushGoFunctionAsCFunction(playMusic(gp)) },
		"StopMusic":                         func() { gp.script.L.PushGoFunctionAsCFunction(stopMusic(gp)) },
		"SetMusicParam":                     func() { gp.script.L.PushGoFunctionAsCFunction(setMusicParam(gp)) },
		"PlaySound":                         func() { gp.script.L.PushGoFunctionAsCFunction(playSound(gp)) },
		"SetWaypoint":                       func() { gp.script.L.PushGoFunctionAsCFunction(setWaypoint(gp)) },
		"RemoveWaypoint":                    func() { gp.script.L.PushGoFunctionAsCFunction(removeWaypoint(gp)) },
		"Rand":                              func() { gp.script.L.PushGoFunctionAsCFunction(randFunc(gp)) },
		"Sleep":                             func() { gp.script.L.PushGoFunctionAsCFunction(sleepFunc(gp)) },
	})
	gp.script.L.SetMetaTable(-2)
	gp.script.L.SetGlobal("Script")

	registerUtilityFunctions(gp.script.L)
	if player.Lua_store != nil {
		loadGameStateRaw(gp, player.Game_state)
		err := LuaDecodeTable(bytes.NewBuffer(player.Lua_store), gp.script.L, gp.game)
		if err != nil {
			base.Warn().Printf("Error decoding lua state: %v", err)
		}
		gp.script.L.SetGlobal("store")
	} else {
		gp.script.L.NewTable()
		gp.script.L.SetGlobal("store")
	}
	gp.script.sync = make(chan struct{})
	base.Log().Printf("Sync: %p", gp.script.sync)
	res := gp.script.L.DoString(string(prog))
	if !res {
		base.Error().Printf("There was an error running script %s:\n%s", path, prog)
	} else {
		base.Log().Printf("No_init: %v\n", player.No_init)
		go func() {
			gp.script.L.NewTable()
			for k, v := range data {
				gp.script.L.PushString(k)
				gp.script.L.PushString(v)
				gp.script.L.SetTable(-3)
			}
			gp.script.L.SetGlobal("__data")
			gp.script.L.SetExecutionLimit(250000)
			if player.No_init {
				gp.script.syncStart()
				loadGameStateRaw(gp, player.Game_state)
				gp.game.script = gp.script
				gp.script.syncEnd()
			} else {
				gp.script.L.DoString("Init(__data)")
				if gp.game.Side == SideHaunt {
					gp.game.Ai.minions.Activate()
					gp.game.Ai.denizens.Activate()
					gp.game.player_inactive = gp.game.Ai.denizens.Active()
				} else {
					gp.game.Ai.intruders.Activate()
					gp.game.player_inactive = gp.game.Ai.intruders.Active()
				}

			}
			if gp.game == nil {
				base.Error().Printf("Script failed to load a house during Init().")
			} else {
				gp.game.comm.script_to_game <- nil
			}
		}()
	}
}