hedgewars/uScript.pas
author smxx
Tue, 02 Mar 2010 17:22:36 +0000
changeset 2911 cd355befa683
parent 2905 f3c79f7193a9
child 2924 908851e59958
permissions -rw-r--r--
Engine: * Explosive crates: Use them as cover - but watch out, they may explode spreading fire everywhere! * Temporary hack for testing: Replaced all mines with explosive crates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     1
(*
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     3
 * Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com>
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     4
 *
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     8
 *
85f6425a4d74 Engine:
smxx
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    12
 * GNU General Public License for more details.
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    13
 *
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    17
 *)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    18
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    20
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    21
unit uScript;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    22
interface
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    23
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    24
procedure ScriptPrintStack;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    25
procedure ScriptClearStack;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    26
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    27
procedure ScriptLoad(name : shortstring);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    28
procedure ScriptOnGameInit;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    29
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    30
procedure ScriptCall(fname : shortstring);
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    31
function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    32
function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    33
function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    34
function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    35
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    36
procedure init_uScript;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    37
procedure free_uScript;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    38
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    39
implementation
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
    40
{$IFNDEF IPHONEOS}
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    41
uses LuaPas in 'LuaPas.pas',
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    42
	uConsole,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    43
	uMisc,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    44
	uConsts,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    45
	uGears,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    46
	uFloat,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    47
	uWorld,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    48
	uAmmos,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    49
	uSound,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    50
	uTeams,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    51
	uKeys,
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    52
	typinfo;
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
    53
	
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    54
var luaState : Plua_State;
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
    55
	ScriptAmmoStore : shortstring;
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
    56
	ScriptLoaded : boolean;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    57
	
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    58
procedure ScriptPrepareAmmoStore; forward;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    59
procedure ScriptApplyAmmoStore; forward;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    60
procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte); forward;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    61
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    62
// wrapped calls //
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    63
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    64
// functions called from lua:
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    65
// function(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    66
// where L contains the state, returns the number of return values on the stack
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    67
// call lua_gettop(L) to receive number of parameters passed
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    68
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    69
function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    70
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    71
	if lua_gettop(L) = 1 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    72
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    73
		WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    74
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    75
	else
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
    76
		WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    77
	lc_writelntoconsole:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    78
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    79
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    80
function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    81
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    82
	if lua_gettop(L) = 1 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    83
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    84
		ParseCommand(lua_tostring(L ,1), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    85
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    86
	else
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
    87
		WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    88
	lc_parsecommand:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    89
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    90
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    91
function lc_showmission(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    92
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    93
	if lua_gettop(L) = 5 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    94
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    95
		ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    96
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    97
	else
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
    98
		WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
    99
	lc_showmission:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   100
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   101
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   102
function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   103
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   104
	HideMission;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   105
	lc_hidemission:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   106
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   107
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   108
function lc_addgear(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   109
var gear : PGear;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   110
	x, y, s, t: LongInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   111
	dx, dy: hwFloat;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   112
	gt: TGearType;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   113
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   114
	if lua_gettop(L) <> 7 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   115
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   116
		WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   117
		lua_pushnil(L); // return value on stack (nil)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   118
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   119
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   120
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   121
		x:= lua_tointeger(L, 1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   122
		y:= lua_tointeger(L, 2);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   123
		gt:= TGearType(lua_tointeger(L, 3));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   124
		s:= lua_tointeger(L, 4);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   125
		dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   126
		dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   127
		t:= lua_tointeger(L, 7);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   128
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   129
		gear:= AddGear(x, y, gt, s, dx, dy, t);
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   130
		lua_pushnumber(L, gear^.uid)
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   131
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   132
	lc_addgear:= 1; // 1 return value
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   133
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   134
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   135
function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   136
var gear : PGear;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   137
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   138
	if lua_gettop(L) <> 1 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   139
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   140
		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   141
		lua_pushnil(L); // return value on stack (nil)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   142
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   143
	else
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   144
		begin
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   145
		gear:= GearByUID(lua_tointeger(L, 1));
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   146
		if gear <> nil then
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   147
			lua_pushinteger(L, ord(gear^.Kind))
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   148
		end;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   149
	lc_getgeartype:= 1
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   150
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   151
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   152
function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   153
var gear : PGear;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   154
begin
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   155
	if lua_gettop(L) <> 2 then
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   156
		begin
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   157
		WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   158
		end
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   159
	else
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   160
		begin
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   161
		gear:= GearByUID(lua_tointeger(L, 1));
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   162
		if (gear <> nil) and (gear^.Kind = gtHedgehog) then gear^.Health:= lua_tointeger(L, 2)
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   163
		end;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   164
	lc_sethealth:= 0
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   165
end;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   166
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   167
function lc_endgame(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   168
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   169
	GameState:= gsExit;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   170
	lc_endgame:= 0
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   171
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   172
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   173
function lc_findplace(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   174
var gear: PGear;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   175
	fall: boolean;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   176
	left, right: LongInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   177
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   178
	if lua_gettop(L) <> 4 then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   179
		WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   180
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   181
		begin
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   182
		gear:= GearByUID(lua_tointeger(L, 1));
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   183
		fall:= lua_toboolean(L, 2);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   184
		left:= lua_tointeger(L, 3);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   185
		right:= lua_tointeger(L, 4);
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   186
		if gear <> nil then
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   187
			FindPlace(gear, fall, left, right)
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   188
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   189
	lc_findplace:= 0
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   190
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   191
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   192
function lc_playsound(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   193
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   194
	if lua_gettop(L) <> 1 then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   195
		WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   196
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   197
		PlaySound(TSound(lua_tointeger(L, 1)));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   198
	lc_playsound:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   199
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   200
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   201
function lc_addteam(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   202
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   203
	if lua_gettop(L) <> 5 then
2795
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   204
		begin
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   205
		WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!');
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   206
		//lua_pushnil(L)
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   207
		end
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   208
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   209
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   210
		ParseCommand('addteam ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   211
		ParseCommand('grave ' + lua_tostring(L, 3), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   212
		ParseCommand('fort ' + lua_tostring(L, 4), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   213
		ParseCommand('voicepack ' + lua_tostring(L, 5), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   214
		CurrentTeam^.Binds:= DefaultBinds;
2795
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   215
		// fails on x64
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   216
		//lua_pushinteger(L, LongInt(CurrentTeam));
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   217
		end;
2795
c9c830897547 Engine:
smxx
parents: 2794
diff changeset
   218
	lc_addteam:= 0;//1;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   219
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   220
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   221
function lc_addhog(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   222
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   223
	if lua_gettop(L) <> 4 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   224
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   225
		WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   226
		lua_pushnil(L)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   227
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   228
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   229
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   230
		ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   231
		ParseCommand('hat ' + lua_tostring(L, 4), true);
2794
befd3808c63a Engine:
smxx
parents: 2793
diff changeset
   232
		lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   233
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   234
	lc_addhog:= 1;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   235
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   236
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   237
function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   238
var gear: PGear;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   239
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   240
	if lua_gettop(L) <> 1 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   241
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   242
		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   243
		lua_pushnil(L);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   244
		lua_pushnil(L)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   245
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   246
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   247
		begin
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   248
		gear:= GearByUID(lua_tointeger(L, 1));
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   249
		if gear <> nil then
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   250
			begin
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   251
			lua_pushinteger(L, hwRound(gear^.X));
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   252
			lua_pushinteger(L, hwRound(gear^.Y))
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   253
			end
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   254
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   255
	lc_getgearposition:= 2;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   256
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   257
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   258
function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   259
var gear: PGear;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   260
	x, y: LongInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   261
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   262
	if lua_gettop(L) <> 3 then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   263
		WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   264
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   265
		begin
2790
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   266
		gear:= GearByUID(lua_tointeger(L, 1));
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   267
		if gear <> nil then
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   268
			begin
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   269
			x:= lua_tointeger(L, 2);
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   270
			y:= lua_tointeger(L, 3);
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   271
			gear^.X:= int2hwfloat(x);
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   272
			gear^.Y:= int2hwfloat(y);
83630d5f94db Engine:
smxx
parents: 2786
diff changeset
   273
			end
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   274
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   275
	lc_setgearposition:= 0
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   276
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   277
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   278
function lc_setammo(L : Plua_State) : LongInt; Cdecl;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   279
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   280
	if lua_gettop(L) <> 3 then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   281
		WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   282
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   283
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   284
		ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   285
		end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   286
	lc_setammo:= 0
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   287
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   288
///////////////////
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   289
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   290
procedure ScriptPrintStack;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   291
var n, i : LongInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   292
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   293
	n:= lua_gettop(luaState);
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   294
	WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   295
	for i:= 1 to n do
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   296
		if not lua_isboolean(luaState, i) then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   297
			WriteLnToConsole('LUA:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   298
		else if lua_toboolean(luaState, i) then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   299
			WriteLnToConsole('LUA:  ' + inttostr(i) + ': true')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   300
		else
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   301
			WriteLnToConsole('LUA:  ' + inttostr(i) + ': false');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   302
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   303
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   304
procedure ScriptClearStack;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   305
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   306
lua_settop(luaState, 0)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   307
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   308
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   309
procedure ScriptSetInteger(name : shortstring; value : LongInt);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   310
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   311
lua_pushinteger(luaState, value);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   312
lua_setglobal(luaState, Str2PChar(name));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   313
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   314
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   315
procedure ScriptSetString(name : shortstring; value : shortstring);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   316
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   317
lua_pushstring(luaState, Str2PChar(value));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   318
lua_setglobal(luaState, Str2PChar(name));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   319
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   320
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   321
function ScriptGetInteger(name : shortstring) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   322
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   323
lua_getglobal(luaState, Str2PChar(name));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   324
ScriptGetInteger:= lua_tointeger(luaState, -1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   325
lua_pop(luaState, 1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   326
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   327
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   328
function ScriptGetString(name : shortstring) : shortstring;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   329
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   330
lua_getglobal(luaState, Str2PChar(name));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   331
ScriptGetString:= lua_tostring(luaState, -1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   332
lua_pop(luaState, 1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   333
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   334
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   335
procedure ScriptOnGameInit;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   336
begin
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   337
	// not required if there's no script to run
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   338
	if not ScriptLoaded then
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   339
		exit;
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   340
			
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   341
	// push game variables so they may be modified by the script
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   342
	ScriptSetInteger('GameFlags', GameFlags);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   343
	ScriptSetString('Seed', cSeed);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   344
	ScriptSetInteger('TurnTime', cHedgehogTurnTime);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   345
	ScriptSetInteger('CaseFreq', cCaseFactor);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   346
	ScriptSetInteger('LandAdds', cLandAdditions);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   347
	ScriptSetInteger('Delay', cInactDelay);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   348
	ScriptSetString('Map', '');
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   349
	ScriptSetString('Theme', '');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   350
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   351
	ScriptCall('onGameInit');
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   352
	
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   353
	// pop game variables
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   354
	ParseCommand('seed ' + ScriptGetString('Seed'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   355
	ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   356
	ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   357
	ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   358
	ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   359
	ParseCommand('$delay ' + ScriptGetString('Delay'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   360
	if ScriptGetString('Map') <> '' then
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   361
		ParseCommand('map ' + ScriptGetString('Map'), true);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   362
	if ScriptGetString('Theme') <> '' then
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   363
		ParseCommand('theme ' + ScriptGetString('Theme'), true);	
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   364
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   365
	ScriptPrepareAmmoStore;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   366
	ScriptCall('onAmmoStoreInit');
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   367
	ScriptApplyAmmoStore;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   368
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   369
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   370
procedure ScriptLoad(name : shortstring);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   371
var ret : LongInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   372
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   373
	ret:= luaL_loadfile(luaState, Str2PChar(name));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   374
	if ret <> 0 then
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   375
		WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   376
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   377
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   378
		WriteLnToConsole('LUA: ' + name + ' loaded');
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   379
		// call the script file
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   380
		lua_pcall(luaState, 0, 0, 0);
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   381
		ScriptLoaded:= true
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   382
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   383
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   384
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   385
procedure SetGlobals;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   386
begin
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   387
	ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   388
end;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   389
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   390
procedure GetGlobals;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   391
begin
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   392
	TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   393
end;
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   394
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   395
procedure ScriptCall(fname : shortstring);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   396
begin
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   397
	if not ScriptLoaded then
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   398
		exit;
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   399
	SetGlobals;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   400
	lua_getglobal(luaState, Str2PChar(fname));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   401
	if lua_pcall(luaState, 0, 0, 0) <> 0 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   402
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   403
		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   404
		lua_pop(luaState, 1)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   405
		end;
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   406
	GetGlobals;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   407
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   408
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   409
function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   410
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   411
ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   412
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   413
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   414
function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   415
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   416
ScriptCall:= ScriptCall(fname, par1, par2, 0, 0)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   417
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   418
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   419
function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   420
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   421
ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   422
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   423
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   424
function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   425
begin
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   426
	if not ScriptLoaded then
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   427
		exit;
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   428
	SetGlobals;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   429
	lua_getglobal(luaState, Str2PChar(fname));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   430
	lua_pushinteger(luaState, par1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   431
	lua_pushinteger(luaState, par2);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   432
	lua_pushinteger(luaState, par3);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   433
	lua_pushinteger(luaState, par4);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   434
	ScriptCall:= 0;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   435
	if lua_pcall(luaState, 4, 1, 0) <> 0 then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   436
		begin
2791
ac4b69e5f9f7 Engine:
smxx
parents: 2790
diff changeset
   437
		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   438
		lua_pop(luaState, 1)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   439
		end
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   440
	else
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   441
		begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   442
		ScriptCall:= lua_tointeger(luaState, -1);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   443
		lua_pop(luaState, 1)
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   444
		end;
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   445
	GetGlobals;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   446
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   447
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   448
procedure ScriptPrepareAmmoStore;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   449
var i: ShortInt;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   450
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   451
ScriptAmmoStore:= '';
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   452
for i:=1 to ord(High(TAmmoType)) do
2862
1d15ca7b7f64 Engine:
smxx
parents: 2814
diff changeset
   453
	ScriptAmmoStore:= ScriptAmmoStore + '0000';
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   454
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   455
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   456
procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   457
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   458
if (ord(ammo) < 1) or (count > 9) or (count < 0) or (propability < 0) or (propability > 8) then
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   459
	exit;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   460
ScriptAmmoStore[ord(ammo)]:= inttostr(count)[1];
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   461
ScriptAmmoStore[ord(ammo) + ord(high(TAmmoType))]:= inttostr(propability)[1];
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   462
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   463
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   464
procedure ScriptApplyAmmoStore;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   465
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   466
	AddAmmoStore(ScriptAmmoStore);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   467
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   468
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   469
// small helper functions making registering enums a lot easier
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   470
function str(const en : TGearType) : shortstring; overload;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   471
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   472
str:= GetEnumName(TypeInfo(TGearType), ord(en))
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   473
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   474
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   475
function str(const en : TSound) : shortstring; overload;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   476
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   477
str:= GetEnumName(TypeInfo(TSound), ord(en))
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   478
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   479
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   480
function str(const en : TAmmoType) : shortstring; overload;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   481
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   482
str:= GetEnumName(TypeInfo(TAmmoType), ord(en))
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   483
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   484
///////////////////
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   485
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   486
procedure init_uScript;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   487
var at : TGearType;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   488
	am : TAmmoType;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   489
	st : TSound;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   490
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   491
// initialize lua
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   492
luaState:= lua_open;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   493
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   494
// open internal libraries
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   495
luaopen_base(luaState);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   496
luaopen_string(luaState);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   497
luaopen_math(luaState);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   498
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   499
// import some variables
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   500
ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   501
ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   502
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   503
// import game flags
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   504
ScriptSetInteger('gfForts',gfForts);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   505
ScriptSetInteger('gfMultiWeapon',gfMultiWeapon);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   506
ScriptSetInteger('gfSolidLand',gfSolidLand);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   507
ScriptSetInteger('gfBorder',gfBorder);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   508
ScriptSetInteger('gfDivideTeams',gfDivideTeams);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   509
ScriptSetInteger('gfLowGravity',gfLowGravity);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   510
ScriptSetInteger('gfLaserSight',gfLaserSight);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   511
ScriptSetInteger('gfInvulnerable',gfInvulnerable);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   512
ScriptSetInteger('gfMines',gfMines);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   513
ScriptSetInteger('gfVampiric',gfVampiric);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   514
ScriptSetInteger('gfKarma',gfKarma);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   515
ScriptSetInteger('gfArtillery',gfArtillery);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   516
ScriptSetInteger('gfOneClanMode',gfOneClanMode);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   517
ScriptSetInteger('gfRandomOrder',gfRandomOrder);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   518
ScriptSetInteger('gfKing',gfKing);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   519
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   520
// register gear types
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   521
for at:= Low(TGearType) to High(TGearType) do
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   522
	ScriptSetInteger(str(at), ord(at));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   523
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   524
// register sounds
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   525
for st:= Low(TSound) to High(TSound) do
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   526
	ScriptSetInteger(str(st), ord(st));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   527
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   528
// register ammo types
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   529
for am:= Low(TAmmoType) to High(TAmmoType) do
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   530
	ScriptSetInteger(str(am), ord(am));
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   531
	
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   532
// register functions
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   533
lua_register(luaState, 'AddGear', @lc_addgear);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   534
lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   535
lua_register(luaState, 'GetGearType', @lc_getgeartype);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   536
lua_register(luaState, 'EndGame', @lc_endgame);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   537
lua_register(luaState, 'FindPlace', @lc_findplace);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   538
lua_register(luaState, 'SetGearPosition', @lc_setgearposition);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   539
lua_register(luaState, 'GetGearPosition', @lc_getgearposition);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   540
lua_register(luaState, 'ParseCommand', @lc_parsecommand);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   541
lua_register(luaState, 'ShowMission', @lc_showmission);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   542
lua_register(luaState, 'HideMission', @lc_hidemission);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   543
lua_register(luaState, 'SetAmmo', @lc_setammo);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   544
lua_register(luaState, 'PlaySound', @lc_playsound);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   545
lua_register(luaState, 'AddTeam', @lc_addteam);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   546
lua_register(luaState, 'AddHog', @lc_addhog);
2814
71e80c6e74bb Engine:
smxx
parents: 2799
diff changeset
   547
lua_register(luaState, 'SetHealth', @lc_sethealth);
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   548
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   549
ScriptClearStack; // just to be sure stack is empty
2793
6bcf173ba9be Engine:
smxx
parents: 2791
diff changeset
   550
ScriptLoaded:= false;
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   551
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   552
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   553
procedure free_uScript;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   554
begin
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   555
lua_close(luaState);
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   556
end;
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   557
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   558
{$ELSE}
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   559
procedure ScriptPrintStack;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   560
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   561
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   562
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   563
procedure ScriptClearStack;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   564
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   565
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   566
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   567
procedure ScriptLoad(name : shortstring);
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   568
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   569
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   570
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   571
procedure ScriptOnGameInit;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   572
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   573
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   574
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   575
procedure ScriptCall(fname : shortstring);
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   576
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   577
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   578
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   579
function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   580
begin
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   581
ScriptCall:= 0
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   582
end;
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   583
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   584
function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   585
begin
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   586
ScriptCall:= 0
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   587
end;
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   588
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   589
function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   590
begin
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   591
ScriptCall:= 0
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   592
end;
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   593
2905
f3c79f7193a9 Engine:
smxx
parents: 2862
diff changeset
   594
function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
2799
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   595
begin
558b29bf00c5 add a new way to fetch version info from pascal to c
koda
parents: 2798
diff changeset
   596
ScriptCall:= 0
2798
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   597
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   598
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   599
procedure init_uScript;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   600
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   601
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   602
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   603
procedure free_uScript;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   604
begin
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   605
end;
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   606
d48bbe4518e6 Engine:
smxx
parents: 2795
diff changeset
   607
{$ENDIF}
2786
85f6425a4d74 Engine:
smxx
parents:
diff changeset
   608
end.