środa, 12 czerwca 2013

Brainfuck interpreter in Flex/Bison and C

Recently I've had to write Brainfuck interpreter using Flex and Biso 

Brainfuck.l:
/*gedit
*/
%{
#include <stdlib.h>
#include <stdio.h>
#include "Node.h"
#include "Brainfuck.h"
%}
%%
[\-\[\]\<\>\+\,\.] { return *yytext; }
[\n] { return END; }
.
%%
/*main() { printf("%d", yylex());}*/
Brainfuck.y
/*gedit
*/
%{
#include <stdlib.h>
#include <stdio.h>
#include "Node.h"
#define MAX_MEMORY 30000
#define INNER 0
#define LOOP 1

unsigned static char tab[MAX_MEMORY]; //static, no need to initialize with zero
int pointer;
Node *parse(int op, Node *prev, Node *next);
void delete_node(Node *p);
void yyerror(char *string);
int execute(Node *p);
int yylex(void);
%}
%union{
Node *nodePointer;
}
%type <nodePointer> instruction instruction_list
%token END
%%
program:
 instruction_list END { execute($1); delete_node($1); exit(0); }
;
instruction_list:
 instruction { $$ = $1; }
| instruction_list instruction { $$ = parse(INNER, $1, $2); }
;
instruction:
 '<' { $$ = parse('<', NULL, NULL); }
| '>' { $$ = parse('>', NULL, NULL); }
| ',' { $$ = parse(',', NULL, NULL); }
| '.' { $$ = parse('.', NULL, NULL); }
| '+' { $$ = parse('+', NULL, NULL); }
| '-' { $$ = parse('-', NULL, NULL); }
| '[' instruction_list ']' { $$ = parse(LOOP, $2, NULL); }
;
%%
Node *parse(int op, Node *prev, Node *next) {
Node *node;
if ((node = (Node *)malloc(sizeof(Node))) == NULL)
yyerror("Out of memory.");
node->op = op;
node->prev  = prev;
node->next = next;
return node;
}
void delete_node(Node *p){
if (p == NULL)
return;
delete_node(p->next);
delete_node(p->prev);
free(p);
}
void yyerror(char *string){
printf("%s\n", string);
}
int execute(Node *p){
if (p == NULL)
return 0;
switch(p->op){
case '>':
pointer++;
if (pointer >= MAX_MEMORY) {
yyerror("Pointer out of bounds.");
return -1;
}
return 0;
case '<':
pointer--;
if (pointer < 0){
yyerror("Pointer out of bounds.");
return -1;
}
return 0;
case '+':
tab[pointer]++; return 0;
case '-':
tab[pointer]--; return 0;
case '.':
putchar(tab[pointer]); return 0;
case ',':
tab[pointer] = getchar(); return 0;
case LOOP:
while (tab[pointer])
execute(p->prev);
execute(p->next);
return 0;
case INNER:
execute(p->prev);
execute(p->next);
return 0;
}
return -1;
}

int main()
{
pointer = 0;
yyparse();

Node.h
/*
*/
typedef struct Node{
int op;
void *prev;
void *next;
}Node; 

Makefile:
OBJS = Brainfuck.y.o Brainfuck.l.o
GCC = gcc

Brainfuck: $(OBJS)
@echo "   >> Compile and Link"
$(GCC) -o Brainfuck $(OBJS) -lfl
Brainfuck.l.o: Brainfuck.l.c
@echo "   >> Compile Flex generated file"
$(GCC) -c Brainfuck.l.c -o Brainfuck.l.o
Brainfuck.l.c: Brainfuck.l
@echo "   >> Use Flex"
flex Brainfuck.l
mv lex.yy.c Brainfuck.l.c
Brainfuck.y.o: Brainfuck.y.c
@echo "   >> Compile Bison generated file"
$(GCC) -c Brainfuck.y.c -o Brainfuck.y.o
Brainfuck.y.c: Brainfuck.y
@echo "   >> Use Bison"
bison -d Brainfuck.y
@echo "   >> Rename results"
mv Brainfuck.tab.h Brainfuck.h
mv Brainfuck.tab.c Brainfuck.y.c
Brainfuck.y.o: Node.h
clean:
rm *.o Brainfuck.h Brainfuck.l.c Brainfuck.y.c Brainfuck 

wtorek, 12 lutego 2013

Play Tibia against bots? Sure!

Recently I've been out of hosting OTSes. I've played some Dota2, Leauge of Legends, HoN, etc.
I discovered that one thing today OTSes are missing is player (bot) AI.
How funny it would be to just join some war server and play against bots to train your PVP or just join it with your guild to train some tactics?

I started to edit source and create lua scripts to support such feature.
So far I've finished:

  • walking around
  • healing/drinking potions
  • going from point to point with path finding algorithms
  • leaving spawn (protection zone)
  • attacking players who attacked bot


Still, there are plenty things to be done:

  • running away from target
  • keeping distance from the target (mages/palladins)
  • finding potential targets
  • better targetting (i.e. players with lower health, not full hp)
  • casting offensive and utility spells depending of the situation
  • using items
  • using runes
I hope that it will be possible to release some early beta in the end of April, and this idea in Open Tibia world will gain some applause.

wtorek, 5 lutego 2013

Erlang :)

I was writing OpenTibia status checker in Java for my upcomming open tibia sever list. The thing is I wanted to refresh all servers as fast as possible.
Application done with Java, Threads Pool, 32 active threads on 16 cores machine: 7 minutes.
With Erlang I'm able to fetch statuses of 360 servers in less than.. 35 seconds. Nice, hey?

I decided to give Erlang a try - just check the figures they offer to Erlang programmers.

niedziela, 9 grudnia 2012

Symfony2 + Windows + less

Less for windows exists. But using CLI applications on Microsoft's platform is pain in the ass.
Instead of installing less to the PATH env variable I decided to use serverside lessphp (this is NOT recomennded for production env).

Symfony2 + Composer + twitter/bootstrap dependency problem

Today I started my playground with Symfony 2. As it uses Composer as dependency manager -which I have also never used before - I realised that it's gonna be workful weekend.

After almost 2 hours of fightining with twitter/bootstrap package not found error:
 PS D:\Projects\symfony> composer install  
 Loading composer repositories with package information  
 Installing dependencies  
 Your requirements could not be resolved to an installable set of packages.  
  Problem 1  
   - The requested package twitter/bootstrap could not be found in any version, there may be a typo in the package name  
 .  
 Potential causes:  
  - A typo in the package name  
  - The package is not available in a stable-enough version according to your minimum-stability setting  
   see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.  
 Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.  

trying different configurations of composer.json, crawling millions of issues & forums finally I came to solution, which worked:
Repository:

 "repositories": [  
           {  
                "type": "package",  
                "package": {  
                     "version": "master",  
                     "name": "twitter/bootstrap",  
                     "source": {  
                          "url": "https://github.com/twitter/bootstrap.git",  
                          "type": "git",  
                          "reference": "master"  
                     },  
                     "dist": {  
                          "url": "https://github.com/twitter/bootstrap/zipball/master",  
                          "type": "zip"  
                     }  
                }  
           }  
      ],  

require:
 "twitter/bootstrap": "dev-master"  
Hope that this will save someone's time in the future (or mine, if I will forget).

wtorek, 4 grudnia 2012

Tricks for web development enviornment on Windows

Usually I do work on my laptop running Debian. However my desktop PC is running Windows and I don't wanna change it (gaaameees <3).
Recently I found that sitting in front of big screen have many pros and that's why I started to looking for things that would speed up working with many projects.

First of all I installed XAMPP.
Next, in Apache documentation I found module called vhost_alias which allows to map requested address to local file system without touching mod_rewrite rules.
I store my projects in D:/Projects so it looks like following (replace in http-vhosts.conf):


<VirtualHost *>
UseCanonicalName Off
VirtualDocumentRoot "D:/Projects/%1/
<Directory "D:\Projects">
    Order allow,deny
    Allow from all
Options FollowSymLinks
    AllowOverride All
  </Directory>
</VirtualHost>

Next I had to manually add entries to windows/system32/drivers/etc/hosts file, which maps domains to IP address before making DNS request. I found opening this file as administrators really boring plus it took so many time. 
In about 10 minutes I wrote application in C# which allows me to modify this file. It's simple, without support for comments (it will ommit them when saving again):
You can download it here: HostsEditor.exe
Beware: Bitdefendeer removed contents of my hosts file after I added mapping for google.com :P

niedziela, 21 października 2012

wladca's PayGol script

Wladca posted on Otland.net script to allow people to pay with PayGol services.

Code:
<?php
/*


Note:Before starting you have to create an account at http://www.paygol.com/register?affiliatecode=T8Y7-LK0M-NY0R-Y6O3


*/


// check that the request comes from PayGol server
if(!in_array($_SERVER['REMOTE_ADDR'],
  array('109.70.3.48', '109.70.3.146', '109.70.3.58'))) {
  header("HTTP/1.0 403 Forbidden");
  die("Error: Unknown IP");
}
  

// get the variables from PayGol system
$message_id = $_GET['message_id'];
$service_id = $_GET['service_id'];
$shortcode = $_GET['shortcode'];
$keyword = $_GET['keyword'];
$message = $_GET['message'];
$sender = $_GET['sender'];
$operator = $_GET['operator'];
$country = $_GET['country'];
$custom = $_GET['custom'];
$points = $_GET['points'];
$price = $_GET['price'];
$currency = $_GET['currency'];

//Replace these parameters by your database details
$dbhost     = "localhost"; //Your database domain
$dbuser     = "root"; //Database username
$dbpassword = "dbpass"; //Database password
$db         = "dbname"; //Database name

//Connect to Database
$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($db);

$sql = "UPDATE accounts SET premium_points = premium_points+$points WHERE name = '$custom'";
mysql_query($sql);

mysql_close($conn);

?>
He was sure that this script is secure because it allows connection only from IP's of PayGol.
I explained him that user provides the 'custom' GET parameter, so as his account name he can put anything, also something what will for example add points to all users. I fixed that for him but today I came with another idea.

I've created PayGol account, created own multiprice service with price 1 euro, 9000000 points for that and IPN URL pointing to... wladca's website.
Than I turned on testing mode, made the purchase in testing mode and on wladca's server I had 9000000 points :)


So what is the solution for this problem?
Check if parameters passed to your IPN script match the values you obtained from PayGol (especially service_id, price and points).