hedgewars/uScript.pas
changeset 5547 2fe1d68a102c
parent 5529 9a47cff3910a
child 5549 ccfb9b8ab9d1
equal deleted inserted replaced
5545:980d44fe2d4c 5547:2fe1d68a102c
   151 
   151 
   152 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
   152 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
   153 begin
   153 begin
   154     if lua_gettop(L) = 1 then
   154     if lua_gettop(L) = 1 then
   155         begin
   155         begin
   156         WriteLnToConsole('Lua: ' + lua_tostring(L ,1));
   156         WriteLnToConsole('Lua: ' + StrPas(lua_tostring(L ,1)));
   157         end
   157         end
   158     else
   158     else
   159         LuaError('Lua: Wrong number of parameters passed to WriteLnToConsole!');
   159         LuaError('Lua: Wrong number of parameters passed to WriteLnToConsole!');
   160     lc_writelntoconsole:= 0;
   160     lc_writelntoconsole:= 0;
   161 end;
   161 end;
   162 
   162 
   163 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
   163 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
   164 begin
   164 begin
   165     if lua_gettop(L) = 1 then
   165     if lua_gettop(L) = 1 then
   166         begin
   166         begin
   167         ParseCommand(lua_tostring(L ,1), true);
   167         ParseCommand(StrPas(lua_tostring(L ,1)), true);
   168         end
   168         end
   169     else
   169     else
   170         LuaError('Lua: Wrong number of parameters passed to ParseCommand!');
   170         LuaError('Lua: Wrong number of parameters passed to ParseCommand!');
   171     lc_parsecommand:= 0;
   171     lc_parsecommand:= 0;
   172 end;
   172 end;
   173 
   173 
   174 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
   174 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
   175 begin
   175 begin
   176     if lua_gettop(L) = 5 then
   176     if lua_gettop(L) = 5 then
   177         begin
   177         begin
   178         ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
   178         ShowMission(StrPas(lua_tostring(L, 1)), StrPas(lua_tostring(L, 2)), StrPas(lua_tostring(L, 3)), lua_tointeger(L, 4), lua_tointeger(L, 5));
   179         end
   179         end
   180     else
   180     else
   181         LuaError('Lua: Wrong number of parameters passed to ShowMission!');
   181         LuaError('Lua: Wrong number of parameters passed to ShowMission!');
   182     lc_showmission:= 0;
   182     lc_showmission:= 0;
   183 end;
   183 end;
   190 end;
   190 end;
   191 
   191 
   192 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
   192 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
   193 begin
   193 begin
   194     if lua_gettop(L) = 1 then
   194     if lua_gettop(L) = 1 then
   195         AddCaption(lua_tostring(L, 1), cWhiteColor, capgrpMessage)
   195         AddCaption(StrPas(lua_tostring(L, 1)), cWhiteColor, capgrpMessage)
   196     else if lua_gettop(L) = 3 then
   196     else if lua_gettop(L) = 3 then
   197         begin
   197         begin
   198         AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
   198         AddCaption(StrPas(lua_tostring(L, 1)), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
   199         end
   199         end
   200     else
   200     else
   201         LuaError('Lua: Wrong number of parameters passed to AddCaption!');
   201         LuaError('Lua: Wrong number of parameters passed to AddCaption!');
   202     lc_addcaption:= 0;
   202     lc_addcaption:= 0;
   203 end;
   203 end;
   718     else
   718     else
   719         begin
   719         begin
   720         gear:= GearByUID(lua_tointeger(L, 1));
   720         gear:= GearByUID(lua_tointeger(L, 1));
   721         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
   721         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
   722 
   722 
   723 	    hogName:= lua_tostring(L, 2);
   723 	    hogName:= StrPas(lua_tostring(L, 2));
   724             gear^.Hedgehog^.Name:= hogName;
   724             gear^.Hedgehog^.Name:= hogName;
   725 
   725 
   726 	    FreeTexture(gear^.Hedgehog^.NameTagTex);
   726 	    FreeTexture(gear^.Hedgehog^.NameTagTex);
   727             gear^.Hedgehog^.NameTagTex:= RenderStringTex(gear^.Hedgehog^.Name, gear^.Hedgehog^.Team^.Clan^.Color, fnt16);
   727             gear^.Hedgehog^.NameTagTex:= RenderStringTex(gear^.Hedgehog^.Name, gear^.Hedgehog^.Team^.Clan^.Color, fnt16);
   728 
   728 
   855         if gear <> nil then
   855         if gear <> nil then
   856             begin
   856             begin
   857             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
   857             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
   858             if vgear <> nil then
   858             if vgear <> nil then
   859                begin
   859                begin
   860                vgear^.Text:= lua_tostring(L, 2);
   860                vgear^.Text:= StrPas(lua_tostring(L, 2));
   861                vgear^.Hedgehog:= gear^.Hedgehog;
   861                vgear^.Hedgehog:= gear^.Hedgehog;
   862                vgear^.FrameTicks:= lua_tointeger(L, 3);
   862                vgear^.FrameTicks:= lua_tointeger(L, 3);
   863                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then vgear^.FrameTicks:= 1;
   863                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then vgear^.FrameTicks:= 1;
   864                lua_pushinteger(L, vgear^.Uid)
   864                lua_pushinteger(L, vgear^.Uid)
   865                end
   865                end
  1113     lc_playsound:= 0;
  1113     lc_playsound:= 0;
  1114 end;
  1114 end;
  1115 
  1115 
  1116 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1116 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1117 var np: LongInt;
  1117 var np: LongInt;
       
  1118     voice: shortstring;
  1118 begin
  1119 begin
  1119     np:= lua_gettop(L);
  1120     np:= lua_gettop(L);
  1120     if (np < 5) or (np > 6) then
  1121     if (np < 5) or (np > 6) then
  1121         begin
  1122         begin
  1122         LuaError('Lua: Wrong number of parameters passed to AddTeam!');
  1123         LuaError('Lua: Wrong number of parameters passed to AddTeam!');
  1123         //lua_pushnil(L)
  1124         //lua_pushnil(L)
  1124         end
  1125         end
  1125     else
  1126     else
  1126         begin
  1127         begin
  1127         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
  1128 (*
  1128         ParseCommand('grave ' + lua_tostring(L, 3), true);
  1129  FIXME FIXME FIXME FIXME
  1129         ParseCommand('fort ' + lua_tostring(L, 4), true);
  1130  Something is very wrong here.
  1130         ParseCommand('voicepack ' + lua_tostring(L, 5), true);
  1131  For some reason, if I assign voice after fort (or don't use the variable) it is empty.
  1131         if (np = 6) then ParseCommand('flag ' + lua_tostring(L, 6), true);
  1132 *)
       
  1133         voice:= StrPas(lua_tostring(L, 5));
       
  1134         ParseCommand('addteam x ' + StrPas(lua_tostring(L, 2)) + ' ' + StrPas(lua_tostring(L, 1)), true);
       
  1135         ParseCommand('grave ' + StrPas(lua_tostring(L, 3)), true);
       
  1136         ParseCommand('fort ' + StrPas(lua_tostring(L, 4)), true);
       
  1137         ParseCommand('voicepack ' + voice, true);
       
  1138         if (np = 6) then ParseCommand('flag ' + StrPas(lua_tostring(L, 6)), true);
  1132         CurrentTeam^.Binds:= DefaultBinds
  1139         CurrentTeam^.Binds:= DefaultBinds
  1133         // fails on x64
  1140         // fails on x64
  1134         //lua_pushinteger(L, LongInt(CurrentTeam));
  1141         //lua_pushinteger(L, LongInt(CurrentTeam));
  1135         end;
  1142         end;
  1136     lc_addteam:= 0;//1;
  1143     lc_addteam:= 0;//1;
  1144         LuaError('Lua: Wrong number of parameters passed to AddHog!');
  1151         LuaError('Lua: Wrong number of parameters passed to AddHog!');
  1145         lua_pushnil(L)
  1152         lua_pushnil(L)
  1146         end
  1153         end
  1147     else
  1154     else
  1148         begin
  1155         begin
  1149         temp:= lua_tostring(L, 4);
  1156         temp:= StrPas(lua_tostring(L, 4));
  1150         ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
  1157         ParseCommand('addhh ' + StrPas(lua_tostring(L, 2)) + ' ' + StrPas(lua_tostring(L, 3)) + ' ' + StrPas(lua_tostring(L, 1)), true);
  1151         ParseCommand('hat ' + temp, true);
  1158         ParseCommand('hat ' + temp, true);
  1152         lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
  1159         lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
  1153         end;
  1160         end;
  1154     lc_addhog:= 1;
  1161     lc_addhog:= 1;
  1155 end;
  1162 end;
  1449         end
  1456         end
  1450     else
  1457     else
  1451         begin
  1458         begin
  1452         gear:= GearByUID(lua_tointeger(L, 1));
  1459         gear:= GearByUID(lua_tointeger(L, 1));
  1453         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1460         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1454             hat:= lua_tostring(L, 2);
  1461             hat:= StrPas(lua_tostring(L, 2));
  1455             gear^.Hedgehog^.Hat:= hat;
  1462             gear^.Hedgehog^.Hat:= hat;
  1456             LoadHedgehogHat(gear, hat);
  1463             LoadHedgehogHat(gear, hat);
  1457         end;
  1464         end;
  1458     lc_sethoghat:= 0;
  1465     lc_sethoghat:= 0;
  1459 end;
  1466 end;
  1485 begin
  1492 begin
  1486     n:= lua_gettop(luaState);
  1493     n:= lua_gettop(luaState);
  1487     WriteLnToConsole('Lua: Stack (' + inttostr(n) + ' elements):');
  1494     WriteLnToConsole('Lua: Stack (' + inttostr(n) + ' elements):');
  1488     for i:= 1 to n do
  1495     for i:= 1 to n do
  1489         if not lua_isboolean(luaState, i) then
  1496         if not lua_isboolean(luaState, i) then
  1490             WriteLnToConsole('Lua:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
  1497             WriteLnToConsole('Lua:  ' + inttostr(i) + ': ' + StrPas(lua_tostring(luaState, i)))
  1491         else if lua_toboolean(luaState, i) then
  1498         else if lua_toboolean(luaState, i) then
  1492             WriteLnToConsole('Lua:  ' + inttostr(i) + ': true')
  1499             WriteLnToConsole('Lua:  ' + inttostr(i) + ': true')
  1493         else
  1500         else
  1494             WriteLnToConsole('Lua:  ' + inttostr(i) + ': false');
  1501             WriteLnToConsole('Lua:  ' + inttostr(i) + ': false');
  1495 end;
  1502 end;
  1525 end;
  1532 end;
  1526 
  1533 
  1527 function ScriptGetString(name : shortstring) : shortstring;
  1534 function ScriptGetString(name : shortstring) : shortstring;
  1528 begin
  1535 begin
  1529 lua_getglobal(luaState, Str2PChar(name));
  1536 lua_getglobal(luaState, Str2PChar(name));
  1530 ScriptGetString:= lua_tostring(luaState, -1);
  1537 ScriptGetString:= StrPas(lua_tostring(luaState, -1));
  1531 lua_pop(luaState, 1);
  1538 lua_pop(luaState, 1);
  1532 end;
  1539 end;
  1533 
  1540 
  1534 procedure ScriptOnGameInit;
  1541 procedure ScriptOnGameInit;
  1535 begin
  1542 begin
  1602 if not FileExists(s) then s:= Pathz[ptData] + '/' + name;
  1609 if not FileExists(s) then s:= Pathz[ptData] + '/' + name;
  1603 ret:= luaL_loadfile(luaState, Str2PChar(s));
  1610 ret:= luaL_loadfile(luaState, Str2PChar(s));
  1604 if ret <> 0 then
  1611 if ret <> 0 then
  1605     begin
  1612     begin
  1606     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  1613     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
  1607     LuaError('Lua: ' + lua_tostring(luaState, -1));
  1614     LuaError('Lua: ' + StrPas(lua_tostring(luaState, -1)));
  1608     end
  1615     end
  1609 else
  1616 else
  1610     begin
  1617     begin
  1611     WriteLnToConsole('Lua: ' + name + ' loaded');
  1618     WriteLnToConsole('Lua: ' + name + ' loaded');
  1612     // call the script file
  1619     // call the script file
  1638     exit;
  1645     exit;
  1639 SetGlobals;
  1646 SetGlobals;
  1640 lua_getglobal(luaState, Str2PChar(fname));
  1647 lua_getglobal(luaState, Str2PChar(fname));
  1641 if lua_pcall(luaState, 0, 0, 0) <> 0 then
  1648 if lua_pcall(luaState, 0, 0, 0) <> 0 then
  1642     begin
  1649     begin
  1643     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  1650     LuaError('Lua: Error while calling ' + fname + ': ' + StrPas(lua_tostring(luaState, -1)));
  1644     lua_pop(luaState, 1)
  1651     lua_pop(luaState, 1)
  1645     end;
  1652     end;
  1646 GetGlobals;
  1653 GetGlobals;
  1647 end;
  1654 end;
  1648 
  1655 
  1653 lua_getglobal(luaState, Str2PChar('ParseCommandOverride'));
  1660 lua_getglobal(luaState, Str2PChar('ParseCommandOverride'));
  1654 lua_pushstring(luaState, Str2PChar(key));
  1661 lua_pushstring(luaState, Str2PChar(key));
  1655 lua_pushstring(luaState, Str2PChar(value));
  1662 lua_pushstring(luaState, Str2PChar(value));
  1656 if lua_pcall(luaState, 2, 1, 0) <> 0 then
  1663 if lua_pcall(luaState, 2, 1, 0) <> 0 then
  1657     begin
  1664     begin
  1658     LuaError('Lua: Error while calling ParseCommandOverride: ' + lua_tostring(luaState, -1));
  1665     LuaError('Lua: Error while calling ParseCommandOverride: ' + StrPas(lua_tostring(luaState, -1)));
  1659     lua_pop(luaState, 1)
  1666     lua_pop(luaState, 1)
  1660     end
  1667     end
  1661 else
  1668 else
  1662     begin
  1669     begin
  1663     ParseCommandOverride:= lua_tostring(luaState, -1);
  1670     ParseCommandOverride:= StrPas(lua_tostring(luaState, -1));
  1664     lua_pop(luaState, 1)
  1671     lua_pop(luaState, 1)
  1665     end;
  1672     end;
  1666 end;
  1673 end;
  1667 
  1674 
  1668 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
  1675 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
  1691 lua_pushinteger(luaState, par3);
  1698 lua_pushinteger(luaState, par3);
  1692 lua_pushinteger(luaState, par4);
  1699 lua_pushinteger(luaState, par4);
  1693 ScriptCall:= 0;
  1700 ScriptCall:= 0;
  1694 if lua_pcall(luaState, 4, 1, 0) <> 0 then
  1701 if lua_pcall(luaState, 4, 1, 0) <> 0 then
  1695     begin
  1702     begin
  1696     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
  1703     LuaError('Lua: Error while calling ' + fname + ': ' + StrPas(lua_tostring(luaState, -1)));
  1697     lua_pop(luaState, 1)
  1704     lua_pop(luaState, 1)
  1698     end
  1705     end
  1699 else
  1706 else
  1700     begin
  1707     begin
  1701     ScriptCall:= lua_tointeger(luaState, -1);
  1708     ScriptCall:= lua_tointeger(luaState, -1);