• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

OTPHP - Create LUA scripts using PHP!

Paxton

Banned User
Joined
Feb 23, 2008
Messages
4,110
Reaction score
48
Location
London, UK
Well, I came up with idea to make PHP to Lua converter especially for Open Tibia.

Basically, if you want to write in PHP but get the lua results on your server, you could use this :)

I'm not releasing this yet, I just made first version with basic functions to make it generate simple script and well, it worked. Obviously there are still few things here and there.

What advantages does it give you.

* Well, if you don't like LUA syntax, its good for you.
* I would probably add some checks and tips so it actually helps you writing the code
* Object orientated, it would be. So you can write like rev-scripts but actually generate normal lua
and probably more ;)

For now I wrote just the basic functions, and I found very easy script to recreate using this system.

I used this script: http://otland.net/f81/floor-tp-vips-96775/ made by AlcikOTS

Original script:
PHP:
--Simple script by AlcikOTS. I'am new in this things so sorry if I make any mistakes.
 
function onStepIn(cid, item, frompos, item2, topos)
 
    local vip = getPlayerStorageValue(cid,11551) >= 1 --Vip Storage.
    local back = {x=1000, y=1000, z=7} --Place where non vip is teleported.
        local allowed = {x=1216, y=1038, z=7} --Place where VIPs are teleported.
 
 
    if(vip) then
        doPlayerSendTextMessage(cid, 19, "You are VIP so you are allowed to pass.")
                doSendMagicEffect(getPlayerPosition(cid),2) 
                doTeleportThing(cid, allowed) 
                doSendMagicEffect(allowed,10)
    else
        doPlayerSendTextMessage(cid, 19, "You can't pass because you don't have Vip! You can get it in our SMS Shop.")
                doTeleportThing(cid, back)
    end
end

Using OTPHP
PHP:
<?php
	//Load OTPHP 
	require 'lib/base.php';
	
	//Main function
	OTPHP::main(
		
		//Movement event
		OT_Event::onStepIn(function() {
			
			//Lets define some local variables
			LUA::local("vip", OT::getPlayerStorageValue(CID, 1155));
			
			//We can pass arrays - I will make function specific for positions :)
			LUA::local("back", array(
				'x'=>1000,
				'y'=>1000,
				'z'=>7
			));
			
			LUA::local("allowed", array(
				'x'=>1216,
				'y'=>1038,
				'z'=>7
			));
			
			//If statement, first argument is the statment can be multi function as well
			LUA::_if(LUA::local('vip'), function() {
				
				OTPHP::out(OT::doPlayerSendTextMessage(CID, 19, "You are VIP so you are allowed to pass."));
				OTPHP::out(OT::doSendMagicEffect(OT::getPlayerPosition(CID), 2));
				OTPHP::out(OT::doSendMagicEffect(LUA::local('allowed'), 2));
			
			}, function() {
				
				OTPHP::out(OT::doPlayerSendTextMessage(CID, 19, "You can't pass because you don't have Vip! You can get it in our SMS Shop."));
				OTPHP::out(OT::doTeleportThing(CID, LUA::local('back')));
				
			});
			
		})
	);
?>

And the results generated by OTPHP:
PHP:
function onStepIn(cid, item, frompos, item2, topos) 
local vip = getPlayerStorageValue(cid, 1155) 
local back = {x = 1000, y = 1000, z = 7} 
local allowed = {x = 1216, y = 1038, z = 7} 
if(vip) then
doPlayerSendTextMessage(cid, 19, "You are VIP so you are allowed to pass.")
doSendMagicEffect(getPlayerPosition(cid), 2) 
doSendMagicEffect(allowed, 2) 

else
doPlayerSendTextMessage(cid, 19, "You can't pass because you don't have Vip! You can get it in our SMS Shop.")
doTeleportThing(cid, back) 

end
end

I haven't added tabbing yet :P

Please discuss this, and see if something like this would be helpful. If so, I would be glad to develop this more :)

btw. I'm not sure if the LUA script is actually working, I've just replicated it using OTPHP, it's just to give you the idea, you know ;)

Thanks.
 
Last edited:
I don't really think this is that useful. You would first have to learn the system of both the distro and this, just to write more lines of code than you would have if you simply went with LUA from the beggining. The code doesn't look too great either.

OTPHP
PHP:
 OTPHP::out(OT::doPlayerSendTextMessage(CID, 19, "You are VIP so you are allowed to pass."));

LUA
Code:
doPlayerSendTextMessage(cid, 19, "You are VIP so you are allowed to pass.")

Looks rather ugly.

The only reason, even if little IMO, to develop this, is if you'll learn anything from it.
 
I don't really think this is that useful. You would first have to learn the system of both the distro and this, just to write more lines of code than you would have if you simply went with LUA from the beggining. The code doesn't look too great either.

OTPHP
PHP:
 OTPHP::out(OT::doPlayerSendTextMessage(CID, 19, "You are VIP so you are allowed to pass."));

LUA
Code:
doPlayerSendTextMessage(cid, 19, "You are VIP so you are allowed to pass.")

Looks rather ugly.

The only reason, even if little IMO, to develop this, is if you'll learn anything from it.

Thanks.

I'm not really going to learn anything new from developing this but it is very useful for people like me. If I would be going to make an OT right now I would use this because I hate lua style syntax and I don't really like writing in it at all, so this would be extremely helpful.

Another thing is that this could be object orientated like I said in my first post, it's good for people that prefer this way.

And another thing is that I could write compiler into what you actually write, so I would check if the code you make actually makes sense and if it doesn't it will tell you whats wrong, so you wouldn't get no tfs errors etc.

I know it makes it a bit longer, but this is just the first version, everything can be changed :)

It could be $player->sendMessage("Message here", 19); for example :)
 
Last edited:
Code:
if(runkit_return_value_used () )
	return $output;
else
	echo $output;

Code:
<?php
    //Load OTPHP 
    require 'lib/base.php';
    
    //Main function
    OTPHP::main(
        
        //Movement event
        OT_Event::onStepIn(function() {
            
            //Lets define some local variables
            LUA::local("vip", OT::getPlayerStorageValue(CID, 1155));
            
            //We can pass arrays - I will make function specific for positions :)
            LUA::local("back", array(
                'x'=>1000,
                'y'=>1000,
                'z'=>7
            ));
            
            LUA::local("allowed", array(
                'x'=>1216,
                'y'=>1038,
                'z'=>7
            ));
            
            //If statement, first argument is the statment can be multi function as well
            LUA::_if(LUA::local('vip'), function() {
                
                OT::doPlayerSendTextMessage(CID, 19, "You are VIP so you are allowed to pass.");
                OT::doSendMagicEffect(OT::getPlayerPosition(CID), 2);
                OT::doSendMagicEffect(LUA::local('allowed'), 2);
            
            }, function() {
                
                OT::doPlayerSendTextMessage(CID, 19, "You can't pass because you don't have Vip! You can get it in our SMS Shop.");
                OT::doTeleportThing(CID, LUA::local('back'));
                
            });
            
        })
    );
?>

It's a bit better atleast.
 
Sorry, but this really isn't that useful...

I think if anybody knows PHP well, they can easily learn lua in a couple days.. It looks like you're using all the lua/tfs default function names anyways, except in PHP. If someone spent the time to learn all those, they could have already learned to code it the proper way and save themselves time overall.
 
Please discuss this, and see if something like this would be helpful. If so, I would be glad to develop this more :)

Looks way more complicated than clean lua. I dont understand how do you like OTPHP syntax more than lua. Lua syntax is very easy and intuitive.
Sorry I dont find it usefull.
 
The syntax can be easily changed, and I made the same names just because its a first release to show how it would work, and actually using this could be actually faster!

Think if I would make a framework for this, image you just write 1 function and it generates half code for you, maybe even some programming patterns would be used.
As an example I just came out with, for example quest function. You just specify few arguments and it does everything for you.
 
Last edited:
i dont think it is that useful like devianceone said if you know any scripting language you can easily learn lua in no time
 
i dont think it is that useful like devianceone said if you know any scripting language you can easily learn lua in no time

Yes but you don't get the point of it :P It's not if you know LUA or not, maybe some part yes. But the major point of this is to make writing in LUA more automized, what I mean this could become a framework to write scripts. Like I said before, for example you write few functions and it generates the whole script for you.

Doing like that, you would be able to make hundreds of quests etc in no time.
 
lua can already be object oriented, not natively but it can emulate it using metatables.
 
I don't think you could really build the quest you want if only a few functions could generate enough code for a 'quest'. The majority of people who would use this probably wont know PHP enough to make their own functions to generate the lua code they really want, and would probably only use the examples you provide as their base. I still don't think learning a language to generate another language is worth the effort of using. I could just as easily create a few functions in lua to help with whatever 100 quests I needed to make it a lot faster, efficient and easier.
 
a nice idea but how you will connect the PHP to lua states on the server?
It's a big problem, because server got too many lua states, one per module.

a big problem is share the same lua state between 2 files =x



Some time ago i did an lua webserver (version for open tibia)
you can run things like this:
localhost/kickall.lua
LUA:
Kick all players...<br>
<%lua
   local start = os.clock()
   for i,cid in pairs(getOnlinePlayers()) do
      doRemoveCreature(cid)
   end
   web.echo('Finished in <b>'..(os.clock()-start)..'</b> seconds.<br>')
%>
bye :P
 
What the hell? I've been doing stuff in PHP for the last month, but this is awkward.

I think it would be too restrictive since Lua scripts can be pretty err.. dynamic! You couldn't make advanced stuff with this altough that probably wouldn't be required either.
a nice idea but how you will connect the PHP to lua states on the server?
It's a big problem, because server got too many lua states, one per module.
He won't? It'll be used only to output Lua scripts, not execute them or something
 
What the hell? I've been doing stuff in PHP for the last month, but this is awkward.

I think it would be too restrictive since Lua scripts can be pretty err.. dynamic! You couldn't make advanced stuff with this altough that probably wouldn't be required either.

He won't? It'll be used only to output Lua scripts, not execute them or something

i sayd its hard to use the same state by php and server at same time.
BUT, you can do some WOP (Workaround-oriented programming) and every function in PHP add an line on a file and server reads that line and execute ^^
 
Last edited:
i sayd its hard to use the same state by php and server at same time.
BUT, you can do some WOP (Workaround-oriented programming) and every function in PHP add an line on a file and server reads that line and execute ^^

You write your code in PHP, web browser outputs it and you copy it to your server which runs like normal.

Cyko, well this is basically able to generate anything so I guess there wouldn't be problem on how dynamic LUA can be.

btw why is it awkward cyko? :P
 
Back
Top